|
| 1 | +/* |
| 2 | + * Copyright 2000-2024 Vaadin Ltd. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | +package com.vaadin.flow.connect; |
| 17 | + |
| 18 | +import com.vaadin.flow.testutil.ChromeDeviceTest; |
| 19 | +import org.junit.Assert; |
| 20 | +import org.junit.Test; |
| 21 | +import org.openqa.selenium.By; |
| 22 | +import org.openqa.selenium.JavascriptExecutor; |
| 23 | + |
| 24 | +public class ServiceWorkerIT extends ChromeDeviceTest { |
| 25 | + |
| 26 | + @Test |
| 27 | + public void onlineRoot_serviceWorkerInstalled_serviceWorkerActive() { |
| 28 | + getDriver().get(getRootURL() + "/"); |
| 29 | + waitForDevServer(); |
| 30 | + waitForServiceWorkerReady(); |
| 31 | + |
| 32 | + boolean serviceWorkerActive = (boolean) ((JavascriptExecutor) getDriver()) |
| 33 | + .executeAsyncScript( |
| 34 | + "const resolve = arguments[arguments.length - 1];" |
| 35 | + + "navigator.serviceWorker.ready.then( function(reg) { resolve(!!reg.active); });"); |
| 36 | + Assert.assertTrue("service worker not installed", serviceWorkerActive); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void offlineRoot_reload_viewReloaded() { |
| 41 | + openPageAndPreCacheWhenDevelopmentMode("/"); |
| 42 | + |
| 43 | + // Confirm that app shell is loaded |
| 44 | + Assert.assertNotNull("Should have outlet when loaded online", |
| 45 | + findElement(By.id("outlet"))); |
| 46 | + |
| 47 | + // Confirm that client side view is loaded |
| 48 | + Assert.assertNotNull( |
| 49 | + "Should have <test-view> in DOM when loaded online", |
| 50 | + findElement(By.tagName("test-view"))); |
| 51 | + |
| 52 | + // Reload the page in offline mode |
| 53 | + executeScript("window.location.reload();"); |
| 54 | + waitUntil(webDriver -> ((JavascriptExecutor) driver) |
| 55 | + .executeScript("return document.readyState") |
| 56 | + .equals("complete")); |
| 57 | + } |
| 58 | + |
| 59 | + private void openPageAndPreCacheWhenDevelopmentMode(String targetView) { |
| 60 | + openPageAndPreCacheWhenDevelopmentMode(targetView, () -> { |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + private void openPageAndPreCacheWhenDevelopmentMode(String targetView, |
| 65 | + Runnable activateViews) { |
| 66 | + getDriver().get(getRootURL() + targetView); |
| 67 | + waitForDevServer(); |
| 68 | + waitForServiceWorkerReady(); |
| 69 | + |
| 70 | + boolean isDevMode = Boolean.getBoolean("vaadin.test.developmentMode"); |
| 71 | + if (isDevMode) { |
| 72 | + // In production mode all views are supposed to be already in the |
| 73 | + // bundle, but in dev mode they are loaded at runtime |
| 74 | + // So, for dev mode, pre cache required views |
| 75 | + activateViews.run(); |
| 76 | + |
| 77 | + // In addition not all external resources are cached when the page |
| 78 | + // opens |
| 79 | + // first time, so we need to reload the page even if there is no |
| 80 | + // navigation to other views |
| 81 | + getDriver().get(getRootURL() + targetView); |
| 82 | + waitForDevServer(); |
| 83 | + waitForServiceWorkerReady(); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + protected String getRootURL() { |
| 89 | + return super.getRootURL(); |
| 90 | + } |
| 91 | +} |
0 commit comments