2 <script src="../resources/testharness.js"></script>
3 <script src="../resources/testharnessreport.js"></script>
4 <script src="resources/test-helpers.js"></script>
7 var worker = 'resources/fetch-event-test-worker.js';
9 async_test(function(t) {
10 var scope = 'resources/simple.html?string';
11 service_worker_unregister_and_register(t, worker, scope)
12 .then(function(registration) {
13 return wait_for_update(t, registration);
15 .then(t.step_func(function(sw) {
16 return wait_for_state(t, sw, 'activated');
18 .then(t.step_func(function() { return with_iframe(scope); }))
19 .then(t.step_func(function(frame) {
21 frame.contentDocument.body.textContent,
23 'Service Worker should respond to fetch with a test string');
25 frame.contentDocument.contentType,
27 'The content type of the response created with a string should be text/plain');
29 frame.contentDocument.characterSet,
31 'The character set of the response created with a string should be UTF-8');
32 return service_worker_unregister_and_done(t, scope);
34 .catch(unreached_rejection(t));
35 }, 'Service Worker responds to fetch event with string');
37 async_test(function(t) {
38 var scope = 'resources/simple.html?blob';
39 service_worker_unregister_and_register(t, worker, scope)
40 .then(function(registration) {
41 return wait_for_update(t, registration);
43 .then(t.step_func(function(sw) {
44 return wait_for_state(t, sw, 'activated');
46 .then(t.step_func(function() { return with_iframe(scope); }))
47 .then(t.step_func(function(frame) {
49 frame.contentDocument.body.textContent,
51 'Service Worker should respond to fetch with a test string');
52 return service_worker_unregister_and_done(t, scope);
54 .catch(unreached_rejection(t));
55 }, 'Service Worker responds to fetch event with blob body');
57 async_test(function(t) {
58 var scope = 'resources/simple.html?referrer';
59 service_worker_unregister_and_register(t, worker, scope)
60 .then(function(registration) {
61 return wait_for_update(t, registration);
63 .then(t.step_func(function(sw) {
64 return wait_for_state(t, sw, 'activated');
66 .then(t.step_func(function() { return with_iframe(scope); }))
67 .then(t.step_func(function(frame) {
69 frame.contentDocument.body.textContent,
70 'Referrer: ' + document.location.href,
71 'Service Worker should respond to fetch with the referrer URL');
72 return service_worker_unregister_and_done(t, scope);
74 .catch(unreached_rejection(t));
75 }, 'Service Worker responds to fetch event with the referrer URL');
77 async_test(function(t) {
78 var scope = 'resources/simple.html?ignore';
79 service_worker_unregister_and_register(t, worker, scope)
80 .then(function(registration) {
81 return wait_for_update(t, registration);
83 .then(t.step_func(function(sw) {
84 return wait_for_state(t, sw, 'activated');
86 .then(t.step_func(function() { return with_iframe(scope); }))
87 .then(t.step_func(function(frame) {
88 assert_equals(frame.contentDocument.body.textContent,
89 'Here\'s a simple html file.\n',
90 'Response should come from fallback to native fetch');
91 return service_worker_unregister_and_done(t, scope);
93 .catch(unreached_rejection(t));
94 }, 'Service Worker does not respond to fetch event');
96 async_test(function(t) {
97 var scope = 'resources/simple.html?null';
98 service_worker_unregister_and_register(t, worker, scope)
99 .then(function(registration) {
100 return wait_for_update(t, registration);
102 .then(t.step_func(function(sw) {
103 return wait_for_state(t, sw, 'activated');
105 .then(t.step_func(function() { return with_iframe(scope); }))
106 .then(t.step_func(function(frame) {
107 assert_equals(frame.contentDocument.body.textContent,
109 'Response should be the empty string');
110 return service_worker_unregister_and_done(t, scope);
112 .catch(unreached_rejection(t));
113 }, 'Service Worker responds to fetch event with null response body');
115 async_test(function(t) {
116 var scope = 'resources/simple.html?reject';
117 service_worker_unregister_and_register(t, worker, scope)
118 .then(function(registration) {
119 return wait_for_update(t, registration);
121 .then(t.step_func(function(sw) {
122 return wait_for_state(t, sw, 'activated');
124 .then(t.step_func(function() { return with_iframe(scope); }))
125 .then(t.step_func(function(frame) {
126 assert_equals(frame.contentDocument.body.textContent,
127 'Here\'s a simple html file.\n',
128 'Response should come from fallback to native fetch');
129 return service_worker_unregister_and_done(t, scope);
131 .catch(unreached_rejection(t));
132 }, 'Service Worker rejects fetch event');
134 async_test(function(t) {
135 var scope = 'resources/simple.html?fetch';
136 service_worker_unregister_and_register(t, worker, scope)
137 .then(function(registration) {
138 return wait_for_update(t, registration);
140 .then(t.step_func(function(sw) {
141 return wait_for_state(t, sw, 'activated');
143 .then(t.step_func(function() { return with_iframe(scope); }))
144 .then(t.step_func(function(frame) {
145 assert_equals(frame.contentDocument.body.textContent,
146 'Here\'s an other html file.\n',
147 'Response should come from fetched other file');
148 return service_worker_unregister_and_done(t, scope);
150 .catch(unreached_rejection(t));
151 }, 'Service Worker fetches other file in fetch event');