Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2000-2021 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.cdi.itest.routecontext;

import jakarta.annotation.PostConstruct;
import jakarta.enterprise.inject.Instance;
import jakarta.inject.Inject;

import com.vaadin.cdi.annotation.CdiComponent;
import com.vaadin.cdi.annotation.RouteScopeOwner;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.NativeLabel;
import com.vaadin.flow.router.Route;

/**
* A plain sibling of {@link LayoutScopedPreservedView} under the same layout.
* The layout instance is reused when navigating between the two, so the beans
* it owns must not be recreated.
*/
@Route(value = "layout-scoped-plain", layout = MainLayout.class)
@CdiComponent
public class LayoutScopedPlainView extends Div {

public static final String LAYOUT_BEAN_LABEL = "plain-layout-bean";

@Inject
@RouteScopeOwner(MainLayout.class)
private Instance<MainLayoutBean> injection;

@PostConstruct
private void init() {
NativeLabel beanLabel = new NativeLabel(injection.get().getData());
beanLabel.setId(LAYOUT_BEAN_LABEL);
add(beanLabel);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2000-2021 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.cdi.itest.routecontext;

import jakarta.enterprise.inject.Instance;
import jakarta.inject.Inject;

import com.vaadin.cdi.annotation.CdiComponent;
import com.vaadin.cdi.annotation.RouteScopeOwner;
import com.vaadin.flow.component.AttachEvent;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.NativeLabel;
import com.vaadin.flow.router.PreserveOnRefresh;
import com.vaadin.flow.router.Route;

/**
* A preserved child of {@link MainLayout}, which is not preserved itself. Flow
* preserves the whole chain, so the beans owned by the layout are shared with
* the UI created by a page refresh.
*/
@PreserveOnRefresh
@Route(value = "layout-scoped-preserved", layout = MainLayout.class)
@CdiComponent
public class LayoutScopedPreservedView extends Div {

public static final String LAYOUT_BEAN_LABEL = "preserved-layout-bean";

@Inject
@RouteScopeOwner(MainLayout.class)
private Instance<MainLayoutBean> injection;

private NativeLabel beanLabel;

@Override
protected void onAttach(AttachEvent attachEvent) {
if (beanLabel != null) {
remove(beanLabel);
}
beanLabel = new NativeLabel(injection.get().getData());
beanLabel.setId(LAYOUT_BEAN_LABEL);
add(beanLabel);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,19 @@ public class MainLayout extends Div implements RouterLayout {
public static final String INVALID = "invalid";
public static final String PARENT_NO_OWNER = "parent-no-owner";
public static final String CHILD_NO_OWNER = "child-no-owner";
public static final String LAYOUT_PRESERVED = "layout-preserved";
public static final String LAYOUT_PLAIN = "layout-plain";

private NativeLabel uiIdLabel;

public MainLayout() {
add(new RouterLink(PRESERVE, PreserveOnRefreshView.class),
new RouterLink(INVALID, InvalidView.class),
new RouterLink(PARENT_NO_OWNER, ParentNoOwnerView.class),
new RouterLink(CHILD_NO_OWNER, ChildNoOwnerView.class));
new RouterLink(CHILD_NO_OWNER, ChildNoOwnerView.class),
new RouterLink(LAYOUT_PRESERVED,
LayoutScopedPreservedView.class),
new RouterLink(LAYOUT_PLAIN, LayoutScopedPlainView.class));
;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2000-2021 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.cdi.itest.routecontext;

import jakarta.annotation.PostConstruct;

import java.util.UUID;

import com.vaadin.cdi.annotation.CdiComponent;
import com.vaadin.cdi.annotation.RouteScopeOwner;
import com.vaadin.cdi.annotation.RouteScoped;

/**
* A bean owned by the layout shared by a preserved and a plain child view.
*/
@RouteScoped
@RouteScopeOwner(MainLayout.class)
@CdiComponent
public class MainLayoutBean extends AbstractCountedBean {

@PostConstruct
private void init() {
setData(UUID.randomUUID().toString());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WindowType;

import com.vaadin.cdi.itest.routecontext.ApartBean;
import com.vaadin.cdi.itest.routecontext.AssignedBean;
Expand All @@ -39,7 +40,10 @@
import com.vaadin.cdi.itest.routecontext.EventView;
import com.vaadin.cdi.itest.routecontext.LayoutEventView;
import com.vaadin.cdi.itest.routecontext.LayoutEventView2;
import com.vaadin.cdi.itest.routecontext.LayoutScopedPlainView;
import com.vaadin.cdi.itest.routecontext.LayoutScopedPreservedView;
import com.vaadin.cdi.itest.routecontext.MainLayout;
import com.vaadin.cdi.itest.routecontext.MainLayoutBean;
import com.vaadin.cdi.itest.routecontext.MasterView;
import com.vaadin.cdi.itest.routecontext.PostponeView;
import com.vaadin.cdi.itest.routecontext.PreserveOnRefreshBean;
Expand Down Expand Up @@ -297,6 +301,76 @@ public void routeScopedBeanIsDestroyedOnNavigationOutOfViewAfterPreserveOnRefres
assertDestroyed(PreserveOnRefreshBean.class, 1);
}

@Test
public void noPreserveOnRefresh_routeTargetIsRecreatedOnRefresh()
throws IOException {
getDriver().get(getDriver().getCurrentUrl());

// UI ID has to be updated: all bean creations/removals will be done
// now within the new UI
uiId = getText(MainLayout.UIID);

assertConstructed(RootView.class, 1);
assertDestroyed(RootView.class, 0);
assertRootViewIsRendered();
}

@Test
public void noPreserveOnRefresh_otherUIWithSameWindowName_routeTargetIsNotShared()
throws IOException {
String url = getDriver().getCurrentUrl();
String windowName = (String) executeScript("return window.name;");

// Open a second tab and make it report the window name of the first
// one, as a duplicated tab or a restored browser session does. Both
// UIs are alive at the same time, so a route scope keyed by window
// name would be shared between them.
getDriver().switchTo().newWindow(WindowType.TAB);
getDriver().get(url);
executeScript("window.name = arguments[0];", windowName);
// same origin reload, so the window name is retained
getDriver().navigate().refresh();

uiId = getText(MainLayout.UIID);

assertConstructed(RootView.class, 1);
assertDestroyed(RootView.class, 0);
assertRootViewIsRendered();
}

@Test
public void sharedLayout_navigateBetweenPreservedAndPlainChild_layoutBeanIsKept()
throws IOException {
follow(MainLayout.LAYOUT_PRESERVED);
String beanData = getText(LayoutScopedPreservedView.LAYOUT_BEAN_LABEL);

assertConstructed(MainLayoutBean.class, 1);
assertDestroyed(MainLayoutBean.class, 0);

// the layout instance is reused, so the beans it owns must not be
// recreated even if the navigation chain is not preserved anymore
follow(MainLayout.LAYOUT_PLAIN);

Assert.assertEquals(beanData,
getText(LayoutScopedPlainView.LAYOUT_BEAN_LABEL));
assertConstructed(MainLayoutBean.class, 1);
assertDestroyed(MainLayoutBean.class, 0);

// ... and the other way around
follow(MainLayout.LAYOUT_PRESERVED);

Assert.assertEquals(beanData,
getText(LayoutScopedPreservedView.LAYOUT_BEAN_LABEL));
assertConstructed(MainLayoutBean.class, 1);
assertDestroyed(MainLayoutBean.class, 0);
}

private void assertRootViewIsRendered() {
Assert.assertNotNull(
"The root view is expected to be rendered in the current UI",
findElement(By.linkText(RootView.MASTER)));
}

@Test
public void preserveOnRefresh_beanIsNotDestroyed() throws IOException {
follow(MainLayout.PRESERVE);
Expand Down
Loading
Loading