2 <title>Service Worker: FetchEvent for resources
</title>
3 <script src=
"../resources/testharness.js"></script>
4 <script src=
"../resources/testharnessreport.js"></script>
5 <script src=
"../resources/get-host-info.js"></script>
6 <script src=
"resources/test-helpers.js?pipe=sub"></script>
9 var expected_results
= {};
11 function image_test(frame
, url
, cross_origin
,
12 expected_mode
, expected_credentials
) {
13 var actual_url
= url
+ (++url_count
);
14 expected_results
[actual_url
] = {
15 cross_origin
: cross_origin
,
17 credentials
: expected_credentials
,
19 message
: 'Image load (url:' +
20 actual_url
+ ' cross_origin:' + cross_origin
+ ')'
22 return frame
.contentWindow
.load_image(actual_url
, cross_origin
);
25 function script_test(frame
, url
, cross_origin
,
26 expected_mode
, expected_credentials
) {
27 var actual_url
= url
+ (++url_count
);
28 expected_results
[actual_url
] = {
29 cross_origin
: cross_origin
,
31 credentials
: expected_credentials
,
33 message
: 'Script load (url:' +
34 actual_url
+ ' cross_origin:' + cross_origin
+ ')'
36 return frame
.contentWindow
.load_script(actual_url
, cross_origin
);
39 function css_test(frame
, url
, cross_origin
,
40 expected_mode
, expected_credentials
) {
41 var actual_url
= url
+ (++url_count
);
42 expected_results
[actual_url
] = {
43 cross_origin
: cross_origin
,
45 credentials
: expected_credentials
,
47 message
: 'CSS load (url:' +
48 actual_url
+ ' cross_origin:' + cross_origin
+ ')'
50 return frame
.contentWindow
.load_css(actual_url
, cross_origin
);
53 function font_face_test(frame
, url
, expected_mode
,
54 expected_credentials
) {
55 var actual_url
= url
+ (++url_count
);
56 expected_results
[actual_url
] = {
59 credentials
: expected_credentials
,
61 message
: 'FontFace load (url:' + actual_url
+ ')'
63 return frame
.contentWindow
.load_font(actual_url
);
66 function css_image_test(frame
, url
, type
,
67 expected_mode
, expected_credentials
) {
68 var actual_url
= url
+ (++url_count
);
69 expected_results
[actual_url
] = {
72 credentials
: expected_credentials
,
74 message
: 'CSSImage load (url:' + actual_url
+ ' type:' + type
+ ')'
76 return frame
.contentWindow
.load_css_image(actual_url
, type
);
79 function css_image_set_test(frame
, url
, type
,
80 expected_mode
, expected_credentials
) {
81 var actual_url
= url
+ (++url_count
);
82 expected_results
[actual_url
] = {
85 credentials
: expected_credentials
,
87 message
: 'CSSImageSet load (url:' + actual_url
+ ' type:' + type
+ ')'
89 return frame
.contentWindow
.load_css_image_set(actual_url
, type
);
92 async_test(function(t
) {
93 var SCOPE
= 'resources/fetch-request-resources-iframe.html';
94 var SCRIPT
= 'resources/fetch-request-resources-worker.js';
95 var host_info
= get_host_info();
97 host_info
['HTTP_ORIGIN'] + base_path() + 'resources/dummy?test';
99 host_info
['HTTP_REMOTE_ORIGIN'] + base_path() + 'resources/dummy?test';
102 service_worker_unregister_and_register(t
, SCRIPT
, SCOPE
)
103 .then(function(registration
) {
104 worker
= registration
.installing
;
105 return wait_for_state(t
, worker
, 'activated');
108 return new Promise(function(resolve
) {
109 var channel
= new MessageChannel();
110 channel
.port1
.onmessage
= t
.step_func(function(msg
) {
111 if (msg
.data
.ready
) {
115 var result
= msg
.data
;
116 var expected
= expected_results
[result
.url
];
121 result
.mode
, expected
.mode
,
122 'mode of ' + expected
.message
+ ' must be ' +
123 expected
.mode
+ '.');
125 result
.credentials
, expected
.credentials
,
126 'credentials of ' + expected
.message
+ ' must be ' +
127 expected
.credentials
+ '.');
129 result
.redirect
, expected
.redirect
,
130 'redirect mode of ' + expected
.message
+ ' must be ' +
131 expected
.redirect
+ '.');
133 delete expected_results
[result
.url
];
134 if (url_count
== 0) {
136 service_worker_unregister_and_done(t
, SCOPE
);
140 {port
: channel
.port2
}, [channel
.port2
]);
143 .then(function() { return with_iframe(SCOPE
); })
146 image_test(f
, LOCAL_URL
, '', 'no-cors', 'same-origin');
147 image_test(f
, LOCAL_URL
, 'anonymous', 'cors', 'omit');
148 image_test(f
, LOCAL_URL
, 'use-credentials', 'cors', 'include');
149 image_test(f
, REMOTE_URL
, '', 'no-cors', 'same-origin');
150 image_test(f
, REMOTE_URL
, 'anonymous', 'cors', 'omit');
151 image_test(f
, REMOTE_URL
, 'use-credentials', 'cors', 'include');
153 script_test(f
, LOCAL_URL
, '', 'no-cors', 'same-origin');
154 script_test(f
, LOCAL_URL
, 'anonymous', 'cors', 'omit');
155 script_test(f
, LOCAL_URL
, 'use-credentials', 'cors', 'include');
156 script_test(f
, REMOTE_URL
, '', 'no-cors', 'same-origin');
157 script_test(f
, REMOTE_URL
, 'anonymous', 'cors', 'omit');
158 script_test(f
, REMOTE_URL
, 'use-credentials', 'cors', 'include');
160 css_test(f
, LOCAL_URL
, '', 'no-cors', 'same-origin');
161 css_test(f
, LOCAL_URL
, 'anonymous', 'cors', 'omit');
162 css_test(f
, LOCAL_URL
, 'use-credentials', 'cors', 'include');
163 css_test(f
, REMOTE_URL
, '', 'no-cors', 'same-origin');
164 css_test(f
, REMOTE_URL
, 'anonymous', 'cors', 'omit');
165 css_test(f
, REMOTE_URL
, 'use-credentials', 'cors', 'include');
167 font_face_test(f
, LOCAL_URL
, 'cors', 'include');
168 font_face_test(f
, REMOTE_URL
, 'cors', 'omit');
170 css_image_test(f
, LOCAL_URL
, 'backgroundImage',
171 'no-cors', 'same-origin');
172 css_image_test(f
, REMOTE_URL
, 'backgroundImage',
173 'no-cors', 'same-origin');
174 css_image_test(f
, LOCAL_URL
, 'shapeOutside', 'cors', 'omit');
175 css_image_test(f
, REMOTE_URL
, 'shapeOutside', 'cors', 'omit');
177 css_image_set_test(f
, LOCAL_URL
, 'backgroundImage',
178 'no-cors', 'same-origin');
179 css_image_set_test(f
, REMOTE_URL
, 'backgroundImage',
180 'no-cors', 'same-origin');
181 css_image_set_test(f
, LOCAL_URL
, 'shapeOutside', 'cors', 'omit');
182 css_image_set_test(f
, REMOTE_URL
, 'shapeOutside', 'cors', 'omit');
185 .catch(unreached_rejection(t
));
186 }, 'Verify FetchEvent for resources.');