From a0150a9899afd1d6e1251d9281262eddafd1d417 Mon Sep 17 00:00:00 2001 From: Teemu Suo-Anttila Date: Fri, 29 Dec 2017 10:53:45 +0200 Subject: [PATCH] Upgrade Vaadin CDI version to 3.0.0.alpha2 (#10226) This patch also adds tests for PushState and URI Fragment navigation with CDI. --- bom/pom.xml | 2 +- test/cdi/pom.xml | 21 +- .../java/com/vaadin/test/cdi/MyVaadinUI.java | 26 --- .../java/com/vaadin/test/cdi/ui/BaseUI.java | 76 ++++++++ .../com/vaadin/test/cdi/ui/RootPathUI.java | 12 ++ .../com/vaadin/test/cdi/ui/SubPathUI.java | 14 ++ .../vaadin/test/cdi/views/AlwaysNewView.java | 8 + .../vaadin/test/cdi/views/DefaultView.java | 24 +++ .../test/cdi/views/GreetingService.java | 21 ++ .../vaadin/test/cdi/views/GreetingView.java | 52 +++++ .../vaadin/test/cdi/views/NameBasedView.java | 8 + .../vaadin/test/cdi/views/ParamBasedView.java | 8 + .../vaadin/test/cdi/views/UIScopedView.java | 10 + .../com/vaadin/test/cdi/CDINavigationIT.java | 88 +++++++++ .../test/cdi/CDIPushStateNavigationIT.java | 13 ++ .../com/vaadin/test/cdi/VaadinCDISmokeIT.java | 181 +++++++++++++++++- 16 files changed, 510 insertions(+), 54 deletions(-) delete mode 100644 test/cdi/src/main/java/com/vaadin/test/cdi/MyVaadinUI.java create mode 100644 test/cdi/src/main/java/com/vaadin/test/cdi/ui/BaseUI.java create mode 100644 test/cdi/src/main/java/com/vaadin/test/cdi/ui/RootPathUI.java create mode 100644 test/cdi/src/main/java/com/vaadin/test/cdi/ui/SubPathUI.java create mode 100644 test/cdi/src/main/java/com/vaadin/test/cdi/views/AlwaysNewView.java create mode 100644 test/cdi/src/main/java/com/vaadin/test/cdi/views/DefaultView.java create mode 100644 test/cdi/src/main/java/com/vaadin/test/cdi/views/GreetingService.java create mode 100644 test/cdi/src/main/java/com/vaadin/test/cdi/views/GreetingView.java create mode 100644 test/cdi/src/main/java/com/vaadin/test/cdi/views/NameBasedView.java create mode 100644 test/cdi/src/main/java/com/vaadin/test/cdi/views/ParamBasedView.java create mode 100644 test/cdi/src/main/java/com/vaadin/test/cdi/views/UIScopedView.java create mode 100644 test/cdi/src/test/java/com/vaadin/test/cdi/CDINavigationIT.java create mode 100644 test/cdi/src/test/java/com/vaadin/test/cdi/CDIPushStateNavigationIT.java diff --git a/bom/pom.xml b/bom/pom.xml index a0f8e9a1a4..124d8b2331 100644 --- a/bom/pom.xml +++ b/bom/pom.xml @@ -19,7 +19,7 @@ 3.0.0.alpha1 5.1.1 - 2.0.0 + 3.0.0.alpha2 2.0.0 diff --git a/test/cdi/pom.xml b/test/cdi/pom.xml index 3fd5a0a2c9..3462974b74 100644 --- a/test/cdi/pom.xml +++ b/test/cdi/pom.xml @@ -10,6 +10,9 @@ vaadin-test-cdi war + + true + @@ -32,26 +35,10 @@ ROOT - - - org.eclipse.jetty - jetty-maven-plugin - ${jetty.version} - - - start-jetty - - - - stop-jetty - - - - org.wildfly.plugins wildfly-maven-plugin - 1.0.2.Final + 1.2.0.Final start-wildfly diff --git a/test/cdi/src/main/java/com/vaadin/test/cdi/MyVaadinUI.java b/test/cdi/src/main/java/com/vaadin/test/cdi/MyVaadinUI.java deleted file mode 100644 index ba0398845c..0000000000 --- a/test/cdi/src/main/java/com/vaadin/test/cdi/MyVaadinUI.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.vaadin.test.cdi; - -import javax.inject.Inject; - -import com.vaadin.annotations.Theme; -import com.vaadin.cdi.CDIUI; -import com.vaadin.server.VaadinRequest; -import com.vaadin.ui.Button; -import com.vaadin.ui.Notification; -import com.vaadin.ui.UI; - -@Theme("valo") -@CDIUI("") -@SuppressWarnings("serial") -public class MyVaadinUI extends UI { - - @Inject - private ThankYouService service; - - @Override - protected void init(VaadinRequest request) { - setContent(new Button("Click Me", - event -> Notification.show(service.getText()))); - } - -} diff --git a/test/cdi/src/main/java/com/vaadin/test/cdi/ui/BaseUI.java b/test/cdi/src/main/java/com/vaadin/test/cdi/ui/BaseUI.java new file mode 100644 index 0000000000..d582f32c10 --- /dev/null +++ b/test/cdi/src/main/java/com/vaadin/test/cdi/ui/BaseUI.java @@ -0,0 +1,76 @@ +package com.vaadin.test.cdi.ui; + +import java.util.stream.Stream; + +import javax.enterprise.event.Observes; +import javax.inject.Inject; + +import com.vaadin.cdi.AfterViewChange; +import com.vaadin.cdi.CDINavigator; +import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent; +import com.vaadin.server.Page; +import com.vaadin.server.VaadinRequest; +import com.vaadin.ui.Button; +import com.vaadin.ui.HorizontalLayout; +import com.vaadin.ui.Notification; +import com.vaadin.ui.Notification.Type; +import com.vaadin.ui.Panel; +import com.vaadin.ui.UI; +import com.vaadin.ui.VerticalLayout; +import com.vaadin.ui.themes.ValoTheme; + +public abstract class BaseUI extends UI { + + public static final String NAVIGATION_TEXT = "Navigated to: %s"; + + @Inject + /* This should be automatic */ + private CDINavigator navigator; + + @Override + protected void init(VaadinRequest request) { + Page.getCurrent().getStyles() + .add(".vertical-wrap-flex { " + + "display: flex; flex-wrap: wrap; flex-direction: column;" + + "}"); + + VerticalLayout content = new VerticalLayout(); + Panel container = new Panel(); + HorizontalLayout naviBar = new HorizontalLayout(); + + content.addComponents(naviBar); + content.addComponentsAndExpand(container); + + container.setSizeFull(); + content.setSizeFull(); + + setContent(content); + + // Create navigation bar links + Button firstLink = new Button("Default", + e -> getNavigator().navigateTo("")); + firstLink.addStyleName(ValoTheme.BUTTON_LINK); + naviBar.addComponent(firstLink); + + Stream.of("param", "param/foo", "name/foo", "name/bar", "new", + "persisting", "persisting/foo") + .map(n -> new Button(n, e -> getNavigator().navigateTo(n))) + .forEach(b -> { + b.addStyleName(ValoTheme.BUTTON_LINK); + naviBar.addComponent(b); + }); + + // This should be automatic + navigator.init(this, container); + setNavigator(navigator); + } + + protected void notifyNavigationToNonDefault( + @Observes @AfterViewChange ViewChangeEvent event) { + if (!event.getViewName().isEmpty()) { + Notification.show( + String.format(NAVIGATION_TEXT, event.getViewName()), + Type.TRAY_NOTIFICATION); + } + } +} diff --git a/test/cdi/src/main/java/com/vaadin/test/cdi/ui/RootPathUI.java b/test/cdi/src/main/java/com/vaadin/test/cdi/ui/RootPathUI.java new file mode 100644 index 0000000000..b5f29ddfe9 --- /dev/null +++ b/test/cdi/src/main/java/com/vaadin/test/cdi/ui/RootPathUI.java @@ -0,0 +1,12 @@ +package com.vaadin.test.cdi.ui; + +import com.vaadin.annotations.PreserveOnRefresh; +import com.vaadin.annotations.Theme; +import com.vaadin.cdi.CDIUI; + +@Theme("valo") +@CDIUI("") +@SuppressWarnings("serial") +@PreserveOnRefresh +public class RootPathUI extends BaseUI { +} diff --git a/test/cdi/src/main/java/com/vaadin/test/cdi/ui/SubPathUI.java b/test/cdi/src/main/java/com/vaadin/test/cdi/ui/SubPathUI.java new file mode 100644 index 0000000000..12382b0f1f --- /dev/null +++ b/test/cdi/src/main/java/com/vaadin/test/cdi/ui/SubPathUI.java @@ -0,0 +1,14 @@ +package com.vaadin.test.cdi.ui; + +import com.vaadin.annotations.PreserveOnRefresh; +import com.vaadin.annotations.Theme; +import com.vaadin.cdi.CDIUI; +import com.vaadin.navigator.PushStateNavigation; + +@Theme("valo") +@SuppressWarnings("serial") +@CDIUI("subpath") +@PreserveOnRefresh +@PushStateNavigation +public class SubPathUI extends RootPathUI { +} diff --git a/test/cdi/src/main/java/com/vaadin/test/cdi/views/AlwaysNewView.java b/test/cdi/src/main/java/com/vaadin/test/cdi/views/AlwaysNewView.java new file mode 100644 index 0000000000..d2c3014052 --- /dev/null +++ b/test/cdi/src/main/java/com/vaadin/test/cdi/views/AlwaysNewView.java @@ -0,0 +1,8 @@ +package com.vaadin.test.cdi.views; + +import com.vaadin.cdi.CDIView; +import com.vaadin.cdi.ViewContextStrategy.Always; + +@CDIView(value = "new", contextStrategy = Always.class) +public class AlwaysNewView extends GreetingView { +} diff --git a/test/cdi/src/main/java/com/vaadin/test/cdi/views/DefaultView.java b/test/cdi/src/main/java/com/vaadin/test/cdi/views/DefaultView.java new file mode 100644 index 0000000000..eb4494aa6d --- /dev/null +++ b/test/cdi/src/main/java/com/vaadin/test/cdi/views/DefaultView.java @@ -0,0 +1,24 @@ +package com.vaadin.test.cdi.views; + +import javax.inject.Inject; + +import com.vaadin.cdi.CDIView; +import com.vaadin.cdi.ViewContextStrategy.Always; +import com.vaadin.navigator.View; +import com.vaadin.test.cdi.ThankYouService; +import com.vaadin.ui.Button; +import com.vaadin.ui.Component; +import com.vaadin.ui.Notification; + +@CDIView(value = "", contextStrategy = Always.class) +public class DefaultView implements View { + + @Inject + private ThankYouService service; + + @Override + public Component getViewComponent() { + return new Button("Click Me", + e -> Notification.show(service.getText())); + } +} diff --git a/test/cdi/src/main/java/com/vaadin/test/cdi/views/GreetingService.java b/test/cdi/src/main/java/com/vaadin/test/cdi/views/GreetingService.java new file mode 100644 index 0000000000..be1a54711c --- /dev/null +++ b/test/cdi/src/main/java/com/vaadin/test/cdi/views/GreetingService.java @@ -0,0 +1,21 @@ +package com.vaadin.test.cdi.views; + +import javax.enterprise.context.Dependent; + +@Dependent +public class GreetingService { + + private int count; + + public String getGreeting(String name) { + ++count; + if (name.isEmpty()) { + return "Hello!"; + } + return "Hello, " + name + "!"; + } + + public int getCallCount() { + return count; + } +} diff --git a/test/cdi/src/main/java/com/vaadin/test/cdi/views/GreetingView.java b/test/cdi/src/main/java/com/vaadin/test/cdi/views/GreetingView.java new file mode 100644 index 0000000000..49f60a8757 --- /dev/null +++ b/test/cdi/src/main/java/com/vaadin/test/cdi/views/GreetingView.java @@ -0,0 +1,52 @@ +package com.vaadin.test.cdi.views; + +import javax.inject.Inject; + +import com.vaadin.cdi.CDIView; +import com.vaadin.navigator.View; +import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent; +import com.vaadin.ui.Alignment; +import com.vaadin.ui.CssLayout; +import com.vaadin.ui.Label; +import com.vaadin.ui.VerticalLayout; + +public class GreetingView extends VerticalLayout implements View { + + public static final String CALL_COUNT_FORMAT = "Current call count: %d"; + + @Inject + private GreetingService service; + + CssLayout greetingLog = new CssLayout(); + Label callCount = new Label(""); + + public GreetingView() { + addComponentsAndExpand(greetingLog); + addComponent(callCount); + setComponentAlignment(callCount, Alignment.BOTTOM_LEFT); + greetingLog.setSizeFull(); + greetingLog.addStyleName("vertical-wrap-flex"); + greetingLog.setId("log"); + callCount.setId("callCount"); + setSizeFull(); + + setExpandRatio(greetingLog, 1.0f); + + setId(getClass().getAnnotation(CDIView.class).value()); + } + + private void updateCallCount() { + callCount.setValue( + String.format(CALL_COUNT_FORMAT, service.getCallCount())); + } + + private void greet(String name) { + greetingLog.addComponent(new Label(service.getGreeting(name))); + updateCallCount(); + } + + @Override + public void enter(ViewChangeEvent event) { + greet(event.getParameters()); + } +} diff --git a/test/cdi/src/main/java/com/vaadin/test/cdi/views/NameBasedView.java b/test/cdi/src/main/java/com/vaadin/test/cdi/views/NameBasedView.java new file mode 100644 index 0000000000..cc4635bac0 --- /dev/null +++ b/test/cdi/src/main/java/com/vaadin/test/cdi/views/NameBasedView.java @@ -0,0 +1,8 @@ +package com.vaadin.test.cdi.views; + +import com.vaadin.cdi.CDIView; +import com.vaadin.cdi.ViewContextStrategy.ViewName; + +@CDIView(value = "name", contextStrategy = ViewName.class) +public class NameBasedView extends GreetingView { +} diff --git a/test/cdi/src/main/java/com/vaadin/test/cdi/views/ParamBasedView.java b/test/cdi/src/main/java/com/vaadin/test/cdi/views/ParamBasedView.java new file mode 100644 index 0000000000..72198b3aa0 --- /dev/null +++ b/test/cdi/src/main/java/com/vaadin/test/cdi/views/ParamBasedView.java @@ -0,0 +1,8 @@ +package com.vaadin.test.cdi.views; + +import com.vaadin.cdi.CDIView; +import com.vaadin.cdi.ViewContextStrategy.ViewNameAndParameters; + +@CDIView(value = "param", contextStrategy = ViewNameAndParameters.class) +public class ParamBasedView extends GreetingView { +} diff --git a/test/cdi/src/main/java/com/vaadin/test/cdi/views/UIScopedView.java b/test/cdi/src/main/java/com/vaadin/test/cdi/views/UIScopedView.java new file mode 100644 index 0000000000..932191291e --- /dev/null +++ b/test/cdi/src/main/java/com/vaadin/test/cdi/views/UIScopedView.java @@ -0,0 +1,10 @@ +package com.vaadin.test.cdi.views; + +import com.vaadin.cdi.CDIView; +import com.vaadin.cdi.UIScoped; +import com.vaadin.cdi.ViewContextStrategy.Always; + +@UIScoped +@CDIView(value = "persisting", contextStrategy = Always.class) +public class UIScopedView extends GreetingView { +} diff --git a/test/cdi/src/test/java/com/vaadin/test/cdi/CDINavigationIT.java b/test/cdi/src/test/java/com/vaadin/test/cdi/CDINavigationIT.java new file mode 100644 index 0000000000..017a229d08 --- /dev/null +++ b/test/cdi/src/test/java/com/vaadin/test/cdi/CDINavigationIT.java @@ -0,0 +1,88 @@ +package com.vaadin.test.cdi; + +import java.util.List; +import java.util.stream.Collectors; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.openqa.selenium.By; + +import com.vaadin.testbench.annotations.RunLocally; +import com.vaadin.testbench.elements.ButtonElement; +import com.vaadin.testbench.elements.CssLayoutElement; +import com.vaadin.testbench.elements.LabelElement; +import com.vaadin.testbench.parallel.Browser; +import com.vaadin.testbench.parallel.ParallelRunner; +import com.vaadin.testbench.parallel.ParallelTest; + +@RunWith(ParallelRunner.class) +@RunLocally(Browser.PHANTOMJS) +public class CDINavigationIT extends ParallelTest { + + protected static final String BASE_URL = "http://localhost:8080"; + + @Override + public void setup() throws Exception { + super.setup(); + + testBench().resizeViewPortTo(1024, 600); + getDriver().get(getTestURL()); + } + + protected String getUIPath() { + return "/"; + } + + protected String getViewSeparator() { + return "#!"; + } + + @Test + public void testNavigation() { + navigateTo("new"); + navigateTo("persisting"); + navigateTo("param/foo"); + } + + @Test + public void testReloadPage() { + navigateTo("name/foo"); + navigateTo("name/bar"); + + List content = getLogContent(); + + getDriver().navigate().refresh(); + Assert.assertTrue(isElementPresent(By.id("name"))); + Assert.assertEquals("Content was lost when reloading", content, + getLogContent()); + } + + protected String getTestURL() { + return BASE_URL + getUIPath(); + } + + private List getLogContent() { + return $(CssLayoutElement.class).$$(LabelElement.class).all().stream() + .map(LabelElement::getText).collect(Collectors.toList()); + } + + protected void navigateTo(String state) { + navigateTo(state, getUIPath(), getViewSeparator()); + } + + protected void navigateTo(String state, String uiPath, + String viewSeparator) { + $(ButtonElement.class).caption(state).first().click(); + + String id = state; + if (id.contains("/")) { + id = id.substring(0, id.indexOf("/")); + } + + Assert.assertTrue(isElementPresent(By.id(id))); + Assert.assertEquals("Navigation to state '" + state + "' failed", + BASE_URL + uiPath + viewSeparator + state, + getDriver().getCurrentUrl()); + } +} diff --git a/test/cdi/src/test/java/com/vaadin/test/cdi/CDIPushStateNavigationIT.java b/test/cdi/src/test/java/com/vaadin/test/cdi/CDIPushStateNavigationIT.java new file mode 100644 index 0000000000..481bb9a775 --- /dev/null +++ b/test/cdi/src/test/java/com/vaadin/test/cdi/CDIPushStateNavigationIT.java @@ -0,0 +1,13 @@ +package com.vaadin.test.cdi; + +public class CDIPushStateNavigationIT extends CDINavigationIT { + + protected String getUIPath() { + return "/subpath"; + } + + @Override + protected String getViewSeparator() { + return "/"; + } +} diff --git a/test/cdi/src/test/java/com/vaadin/test/cdi/VaadinCDISmokeIT.java b/test/cdi/src/test/java/com/vaadin/test/cdi/VaadinCDISmokeIT.java index 95cebdc974..3cb1169a35 100644 --- a/test/cdi/src/test/java/com/vaadin/test/cdi/VaadinCDISmokeIT.java +++ b/test/cdi/src/test/java/com/vaadin/test/cdi/VaadinCDISmokeIT.java @@ -3,36 +3,197 @@ package com.vaadin.test.cdi; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -import org.junit.Before; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + import org.junit.Rule; import org.junit.Test; -import org.openqa.selenium.phantomjs.PhantomJSDriver; +import org.junit.runner.RunWith; +import com.vaadin.test.cdi.ui.RootPathUI; +import com.vaadin.test.cdi.views.GreetingService; +import com.vaadin.test.cdi.views.GreetingView; import com.vaadin.testbench.ScreenshotOnFailureRule; -import com.vaadin.testbench.TestBenchTestCase; +import com.vaadin.testbench.annotations.RunLocally; import com.vaadin.testbench.elements.ButtonElement; +import com.vaadin.testbench.elements.CssLayoutElement; +import com.vaadin.testbench.elements.LabelElement; import com.vaadin.testbench.elements.NotificationElement; +import com.vaadin.testbench.parallel.Browser; +import com.vaadin.testbench.parallel.ParallelRunner; +import com.vaadin.testbench.parallel.ParallelTest; + +@RunWith(ParallelRunner.class) +@RunLocally(Browser.PHANTOMJS) +public class VaadinCDISmokeIT extends ParallelTest { -public class VaadinCDISmokeIT extends TestBenchTestCase { + private static final String BASE_URL = "http://localhost:8080/"; @Rule public ScreenshotOnFailureRule rule = new ScreenshotOnFailureRule(this, true); - @Before - public void setup() { - // Screenshot rule tears down the driver - setDriver(new PhantomJSDriver()); + @Override + public void setup() throws Exception { + super.setup(); + + testBench().resizeViewPortTo(1024, 600); } @Test public void testPageLoadsAndCanBeInterractedWith() { - getDriver().navigate().to("http://localhost:8080/"); + getDriver().navigate().to(BASE_URL); - $(ButtonElement.class).first().click(); + $(ButtonElement.class).caption("Click Me").first().click(); assertTrue($(NotificationElement.class).exists()); assertEquals(ThankYouServiceImpl.THANK_YOU_TEXT, $(NotificationElement.class).first().getText()); } + + @Test + public void testAlwaysNewViewIsRecrated() { + getDriver().navigate().to(BASE_URL); + navigateAndGetLogContent("new"); + + // A new navigation event should happen when navigating to view again + navigateAndGetLogContent("new"); + + assertEquals("GreetingService should've been only called once.", + String.format(GreetingView.CALL_COUNT_FORMAT, 1), + $(LabelElement.class).id("callCount").getText()); + } + + @Test + public void testParameterChangeRecreatedView() { + GreetingService service = new GreetingService(); + + getDriver().navigate().to(BASE_URL); + navigateAndGetLogContent("param"); + + assertEquals("Greeting service was not called with empty parameter.", + service.getGreeting(""), $(CssLayoutElement.class).id("log") + .$(LabelElement.class).first().getText()); + + // Navigation event is fired with same view and different parameters + navigateAndGetLogContent("param", "foo"); + + assertEquals("Greeting service was not called with correct parameters.", + service.getGreeting("foo"), $(CssLayoutElement.class).id("log") + .$(LabelElement.class).first().getText()); + + assertEquals("GreetingService should've been only called once.", + String.format(GreetingView.CALL_COUNT_FORMAT, 1), + $(LabelElement.class).id("callCount").getText()); + } + + @Test + public void testParameterChangeUsesSameView() { + GreetingService service = new GreetingService(); + + getDriver().navigate().to(BASE_URL); + navigateAndGetLogContent("name", "foo"); + + assertEquals("Greeting service was not called with 'foo' parameter.", + service.getGreeting("foo"), $(CssLayoutElement.class).id("log") + .$(LabelElement.class).first().getText()); + + // Navigation event fired with same view and different parameters + navigateAndGetLogContent("name", "bar"); + + assertEquals("GreetingService should've been only called twice.", + String.format(GreetingView.CALL_COUNT_FORMAT, 2), + $(LabelElement.class).id("callCount").getText()); + + assertEquals("Greeting service was not called with 'bar' parameter.", + service.getGreeting("bar"), $(CssLayoutElement.class).id("log") + .$(LabelElement.class).get(1).getText()); + } + + @Test + public void testUIScopedView() { + GreetingService service = new GreetingService(); + List expectedLogContents = new ArrayList<>(); + + getDriver().navigate().to(BASE_URL); + expectedLogContents + .add(navigateAndGetLogContent("persisting", "foo", service)); + + // Navigation event should with same view and different parameters + expectedLogContents + .add(navigateAndGetLogContent("persisting", "", service)); + + // Navigating to another view should not lose the UI Scoped view. + navigateAndGetLogContent("new"); + expectedLogContents + .add(navigateAndGetLogContent("persisting", "", service)); + + assertEquals("GreetingService unexpected call count", + String.format(GreetingView.CALL_COUNT_FORMAT, + service.getCallCount()), + $(LabelElement.class).id("callCount").getText()); + + assertEquals("Unexpected contents in the log.", expectedLogContents, + $(CssLayoutElement.class).id("log").$(LabelElement.class).all() + .stream().map(LabelElement::getText) + .collect(Collectors.toList())); + } + + @Test + public void testPreserveOnRefreshWithViewName() { + GreetingService service = new GreetingService(); + List expectedLogContents = new ArrayList<>(); + + getDriver().navigate().to(BASE_URL); + expectedLogContents + .add(navigateAndGetLogContent("name", "foo", service)); + + // Navigation event should with same view and different parameters + expectedLogContents + .add(navigateAndGetLogContent("name", "bar", service)); + + // Reload the page. + getDriver().navigate().refresh(); + + assertEquals("GreetingService should've been only called twice.", + String.format(GreetingView.CALL_COUNT_FORMAT, + service.getCallCount()), + $(LabelElement.class).id("callCount").getText()); + + assertEquals("Unexpected contents in the log.", expectedLogContents, + $(CssLayoutElement.class).id("log").$(LabelElement.class).all() + .stream().map(LabelElement::getText) + .collect(Collectors.toList())); + } + + private void assertNavigationNotification(String string) { + waitUntil(e -> isElementPresent(NotificationElement.class)); + + NotificationElement notification = $(NotificationElement.class).first(); + assertEquals(String.format(RootPathUI.NAVIGATION_TEXT, string), + notification.getText()); + + // Close all notifications. + while (isElementPresent(NotificationElement.class)) { + $(NotificationElement.class).first().close(); + } + } + + private String navigateAndGetLogContent(String viewName) { + return navigateAndGetLogContent(viewName, "", new GreetingService()); + } + + private String navigateAndGetLogContent(String viewName, String parameter) { + return navigateAndGetLogContent(viewName, parameter, new GreetingService()); + } + + private String navigateAndGetLogContent(String viewName, String parameter, + GreetingService service) { + String fullViewName = viewName + + (parameter.isEmpty() ? "" : "/" + parameter); + $(ButtonElement.class).caption(fullViewName).first().click(); + assertNavigationNotification(viewName); + return service.getGreeting(parameter); + } } -- 2.39.5