Upstream version 10.39.225.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 // Temporary measure to diagnose timeouts on Win XP. Let the W3C harness
8 // timeout before run-webkit-tests does, so it can report the particular
9 // test that timed out.
10 // FIXME: Remove after the cause of timeout is fixed.
11 var properties = {timeout: 5500};
12
13 var worker = 'resources/fetch-event-test-worker.js';
14
15 async_test(function(t) {
16     var scope = 'resources/simple.html?string';
17     service_worker_unregister_and_register(t, worker, scope)
18       .then(function(reg) { return wait_for_activated(t, reg); })
19       .then(function() { return with_iframe(scope); })
20       .then(function(frame) {
21           assert_equals(
22             frame.contentDocument.body.textContent,
23             'Test string',
24             'Service Worker should respond to fetch with a test string');
25           assert_equals(
26             frame.contentDocument.contentType,
27             'text/plain',
28             'The content type of the response created with a string should be text/plain');
29           assert_equals(
30             frame.contentDocument.characterSet,
31             'UTF-8',
32             'The character set of the response created with a string should be UTF-8');
33           unload_iframe(frame);
34           return service_worker_unregister_and_done(t, scope);
35         })
36       .catch(unreached_rejection(t));
37   }, 'Service Worker responds to fetch event with string', properties);
38
39 async_test(function(t) {
40     var scope = 'resources/simple.html?blob';
41     service_worker_unregister_and_register(t, worker, scope)
42       .then(function(reg) { return wait_for_activated(t, reg); })
43       .then(function() { return with_iframe(scope); })
44       .then(function(frame) {
45           assert_equals(
46             frame.contentDocument.body.textContent,
47             'Test blob',
48             'Service Worker should respond to fetch with a test string');
49           unload_iframe(frame);
50           return service_worker_unregister_and_done(t, scope);
51         })
52       .catch(unreached_rejection(t));
53   }, 'Service Worker responds to fetch event with blob body', properties);
54
55 async_test(function(t) {
56     var scope = 'resources/simple.html?referrer';
57     service_worker_unregister_and_register(t, worker, scope)
58       .then(function(reg) { return wait_for_activated(t, reg); })
59       .then(function() { return with_iframe(scope); })
60       .then(function(frame) {
61           assert_equals(
62             frame.contentDocument.body.textContent,
63             'Referrer: ' + document.location.href,
64             'Service Worker should respond to fetch with the referrer URL');
65           unload_iframe(frame);
66           return service_worker_unregister_and_done(t, scope);
67         })
68       .catch(unreached_rejection(t));
69   }, 'Service Worker responds to fetch event with the referrer URL', properties);
70
71 async_test(function(t) {
72     var scope = 'resources/simple.html?ignore';
73     service_worker_unregister_and_register(t, worker, scope)
74       .then(function(reg) { return wait_for_activated(t, reg); })
75       .then(function() { return with_iframe(scope); })
76       .then(function(frame) {
77           assert_equals(frame.contentDocument.body.textContent,
78                         'Here\'s a simple html file.\n',
79                         'Response should come from fallback to native fetch');
80           unload_iframe(frame);
81           return service_worker_unregister_and_done(t, scope);
82         })
83       .catch(unreached_rejection(t));
84   }, 'Service Worker does not respond to fetch event', properties);
85
86 async_test(function(t) {
87     var scope = 'resources/simple.html?null';
88     service_worker_unregister_and_register(t, worker, scope)
89       .then(function(reg) { return wait_for_activated(t, reg); })
90       .then(function() { return with_iframe(scope); })
91       .then(function(frame) {
92           assert_equals(frame.contentDocument.body.textContent,
93                         '',
94                         'Response should be the empty string');
95           unload_iframe(frame);
96           return service_worker_unregister_and_done(t, scope);
97         })
98       .catch(unreached_rejection(t));
99   }, 'Service Worker responds to fetch event with null response body', properties);
100
101 async_test(function(t) {
102     var scope = 'resources/simple.html?fetch';
103     service_worker_unregister_and_register(t, worker, scope)
104       .then(function(reg) { return wait_for_activated(t, reg); })
105       .then(function() { return with_iframe(scope); })
106       .then(function(frame) {
107           assert_equals(frame.contentDocument.body.textContent,
108                         'Here\'s an other html file.\n',
109                         'Response should come from fetched other file');
110           unload_iframe(frame);
111           return service_worker_unregister_and_done(t, scope);
112         })
113       .catch(unreached_rejection(t));
114   }, 'Service Worker fetches other file in fetch event', properties);
115
116 async_test(function(t) {
117     var scope = 'resources/simple.html?form-post';
118     var frame_name = 'xhr-post-frame';
119     service_worker_unregister_and_register(t, worker, scope)
120       .then(function(reg) { return wait_for_activated(t, reg); })
121       .then(function(sw) {
122          return new Promise(function(resolve) {
123             var frame = document.createElement('iframe');
124             frame.name = frame_name;
125             document.body.appendChild(frame);
126             var form = document.createElement('form');
127             form.target = frame_name;
128             form.action = scope;
129             form.method = 'post';
130             var input1 = document.createElement('input');
131             input1.type = 'text';
132             input1.value = 'testValue1';
133             input1.name = 'testName1'
134             form.appendChild(input1);
135             var input2 = document.createElement('input');
136             input2.type = 'text';
137             input2.value = 'testValue2';
138             input2.name = 'testName2'
139             form.appendChild(input2);
140             document.body.appendChild(form);
141             frame.onload = function() {
142               document.body.removeChild(form);
143               resolve(frame);
144             };
145             form.submit();
146           });
147         })
148       .then(function(frame) {
149           assert_equals(frame.contentDocument.body.textContent,
150                         'POST:testName1=testValue1&testName2=testValue2');
151           document.body.removeChild(frame);
152           return service_worker_unregister_and_done(t, scope);
153         })
154       .catch(unreached_rejection(t));
155   }, 'Service Worker responds to fetch event with POST form', properties);
156
157 async_test(function(t) {
158     var scope = 'resources/simple.html?multiple-respond-with';
159     service_worker_unregister_and_register(t, worker, scope)
160       .then(function(reg) { return wait_for_activated(t, reg); })
161       .then(function() { return with_iframe(scope); })
162       .then(function(frame) {
163           assert_equals(
164             frame.contentDocument.body.textContent,
165             '(0)',
166             'Response should be the argument of the first respondWith() call.');
167           unload_iframe(frame);
168           return with_iframe(scope);
169         })
170       .then(function(frame) {
171           assert_equals(
172             frame.contentDocument.body.textContent,
173             '(0)(1)[InvalidStateError](2)[InvalidStateError](0)',
174             'Multiple calls of respondWith must throw InvalidStateErrors.');
175           unload_iframe(frame);
176           return service_worker_unregister_and_done(t, scope);
177         })
178       .catch(unreached_rejection(t));
179   }, 'Multiple calls of respondWith must throw InvalidStateErrors', properties);
180
181 </script>
182 </body>