1
1
<!DOCTYPE html>
2
2
< script src ="/resources/testharness.js "> </ script >
3
3
< script src ="/resources/testharnessreport.js "> </ script >
4
- < script src ="/common/get-host-info.sub.js "> </ script >
5
4
< script src ="resources/test-helpers.sub.js "> </ script >
6
- < body >
5
+
7
6
< script >
8
7
const worker = 'resources/fetch-event-within-sw-worker.js' ;
9
8
10
- function reset ( ) {
11
- for ( const iframe of [ ... document . querySelectorAll ( '.test-iframe' ) ] ) {
12
- iframe . remove ( ) ;
13
- }
14
- return navigator . serviceWorker . getRegistrations ( ) . then ( registrations => {
15
- return Promise . all ( registrations . map ( r => r . unregister ( ) ) ) ;
16
- } ) . then ( ( ) => caches . keys ( ) ) . then ( cacheKeys => {
17
- return Promise . all ( cacheKeys . map ( c => caches . delete ( c ) ) ) ;
18
- } ) ;
9
+ async function registerSwAndOpenFrame ( t ) {
10
+ const registration = await navigator . serviceWorker . register (
11
+ worker , { scope : 'resources/' } ) ;
12
+ t . add_cleanup ( ( ) => registration . unregister ( ) ) ;
13
+ await wait_for_state ( t , registration . installing , 'activated' ) ;
14
+
15
+ const frame = await with_iframe ( 'resources/simple.html' ) ;
16
+ t . add_cleanup ( ( ) => frame . remove ( ) ) ;
17
+ return frame ;
19
18
}
20
19
21
- add_completion_callback ( reset ) ;
20
+ async function deleteCashes ( ) {
21
+ const cacheKeys = await caches . keys ( ) ;
22
+ await Promise . all ( cacheKeys . map ( c => caches . delete ( c ) ) ) ;
23
+ }
22
24
23
- function regReady ( reg ) {
24
- return new Promise ( ( resolve , reject ) => {
25
- if ( reg . active ) {
26
- resolve ( ) ;
27
- return ;
28
- }
29
- const nextWorker = reg . waiting || reg . installing ;
25
+ promise_test ( async t => {
26
+ t . add_cleanup ( deleteCashes ) ;
30
27
31
- nextWorker . addEventListener ( 'statechange' , ( ) => {
32
- if ( nextWorker . state == 'redundant' ) {
33
- reject ( Error ( `Service worker failed to install` ) ) ;
34
- return ;
35
- }
36
- if ( nextWorker . state == 'activated' ) {
37
- resolve ( ) ;
38
- }
39
- } ) ;
40
- } ) ;
41
- }
28
+ const iframe = await registerSwAndOpenFrame ( t ) ;
29
+ const fetchText =
30
+ await iframe . contentWindow . fetch ( 'dummy.txt' ) . then ( r => r . text ( ) ) ;
42
31
43
- function registerSwAndOpenFrame ( ) {
44
- return reset ( ) . then ( ( ) => navigator . serviceWorker . register ( worker , { scope : 'resources/' } ) )
45
- . then ( reg => regReady ( reg ) )
46
- . then ( ( ) => with_iframe ( 'resources/simple.html' ) ) ;
47
- }
32
+ const cache = await iframe . contentWindow . caches . open ( 'test' ) ;
33
+ await cache . add ( 'dummy.txt' ) ;
48
34
49
- promise_test ( ( ) => {
50
- return registerSwAndOpenFrame ( ) . then ( iframe => {
51
- return Promise . all ( [
52
- iframe . contentWindow . fetch ( 'dummy.txt' ) . then ( r => r . text ( ) ) ,
53
- iframe . contentWindow . caches . open ( 'test' )
54
- . then ( cache =>
55
- cache . add ( 'dummy.txt' ) . then ( ( ) => cache . match ( 'dummy.txt' ) )
56
- ) . then ( response => {
57
- if ( ! response ) return 'cache match failed' ;
58
- return response . text ( ) ;
59
- } )
60
- ] )
61
- } ) . then ( ( [ fetchText , cacheText ] ) => {
62
- assert_equals ( fetchText , 'intercepted' , 'fetch intercepted' ) ;
63
- assert_equals ( cacheText , 'intercepted' , 'cache.add intercepted' ) ;
64
- } ) ;
35
+ const response = await cache . match ( 'dummy.txt' ) ;
36
+ const cacheText = await ( response ? response . text ( ) : 'cache match failed' ) ;
37
+ assert_equals ( fetchText , 'intercepted' , 'fetch intercepted' ) ;
38
+ assert_equals ( cacheText , 'intercepted' , 'cache.add intercepted' ) ;
65
39
} , 'Service worker intercepts requests from window' ) ;
66
40
67
- promise_test ( ( ) => {
68
- return registerSwAndOpenFrame ( ) . then ( iframe => {
69
- return Promise . all ( [
70
- iframe . contentWindow . fetch ( 'dummy.txt-inner-fetch' ) . then ( r => r . text ( ) ) ,
71
- iframe . contentWindow . fetch ( 'dummy.txt-inner-cache' ) . then ( r => r . text ( ) )
72
- ] )
73
- } ) . then ( ( [ fetchText , cacheText ] ) => {
74
- assert_equals ( fetchText , 'Hello world\n' , 'fetch within SW not intercepted' ) ;
75
- assert_equals ( cacheText , 'Hello world\n' , 'cache.add within SW not intercepted' ) ;
76
- } ) ;
77
- } , `Service worker does not intercept fetch/cache requests within service worker` ) ;
78
- </ script >
79
- </ body >
41
+ promise_test ( async t => {
42
+ const iframe = await registerSwAndOpenFrame ( t ) ;
43
+ const [ fetchText , cacheText ] = await Promise . all ( [
44
+ iframe . contentWindow . fetch ( 'dummy.txt-inner-fetch' ) . then ( r => r . text ( ) ) ,
45
+ iframe . contentWindow . fetch ( 'dummy.txt-inner-cache' ) . then ( r => r . text ( ) )
46
+ ] ) ;
47
+ assert_equals ( fetchText , 'Hello world\n' , 'fetch within SW not intercepted' ) ;
48
+ assert_equals ( cacheText , 'Hello world\n' ,
49
+ 'cache.add within SW not intercepted' ) ;
50
+ } , 'Service worker does not intercept fetch/cache requests within service ' +
51
+ 'worker' ) ;
52
+ </ script >
0 commit comments