Chromium Code Reviews| Index: LayoutTests/http/tests/serviceworker/fetch-event.html |
| diff --git a/LayoutTests/http/tests/serviceworker/fetch-event.html b/LayoutTests/http/tests/serviceworker/fetch-event.html |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5a527d4d973d92fdb7bb27d87b20a20c8852bdf6 |
| --- /dev/null |
| +++ b/LayoutTests/http/tests/serviceworker/fetch-event.html |
| @@ -0,0 +1,101 @@ |
| +<!DOCTYPE html> |
| +<script src="../resources/testharness.js"></script> |
| +<script src="../resources/testharnessreport.js"></script> |
| +<script src="resources/test-helpers.js"></script> |
| +<body> |
| +<script> |
| +// FIXME: When request.url is implemented (crbug.com/377373), move all these |
| +// tests into a single Service Worker. The Service Worker can respond |
| +// differently depending on the request URL, allowing for different test cases. |
| +(function () { |
| + var t = async_test('Service Worker responds to fetch event with blob body'); |
| + var scope = '/helloworld/*'; |
| + |
| + service_worker_unregister_and_register( |
| + t, 'resources/hello-world-worker.js', scope, onRegister); |
| + |
| + function onRegister(sw) { |
| + sw.addEventListener('statechange', t.step_func(onStateChange)); |
| + } |
| + |
| + function onStateChange(event) { |
| + if (event.target.state != 'active') |
| + return; |
| + with_iframe('/helloworld/hello', t.step_func(function(frame) { |
| + assert_equals(frame.contentDocument.body.textContent, 'hello, world', |
| + 'Service Worker should respond to fetch with a fixed-string body'); |
| + service_worker_unregister_and_done(t, scope); |
| + })); |
| + } |
| +}()); |
| + |
| +(function () { |
| + function fallback_to_network_test(t, worker) { |
| + return new Promise(function(resolve, reject) { |
| + var scope = 'resources/simple.html'; |
| + t.step(doTest); |
| + |
| + function doTest() { |
| + service_worker_unregister_and_register( |
| + t, worker, scope, onRegister); |
| + } |
| + |
| + function onRegister(sw) { |
| + sw.addEventListener('statechange', t.step_func(onStateChange)); |
| + } |
| + |
| + function onStateChange(event) { |
| + if (event.target.state != 'active') |
| + return; |
| + with_iframe(scope, t.step_func(function(frame) { |
| + assert_equals(frame.contentDocument.body.textContent, "Here's a simple html file.\n", |
| + 'Response should come from fallback to native fetch'); |
| + navigator.serviceWorker.unregister(scope).then( |
| + t.step_func(onUnregister), |
| + unreached_rejection(t, 'Unregister should not fail') |
| + ); |
| + })); |
| + } |
| + |
| + function onUnregister() { |
| + t.done(); |
| + resolve(); |
| + } |
| + }); |
| + } |
| + |
| + var tests = [{ name: 'Service Worker does not respond to fetch event', worker: 'resources/empty-worker.js' }, |
| + { name: 'Service Worker rejects fetch event', worker: 'resources/reject-fetch-event-worker.js' }]; |
| + var sequence = Promise.resolve(); |
| + tests.forEach(function(test) { |
| + sequence = sequence.then(fallback_to_network_test(async_test(test.name), test.worker)); |
| + }); |
| +}()); |
| + |
| +(function () { |
| + var t = async_test('Service Worker responds to fetch event with null response body'); |
| + // Use an actual file (test-helpers.js) to show we're not just defaulting to empty string on |
| + // a non-existent file. simple.html would be better but this test runs concurrently with |
|
jsbell
2014/06/03 17:51:26
Would 'resources/simple.html?null' work? I'm not 1
|
| + // other tests that use simple.html, and it'd be a race over which SW is used. |
| + // (This is moot when the tests are migrated to a single SW). |
| + var scope = 'resources/test-helpers.js'; |
| + |
| + service_worker_unregister_and_register( |
| + t, 'resources/null-response-body-worker.js', scope, onRegister); |
| + |
| + function onRegister(sw) { |
| + sw.addEventListener('statechange', t.step_func(onStateChange)); |
| + } |
| + |
| + function onStateChange(event) { |
| + if (event.target.state != 'active') |
| + return; |
| + with_iframe(scope, t.step_func(function(frame) { |
| + assert_equals(frame.contentDocument.body.textContent, "", |
| + 'Response should be the empty string'); |
| + service_worker_unregister_and_done(t, scope); |
| + })); |
| + } |
| +}()); |
| +</script> |
| +</body> |