How to use service_worker_unregister_and_register method in wpt

Best JavaScript code snippet using wpt

test-helpers.js

Source:test-helpers.js Github

copy

Full Screen

1// Adapter for testharness.js-style tests with Service Workers2function service_worker_unregister_and_register(test, url, scope) {3 if (!scope || scope.length == 0)4 return Promise.reject(new Error('tests must define a scope'));5 var options = { scope: scope };6 return service_worker_unregister(test, scope)7 .then(function() {8 return navigator.serviceWorker.register(url, options);9 })10 .catch(unreached_rejection(test,11 'unregister and register should not fail'));12}13function service_worker_unregister(test, documentUrl) {14 return navigator.serviceWorker.getRegistration(documentUrl)15 .then(function(registration) {16 if (registration)17 return registration.unregister();18 })19 .catch(unreached_rejection(test, 'unregister should not fail'));20}21function service_worker_unregister_and_done(test, scope) {22 return service_worker_unregister(test, scope)23 .then(test.done.bind(test));24}25function unreached_fulfillment(test, prefix) {26 return test.step_func(function(result) {27 var error_prefix = prefix || 'unexpected fulfillment';28 assert_unreached(error_prefix + ': ' + result);29 });30}31// Rejection-specific helper that provides more details32function unreached_rejection(test, prefix) {33 return test.step_func(function(error) {34 var reason = error.message || error.name || error;35 var error_prefix = prefix || 'unexpected rejection';36 assert_unreached(error_prefix + ': ' + reason);37 });38}39// Adds an iframe to the document and returns a promise that resolves to the40// iframe when it finishes loading. The caller is responsible for removing the41// iframe later if needed.42function with_iframe(url) {43 return new Promise(function(resolve) {44 var frame = document.createElement('iframe');45 frame.src = url;46 frame.onload = function() { resolve(frame); };47 document.body.appendChild(frame);48 });49}50function normalizeURL(url) {51 return new URL(url, self.location).toString().replace(/#.*$/, '');52}53function wait_for_update(test, registration) {54 if (!registration || registration.unregister == undefined) {55 return Promise.reject(new Error(56 'wait_for_update must be passed a ServiceWorkerRegistration'));57 }58 return new Promise(test.step_func(function(resolve) {59 registration.addEventListener('updatefound', test.step_func(function() {60 resolve(registration.installing);61 }));62 }));63}64function wait_for_state(test, worker, state) {65 if (!worker || worker.state == undefined) {66 return Promise.reject(new Error(67 'wait_for_state must be passed a ServiceWorker'));68 }69 if (worker.state === state)70 return Promise.resolve(state);71 if (state === 'installing') {72 switch (worker.state) {73 case 'installed':74 case 'activating':75 case 'activated':76 case 'redundant':77 return Promise.reject(new Error(78 'worker is ' + worker.state + ' but waiting for ' + state));79 }80 }81 if (state === 'installed') {82 switch (worker.state) {83 case 'activating':84 case 'activated':85 case 'redundant':86 return Promise.reject(new Error(87 'worker is ' + worker.state + ' but waiting for ' + state));88 }89 }90 if (state === 'activating') {91 switch (worker.state) {92 case 'activated':93 case 'redundant':94 return Promise.reject(new Error(95 'worker is ' + worker.state + ' but waiting for ' + state));96 }97 }98 if (state === 'activated') {99 switch (worker.state) {100 case 'redundant':101 return Promise.reject(new Error(102 'worker is ' + worker.state + ' but waiting for ' + state));103 }104 }105 return new Promise(test.step_func(function(resolve) {106 worker.addEventListener('statechange', test.step_func(function() {107 if (worker.state === state)108 resolve(state);109 }));110 }));111}112// Declare a test that runs entirely in the ServiceWorkerGlobalScope. The |url|113// is the service worker script URL. This function:114// - Instantiates a new test with the description specified in |description|.115// The test will succeed if the specified service worker can be successfully116// registered and installed.117// - Creates a new ServiceWorker registration with a scope unique to the current118// document URL. Note that this doesn't allow more than one119// service_worker_test() to be run from the same document.120// - Waits for the new worker to begin installing.121// - Imports tests results from tests running inside the ServiceWorker.122function service_worker_test(url, description) {123 // If the document URL is https://example.com/document and the script URL is124 // https://example.com/script/worker.js, then the scope would be125 // https://example.com/script/scope/document.126 var scope = new URL('scope' + window.location.pathname,127 new URL(url, window.location)).toString();128 promise_test(function(test) {129 return service_worker_unregister_and_register(test, url, scope)130 .then(function(registration) {131 add_completion_callback(function() {132 registration.unregister();133 });134 return wait_for_update(test, registration)135 .then(function(worker) {136 return fetch_tests_from_worker(worker);137 });138 });139 }, description);140}141function get_host_info() {142 var ORIGINAL_HOST = '127.0.0.1';143 var REMOTE_HOST = 'localhost';...

Full Screen

Full Screen

connect-tests.js

Source:connect-tests.js Github

copy

Full Screen

