Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / http / tests / serviceworker / fetch-event.html
1 <!DOCTYPE html>
2 <script src="../resources/testharness.js"></script>
3 <script src="../resources/testharnessreport.js"></script>
4 <script src="resources/test-helpers.js"></script>
5 <body>
6 <script>
7 var worker = 'resources/fetch-event-test-worker.js';
8
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);
14         })
15       .then(t.step_func(function(sw) {
16           return wait_for_state(t, sw, 'activated');
17         }))
18       .then(t.step_func(function() { return with_iframe(scope); }))
19       .then(t.step_func(function(frame) {
20           assert_equals(
21             frame.contentDocument.body.textContent,
22             'Test string',
23             'Service Worker should respond to fetch with a test string');
24           assert_equals(
25             frame.contentDocument.contentType,
26             'text/plain',
27             'The content type of the response created with a string should be text/plain');
28           assert_equals(
29             frame.contentDocument.characterSet,
30             'UTF-8',
31             'The character set of the response created with a string should be UTF-8');
32           return service_worker_unregister_and_done(t, scope);
33         }))
34       .catch(unreached_rejection(t));
35   }, 'Service Worker responds to fetch event with string');
36
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);
42         })
43       .then(t.step_func(function(sw) {
44           return wait_for_state(t, sw, 'activated');
45         }))
46       .then(t.step_func(function() { return with_iframe(scope); }))
47       .then(t.step_func(function(frame) {
48           assert_equals(
49             frame.contentDocument.body.textContent,
50             'Test blob',
51             'Service Worker should respond to fetch with a test string');
52           return service_worker_unregister_and_done(t, scope);
53         }))
54       .catch(unreached_rejection(t));
55   }, 'Service Worker responds to fetch event with blob body');
56
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);
62         })
63       .then(t.step_func(function(sw) {
64           return wait_for_state(t, sw, 'activated');
65         }))
66       .then(t.step_func(function() { return with_iframe(scope); }))
67       .then(t.step_func(function(frame) {
68           assert_equals(
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);
73         }))
74       .catch(unreached_rejection(t));
75   }, 'Service Worker responds to fetch event with the referrer URL');
76
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);
82         })
83       .then(t.step_func(function(sw) {
84           return wait_for_state(t, sw, 'activated');
85         }))
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);
92         }))
93       .catch(unreached_rejection(t));
94   }, 'Service Worker does not respond to fetch event');
95
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);
101         })
102       .then(t.step_func(function(sw) {
103           return wait_for_state(t, sw, 'activated');
104         }))
105       .then(t.step_func(function() { return with_iframe(scope); }))
106       .then(t.step_func(function(frame) {
107           assert_equals(frame.contentDocument.body.textContent,
108                         '',
109                         'Response should be the empty string');
110           return service_worker_unregister_and_done(t, scope);
111         }))
112       .catch(unreached_rejection(t));
113   }, 'Service Worker responds to fetch event with null response body');
114
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);
120         })
121       .then(t.step_func(function(sw) {
122           return wait_for_state(t, sw, 'activated');
123         }))
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);
130         }))
131       .catch(unreached_rejection(t));
132   }, 'Service Worker rejects fetch event');
133
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);
139         })
140       .then(t.step_func(function(sw) {
141           return wait_for_state(t, sw, 'activated');
142         }))
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);
149         }))
150       .catch(unreached_rejection(t));
151   }, 'Service Worker fetches other file in fetch event');
152 </script>
153 </body>