Change-Id: Iac6dc5caf9ece76d9289da05f83289152b005406tags/7.4.0.beta1
@@ -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 |
@@ -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(); | |||
} | |||
} |
@@ -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; | |||
} | |||
/** |
@@ -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; | |||
} | |||
} |
@@ -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)); | |||
} | |||
} |
@@ -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; | |||
} | |||
} |
@@ -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; | |||
} | |||
} |
@@ -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]); | |||
} | |||
} |