cookieStore_subscribe_arguments.https.any.js (6894B)
1 // META: title=Cookie Store API: cookieStore.subscribe() arguments 2 // META: global=window,serviceworker 3 // META: script=/service-workers/service-worker/resources/test-helpers.sub.js 4 5 'use strict'; 6 7 promise_test(async testCase => { 8 if (self.GLOBAL.isWindow()) { 9 const registration = await service_worker_unregister_and_register( 10 testCase, 'resources/empty_sw.js', 11 '/cookiestore/resources/does/not/exist'); 12 testCase.add_cleanup(() => registration.unregister()); 13 14 // Must wait for the service worker to enter the 'activated' state before 15 // subscribing to cookiechange events. 16 await wait_for_state(testCase, registration.installing, 'activated'); 17 18 self.registration = registration; 19 } else if (!self.registration.active) { 20 // If service worker is not active yet, it must wait for it to enter the 21 // 'activated' state before subscribing to cookiechange events. 22 await new Promise(resolve => self.addEventListener('activate', resolve)); 23 } 24 25 let subscriptions = 26 [{name: 'cookie-name'}, {url: self.registration.scope + '/subdir'}]; 27 await self.registration.cookies.subscribe(subscriptions); 28 testCase.add_cleanup( 29 () => self.registration.cookies.unsubscribe(subscriptions)); 30 31 subscriptions = await self.registration.cookies.getSubscriptions(); 32 assert_equals(subscriptions.length, 2); 33 assert_equals(subscriptions[0].name, 'cookie-name'); 34 assert_equals(subscriptions[0].url, self.registration.scope); 35 assert_equals(subscriptions[1].name, undefined) 36 assert_equals(subscriptions[1].url, self.registration.scope + '/subdir') 37 }, 'cookieStore.subscribe without name or url in options'); 38 39 promise_test(async testCase => { 40 if (self.GLOBAL.isWindow()) { 41 const registration = await service_worker_unregister_and_register( 42 testCase, 'resources/empty_sw.js', 43 '/cookiestore/resources/does/not/exist'); 44 45 // Must wait for the service worker to enter the 'activated' state before 46 // subscribing to cookiechange events. 47 await wait_for_state(testCase, registration.installing, 'activated'); 48 testCase.add_cleanup(() => self.registration.unregister()); 49 50 self.registration = registration; 51 } else if (!self.registration.active) { 52 // If service worker is not active yet, it must wait for it to enter the 53 // 'activated' state before subscribing to cookiechange events. 54 await new Promise(resolve => self.addEventListener('activate', resolve)); 55 } 56 57 let subscriptions = [{}]; 58 await self.registration.cookies.subscribe(subscriptions); 59 testCase.add_cleanup( 60 () => self.registration.cookies.unsubscribe(subscriptions)); 61 62 subscriptions = await self.registration.cookies.getSubscriptions(); 63 assert_equals(subscriptions.length, 1); 64 assert_equals(subscriptions[0].name, undefined); 65 assert_equals(subscriptions[0].url, self.registration.scope); 66 }, 'cookieStore.subscribe with empty option'); 67 68 promise_test(async testCase => { 69 if (self.GLOBAL.isWindow()) { 70 const registration = await service_worker_unregister_and_register( 71 testCase, 'resources/empty_sw.js', 72 '/cookiestore/resources/does/not/exist'); 73 testCase.add_cleanup(() => registration.unregister()); 74 75 // Must wait for the service worker to enter the 'activated' state before 76 // subscribing to cookiechange events. 77 await wait_for_state(testCase, registration.installing, 'activated'); 78 79 self.registration = registration; 80 } else if (!self.registration.active) { 81 // If service worker is not active yet, it must wait for it to enter the 82 // 'activated' state before subscribing to cookiechange events. 83 await new Promise(resolve => self.addEventListener('activate', resolve)); 84 } 85 86 await promise_rejects_js( 87 testCase, TypeError, 88 registration.cookies.subscribe( 89 {name: 'cookie-name', url: '/wrong/path'})); 90 }, 'cookieStore.subscribe with invalid url path in option'); 91 92 promise_test(async testCase => { 93 if (self.GLOBAL.isWindow()) { 94 const registration = await service_worker_unregister_and_register( 95 testCase, 'resources/empty_sw.js', 96 '/cookiestore/resources/does/not/exist'); 97 testCase.add_cleanup(() => registration.unregister()); 98 99 // Must wait for the service worker to enter the 'activated' state before 100 // subscribing to cookiechange events. 101 await wait_for_state(testCase, registration.installing, 'activated'); 102 103 self.registration = registration; 104 } else if (!self.registration.active) { 105 // Must wait for the service worker to enter the 'activated' state before 106 // subscribing to cookiechange events. 107 await new Promise(resolve => self.addEventListener('activate', resolve)); 108 } 109 110 let subscriptions = [{name: 'cookie-name'}]; 111 // Call subscribe for same subscription multiple times to verify that it is 112 // idempotent. 113 await self.registration.cookies.subscribe(subscriptions); 114 await self.registration.cookies.subscribe(subscriptions); 115 await self.registration.cookies.subscribe(subscriptions); 116 testCase.add_cleanup(() => registration.cookies.unsubscribe(subscriptions)); 117 118 subscriptions = await registration.cookies.getSubscriptions(); 119 assert_equals(subscriptions.length, 1); 120 assert_equals(subscriptions[0].name, 'cookie-name'); 121 assert_equals(subscriptions[0].url, registration.scope); 122 }, 'cookieStore.subscribe is idempotent'); 123 124 promise_test(async testCase => { 125 if (self.GLOBAL.isWindow()) { 126 const registration = await service_worker_unregister_and_register( 127 testCase, 'resources/empty_sw.js', 128 '/cookiestore/resources/does/not/exist'); 129 testCase.add_cleanup(() => registration.unregister()); 130 131 // Must wait for the service worker to enter the 'activated' state before 132 // subscribing to cookiechange events. 133 await wait_for_state(testCase, registration.installing, 'activated'); 134 135 self.registration = registration; 136 } else if (!self.registration.active) { 137 // Must wait for the service worker to enter the 'activated' state before 138 // subscribing to cookiechange events. 139 await new Promise(resolve => self.addEventListener('activate', resolve)); 140 } 141 142 let subscriptions = [ 143 {name: 'cookie-name1'}, 144 {name: 'cookie-name2'}, 145 ]; 146 await self.registration.cookies.subscribe(subscriptions); 147 testCase.add_cleanup(() => registration.cookies.unsubscribe(subscriptions)); 148 149 // Call unsubscribe for same subscription multiple times to verify that it 150 // is idempotent. 151 await registration.cookies.unsubscribe([subscriptions[0]]); 152 await registration.cookies.unsubscribe([subscriptions[0]]); 153 await registration.cookies.unsubscribe([subscriptions[0]]); 154 155 subscriptions = await registration.cookies.getSubscriptions(); 156 assert_equals(subscriptions.length, 1); 157 assert_equals(subscriptions[0].name, 'cookie-name2'); 158 assert_equals(subscriptions[0].url, registration.scope); 159 }, 'CookieStore.unsubscribe is idempotent');