]> source.dussan.org Git - vaadin-framework.git/commitdiff
Use isConnectorEnabled instead of isEnabled for client requests(#12781).
authorDenis Anisimov <denis@vaadin.com>
Sun, 14 Sep 2014 12:26:37 +0000 (15:26 +0300)
committerVaadin Code Review <review@vaadin.com>
Thu, 16 Oct 2014 07:16:15 +0000 (07:16 +0000)
Change-Id: Iac6dc5caf9ece76d9289da05f83289152b005406

server/src/com/vaadin/server/DragAndDropService.java
server/src/com/vaadin/ui/Button.java
server/src/com/vaadin/ui/Calendar.java
server/src/com/vaadin/ui/Form.java
server/tests/src/com/vaadin/server/DragAndDropServiceTest.java [new file with mode: 0644]
server/tests/src/com/vaadin/tests/server/component/button/ButtonClick.java
server/tests/src/com/vaadin/tests/server/component/calendar/CalendarBasics.java
server/tests/src/com/vaadin/tests/server/component/form/FormTest.java [new file with mode: 0644]

index c21f27de973845aab23fdfd1155bad8c55b3fbd3..49a65a09b8a7db4dd52ae23231f6a1568f9700ad 100644 (file)
@@ -38,6 +38,7 @@ import com.vaadin.shared.communication.SharedState;
 import com.vaadin.shared.ui.dd.DragEventType;
 import com.vaadin.ui.Component;
 import com.vaadin.ui.UI;
+
 import elemental.json.JsonObject;
 
 public class DragAndDropService implements VariableOwner, ClientConnector {
@@ -64,7 +65,7 @@ public class DragAndDropService implements VariableOwner, ClientConnector {
 
         final Component sourceComponent = (Component) variables
                 .get("component");
-        if (sourceComponent != null && !sourceComponent.isEnabled()) {
+        if (sourceComponent != null && !sourceComponent.isConnectorEnabled()) {
             // source component not supposed to be enabled
             getLogger().warning(
                     "Client dropped from " + sourceComponent
index 76b82aa03474aef9253e27ada07afaed82296f78..e58ad7bee54728ab1cc2d4e68539c7fb8cbb68ef 100644 (file)
@@ -359,7 +359,7 @@ public class Button extends AbstractComponent implements
      * No action is taken is the button is disabled.
      */
     public void click() {
-        if (isEnabled() && !isReadOnly()) {
+        if (isConnectorEnabled() && !isReadOnly()) {
             fireClick();
         }
     }
index 59dfceec9b17f5d6efe253d1ec1b09fcc928d124..63ac9fe35c620b36c3fef4d1321cd23a6bea6420 100644 (file)
@@ -890,17 +890,21 @@ public class Calendar extends AbstractComponent implements
      * @see #isEventClickAllowed()
      */
     protected boolean isClientChangeAllowed() {
-        return !isReadOnly() && isEnabled();
+        return !isReadOnly();
     }
 
     /**
-     * Is the user allowed to trigger click events
+     * Is the user allowed to trigger click events. Returns {@code true} by
+     * default. Subclass can override this method to disallow firing event
+     * clicks got from the client side.
      * 
      * @return true if the client is allowed to click events
      * @see #isClientChangeAllowed()
+     * @deprecated Override {@link #fireEventClick(Integer)} instead.
      */
+    @Deprecated
     protected boolean isEventClickAllowed() {
-        return isEnabled();
+        return true;
     }
 
     /**
index 7b0c49ec95727ccfcc71deca0b8d9a360390bdac..48239b09e34815c1dc8889186395b48335431530 100644 (file)
@@ -1064,7 +1064,7 @@ public class Form extends AbstractField<Object> implements Item.Editor,
             for (Object id : itemPropertyIds) {
                 if (id != null) {
                     Field<?> field = getField(id);
-                    if (field.isEnabled() && !field.isReadOnly()) {
+                    if (field.isConnectorEnabled() && !field.isReadOnly()) {
                         return field;
                     }
                 }
diff --git a/server/tests/src/com/vaadin/server/DragAndDropServiceTest.java b/server/tests/src/com/vaadin/server/DragAndDropServiceTest.java
new file mode 100644 (file)
index 0000000..a88cf13
--- /dev/null
@@ -0,0 +1,71 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.server;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.logging.Level;
+import java.util.logging.LogRecord;
+import java.util.logging.Logger;
+import java.util.logging.StreamHandler;
+
+import org.easymock.EasyMock;
+import org.junit.Assert;
+import org.junit.Test;
+
+import com.vaadin.ui.AbstractComponent;
+
+/**
+ * Tests for {@link DragAndDropService}.
+ * 
+ * @author Vaadin Ltd
+ */
+public class DragAndDropServiceTest {
+
+    @Test
+    public void changeVariables_isConnectorEnabledCalled() {
+        final List<Level> levels = new ArrayList<Level>();
+        Logger.getLogger(DragAndDropService.class.getName()).addHandler(
+                new StreamHandler() {
+                    @Override
+                    public synchronized void publish(LogRecord record) {
+                        levels.add(record.getLevel());
+                    }
+                });
+        Map<String, Object> variables = new HashMap<String, Object>();
+        final boolean[] isConnectorEnabledCalled = new boolean[1];
+        AbstractComponent component = new AbstractComponent() {
+            @Override
+            public boolean isConnectorEnabled() {
+                isConnectorEnabledCalled[0] = true;
+                return false;
+            }
+        };
+        variables.put("component", component);
+
+        DragAndDropService service = new DragAndDropService(
+                EasyMock.createMock(VaadinSession.class));
+        service.changeVariables(null, variables);
+
+        Assert.assertTrue("isConnectorEnabled() method is not called",
+                isConnectorEnabledCalled[0]);
+        Assert.assertTrue("No warning on drop to disabled target",
+                levels.contains(Level.WARNING));
+
+    }
+}
index f82bbfe90776ddbbdc4641337faf7f3da1fb3cec..b41e93900f62749d6bec1513d35ba5942502e9b1 100644 (file)
@@ -1,11 +1,12 @@
 package com.vaadin.tests.server.component.button;
 
-import static org.junit.Assert.assertEquals;
-
+import org.junit.Assert;
 import org.junit.Test;
 
+import com.vaadin.server.VaadinRequest;
 import com.vaadin.ui.Button;
 import com.vaadin.ui.Button.ClickEvent;
+import com.vaadin.ui.UI;
 
 /**
  * Tests the public click() method.
@@ -16,7 +17,7 @@ public class ButtonClick {
     @Test
     public void testClick() {
         getButton().click();
-        assertEquals(clicked, true);
+        Assert.assertTrue("Button doesn't fire clicks", clicked);
     }
 
     @Test
@@ -24,7 +25,7 @@ public class ButtonClick {
         Button b = getButton();
         b.setEnabled(false);
         b.click();
-        assertEquals(clicked, false);
+        Assert.assertFalse("Disabled button fires click events", clicked);
     }
 
     @Test
@@ -32,17 +33,50 @@ public class ButtonClick {
         Button b = getButton();
         b.setReadOnly(true);
         b.click();
-        assertEquals(clicked, false);
+        Assert.assertFalse("Read only button fires click events", clicked);
+    }
+
+    @Test
+    public void testClickConnectorDisabled() {
+        Button b = new Button() {
+            @Override
+            public boolean isConnectorEnabled() {
+                return false;
+            }
+        };
+        UI ui = createUI();
+        b.setParent(ui);
+        addClickListener(b);
+        b.click();
+        Assert.assertFalse("Button with disabled connector fires click events",
+                clicked);
     }
 
     private Button getButton() {
         Button b = new Button();
-        b.addListener(new Button.ClickListener() {
+        UI ui = createUI();
+        b.setParent(ui);
+        addClickListener(b);
+        return b;
+    }
+
+    private UI createUI() {
+        UI ui = new UI() {
+
+            @Override
+            protected void init(VaadinRequest request) {
+            }
+        };
+        return ui;
+    }
+
+    private void addClickListener(Button b) {
+        clicked = false;
+        b.addClickListener(new Button.ClickListener() {
             @Override
             public void buttonClick(ClickEvent ev) {
                 clicked = true;
             }
         });
-        return b;
     }
 }
index ab2bc7c8c0daafa0fddb2c9e0d32b8cb920c29e3..773631642a0e7f2cf65d4fc6562a1f02711c2a0c 100644 (file)
@@ -24,6 +24,7 @@ import java.util.GregorianCalendar;
 import java.util.Locale;
 import java.util.TimeZone;
 
+import org.junit.Assert;
 import org.junit.Test;
 
 import com.vaadin.ui.Calendar;
@@ -207,4 +208,29 @@ public class CalendarBasics {
         assertEquals(23, calendar.getLastVisibleHourOfDay());
     }
 
+    @Test
+    public void isClientChangeAllowed_connectorEnabled() {
+        TestCalendar calendar = new TestCalendar(true);
+        Assert.assertTrue(
+                "Calendar with enabled connector doesn't allow client change",
+                calendar.isClientChangeAllowed());
+    }
+
+    private static class TestCalendar extends Calendar {
+        TestCalendar(boolean connectorEnabled) {
+            isConnectorEnabled = connectorEnabled;
+        }
+
+        @Override
+        public boolean isConnectorEnabled() {
+            return isConnectorEnabled;
+        }
+
+        @Override
+        public boolean isClientChangeAllowed() {
+            return super.isClientChangeAllowed();
+        }
+
+        private final boolean isConnectorEnabled;
+    }
 }
diff --git a/server/tests/src/com/vaadin/tests/server/component/form/FormTest.java b/server/tests/src/com/vaadin/tests/server/component/form/FormTest.java
new file mode 100644 (file)
index 0000000..2075f7b
--- /dev/null
@@ -0,0 +1,68 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.tests.server.component.form;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import com.vaadin.ui.Form;
+import com.vaadin.ui.TextField;
+
+/**
+ * Test for {@link Form}.
+ * 
+ * @author Vaadin Ltd
+ */
+public class FormTest {
+
+    @Test
+    public void testFocus() {
+        Form form = new Form();
+        final boolean firstFieldIsFocused[] = new boolean[1];
+        TextField field1 = new TextField() {
+            @Override
+            public boolean isConnectorEnabled() {
+                return false;
+            }
+
+            @Override
+            public void focus() {
+                firstFieldIsFocused[0] = true;
+            }
+        };
+
+        final boolean secondFieldIsFocused[] = new boolean[1];
+        TextField field2 = new TextField() {
+            @Override
+            public boolean isConnectorEnabled() {
+                return true;
+            }
+
+            @Override
+            public void focus() {
+                secondFieldIsFocused[0] = true;
+            }
+        };
+        form.addField("a", field1);
+        form.addField("b", field2);
+        form.focus();
+
+        Assert.assertTrue("Field with enabled connector is not focused",
+                secondFieldIsFocused[0]);
+        Assert.assertFalse("Field with disabled connector is focused",
+                firstFieldIsFocused[0]);
+    }
+}