aboutsummaryrefslogtreecommitdiffstats
path: root/uitest
diff options
context:
space:
mode:
authorArtur <artur@vaadin.com>2017-06-20 06:20:17 +0300
committerIlia Motornyi <elmot@vaadin.com>2017-06-20 06:20:17 +0300
commitb5835ba8bda071e5442a93342605692b32cc9602 (patch)
tree24e1cf64b07ad150216bac3f47140ecf770f17db /uitest
parentfd78c9b4c656125ef03d1859e981869eb589c447 (diff)
downloadvaadin-framework-b5835ba8bda071e5442a93342605692b32cc9602.tar.gz
vaadin-framework-b5835ba8bda071e5442a93342605692b32cc9602.zip
Add View.beforeLeave to support delayed navigation
Diffstat (limited to 'uitest')
-rw-r--r--uitest/src/main/java/com/vaadin/tests/navigator/DelayedViewLeaveConfirmation.java114
-rw-r--r--uitest/src/test/java/com/vaadin/tests/navigator/DelayedViewLeaveConfirmationTest.java136
2 files changed, 250 insertions, 0 deletions
diff --git a/uitest/src/main/java/com/vaadin/tests/navigator/DelayedViewLeaveConfirmation.java b/uitest/src/main/java/com/vaadin/tests/navigator/DelayedViewLeaveConfirmation.java
new file mode 100644
index 0000000000..adfd48c41c
--- /dev/null
+++ b/uitest/src/main/java/com/vaadin/tests/navigator/DelayedViewLeaveConfirmation.java
@@ -0,0 +1,114 @@
+/*
+ * Copyright 2000-2016 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.navigator;
+
+import com.vaadin.navigator.Navigator;
+import com.vaadin.navigator.View;
+import com.vaadin.navigator.ViewBeforeLeaveEvent;
+import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
+import com.vaadin.navigator.ViewLeaveAction;
+import com.vaadin.server.VaadinRequest;
+import com.vaadin.tests.components.AbstractTestUI;
+import com.vaadin.ui.Button;
+import com.vaadin.ui.HorizontalLayout;
+import com.vaadin.ui.Label;
+import com.vaadin.ui.TextField;
+import com.vaadin.ui.VerticalLayout;
+import com.vaadin.ui.Window;
+
+public class DelayedViewLeaveConfirmation extends AbstractTestUI {
+
+ public static class OtherView extends VerticalLayout implements View {
+ public OtherView() {
+ addComponent(new Label("Just another view"));
+ }
+
+ @Override
+ public void enter(ViewChangeEvent event) {
+
+ }
+ }
+
+ public static class MainView extends VerticalLayout implements View {
+ private Label saved;
+ private TextField input;
+
+ public MainView() {
+ saved = new Label("Initial");
+ saved.setCaption("Saved value");
+ input = new TextField("Enter a value");
+ input.setId("input");
+ Button navigateAway = new Button("Navigate to the other view",
+ e -> {
+ getUI().getNavigator().navigateTo("other");
+ });
+ Button logout = new Button("Simulate logout", e -> {
+ getUI().getNavigator().runAfterLeaveConfirmation(() -> {
+ removeAllComponents();
+ addComponent(new Label("You have been logged out"));
+ getUI().getPage().setUriFragment("", false);
+ });
+ });
+ navigateAway.setId("navigateAway");
+ logout.setId("logout");
+ addComponents(saved, input, navigateAway, logout);
+ }
+
+ @Override
+ public void enter(ViewChangeEvent event) {
+ input.setValue(saved.getValue());
+ }
+
+ @Override
+ public void beforeLeave(ViewBeforeLeaveEvent event) {
+ boolean hasChanges = !(saved.getValue().equals(input.getValue()));
+ if (hasChanges) {
+ getUI().addWindow(new ConfirmationWindow(event::navigate));
+ } else {
+ event.navigate();
+ }
+ }
+
+ }
+
+ public static class ConfirmationWindow extends Window {
+ public ConfirmationWindow(ViewLeaveAction action) {
+ super();
+ VerticalLayout layout = new VerticalLayout();
+ layout.addComponent(new Label(
+ "You have unsaved changes. Are you sure you want to leave?"));
+ Button leave = new Button("YES, LEAVE!", e -> {
+ close();
+ action.run();
+ });
+ leave.setId("leave");
+ Button stay = new Button("NO, STAY!", e -> {
+ close();
+ });
+ stay.setId("stay");
+ layout.addComponents(new HorizontalLayout(leave, stay));
+ setContent(layout);
+ }
+ }
+
+ @Override
+ protected void setup(VaadinRequest request) {
+ setNavigator(new Navigator(this, this));
+ getNavigator().addView("main", MainView.class);
+ getNavigator().addView("other", OtherView.class);
+ }
+
+}
diff --git a/uitest/src/test/java/com/vaadin/tests/navigator/DelayedViewLeaveConfirmationTest.java b/uitest/src/test/java/com/vaadin/tests/navigator/DelayedViewLeaveConfirmationTest.java
new file mode 100644
index 0000000000..b3f7053cda
--- /dev/null
+++ b/uitest/src/test/java/com/vaadin/tests/navigator/DelayedViewLeaveConfirmationTest.java
@@ -0,0 +1,136 @@
+/*
+ * Copyright 2000-2016 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.navigator;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import com.vaadin.testbench.elements.ButtonElement;
+import com.vaadin.testbench.elements.LabelElement;
+import com.vaadin.testbench.elements.TextFieldElement;
+import com.vaadin.testbench.elements.WindowElement;
+import com.vaadin.tests.tb3.SingleBrowserTest;
+
+public class DelayedViewLeaveConfirmationTest extends SingleBrowserTest {
+
+ @Test
+ public void navigateAwayWithoutChanges() {
+ openMainView();
+ navigateToOtherView();
+ assertOnOtherView();
+ }
+
+ @Test
+ public void cancelNavigateAwayWithChanges() {
+ openMainView();
+ updateValue();
+ navigateToOtherView();
+ assertOnMainView();
+ chooseToStay();
+ assertOnMainView();
+ }
+
+ @Test
+ public void confirmNavigateAwayWithChanges() {
+ openMainView();
+ updateValue();
+ navigateToOtherView();
+ assertOnMainView();
+ chooseToLeave();
+ assertOnOtherView();
+ }
+
+ @Test
+ public void confirmLogoutWithChanges() {
+ openMainView();
+ updateValue();
+ logout();
+ assertOnMainView();
+ chooseToLeave();
+ assertLoggedOut();
+ }
+
+ @Test
+ public void cancelLogoutWithChanges() {
+ openMainView();
+ updateValue();
+ logout();
+ assertOnMainView();
+ chooseToStay();
+ assertOnMainView();
+ }
+
+ @Test
+ public void logoutWithoutChanges() {
+ openMainView();
+ getLogout().click();
+ assertLoggedOut();
+
+ }
+
+ private void openMainView() {
+ String url = getTestURL(DelayedViewLeaveConfirmation.class);
+ url += "#!main";
+
+ driver.get(url);
+ }
+
+ private void navigateToOtherView() {
+ getNavigateAway().click();
+ }
+
+ private void logout() {
+ getLogout().click();
+ }
+
+ private void assertOnOtherView() {
+ Assert.assertEquals("Just another view",
+ $(LabelElement.class).first().getText());
+ }
+
+ private void assertOnMainView() {
+ Assert.assertEquals("Saved value",
+ $(LabelElement.class).first().getCaption());
+ }
+
+ private void assertLoggedOut() {
+ Assert.assertEquals("You have been logged out",
+ $(LabelElement.class).first().getText());
+ }
+
+ private void chooseToStay() {
+ $(WindowElement.class).first().$(ButtonElement.class).id("stay")
+ .click();
+ }
+
+ private void chooseToLeave() {
+ $(WindowElement.class).first().$(ButtonElement.class).id("leave")
+ .click();
+ }
+
+ private void updateValue() {
+ TextFieldElement input = $(TextFieldElement.class).id("input");
+ input.setValue(input.getValue() + "-upd");
+ }
+
+ private ButtonElement getNavigateAway() {
+ return $(ButtonElement.class).id("navigateAway");
+ }
+
+ private ButtonElement getLogout() {
+ return $(ButtonElement.class).id("logout");
+ }
+}