...14 promise_test(function(t) {15 var scope = sw_scope + '/empty';16 var sw_url = 'resources/empty-worker.js';17 return assert_promise_rejects(18 service_worker_unregister_and_register(t, sw_url, scope)19 .then(function(registration) {20 return wait_for_state(t, registration.installing, 'activated');21 })22 .then(function() {23 return connect_method(t, scope + '/service');24 }),25 'AbortError',26 'navigator.connect should fail with an AbortError');27 }, 'Connection fails if service worker doesn\'t handle connect event.');28 promise_test(function(t) {29 var scope = sw_scope + '/rejecting';30 var sw_url = 'resources/rejecting-worker.js';31 return assert_promise_rejects(32 service_worker_unregister_and_register(t, sw_url, scope)33 .then(function(registration) {34 return wait_for_state(t, registration.installing, 'activated');35 })36 .then(function() {37 return connect_method(t, scope + '/service');38 }),39 'AbortError',40 'navigator.connect should fail with an AbortError');41 }, 'Connection fails if service worker rejects connection event.');42 promise_test(function(t) {43 var scope = sw_scope + '/accepting';44 var sw_url = 'resources/accepting-worker.js';45 return service_worker_unregister_and_register(t, sw_url, scope)46 .then(function(registration) {47 return wait_for_state(t, registration.installing, 'activated');48 })49 .then(function() {50 return connect_method(t, scope + '/service');51 })52 .then(function(port) {53 assert_class_string(port, 'MessagePort');54 return service_worker_unregister(t, scope);55 });56 }, 'Connection succeeds if service worker accepts connection event.');57 promise_test(function(t) {58 var scope = sw_scope + '/async-rejecting';59 var sw_url = 'resources/async-connect-worker.js';60 return assert_promise_rejects(61 service_worker_unregister_and_register(t, sw_url, scope)62 .then(function(registration) {63 return wait_for_state(t, registration.installing, 'activated');64 })65 .then(function() {66 return connect_method(t, scope + '/service?reject');67 }),68 'AbortError',69 'navigator.connect should fail with an AbortError');70 }, 'Connection fails if service worker rejects connection event async.');71 promise_test(function(t) {72 var scope = sw_scope + '/async-accepting';73 var sw_url = 'resources/async-connect-worker.js';74 return service_worker_unregister_and_register(t, sw_url, scope)75 .then(function(registration) {76 return wait_for_state(t, registration.installing, 'activated');77 })78 .then(function() {79 return connect_method(t, scope + '/service?accept');80 })81 .then(function(port) {82 assert_class_string(port, 'MessagePort');83 return service_worker_unregister(t, scope);84 });85 }, 'Connection succeeds if service worker accepts connection event async.');...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1wptbho.service_worker_unregister_and_register();2wptbho.service_worker_unregister();3wptbho.service_worker_register();46. service_worker_unregister_and_register();57. service_worker_unregister();68. service_worker_register();79. service_worker_unregister_and_register();810. service_worker_unregister();911. service_worker_register();10Cr-Commit-Position: refs/heads/master@{#373640}11Cr-Commit-Position: refs/heads/master@{#373641}12Cr-Commit-Position: refs/heads/master@{#373642}13Cr-Commit-Position: refs/heads/master@{#373643}14Cr-Commit-Position: refs/heads/master@{#373644}15Cr-Commit-Position: refs/heads/master@{#373645}16Cr-Commit-Position: refs/heads/master@{#373646}17Cr-Commit-Position: refs/heads/master@{#373647}18Cr-Commit-Position: refs/heads/master@{#373648}19Cr-Commit-Position: refs/heads/master@{#373649}

Full Screen

Using AI Code Generation

copy

Full Screen

1importScripts('/resources/testharness.js');2importScripts('/resources/testharnessreport.js');3importScripts('/service-workers/service-worker/resources/test-helpers.js');4promise_test(function(t) {5 var script = 'resources/blank-worker.js';6 var scope = 'resources/blank.html';7 var registration;8 return service_worker_unregister_and_register(t, script, scope)9 .then(function(r) {10 registration = r;11 return wait_for_state(t, registration.installing, 'activated');12 })13 .then(function() {14 return registration.unregister();15 })16 .then(function() {17 assert_equals(registration.installing, null);18 assert_equals(registration.waiting, null);19 assert_equals(registration.active, null);20 return navigator.serviceWorker.getRegistration(scope);21 })22 .then(function(r) {23 assert_equals(r, undefined);24 });25}, 'Unregistering a service worker unregisters it.');26importScripts('/resources/testharness.js');27importScripts('/resources/testharnessreport.js');28importScripts('/service-workers/service-worker/resources/test-helpers.js');29promise_test(function(t) {30 var script = 'resources/blank-worker.js';31 var scope = 'resources/blank.html';32 var registration;33 return service_worker_unregister_and_register(t, script, scope)34 .then(function(r) {35 registration = r;36 return wait_for_state(t, registration.installing, 'activated');37 })38 .then(function() {39 return registration.unregister();40 })41 .then(function() {42 assert_equals(registration.installing, null);43 assert_equals(registration.waiting, null);44 assert_equals(registration.active, null);45 return navigator.serviceWorker.getRegistration(scope);46 })47 .then(function(r) {48 assert_equals(r, undefined);49 });50}, 'Unregistering a service worker unregisters it.');

Full Screen

Using AI Code Generation

copy

Full Screen

1importScripts('/resources/testharness.js');2importScripts('/resources/testharnessreport.js');3importScripts('wpt-helpers.js');4promise_test(t => {5 return service_worker_unregister_and_register(t, 'resources/empty-sw.js')6 .then(registration => {7 assert_equals(registration.installing.state, 'activated');8 });9}, 'Service worker unregistered and registered');10function service_worker_unregister_and_register(t, script_url) {11 return navigator.serviceWorker.getRegistration(script_url)12 .then(registration => {13 if (registration) {14 return registration.unregister();15 }16 })17 .then(() => {18 return navigator.serviceWorker.register(script_url);19 });20}

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run wpt automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful