1616package com .vaadin .flow .server ;
1717
1818import javax .servlet .ServletContext ;
19-
2019import java .io .ByteArrayOutputStream ;
2120import java .io .IOException ;
2221import java .util .List ;
2625import org .junit .Assert ;
2726import org .junit .BeforeClass ;
2827import org .junit .Test ;
28+ import org .mockito .MockedStatic ;
2929import org .mockito .Mockito ;
3030
31+ import com .vaadin .experimental .FeatureFlags ;
32+ import com .vaadin .flow .server .startup .ApplicationConfiguration ;
33+
3134@ PWA (name = "foo" , shortName = "bar" )
3235public class PwaRegistryTest {
3336
3437 @ PWA (name = "Custom Icon Path" , shortName = "CIP" , iconPath = "icons/splash/foo.png" )
3538 private static class PwaWithCustomIconPath {
3639 }
3740
41+ @ PWA (name = "Custom Icon Path" , shortName = "COP" , offlinePath = "some/path.html" )
42+ private static class PwaWithCustomOfflinePath {
43+ }
44+
3845 private static List <PwaIcon > splashIconsForAppleDevices ;
3946
4047 @ BeforeClass
4148 public static void initPwaWithCustomIconPath () throws IOException {
42- ServletContext context = Mockito .mock (ServletContext .class );
43- PwaRegistry registry = new PwaRegistry (
44- PwaWithCustomIconPath .class .getAnnotation (PWA .class ), context );
49+ PwaRegistry registry = preparePwaRegistry (
50+ PwaWithCustomIconPath .class .getAnnotation (PWA .class ), true );
4551 splashIconsForAppleDevices = registry .getIcons ().stream ().filter (
4652 icon -> "apple-touch-startup-image" .equals (icon .getRel ()))
4753 .collect (Collectors .toList ());
@@ -50,15 +56,14 @@ public static void initPwaWithCustomIconPath() throws IOException {
5056 @ Test
5157 public void pwaIconIsGeneratedBasedOnClasspathIcon_servletContextHasNoResources ()
5258 throws IOException {
53- ServletContext context = Mockito .mock (ServletContext .class );
5459 // PWA annotation has default value for "iconPath" but servlet context
5560 // has no resource for that path, in that case the ClassPath URL will be
5661 // checked which is "META-INF/resources/icons/icon.png" (this path
5762 // available is in the test resources folder). The icon in this path
5863 // differs from the default icon and set of icons will be generated
5964 // based on it
60- PwaRegistry registry = new PwaRegistry (
61- PwaRegistryTest .class .getAnnotation (PWA .class ), context );
65+ PwaRegistry registry = preparePwaRegistry (
66+ PwaRegistryTest .class .getAnnotation (PWA .class ), true );
6267 List <PwaIcon > icons = registry .getIcons ();
6368 // This icon has width 32 and it's generated based on a custom icon (see
6469 // above)
@@ -70,6 +75,40 @@ public void pwaIconIsGeneratedBasedOnClasspathIcon_servletContextHasNoResources(
7075 Assert .assertEquals (26 , stream .toByteArray ()[36 ]);
7176 }
7277
78+ private static PwaRegistry preparePwaRegistry (PWA pwa , boolean viteEnabled )
79+ throws IOException {
80+ try (MockedStatic <VaadinService > vaadinService = Mockito
81+ .mockStatic (VaadinService .class );
82+ MockedStatic <ApplicationConfiguration > configuration = Mockito
83+ .mockStatic (ApplicationConfiguration .class );
84+ MockedStatic <FeatureFlags > featureFlags = Mockito
85+ .mockStatic (FeatureFlags .class )) {
86+
87+ VaadinService vaadinServiceMocked = Mockito
88+ .mock (VaadinService .class );
89+ VaadinContext vaadinContext = Mockito .mock (VaadinContext .class );
90+ ApplicationConfiguration applicationConfiguration = Mockito
91+ .mock (ApplicationConfiguration .class );
92+
93+ vaadinService .when (VaadinService ::getCurrent )
94+ .thenReturn (vaadinServiceMocked );
95+ Mockito .when (vaadinServiceMocked .getContext ())
96+ .thenReturn (vaadinContext );
97+ configuration
98+ .when (() -> ApplicationConfiguration .get (Mockito .any ()))
99+ .thenReturn (applicationConfiguration );
100+
101+ FeatureFlags flags = Mockito .mock (FeatureFlags .class );
102+ Mockito .when (flags .isEnabled (FeatureFlags .VITE ))
103+ .thenReturn (viteEnabled );
104+ featureFlags .when (() -> FeatureFlags .get (Mockito .any ()))
105+ .thenReturn (flags );
106+
107+ ServletContext context = Mockito .mock (ServletContext .class );
108+ return new PwaRegistry (pwa , context );
109+ }
110+ }
111+
73112 @ Test
74113 public void pwaWithCustomBaseIconPath_splashScreenIconForAllSupportedAppleDevicesAndOrientationsAreGenerated () {
75114 Assert .assertEquals (26 , splashIconsForAppleDevices .size ());
@@ -262,4 +301,43 @@ public void pwaWithCustomBaseIconPath_splashScreenIconForIPhoneDevices_includeBo
262301 .stream ().filter (media -> media .contains ("landscape" )).count ());
263302 }
264303
304+ @ Test
305+ public void pwaWithCustomOfflinePath_viteIsEnabled_getRuntimeServiceWorkerJsContainsCustomOfflinePath ()
306+ throws IOException {
307+ PwaRegistry registry = preparePwaRegistry (
308+ PwaWithCustomOfflinePath .class .getAnnotation (PWA .class ), true );
309+ Assert .assertTrue (registry .getRuntimeServiceWorkerJs ()
310+ .contains ("some/path.html" ));
311+ Assert .assertFalse (registry .getRuntimeServiceWorkerJs ()
312+ .contains ("{ url: '.', revision:" ));
313+ }
314+
315+ @ Test
316+ public void pwaWithCustomOfflinePath_viteIsNotEnabled_getRuntimeServiceWorkerJsContainsCustomOfflinePath ()
317+ throws IOException {
318+ PwaRegistry registry = preparePwaRegistry (
319+ PwaWithCustomOfflinePath .class .getAnnotation (PWA .class ), false );
320+ Assert .assertTrue (registry .getRuntimeServiceWorkerJs ()
321+ .contains ("some/path.html" ));
322+ Assert .assertFalse (registry .getRuntimeServiceWorkerJs ()
323+ .contains ("{ url: '.', revision:" ));
324+ }
325+
326+ @ Test
327+ public void pwaWithoutCustomOfflinePath_viteIsEnabled_getRuntimeServiceWorkerJsContainsCustomOfflinePath ()
328+ throws IOException {
329+ PwaRegistry registry = preparePwaRegistry (
330+ PwaRegistryTest .class .getAnnotation (PWA .class ), true );
331+ Assert .assertTrue (registry .getRuntimeServiceWorkerJs ()
332+ .contains ("{ url: '.', revision:" ));
333+ }
334+
335+ @ Test
336+ public void pwaWithoutCustomOfflinePath_viteIsNotEnabled_getRuntimeServiceWorkerJsDoesNotContainsRoot ()
337+ throws IOException {
338+ PwaRegistry registry = preparePwaRegistry (
339+ PwaRegistryTest .class .getAnnotation (PWA .class ), false );
340+ Assert .assertFalse (registry .getRuntimeServiceWorkerJs ()
341+ .contains ("{ url: '.', revision:" ));
342+ }
265343}
0 commit comments