diff options
Diffstat (limited to 'test/cdi/src')
14 files changed, 505 insertions, 36 deletions
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<String> 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<String> 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<String> 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<String> 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); + } } |