Search in sources :

Example 1 with PwaConfiguration

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);
}
Also used : AppShellConfigurator(com.vaadin.flow.component.page.AppShellConfigurator) PwaConfiguration(com.vaadin.flow.server.PwaConfiguration) PWA(com.vaadin.flow.server.PWA)

Example 2 with PwaConfiguration

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));
}
Also used : PwaConfiguration(com.vaadin.flow.server.PwaConfiguration) PwaRegistry(com.vaadin.flow.server.PwaRegistry) Test(org.junit.Test)

Example 3 with PwaConfiguration

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));
}
Also used : PwaConfiguration(com.vaadin.flow.server.PwaConfiguration) StringWriter(java.io.StringWriter) PwaRegistry(com.vaadin.flow.server.PwaRegistry) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 4 with PwaConfiguration

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")));
}
Also used : MockVaadinServletService(com.vaadin.flow.server.MockVaadinServletService) MockVaadinSession(com.vaadin.flow.server.MockVaadinSession) VaadinSession(com.vaadin.flow.server.VaadinSession) Element(org.jsoup.nodes.Element) MockDeploymentConfiguration(com.vaadin.tests.util.MockDeploymentConfiguration) VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) MockVaadinServletService(com.vaadin.flow.server.MockVaadinServletService) VaadinServletService(com.vaadin.flow.server.VaadinServletService) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PwaIcon(com.vaadin.flow.server.PwaIcon) VaadinResponse(com.vaadin.flow.server.VaadinResponse) PwaConfiguration(com.vaadin.flow.server.PwaConfiguration) MockVaadinSession(com.vaadin.flow.server.MockVaadinSession) PwaRegistry(com.vaadin.flow.server.PwaRegistry) Test(org.junit.Test)

Example 5 with PwaConfiguration

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());
}
Also used : PwaConfiguration(com.vaadin.flow.server.PwaConfiguration) Test(org.junit.Test)

Aggregations

PwaConfiguration (com.vaadin.flow.server.PwaConfiguration)13 Test (org.junit.Test)10 PwaRegistry (com.vaadin.flow.server.PwaRegistry)3 AppShellConfigurator (com.vaadin.flow.component.page.AppShellConfigurator)2 PWA (com.vaadin.flow.server.PWA)2 File (java.io.File)2 Lookup (com.vaadin.flow.di.Lookup)1 MockVaadinServletService (com.vaadin.flow.server.MockVaadinServletService)1 MockVaadinSession (com.vaadin.flow.server.MockVaadinSession)1 PwaIcon (com.vaadin.flow.server.PwaIcon)1 VaadinResponse (com.vaadin.flow.server.VaadinResponse)1 VaadinServletRequest (com.vaadin.flow.server.VaadinServletRequest)1 VaadinServletService (com.vaadin.flow.server.VaadinServletService)1 VaadinSession (com.vaadin.flow.server.VaadinSession)1 MockDeploymentConfiguration (com.vaadin.tests.util.MockDeploymentConfiguration)1 JsonObject (elemental.json.JsonObject)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 Annotation (java.lang.annotation.Annotation)1