Browse Source

Use isConnectorEnabled instead of isEnabled for client requests(#12781).

Change-Id: Iac6dc5caf9ece76d9289da05f83289152b005406
tags/7.4.0.beta1
Denis Anisimov 9 years ago
parent
commit
3f53527ee7

+ 2
- 1
server/src/com/vaadin/server/DragAndDropService.java View File

import com.vaadin.shared.ui.dd.DragEventType; import com.vaadin.shared.ui.dd.DragEventType;
import com.vaadin.ui.Component; import com.vaadin.ui.Component;
import com.vaadin.ui.UI; import com.vaadin.ui.UI;

import elemental.json.JsonObject; import elemental.json.JsonObject;


public class DragAndDropService implements VariableOwner, ClientConnector { public class DragAndDropService implements VariableOwner, ClientConnector {


final Component sourceComponent = (Component) variables final Component sourceComponent = (Component) variables
.get("component"); .get("component");
if (sourceComponent != null && !sourceComponent.isEnabled()) {
if (sourceComponent != null && !sourceComponent.isConnectorEnabled()) {
// source component not supposed to be enabled // source component not supposed to be enabled
getLogger().warning( getLogger().warning(
"Client dropped from " + sourceComponent "Client dropped from " + sourceComponent

+ 1
- 1
server/src/com/vaadin/ui/Button.java View File

* No action is taken is the button is disabled. * No action is taken is the button is disabled.
*/ */
public void click() { public void click() {
if (isEnabled() && !isReadOnly()) {
if (isConnectorEnabled() && !isReadOnly()) {
fireClick(); fireClick();
} }
} }

+ 7
- 3
server/src/com/vaadin/ui/Calendar.java View File

* @see #isEventClickAllowed() * @see #isEventClickAllowed()
*/ */
protected boolean isClientChangeAllowed() { 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 * @return true if the client is allowed to click events
* @see #isClientChangeAllowed() * @see #isClientChangeAllowed()
* @deprecated Override {@link #fireEventClick(Integer)} instead.
*/ */
@Deprecated
protected boolean isEventClickAllowed() { protected boolean isEventClickAllowed() {
return isEnabled();
return true;
} }


/** /**

+ 1
- 1
server/src/com/vaadin/ui/Form.java View File

for (Object id : itemPropertyIds) { for (Object id : itemPropertyIds) {
if (id != null) { if (id != null) {
Field<?> field = getField(id); Field<?> field = getField(id);
if (field.isEnabled() && !field.isReadOnly()) {
if (field.isConnectorEnabled() && !field.isReadOnly()) {
return field; return field;
} }
} }

+ 71
- 0
server/tests/src/com/vaadin/server/DragAndDropServiceTest.java View File

/*
* 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));

}
}

+ 41
- 7
server/tests/src/com/vaadin/tests/server/component/button/ButtonClick.java View File

package com.vaadin.tests.server.component.button; package com.vaadin.tests.server.component.button;


import static org.junit.Assert.assertEquals;

import org.junit.Assert;
import org.junit.Test; import org.junit.Test;


import com.vaadin.server.VaadinRequest;
import com.vaadin.ui.Button; import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent; import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.UI;


/** /**
* Tests the public click() method. * Tests the public click() method.
@Test @Test
public void testClick() { public void testClick() {
getButton().click(); getButton().click();
assertEquals(clicked, true);
Assert.assertTrue("Button doesn't fire clicks", clicked);
} }


@Test @Test
Button b = getButton(); Button b = getButton();
b.setEnabled(false); b.setEnabled(false);
b.click(); b.click();
assertEquals(clicked, false);
Assert.assertFalse("Disabled button fires click events", clicked);
} }


@Test @Test
Button b = getButton(); Button b = getButton();
b.setReadOnly(true); b.setReadOnly(true);
b.click(); 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() { private Button getButton() {
Button b = new Button(); 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 @Override
public void buttonClick(ClickEvent ev) { public void buttonClick(ClickEvent ev) {
clicked = true; clicked = true;
} }
}); });
return b;
} }
} }

+ 26
- 0
server/tests/src/com/vaadin/tests/server/component/calendar/CalendarBasics.java View File

import java.util.Locale; import java.util.Locale;
import java.util.TimeZone; import java.util.TimeZone;


import org.junit.Assert;
import org.junit.Test; import org.junit.Test;


import com.vaadin.ui.Calendar; import com.vaadin.ui.Calendar;
assertEquals(23, calendar.getLastVisibleHourOfDay()); 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;
}
} }

+ 68
- 0
server/tests/src/com/vaadin/tests/server/component/form/FormTest.java View File

/*
* 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]);
}
}

Loading…
Cancel
Save