use of com.vaadin.flow.server.PwaConfiguration in project flow by vaadin.
the class FrontendDependencies method computePwaConfiguration.
/**
* Find the class with a {@link com.vaadin.flow.server.PWA} annotation and
* read it into a {@link com.vaadin.flow.server.PwaConfiguration} object.
*
* @throws ClassNotFoundException
*/
private void computePwaConfiguration() throws ClassNotFoundException {
FrontendAnnotatedClassVisitor pwaVisitor = new FrontendAnnotatedClassVisitor(getFinder(), PWA.class.getName());
Class<?> appShellConfiguratorClass = getFinder().loadClass(AppShellConfigurator.class.getName());
for (Class<?> hopefullyAppShellClass : getFinder().getAnnotatedClasses(PWA.class.getName())) {
if (!Arrays.asList(hopefullyAppShellClass.getInterfaces()).contains(appShellConfiguratorClass)) {
throw new IllegalStateException(ERROR_INVALID_PWA_ANNOTATION);
}
pwaVisitor.visitClass(hopefullyAppShellClass.getName());
}
Set<String> dependencies = pwaVisitor.getValues("name");
if (dependencies.size() > 1) {
throw new IllegalStateException(ERROR_INVALID_PWA_ANNOTATION);
}
if (dependencies.isEmpty()) {
this.pwaConfiguration = new PwaConfiguration(useV14Bootstrap);
return;
}
String name = pwaVisitor.getValue("name");
String shortName = pwaVisitor.getValue("shortName");
String description = pwaVisitor.getValue("description");
String backgroundColor = pwaVisitor.getValue("backgroundColor");
String themeColor = pwaVisitor.getValue("themeColor");
String iconPath = pwaVisitor.getValue("iconPath");
String manifestPath = pwaVisitor.getValue("manifestPath");
String offlinePath = pwaVisitor.getValue("offlinePath");
String display = pwaVisitor.getValue("display");
String startPath = pwaVisitor.getValue("startPath");
List<String> offlineResources = pwaVisitor.getValue("offlineResources");
boolean offline = pwaVisitor.getValue("offline");
this.pwaConfiguration = new PwaConfiguration(true, name, shortName, description, backgroundColor, themeColor, iconPath, manifestPath, offlinePath, display, startPath, offlineResources.toArray(new String[] {}), offline, useV14Bootstrap);
}
use of com.vaadin.flow.server.PwaConfiguration in project flow by vaadin.
the class PwaHandlerTest method handleRequest_pwaRegistryConfigIsDisabled_returnsFalse.
@Test
public void handleRequest_pwaRegistryConfigIsDisabled_returnsFalse() throws IOException {
PwaRegistry registry = Mockito.mock(PwaRegistry.class);
PwaConfiguration configuration = Mockito.mock(PwaConfiguration.class);
Mockito.when(registry.getPwaConfiguration()).thenReturn(configuration);
Mockito.when(configuration.isEnabled()).thenReturn(false);
PwaHandler handler = new PwaHandler(() -> registry);
Assert.assertFalse(handler.handleRequest(session, request, response));
}
use of com.vaadin.flow.server.PwaConfiguration in project flow by vaadin.
the class PwaHandlerTest method handleRequest_pwaRegistryConfigIsEnabled_pathIsPwaResource_returnsTrue.
@Test
public void handleRequest_pwaRegistryConfigIsEnabled_pathIsPwaResource_returnsTrue() throws IOException {
PwaRegistry registry = Mockito.mock(PwaRegistry.class);
PwaConfiguration configuration = Mockito.mock(PwaConfiguration.class);
Mockito.when(registry.getPwaConfiguration()).thenReturn(configuration);
Mockito.when(configuration.isEnabled()).thenReturn(true);
PwaHandler handler = new PwaHandler(() -> registry);
Mockito.when(response.getWriter()).thenReturn(new PrintWriter(new StringWriter()));
Mockito.when(registry.getRuntimeServiceWorkerJs()).thenReturn("");
Mockito.when(request.getPathInfo()).thenReturn("/sw-runtime-resources-precache.js");
Assert.assertTrue(handler.handleRequest(session, request, response));
}
use of com.vaadin.flow.server.PwaConfiguration in project flow by vaadin.
the class WebComponentBootstrapHandlerTest method writeBootstrapPage_noPWA.
@Test
public void writeBootstrapPage_noPWA() throws IOException, ServiceException {
TestWebComponentBootstrapHandler handler = new TestWebComponentBootstrapHandler();
PwaRegistry registry = Mockito.mock(PwaRegistry.class);
PwaConfiguration conf = Mockito.mock(PwaConfiguration.class);
Mockito.when(registry.getPwaConfiguration()).thenReturn(conf);
Mockito.when(conf.isEnabled()).thenReturn(true);
Mockito.when(conf.getManifestPath()).thenReturn("bar");
PwaIcon icon = Mockito.mock(PwaIcon.class);
Mockito.when(icon.asElement()).thenReturn(new Element("h1"));
Mockito.when(registry.getHeaderIcons()).thenReturn(Collections.singletonList(icon));
VaadinServletService service = new MockVaadinServletService() {
@Override
protected PwaRegistry getPwaRegistry() {
return registry;
}
};
initLookup(service);
VaadinSession session = new MockVaadinSession(service);
session.lock();
session.setConfiguration(service.getDeploymentConfiguration());
MockDeploymentConfiguration config = (MockDeploymentConfiguration) service.getDeploymentConfiguration();
config.setEnableDevServer(false);
VaadinServletRequest request = Mockito.mock(VaadinServletRequest.class);
Mockito.when(request.getService()).thenReturn(service);
Mockito.when(request.getServletPath()).thenReturn("/");
VaadinResponse response = getMockResponse(null);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Mockito.when(response.getOutputStream()).thenReturn(stream);
handler.synchronizedHandleRequest(session, request, response);
String result = stream.toString(StandardCharsets.UTF_8.name());
MatcherAssert.assertThat(result, CoreMatchers.not(CoreMatchers.containsString("bar")));
MatcherAssert.assertThat(result, CoreMatchers.not(CoreMatchers.containsString("h1")));
MatcherAssert.assertThat(result, CoreMatchers.not(CoreMatchers.containsString("baz")));
}
use of com.vaadin.flow.server.PwaConfiguration in project flow by vaadin.
the class AbstractScannerPwaTest method should_returnDefaultConfiguration_When_AppShellWithoutPwa.
@Test
public void should_returnDefaultConfiguration_When_AppShellWithoutPwa() throws Exception {
PwaConfiguration pwaConfiguration = getPwaConfiguration(AppShellWithoutPwa.class);
assertEquals(PwaConfiguration.DEFAULT_NAME, pwaConfiguration.getAppName());
assertEquals("Flow PWA", pwaConfiguration.getShortName());
assertEquals("", pwaConfiguration.getDescription());
assertEquals(PwaConfiguration.DEFAULT_DISPLAY, pwaConfiguration.getDisplay());
assertEquals(PwaConfiguration.DEFAULT_BACKGROUND_COLOR, pwaConfiguration.getBackgroundColor());
assertEquals(PwaConfiguration.DEFAULT_THEME_COLOR, pwaConfiguration.getThemeColor());
assertEquals(PwaConfiguration.DEFAULT_ICON, pwaConfiguration.getIconPath());
assertEquals(PwaConfiguration.DEFAULT_PATH, pwaConfiguration.getManifestPath());
assertEquals(PwaConfiguration.DEFAULT_OFFLINE_PATH, pwaConfiguration.getOfflinePath());
String[] defaultOfflineResources = {};
assertArrayEquals(defaultOfflineResources, pwaConfiguration.getOfflineResources().toArray());
}
Aggregations