From: Denis Anisimov Date: Sun, 14 Sep 2014 12:26:37 +0000 (+0300) Subject: Use isConnectorEnabled instead of isEnabled for client requests(#12781). X-Git-Tag: 7.4.0.beta1~149 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=3f53527ee7a18a48037575b2562d89668e0a0634;p=vaadin-framework.git Use isConnectorEnabled instead of isEnabled for client requests(#12781). Change-Id: Iac6dc5caf9ece76d9289da05f83289152b005406 --- diff --git a/server/src/com/vaadin/server/DragAndDropService.java b/server/src/com/vaadin/server/DragAndDropService.java index c21f27de97..49a65a09b8 100644 --- a/server/src/com/vaadin/server/DragAndDropService.java +++ b/server/src/com/vaadin/server/DragAndDropService.java @@ -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 diff --git a/server/src/com/vaadin/ui/Button.java b/server/src/com/vaadin/ui/Button.java index 76b82aa034..e58ad7bee5 100644 --- a/server/src/com/vaadin/ui/Button.java +++ b/server/src/com/vaadin/ui/Button.java @@ -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(); } } diff --git a/server/src/com/vaadin/ui/Calendar.java b/server/src/com/vaadin/ui/Calendar.java index 59dfceec9b..63ac9fe35c 100644 --- a/server/src/com/vaadin/ui/Calendar.java +++ b/server/src/com/vaadin/ui/Calendar.java @@ -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; } /** diff --git a/server/src/com/vaadin/ui/Form.java b/server/src/com/vaadin/ui/Form.java index 7b0c49ec95..48239b09e3 100644 --- a/server/src/com/vaadin/ui/Form.java +++ b/server/src/com/vaadin/ui/Form.java @@ -1064,7 +1064,7 @@ public class Form extends AbstractField 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 index 0000000000..a88cf131c5 --- /dev/null +++ b/server/tests/src/com/vaadin/server/DragAndDropServiceTest.java @@ -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 levels = new ArrayList(); + Logger.getLogger(DragAndDropService.class.getName()).addHandler( + new StreamHandler() { + @Override + public synchronized void publish(LogRecord record) { + levels.add(record.getLevel()); + } + }); + Map variables = new HashMap(); + 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)); + + } +} diff --git a/server/tests/src/com/vaadin/tests/server/component/button/ButtonClick.java b/server/tests/src/com/vaadin/tests/server/component/button/ButtonClick.java index f82bbfe907..b41e93900f 100644 --- a/server/tests/src/com/vaadin/tests/server/component/button/ButtonClick.java +++ b/server/tests/src/com/vaadin/tests/server/component/button/ButtonClick.java @@ -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; } } diff --git a/server/tests/src/com/vaadin/tests/server/component/calendar/CalendarBasics.java b/server/tests/src/com/vaadin/tests/server/component/calendar/CalendarBasics.java index ab2bc7c8c0..773631642a 100644 --- a/server/tests/src/com/vaadin/tests/server/component/calendar/CalendarBasics.java +++ b/server/tests/src/com/vaadin/tests/server/component/calendar/CalendarBasics.java @@ -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 index 0000000000..2075f7b115 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/form/FormTest.java @@ -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]); + } +}