Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / http / tests / serviceworker / ready.html
1 <!DOCTYPE html>
2 <title>Service Worker: navigator.serviceWorker.ready</title>
3 <script src="../resources/testharness.js"></script>
4 <script src="../resources/testharnessreport.js"></script>
5 <script src="resources/test-helpers.js"></script>
6 <body>
7 <script>
8 test(function() {
9     var promise = navigator.serviceWorker.ready;
10     assert_equals(promise, navigator.serviceWorker.ready,
11                   'repeated access to ready without intervening ' +
12                   'registrations should return the same Promise object');
13   }, 'ready returns the same Promise object');
14
15 async_test(function(t) {
16     with_iframe('resources/blank.html?uncontrolled')
17       .then(t.step_func(function(frame) {
18           var promise = frame.contentWindow.navigator.serviceWorker.ready;
19           assert_equals(Object.getPrototypeOf(promise),
20                         frame.contentWindow.Promise.prototype,
21                         'the Promise should be in the context of the ' +
22                         'related document');
23           unload_iframe(frame);
24           t.done();
25         }));
26   }, 'ready returns a Promise object in the context of the related document');
27
28 async_test(function(t) {
29     var url = 'resources/empty-worker.js';
30     var scope = 'resources/blank.html?ready-controlled';
31     var expected_url = normalizeURL(url);
32     var frame;
33
34     service_worker_unregister_and_register(t, url, scope)
35       .then(function(registration) {
36           return wait_for_activated(t, registration);
37         })
38       .then(function() { return with_iframe(scope); })
39       .then(function(f) {
40           frame = f;
41           return frame.contentWindow.navigator.serviceWorker.ready;
42         })
43       .then(function(registration) {
44           assert_equals(registration.installing, null,
45                         'installing should be null');
46           assert_equals(registration.waiting, null,
47                         'waiting should be null');
48           assert_equals(registration.active.scriptURL, expected_url,
49                         'active after ready should not be null');
50           assert_equals(
51               frame.contentWindow.navigator.serviceWorker.controller.scriptURL,
52               expected_url,
53               'controlled document should have a controller');
54
55           unload_iframe(frame);
56           service_worker_unregister_and_done(t, scope);
57         })
58       .catch(unreached_rejection(t));
59   }, 'ready on a controlled document');
60
61 async_test(function(t) {
62     var url = 'resources/empty-worker.js';
63     var scope = 'resources/blank.html?ready-potential-controlled';
64     var expected_url = normalizeURL(url);
65     var frame;
66
67     with_iframe(scope)
68       .then(function(f) {
69           frame = f;
70           return navigator.serviceWorker.register(url, {scope:scope});
71         })
72       .then(function() {
73           return frame.contentWindow.navigator.serviceWorker.ready;
74         })
75       .then(function(registration) {
76           assert_equals(registration.installing, null,
77                         'installing should be null');
78           assert_equals(registration.waiting, null,
79                         'waiting should be null.')
80           assert_equals(registration.active.scriptURL, expected_url,
81                         'active after ready should not be null');
82           assert_equals(frame.contentWindow.navigator.serviceWorker.controller,
83                         null,
84                         'uncontrolled document should not have a controller');
85
86           unload_iframe(frame);
87           service_worker_unregister_and_done(t, scope);
88         })
89       .catch(unreached_rejection(t));
90   }, 'ready on a potential controlled document');
91
92 async_test(function(t) {
93     var url = 'resources/empty-worker.js';
94     var scope = 'resources/blank.html?ready-after-unregister';
95     var expected_url = normalizeURL(url);
96     var frame;
97     var registration;
98
99     service_worker_unregister_and_register(t, url, scope)
100       .then(function(r) {
101           registration = r;
102           return wait_for_activated(t, registration);
103         })
104       .then(function() { return with_iframe(scope); })
105       .then(function(f) {
106           frame = f;
107           return registration.unregister();
108         })
109       .then(function() {
110           return frame.contentWindow.navigator.serviceWorker.ready;
111         })
112       .then(function(registration) {
113           assert_equals(registration.installing, null,
114                         'installing should be null');
115           assert_equals(registration.waiting, null,
116                         'waiting should be null');
117           assert_equals(registration.active.scriptURL, expected_url,
118                         'active after ready should not be null');
119           assert_equals(
120               frame.contentWindow.navigator.serviceWorker.controller.scriptURL,
121               expected_url,
122               'controlled document should have a controller');
123
124           unload_iframe(frame);
125           service_worker_unregister_and_done(t, scope);
126         })
127       .catch(unreached_rejection(t));
128   }, 'ready after unregistration');
129
130 // FIXME: When replace() is implemented add a test that .ready is
131 // repeatedly created and settled.
132 </script>