use of com.vaadin.flow.component.page.Page in project flow by vaadin.
the class UIInternals method addExternalDependencies.
private void addExternalDependencies(DependencyInfo dependency) {
Page page = ui.getPage();
dependency.getJavaScripts().stream().filter(js -> UrlUtil.isExternal(js.value())).forEach(js -> page.addJavaScript(js.value(), js.loadMode()));
dependency.getJsModules().stream().filter(js -> UrlUtil.isExternal(js.value())).forEach(js -> page.addJsModule(js.value()));
}
use of com.vaadin.flow.component.page.Page in project flow by vaadin.
the class UIInternals method addComponentDependencies.
/**
* Adds the dependencies defined using {@link StyleSheet} or
* {@link JavaScript} on the given Component class.
*
* @param componentClass
* the component class to read annotations from
*/
public void addComponentDependencies(Class<? extends Component> componentClass) {
Page page = ui.getPage();
DependencyInfo dependencies = ComponentUtil.getDependencies(session.getService(), componentClass);
// In npm mode, add external JavaScripts directly to the page.
addExternalDependencies(dependencies);
addFallbackDependencies(dependencies);
dependencies.getStyleSheets().forEach(styleSheet -> page.addStyleSheet(styleSheet.value(), styleSheet.loadMode()));
}
use of com.vaadin.flow.component.page.Page in project flow by vaadin.
the class JavaScriptBootstrapUITest method server_should_not_doClientRoute_when_navigatingToServer.
@Test
public void server_should_not_doClientRoute_when_navigatingToServer() {
ui.connectClient("foo", "bar", "/clean", "", null);
assertEquals(Tag.HEADER, ui.wrapperElement.getChild(0).getTag());
assertEquals(Tag.H2, ui.wrapperElement.getChild(0).getChild(0).getTag());
ui = Mockito.spy(ui);
Page page = mockPage();
ArgumentCaptor<String> execJs = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<String> execArg = ArgumentCaptor.forClass(String.class);
// Dirty view is allowed after clean view
ui.navigate("dirty");
// A server navigation happens
assertEquals(Tag.SPAN, ui.wrapperElement.getChild(0).getTag());
Mockito.verify(page).executeJs(execJs.capture(), execArg.capture());
assertEquals(CLIENT_PUSHSTATE_TO, execJs.getValue());
final List<String> execValues = execArg.getAllValues();
assertEquals(2, execValues.size());
assertNull(execValues.get(0));
assertEquals("dirty", execArg.getValue());
}
use of com.vaadin.flow.component.page.Page in project flow by vaadin.
the class JavaScriptBootstrapUITest method should_invoke_clientRoute_when_navigationHasNotBeenStarted.
@Test
public void should_invoke_clientRoute_when_navigationHasNotBeenStarted() {
ui = Mockito.spy(ui);
Page page = mockPage();
ArgumentCaptor<String> execJs = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<String> execArg = ArgumentCaptor.forClass(String.class);
ui.navigate("whatever");
Mockito.verify(page).executeJs(execJs.capture(), execArg.capture());
assertEquals(CLIENT_NAVIGATE_TO, execJs.getValue());
assertEquals("whatever", execArg.getValue());
}
use of com.vaadin.flow.component.page.Page in project flow by vaadin.
the class NavigationStateRendererTest method handle_variousInputs_checkPushStateShouldBeCalledOrNot.
@Test
public // - navigation trigger is PAGE_LOAD, HISTORY, or PROGRAMMATIC
void handle_variousInputs_checkPushStateShouldBeCalledOrNot() {
// given a service with instantiator
MockVaadinServletService service = createMockServiceWithInstantiator();
// given a locked session
MockVaadinSession session = new AlwaysLockedVaadinSession(service);
session.setConfiguration(new MockDeploymentConfiguration());
// given a NavigationStateRenderer mapping to RegularView
new NavigationStateBuilder(router).withTarget(RegularView.class).build();
NavigationStateRenderer renderer = new NavigationStateRenderer(navigationStateFromTarget(RegularView.class));
// given a UI with an instrumented Page that records
// getHistory().pushState calls
AtomicBoolean pushStateCalled = new AtomicBoolean(false);
List<Location> pushStateLocations = new ArrayList<>();
MockUI ui = new MockUI(session) {
final Page page = new Page(this) {
final History history = new History(getUI().get()) {
@Override
public void pushState(JsonValue state, Location location) {
pushStateCalled.set(true);
pushStateLocations.add(location);
}
};
@Override
public History getHistory() {
return history;
}
};
@Override
public Page getPage() {
return page;
}
};
renderer.handle(new NavigationEvent(new Router(new TestRouteRegistry()), new Location("regular"), ui, NavigationTrigger.UI_NAVIGATE, null, true));
Assert.assertFalse("No pushState invocation is expected when forwardTo is true.", pushStateCalled.get());
ui.getInternals().clearLastHandledNavigation();
renderer.handle(new NavigationEvent(new Router(new TestRouteRegistry()), new Location("regular"), ui, NavigationTrigger.PROGRAMMATIC));
Assert.assertFalse("No pushState invocation is expected when navigation trigger is PROGRAMMATIC.", pushStateCalled.get());
ui.getInternals().clearLastHandledNavigation();
renderer.handle(new NavigationEvent(new Router(new TestRouteRegistry()), new Location("regular"), ui, NavigationTrigger.HISTORY));
Assert.assertFalse("No pushState invocation is expected when navigation trigger is HISTORY.", pushStateCalled.get());
ui.getInternals().clearLastHandledNavigation();
renderer.handle(new NavigationEvent(new Router(new TestRouteRegistry()), new Location("regular"), ui, NavigationTrigger.PAGE_LOAD));
Assert.assertFalse("No pushState invocation is expected when navigation trigger is PAGE_LOAD.", pushStateCalled.get());
pushStateCalled.set(false);
pushStateLocations.clear();
ui.getInternals().clearLastHandledNavigation();
renderer.handle(new NavigationEvent(new Router(new TestRouteRegistry()), new Location("regular"), ui, NavigationTrigger.UI_NAVIGATE));
Assert.assertTrue("pushState invocation is expected.", pushStateCalled.get());
Assert.assertTrue(pushStateLocations.stream().anyMatch(location -> location.getPath().equals("regular")));
pushStateCalled.set(false);
renderer.handle(new NavigationEvent(new Router(new TestRouteRegistry()), new Location("regular"), ui, NavigationTrigger.UI_NAVIGATE));
Assert.assertFalse("No pushState invocation is expected when navigating to the current location.", pushStateCalled.get());
}
Aggregations