summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArtur Signell <artur@vaadin.com>2016-08-23 22:07:52 +0300
committerArtur Signell <artur@vaadin.com>2016-08-25 21:32:44 +0300
commit9adce4633a0bf0e994567a027fc9888a0db97dbd (patch)
tree6135a2ee452b6d1d0f5cb03fe06a66cba5376bc4
parentee5e3ec7bdf4590c22f555b013de3b9c49cbd553 (diff)
downloadvaadin-framework-9adce4633a0bf0e994567a027fc9888a0db97dbd.tar.gz
vaadin-framework-9adce4633a0bf0e994567a027fc9888a0db97dbd.zip
Convert LoginForm to use new TextField
Add basic tests for logging in and for changing caption Change-Id: I6dca2eedb5832db0eb409f0621092d0e723e0654
-rw-r--r--client/src/main/java/com/vaadin/client/ui/loginform/LoginFormConnector.java35
-rw-r--r--server/src/test/java/com/vaadin/tests/server/component/loginform/LoginFormTest.java107
-rw-r--r--uitest-common/src/main/java/com/vaadin/testbench/customelements/LoginFormElement.java9
-rw-r--r--uitest/src/main/java/com/vaadin/tests/components/loginform/CustomizedLoginFormUI.java (renamed from uitest/src/main/java/com/vaadin/tests/components/loginform/CustomizedLoginFormTest.java)29
-rw-r--r--uitest/src/main/java/com/vaadin/tests/components/loginform/LoginFormTest.java120
-rw-r--r--uitest/src/main/java/com/vaadin/tests/components/loginform/LoginFormUI.java114
-rw-r--r--uitest/src/test/java/com/vaadin/tests/components/loginform/CustomizedLoginFormUITest.java33
-rw-r--r--uitest/src/test/java/com/vaadin/tests/components/loginform/LoginFormUITest.java39
8 files changed, 344 insertions, 142 deletions
diff --git a/client/src/main/java/com/vaadin/client/ui/loginform/LoginFormConnector.java b/client/src/main/java/com/vaadin/client/ui/loginform/LoginFormConnector.java
index dff2c28707..27cb297a57 100644
--- a/client/src/main/java/com/vaadin/client/ui/loginform/LoginFormConnector.java
+++ b/client/src/main/java/com/vaadin/client/ui/loginform/LoginFormConnector.java
@@ -30,10 +30,10 @@ import com.vaadin.client.ComponentConnector;
import com.vaadin.client.ConnectorHierarchyChangeEvent;
import com.vaadin.client.communication.StateChangeEvent;
import com.vaadin.client.ui.AbstractSingleComponentContainerConnector;
+import com.vaadin.client.ui.VTextField;
import com.vaadin.client.ui.button.ButtonConnector;
import com.vaadin.client.ui.nativebutton.NativeButtonConnector;
-import com.vaadin.client.v7.ui.VLegacyTextField;
-import com.vaadin.client.v7.ui.textfield.LegacyTextFieldConnector;
+import com.vaadin.client.ui.textfield.TextFieldConnector;
import com.vaadin.shared.Connector;
import com.vaadin.shared.ui.Connect;
import com.vaadin.shared.ui.loginform.LoginFormConstants;
@@ -44,8 +44,6 @@ import com.vaadin.shared.ui.loginform.LoginFormState;
public class LoginFormConnector
extends AbstractSingleComponentContainerConnector {
- private VLegacyTextField passwordField;
- private VLegacyTextField userField;
private LoginFormRpc loginFormRpc;
@Override
@@ -93,20 +91,25 @@ public class LoginFormConnector
super.onStateChanged(stateChangeEvent);
LoginFormState state = getState();
- userField = configureTextField(state.userNameFieldConnector,
- "username");
- passwordField = configureTextField(state.passwordFieldConnector,
- "password");
+ configureTextField(getUsernameFieldConnector(), "username");
+ configureTextField(getPasswordFieldConnector(), "password");
addSubmitButtonClickHandler(state.loginButtonConnector);
getWidget().setAction(
getResourceUrl(LoginFormConstants.LOGIN_RESOURCE_NAME));
}
- private VLegacyTextField configureTextField(Connector connector,
+ private TextFieldConnector getUsernameFieldConnector() {
+ return (TextFieldConnector) getState().userNameFieldConnector;
+ }
+
+ private TextFieldConnector getPasswordFieldConnector() {
+ return (TextFieldConnector) getState().passwordFieldConnector;
+ }
+
+ private VTextField configureTextField(TextFieldConnector connector,
String id) {
if (connector != null) {
- VLegacyTextField textField = ((LegacyTextFieldConnector) connector)
- .getWidget();
+ VTextField textField = connector.getWidget();
textField.addKeyDownHandler(new SubmitKeyHandler());
@@ -159,11 +162,13 @@ public class LoginFormConnector
}
private void valuesChanged() {
- if (passwordField != null) {
- passwordField.valueChange(true);
+ TextFieldConnector connector = getUsernameFieldConnector();
+ if (connector != null) {
+ connector.flush();
}
- if (userField != null) {
- userField.valueChange(true);
+ connector = getPasswordFieldConnector();
+ if (connector != null) {
+ connector.flush();
}
}
diff --git a/server/src/test/java/com/vaadin/tests/server/component/loginform/LoginFormTest.java b/server/src/test/java/com/vaadin/tests/server/component/loginform/LoginFormTest.java
new file mode 100644
index 0000000000..a69c3f32a2
--- /dev/null
+++ b/server/src/test/java/com/vaadin/tests/server/component/loginform/LoginFormTest.java
@@ -0,0 +1,107 @@
+package com.vaadin.tests.server.component.loginform;
+
+import java.util.Iterator;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.vaadin.tests.util.MockUI;
+import com.vaadin.ui.Button;
+import com.vaadin.ui.Component;
+import com.vaadin.ui.HasComponents;
+import com.vaadin.ui.LoginForm;
+import com.vaadin.ui.NativeButton;
+import com.vaadin.ui.PasswordField;
+import com.vaadin.ui.TextField;
+import com.vaadin.ui.UI;
+
+public class LoginFormTest {
+
+ private LoginForm loginForm;
+
+ @Before
+ public void setup() {
+ loginForm = new LoginForm();
+ }
+
+ @Test
+ public void defaultCaptions() {
+ Assert.assertEquals("Username", loginForm.getUsernameCaption());
+ Assert.assertEquals("Password", loginForm.getPasswordCaption());
+ Assert.assertEquals("Login", loginForm.getLoginButtonCaption());
+ }
+
+ @Test
+ public void changeCaptionsBeforeAttach() {
+ loginForm.setUsernameCaption("u");
+ loginForm.setPasswordCaption("p");
+ loginForm.setLoginButtonCaption("l");
+
+ Assert.assertEquals("u", loginForm.getUsernameCaption());
+ Assert.assertEquals("p", loginForm.getPasswordCaption());
+ Assert.assertEquals("l", loginForm.getLoginButtonCaption());
+ }
+
+ @Test
+ public void changeCaptionsAfterAttach() {
+ UI ui = new MockUI();
+ ui.setContent(loginForm);
+ loginForm.setUsernameCaption("u");
+ loginForm.setPasswordCaption("p");
+ loginForm.setLoginButtonCaption("l");
+
+ Assert.assertEquals("u", loginForm.getUsernameCaption());
+ Assert.assertEquals("p", loginForm.getPasswordCaption());
+ Assert.assertEquals("l", loginForm.getLoginButtonCaption());
+ }
+
+ @Test
+ public void changeCaptionsBeforeAndAfterAttach() {
+ loginForm.setUsernameCaption("a");
+ loginForm.setPasswordCaption("a");
+ loginForm.setLoginButtonCaption("a");
+
+ UI ui = new MockUI();
+ ui.setContent(loginForm);
+ loginForm.setUsernameCaption("u");
+ loginForm.setPasswordCaption("p");
+ loginForm.setLoginButtonCaption("l");
+
+ Assert.assertEquals("u", loginForm.getUsernameCaption());
+ Assert.assertEquals("p", loginForm.getPasswordCaption());
+ Assert.assertEquals("l", loginForm.getLoginButtonCaption());
+ }
+
+ @Test
+ public void customizedFields() {
+ LoginForm customForm = new LoginForm() {
+ @Override
+ protected Button createLoginButton() {
+ return new NativeButton("Do it");
+ }
+
+ @Override
+ protected TextField createUsernameField() {
+ TextField tf = new TextField("Username caption");
+ tf.setPlaceholder("Name goes here");
+ return tf;
+ }
+
+ @Override
+ protected PasswordField createPasswordField() {
+ PasswordField pf = new PasswordField("Password caption");
+ pf.setPlaceholder("Secret goes here");
+ return pf;
+ }
+ };
+
+ UI ui = new MockUI();
+ ui.setContent(customForm);
+ Iterator<Component> i = ((HasComponents) customForm.iterator().next())
+ .iterator();
+ Assert.assertEquals("Username caption", i.next().getCaption());
+ Assert.assertEquals("Password caption", i.next().getCaption());
+ Assert.assertEquals("Do it", i.next().getCaption());
+ }
+}
diff --git a/uitest-common/src/main/java/com/vaadin/testbench/customelements/LoginFormElement.java b/uitest-common/src/main/java/com/vaadin/testbench/customelements/LoginFormElement.java
new file mode 100644
index 0000000000..59cd0444e7
--- /dev/null
+++ b/uitest-common/src/main/java/com/vaadin/testbench/customelements/LoginFormElement.java
@@ -0,0 +1,9 @@
+package com.vaadin.testbench.customelements;
+
+import com.vaadin.testbench.elements.AbstractSingleComponentContainerElement;
+import com.vaadin.testbench.elementsbase.ServerClass;
+
+@ServerClass("com.vaadin.ui.LoginForm")
+public class LoginFormElement extends AbstractSingleComponentContainerElement {
+
+}
diff --git a/uitest/src/main/java/com/vaadin/tests/components/loginform/CustomizedLoginFormTest.java b/uitest/src/main/java/com/vaadin/tests/components/loginform/CustomizedLoginFormUI.java
index 3fde281c20..61c52c0288 100644
--- a/uitest/src/main/java/com/vaadin/tests/components/loginform/CustomizedLoginFormTest.java
+++ b/uitest/src/main/java/com/vaadin/tests/components/loginform/CustomizedLoginFormUI.java
@@ -1,18 +1,33 @@
package com.vaadin.tests.components.loginform;
-public class CustomizedLoginFormTest extends LoginFormTest {
+import java.util.Optional;
+
+import com.vaadin.server.VaadinRequest;
+
+public class CustomizedLoginFormUI extends LoginFormUI {
+
+ @Override
+ protected void setup(VaadinRequest request) {
+ super.setup(request);
+ }
@Override
- protected void setup() {
- super.setup();
+ protected Optional<String> getUsernameCaption() {
+ return Optional.of("Identifiant");
+ }
- loginForm.setUsernameCaption("Identifiant");
- loginForm.setPasswordCaption("Mot de passe");
- loginForm.setLoginButtonCaption("Se connecter");
+ @Override
+ protected Optional<String> getPasswordCaption() {
+ return Optional.of("Mot de passe");
+ }
+
+ @Override
+ protected Optional<String> getLoginCaption() {
+ return Optional.of("Se connecter");
}
@Override
- protected String getDescription() {
+ protected String getTestDescription() {
return "Customization of the captions on the LoginForm component. Three login forms should be visible (undefined height, undefined width, defined height and width). Entering a username+password in a login form and clicking 'login' should replace the login form with a label telling the user name as password. Also a logout button should then be shown and pressing that takes the user back to the original screen with the LoginForm";
}
diff --git a/uitest/src/main/java/com/vaadin/tests/components/loginform/LoginFormTest.java b/uitest/src/main/java/com/vaadin/tests/components/loginform/LoginFormTest.java
deleted file mode 100644
index 100e28171e..0000000000
--- a/uitest/src/main/java/com/vaadin/tests/components/loginform/LoginFormTest.java
+++ /dev/null
@@ -1,120 +0,0 @@
-package com.vaadin.tests.components.loginform;
-
-import com.vaadin.tests.components.TestBase;
-import com.vaadin.ui.Button;
-import com.vaadin.ui.Button.ClickEvent;
-import com.vaadin.ui.Button.ClickListener;
-import com.vaadin.ui.HorizontalLayout;
-import com.vaadin.ui.Label;
-import com.vaadin.ui.LoginForm;
-import com.vaadin.ui.LoginForm.LoginEvent;
-import com.vaadin.ui.LoginForm.LoginListener;
-import com.vaadin.ui.VerticalLayout;
-
-public class LoginFormTest extends TestBase {
-
- private HorizontalLayout loginFormLayout;
- protected LoginForm loginForm;
-
- @Override
- protected void setup() {
- loginFormLayout = new HorizontalLayout();
-
- loginForm = new LoginForm();
- loginForm.setSizeUndefined();
-
- updateCaption();
- loginForm.addLoginListener(new LoginListener() {
-
- /**
- *
- */
- private static final long serialVersionUID = 1L;
-
- @Override
- public void onLogin(LoginEvent event) {
- login(event.getSource(), event.getLoginParameter("username"),
- event.getLoginParameter("password"));
-
- }
- });
-
- loginFormLayout.addComponent(loginForm);
-
- Button changeWidth = new Button("Change width", new ClickListener() {
-
- @Override
- public void buttonClick(ClickEvent event) {
- if (loginForm.getWidth() < 0) {
- loginForm.setWidth("300px");
- } else {
- loginForm.setWidth(null);
- }
- updateCaption();
- }
- });
-
- Button changeHeight = new Button("Change height", new ClickListener() {
-
- @Override
- public void buttonClick(ClickEvent event) {
- if (loginForm.getHeight() < 0) {
- loginForm.setHeight("200px");
- } else {
- loginForm.setHeight(null);
- }
- updateCaption();
- }
- });
-
- addComponent(loginFormLayout);
- addComponent(changeWidth);
- addComponent(changeHeight);
-
- }
-
- protected void updateCaption() {
- float width = loginForm.getWidth();
- float height = loginForm.getHeight();
-
- String w = width < 0 ? "auto" : (int) width + "px";
- String h = height < 0 ? "auto" : (int) height + "px";
-
- loginForm.setCaption("LoginForm (" + w + "/" + h + ")");
- }
-
- protected void login(LoginForm loginForm, String user, String password) {
- VerticalLayout infoLayout = new VerticalLayout();
-
- Label info = new Label(
- "User '" + user + "', password='" + password + "' logged in");
- Button logoutButton = new Button("Log out", new ClickListener() {
-
- @Override
- public void buttonClick(ClickEvent event) {
- Button b = event.getButton();
- loginFormLayout.replaceComponent(b.getParent(),
- (LoginForm) b.getData());
- }
-
- });
- logoutButton.setData(loginForm);
-
- infoLayout.addComponent(info);
- infoLayout.addComponent(logoutButton);
-
- loginFormLayout.replaceComponent(loginForm, infoLayout);
-
- }
-
- @Override
- protected String getDescription() {
- return "Basic test for the LoginForm component. Three login forms should be visible (undefined height, undefined width, defined height and width). Entering a username+password in a login form and clicking 'login' should replace the login form with a label telling the user name as password. Also a logout button should then be shown and pressing that takes the user back to the original screen with the LoginForm";
- }
-
- @Override
- protected Integer getTicketNumber() {
- return 3597;
- }
-
-}
diff --git a/uitest/src/main/java/com/vaadin/tests/components/loginform/LoginFormUI.java b/uitest/src/main/java/com/vaadin/tests/components/loginform/LoginFormUI.java
new file mode 100644
index 0000000000..cdc97d01be
--- /dev/null
+++ b/uitest/src/main/java/com/vaadin/tests/components/loginform/LoginFormUI.java
@@ -0,0 +1,114 @@
+package com.vaadin.tests.components.loginform;
+
+import java.util.Optional;
+
+import com.vaadin.server.VaadinRequest;
+import com.vaadin.tests.components.AbstractTestUI;
+import com.vaadin.ui.Button;
+import com.vaadin.ui.Button.ClickListener;
+import com.vaadin.ui.HorizontalLayout;
+import com.vaadin.ui.Label;
+import com.vaadin.ui.LoginForm;
+import com.vaadin.ui.VerticalLayout;
+
+public class LoginFormUI extends AbstractTestUI {
+
+ private HorizontalLayout loginFormLayout;
+ protected LoginForm loginForm;
+
+ @Override
+ protected void setup(VaadinRequest request) {
+ loginFormLayout = new HorizontalLayout();
+
+ loginForm = new LoginForm();
+ loginForm.setSizeUndefined();
+ getUsernameCaption().ifPresent(loginForm::setUsernameCaption);
+ getPasswordCaption().ifPresent(loginForm::setPasswordCaption);
+ getLoginCaption().ifPresent(loginForm::setLoginButtonCaption);
+ updateCaption();
+ loginForm.addLoginListener(event -> {
+ login(event.getSource(), event.getLoginParameter("username"),
+ event.getLoginParameter("password"));
+ });
+
+ loginFormLayout.addComponent(loginForm);
+
+ Button changeWidth = new Button("Change width",
+ (ClickListener) event -> {
+ if (loginForm.getWidth() < 0) {
+ loginForm.setWidth("300px");
+ } else {
+ loginForm.setWidth(null);
+ }
+ updateCaption();
+ });
+
+ Button changeHeight = new Button("Change height",
+ (ClickListener) event -> {
+ if (loginForm.getHeight() < 0) {
+ loginForm.setHeight("200px");
+ } else {
+ loginForm.setHeight(null);
+ }
+ updateCaption();
+ });
+
+ addComponent(loginFormLayout);
+ addComponent(changeWidth);
+ addComponent(changeHeight);
+
+ }
+
+ protected Optional<String> getUsernameCaption() {
+ return Optional.empty();
+ }
+
+ protected Optional<String> getPasswordCaption() {
+ return Optional.empty();
+ }
+
+ protected Optional<String> getLoginCaption() {
+ return Optional.empty();
+ }
+
+ protected void updateCaption() {
+ float width = loginForm.getWidth();
+ float height = loginForm.getHeight();
+
+ String w = width < 0 ? "auto" : (int) width + "px";
+ String h = height < 0 ? "auto" : (int) height + "px";
+
+ loginForm.setCaption("LoginForm (" + w + "/" + h + ")");
+ }
+
+ protected void login(LoginForm loginForm, String user, String password) {
+ VerticalLayout infoLayout = new VerticalLayout();
+
+ Label info = new Label(
+ "User '" + user + "', password='" + password + "' logged in");
+ info.setId("info");
+ Button logoutButton = new Button("Log out", (ClickListener) event -> {
+ Button b = event.getButton();
+ loginFormLayout.replaceComponent(b.getParent(),
+ (LoginForm) b.getData());
+ });
+ logoutButton.setData(loginForm);
+
+ infoLayout.addComponent(info);
+ infoLayout.addComponent(logoutButton);
+
+ loginFormLayout.replaceComponent(loginForm, infoLayout);
+
+ }
+
+ @Override
+ protected String getTestDescription() {
+ return "Basic test for the LoginForm component.";
+ }
+
+ @Override
+ protected Integer getTicketNumber() {
+ return 3597;
+ }
+
+}
diff --git a/uitest/src/test/java/com/vaadin/tests/components/loginform/CustomizedLoginFormUITest.java b/uitest/src/test/java/com/vaadin/tests/components/loginform/CustomizedLoginFormUITest.java
new file mode 100644
index 0000000000..4db7fb658b
--- /dev/null
+++ b/uitest/src/test/java/com/vaadin/tests/components/loginform/CustomizedLoginFormUITest.java
@@ -0,0 +1,33 @@
+package com.vaadin.tests.components.loginform;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import com.vaadin.testbench.By;
+
+public class CustomizedLoginFormUITest extends LoginFormUITest {
+
+ private static final String LABELLED_BY = "aria-labelledby";
+
+ @Test
+ public void captionsCorrect() {
+ openTestURL();
+ Assert.assertEquals("Identifiant", getUsernameCaption());
+ Assert.assertEquals("Mot de passe", getPasswordCaption());
+ Assert.assertEquals("Se connecter", getLoginCaption());
+ }
+
+ private String getLoginCaption() {
+ return getLogin().getText();
+ }
+
+ private String getPasswordCaption() {
+ String passwordCaptionId = getPassword().getAttribute(LABELLED_BY);
+ return findElement(By.id(passwordCaptionId)).getText();
+ }
+
+ private String getUsernameCaption() {
+ String usernameCaptionId = getUsername().getAttribute(LABELLED_BY);
+ return findElement(By.id(usernameCaptionId)).getText();
+ }
+}
diff --git a/uitest/src/test/java/com/vaadin/tests/components/loginform/LoginFormUITest.java b/uitest/src/test/java/com/vaadin/tests/components/loginform/LoginFormUITest.java
new file mode 100644
index 0000000000..9354a2e8e6
--- /dev/null
+++ b/uitest/src/test/java/com/vaadin/tests/components/loginform/LoginFormUITest.java
@@ -0,0 +1,39 @@
+package com.vaadin.tests.components.loginform;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.openqa.selenium.By;
+import org.openqa.selenium.WebElement;
+
+import com.vaadin.testbench.customelements.LoginFormElement;
+import com.vaadin.testbench.elements.ButtonElement;
+import com.vaadin.tests.tb3.SingleBrowserTest;
+
+public class LoginFormUITest extends SingleBrowserTest {
+
+ @Test
+ public void login() {
+ openTestURL();
+ getUsername().sendKeys("user123");
+ getPassword().sendKeys("pass123");
+ getLogin().click();
+ Assert.assertEquals("User 'user123', password='pass123' logged in",
+ getInfo().getText());
+ }
+
+ protected WebElement getInfo() {
+ return findElement(By.id("info"));
+ }
+
+ protected WebElement getUsername() {
+ return findElement(By.id("username"));
+ }
+
+ protected WebElement getPassword() {
+ return findElement(By.id("password"));
+ }
+
+ protected WebElement getLogin() {
+ return $(LoginFormElement.class).first().$(ButtonElement.class).first();
+ }
+}