]> source.dussan.org Git - vaadin-framework.git/commitdiff
#8209 Added a public click() method to Button, simulating a click on server side
authorJohannes Dahlström <johannes.dahlstrom@vaadin.com>
Tue, 20 Mar 2012 15:10:10 +0000 (15:10 +0000)
committerJohannes Dahlström <johannes.dahlstrom@vaadin.com>
Tue, 20 Mar 2012 15:10:10 +0000 (15:10 +0000)
svn changeset:23275/svn branch:6.8

src/com/vaadin/ui/Button.java
tests/server-side/com/vaadin/tests/server/component/button/ButtonClick.java [new file with mode: 0644]

index 795a13e41a6363812a41b02cfd6c274bc42d90a9..8a98f0ccb02340f58cf57cc85a13858e6bd33faa 100644 (file)
@@ -515,6 +515,17 @@ public class Button extends AbstractField implements FieldEvents.BlurNotifier,
         removeListener(ClickEvent.class, listener, BUTTON_CLICK_METHOD);
     }
 
+    /**
+     * Simulates a button click, notifying all server-side listeners.
+     * 
+     * No action is taken is the button is disabled.
+     */
+    public void click() {
+        if (isEnabled() && !isReadOnly()) {
+            fireClick();
+        }
+    }
+
     /**
      * Fires a click event to all listeners without any event details.
      * 
diff --git a/tests/server-side/com/vaadin/tests/server/component/button/ButtonClick.java b/tests/server-side/com/vaadin/tests/server/component/button/ButtonClick.java
new file mode 100644 (file)
index 0000000..464d131
--- /dev/null
@@ -0,0 +1,47 @@
+package com.vaadin.tests.server.component.button;
+
+import static org.junit.Assert.assertEquals;
+
+import com.vaadin.ui.Button;
+import com.vaadin.ui.Button.ClickEvent;
+
+import org.junit.Test;
+
+/**
+ * Tests the public click() method.
+ */
+public class ButtonClick {
+    private boolean clicked = false;
+
+    @Test
+    public void testClick() {
+        getButton().click();
+        assertEquals(clicked, true);
+    }
+
+    @Test
+    public void testClickDisabled() {
+        Button b = getButton();
+        b.setEnabled(false);
+        b.click();
+        assertEquals(clicked, false);
+    }
+
+    @Test
+    public void testClickReadOnly() {
+        Button b = getButton();
+        b.setReadOnly(true);
+        b.click();
+        assertEquals(clicked, false);
+    }
+
+    private Button getButton() {
+        Button b = new Button();
+        b.addListener(new Button.ClickListener() {
+            public void buttonClick(ClickEvent ev) {
+                clicked = true;
+            }
+        });
+        return b;
+    }
+}