Skip to content

Commit

Permalink
Adding extra logic to wait for controller change on top of activate
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Gaunt committed Mar 5, 2018
1 parent 297b10a commit 99ae898
Show file tree
Hide file tree
Showing 17 changed files with 49 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,21 @@ module.exports = async (swUrl) => {
.then((registration) => {
return _onStateChangePromise(registration, 'activated');
})
.then(() => {
// Ensure the page is being controlled by the SW.
if (navigator.serviceWorker.controller &&
navigator.serviceWorker.controller.scriptURL === swUrl) {
return;
} else {
return new Promise((resolve) => {
navigator.serviceWorker.addEventListener('controllerchange', () => {
if (navigator.serviceWorker.controller.scriptURL === swUrl) {
resolve();
}
});
});
}
})
.then(() => cb())
.catch((err) => cb(err));
}, swUrl);
Expand Down
4 changes: 2 additions & 2 deletions test/workbox-background-sync/integration/test-bg-sync.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const expect = require('chai').expect;

const activateSW = require('../../../infra/testing/activate-sw');
const activateAndControlSW = require('../../../infra/testing/activate-and-control');

describe(`[workbox-background-sync] Load and use Background Sync`, function() {
const testServerAddress = global.__workbox.server.getAddress();
Expand All @@ -27,7 +27,7 @@ describe(`[workbox-background-sync] Load and use Background Sync`, function() {
it(`should load a page with service worker`, async function() {
// Load the page and wait for the first service worker to register and activate.
await global.__workbox.webdriver.get(testingUrl);
await activateSW(swUrl);
await activateAndControlSW(swUrl);

const err = await global.__workbox.webdriver.executeAsyncScript((testingUrl, cb) => {
return fetch(`${testingUrl}example.txt`)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const expect = require('chai').expect;

const activateSW = require('../../../infra/testing/activate-sw');
const activateAndControlSW = require('../../../infra/testing/activate-and-control');

describe(`broadcastCacheUpdate.Plugin`, function() {
const testServerAddress = global.__workbox.server.getAddress();
Expand All @@ -10,7 +10,7 @@ describe(`broadcastCacheUpdate.Plugin`, function() {

it(`should broadcast a message on the expected channel when there's a cache update`, async function() {
await global.__workbox.webdriver.get(testingUrl);
await activateSW(swUrl);
await activateAndControlSW(swUrl);

const supported = await global.__workbox.webdriver.executeScript(() => {
return 'BroadcastChannel' in window;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const expect = require('chai').expect;

const activateSW = require('../../../infra/testing/activate-sw');
const activateAndControlSW = require('../../../infra/testing/activate-and-control');
const cleanSWEnv = require('../../../infra/testing/clean-sw');

describe(`expiration.Plugin`, function() {
Expand Down Expand Up @@ -30,7 +30,7 @@ describe(`expiration.Plugin`, function() {
const swUrl = `${testingUrl}sw-max-entries.js`;

// Wait for the service worker to register and activate.
await activateSW(swUrl);
await activateAndControlSW(swUrl);

await global.__workbox.webdriver.executeAsyncScript((testingUrl, cb) => {
fetch(`${testingUrl}example-1.txt`).then(() => cb()).catch((err) => cb(err.message));
Expand Down Expand Up @@ -86,7 +86,7 @@ describe(`expiration.Plugin`, function() {

// Load the page and wait for the service worker to register and activate.
await global.__workbox.webdriver.get(testingUrl);
await activateSW(swUrl);
await activateAndControlSW(swUrl);

await global.__workbox.webdriver.executeAsyncScript((testingUrl, cb) => {
fetch(`${testingUrl}example-1.txt`).then(() => cb()).catch((err) => cb(err.message));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const expect = require('chai').expect;

const activateSW = require('../../../infra/testing/activate-sw');
const activateAndControlSW = require('../../../infra/testing/activate-and-control');

describe(`cacheableResponse.Plugin`, function() {
const testServerAddress = global.__workbox.server.getAddress();
Expand Down Expand Up @@ -33,7 +33,7 @@ describe(`cacheableResponse.Plugin`, function() {

it(`should load a page and cache entries`, async function() {
// Wait for the service worker to register and activate.
await activateSW(swUrl);
await activateAndControlSW(swUrl);

await global.__workbox.webdriver.executeAsyncScript((testingUrl, cb) => {
fetch(`${testingUrl}example-1.txt`).then(() => cb()).catch((err) => cb(err.message));
Expand Down
4 changes: 2 additions & 2 deletions test/workbox-core/integration/test-core.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const activateSW = require('../../../infra/testing/activate-sw');
const activateAndControlSW = require('../../../infra/testing/activate-and-control');

describe(`[workbox-core] Load core in the browser`, function() {
const testServerAddress = global.__workbox.server.getAddress();
Expand All @@ -7,7 +7,7 @@ describe(`[workbox-core] Load core in the browser`, function() {

it(`should load workbox-core in a service worker.`, async function() {
await global.__workbox.webdriver.get(testingUrl);
await activateSW(swUrl);
await activateAndControlSW(swUrl);

// If the service worker activated, it meant the assertions in sw.js were
// met and workbox-core exposes the expected API and defaults that were
Expand Down
4 changes: 2 additions & 2 deletions test/workbox-google-analytics/integration/basic-example.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const expect = require('chai').expect;
const qs = require('qs');
const {By} = require('selenium-webdriver');
const activateSW = require('../../../infra/testing/activate-sw');
const activateAndControlSW = require('../../../infra/testing/activate-and-control');

describe(`[workbox-google-analytics] Load and use Google Analytics`,
function() {
Expand Down Expand Up @@ -31,7 +31,7 @@ describe(`[workbox-google-analytics] Load and use Google Analytics`,
// Load the page and wait for the first service worker to activate.
await driver.get(testingUrl);

await activateSW(swUrl);
await activateAndControlSW(swUrl);
});

beforeEach(async function() {
Expand Down
6 changes: 3 additions & 3 deletions test/workbox-precaching/integration/precache-and-update.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const expect = require('chai').expect;

const activateSW = require('../../../infra/testing/activate-sw');
const activateAndControlSW = require('../../../infra/testing/activate-and-control');
const cleanSWEnv = require('../../../infra/testing/clean-sw');

describe(`[workbox-precaching] Precache and Update`, function() {
Expand Down Expand Up @@ -45,7 +45,7 @@ describe(`[workbox-precaching] Precache and Update`, function() {
global.__workbox.server.reset();

// Register the first service worker.
await activateSW(SW_1_URL);
await activateAndControlSW(SW_1_URL);

// Check that only the precache cache was created.
const keys = await global.__workbox.webdriver.executeAsyncScript((cb) => {
Expand Down Expand Up @@ -106,7 +106,7 @@ describe(`[workbox-precaching] Precache and Update`, function() {
}

// Activate the second service worker
await activateSW(SW_2_URL);
await activateAndControlSW(SW_2_URL);

// Ensure that the new assets were requested and cache busted.
requestsMade = global.__workbox.server.getRequests();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const expect = require('chai').expect;

const activateSW = require('../../../infra/testing/activate-sw');
const activateAndControlSW = require('../../../infra/testing/activate-and-control');
const cleanSWEnv = require('../../../infra/testing/clean-sw');

describe(`rangeRequests.Plugin`, function() {
Expand All @@ -18,7 +18,7 @@ describe(`rangeRequests.Plugin`, function() {
const dummyBody = '0123456789';

await global.__workbox.webdriver.get(testingUrl);
await activateSW(swUrl);
await activateAndControlSW(swUrl);

const partialResponseBody = await global.__workbox.webdriver.executeAsyncScript((dummyUrl, dummyBody, cb) => {
const dummyResponse = new Response(dummyBody);
Expand Down
4 changes: 2 additions & 2 deletions test/workbox-routing/integration/navigation-route.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const expect = require('chai').expect;

const activateSW = require('../../../infra/testing/activate-sw');
const activateAndControlSW = require('../../../infra/testing/activate-and-control');

describe(`[workbox-routing] Route via NavigationRoute`, function() {
const testServerAddress = global.__workbox.server.getAddress();
Expand All @@ -10,7 +10,7 @@ describe(`[workbox-routing] Route via NavigationRoute`, function() {
it(`should load a page and route requests`, async function() {
// Load the page and wait for the first service worker to register and activate.
await global.__workbox.webdriver.get(testingUrl);
await activateSW(swUrl);
await activateAndControlSW(swUrl);

const nestedUrl = `${testingUrl}TestNavigationURL`;

Expand Down
4 changes: 2 additions & 2 deletions test/workbox-routing/integration/routing-basic.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const expect = require('chai').expect;

const activateSW = require('../../../infra/testing/activate-sw');
const activateAndControlSW = require('../../../infra/testing/activate-and-control');

describe(`[workbox-routing] Basic Route`, function() {
const testServerAddress = global.__workbox.server.getAddress();
Expand All @@ -9,7 +9,7 @@ describe(`[workbox-routing] Basic Route`, function() {

before(async function() {
await global.__workbox.webdriver.get(testingUrl);
await activateSW(swUrl);
await activateAndControlSW(swUrl);
});

it(`should honor a route created by a Route object`, async function() {
Expand Down
4 changes: 2 additions & 2 deletions test/workbox-routing/integration/routing-regex.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const expect = require('chai').expect;

const activateSW = require('../../../infra/testing/activate-sw');
const activateAndControlSW = require('../../../infra/testing/activate-and-control');

describe(`[workbox-routing] Route via RegExp`, function() {
const testServerAddress = global.__workbox.server.getAddress();
Expand All @@ -10,7 +10,7 @@ describe(`[workbox-routing] Route via RegExp`, function() {
it(`should load a page and route requests`, async function() {
// Load the page and wait for the first service worker to register and activate.
await global.__workbox.webdriver.get(testingUrl);
await activateSW(swUrl);
await activateAndControlSW(swUrl);

let testCounter = 0;

Expand Down
4 changes: 2 additions & 2 deletions test/workbox-strategies/integration/test-cacheFirst.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const expect = require('chai').expect;

const activateSW = require('../../../infra/testing/activate-sw');
const activateAndControlSW = require('../../../infra/testing/activate-and-control');

describe(`[workbox-strategies] CacheFirst Requests`, function() {
const testServerAddress = global.__workbox.server.getAddress();
Expand All @@ -10,7 +10,7 @@ describe(`[workbox-strategies] CacheFirst Requests`, function() {
it(`should respond with cached and non-cached entry`, async function() {
// Load the page and wait for the first service worker to register and activate.
await global.__workbox.webdriver.get(testingUrl);
await activateSW(swUrl);
await activateAndControlSW(swUrl);

let response = await global.__workbox.webdriver.executeAsyncScript((cb) => {
fetch(new URL(`/test/workbox-strategies/static/cache-first/example.txt`, location).href)
Expand Down
4 changes: 2 additions & 2 deletions test/workbox-strategies/integration/test-cacheOnly.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const expect = require('chai').expect;

const activateSW = require('../../../infra/testing/activate-sw');
const activateAndControlSW = require('../../../infra/testing/activate-and-control');

describe(`[workbox-strategies] CacheOnly Requests`, function() {
const testServerAddress = global.__workbox.server.getAddress();
Expand All @@ -10,7 +10,7 @@ describe(`[workbox-strategies] CacheOnly Requests`, function() {
it(`should respond with cached and non-cached entry`, async function() {
// Load the page and wait for the first service worker to register and activate.
await global.__workbox.webdriver.get(testingUrl);
await activateSW(swUrl);
await activateAndControlSW(swUrl);

let response = await global.__workbox.webdriver.executeAsyncScript((cb) => {
fetch(new URL(`/CacheOnly/InCache/`, location).href)
Expand Down
4 changes: 2 additions & 2 deletions test/workbox-strategies/integration/test-networkFirst.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const expect = require('chai').expect;

const activateSW = require('../../../infra/testing/activate-sw');
const activateAndControlSW = require('../../../infra/testing/activate-and-control');

describe.only(`[workbox-strategies] NetworkFirst Requests`, function() {
const testServerAddress = global.__workbox.server.getAddress();
Expand Down Expand Up @@ -50,7 +50,7 @@ describe.only(`[workbox-strategies] NetworkFirst Requests`, function() {

// Load the page and wait for the first service worker to register and activate.
await global.__workbox.webdriver.get(testingUrl);
await activateSW(swUrl);
await activateAndControlSW(swUrl);

let response = await global.__workbox.webdriver.executeAsyncScript((cb) => {
fetch(new URL(`/test/uniqueValue`, location).href)
Expand Down
4 changes: 2 additions & 2 deletions test/workbox-strategies/integration/test-networkOnly.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const expect = require('chai').expect;

const activateSW = require('../../../infra/testing/activate-sw');
const activateAndControlSW = require('../../../infra/testing/activate-and-control');

describe.only(`[workbox-strategies] NetworkOnly Requests`, function() {
const testServerAddress = global.__workbox.server.getAddress();
Expand All @@ -12,7 +12,7 @@ describe.only(`[workbox-strategies] NetworkOnly Requests`, function() {

// Load the page and wait for the first service worker to register and activate.
await global.__workbox.webdriver.get(testingUrl);
await activateSW(swUrl);
await activateAndControlSW(swUrl);

let response = await global.__workbox.webdriver.executeAsyncScript((cb) => {
fetch(new URL(`/test/uniqueValue`, location).href)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const expect = require('chai').expect;

const activateSW = require('../../../infra/testing/activate-sw');
const activateAndControlSW = require('../../../infra/testing/activate-and-control');

describe(`[workbox-strategies] StaleWhileRevalidate Requests`, function() {
const testServerAddress = global.__workbox.server.getAddress();
Expand Down Expand Up @@ -50,7 +50,7 @@ describe(`[workbox-strategies] StaleWhileRevalidate Requests`, function() {

// Load the page and wait for the first service worker to register and activate.
await global.__workbox.webdriver.get(testingUrl);
await activateSW(swUrl);
await activateAndControlSW(swUrl);

let response = await global.__workbox.webdriver.executeAsyncScript((cb) => {
fetch(new URL(`/test/uniqueValue`, location).href)
Expand Down

0 comments on commit 99ae898

Please sign in to comment.