summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/service-workers/service-worker/unregister-controller.https.html
blob: 3bf4cff7200e71a7ad157f97f60e0bca45693485 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/test-helpers.sub.js"></script>
<script>
var worker_url = 'resources/simple-intercept-worker.js';

async_test(function(t) {
    var scope =
        'resources/unregister-controller-page.html?load-before-unregister';
    var frame_window;
    var controller;
    var registration;
    var frame;

    service_worker_unregister_and_register(t, worker_url, scope)
      .then(function(r) {
          registration = r;
          return wait_for_state(t, r.installing, 'activated');
        })
      .then(function() {
          return with_iframe(scope);
        })
      .then(function(f) {
          frame = f;
          frame_window = frame.contentWindow;
          controller = frame_window.navigator.serviceWorker.controller;
          assert_true(controller instanceof frame_window.ServiceWorker,
                      'document should load with a controller');
          return registration.unregister();
        })
      .then(function() {
          assert_equals(frame_window.navigator.serviceWorker.controller,
                        controller,
                        'unregistration should not modify controller');
          return frame_window.fetch_url('simple.txt');
        })
      .then(function(response) {
          assert_equals(response, 'intercepted by service worker',
                        'controller should intercept requests');
          frame.remove();
          t.done();
        })
      .catch(unreached_rejection(t));
  }, 'Unregister does not affect existing controller');

async_test(function(t) {
    var scope =
        'resources/unregister-controller-page.html?load-after-unregister';
    var registration;
    var frame;

    service_worker_unregister_and_register(t, worker_url, scope)
      .then(function(r) {
          registration = r;
          return wait_for_state(t, r.installing, 'activated');
        })
      .then(function() {
          return registration.unregister();
        })
      .then(function() {
          return with_iframe(scope);
        })
      .then(function(f) {
          frame = f;
          var frame_window = frame.contentWindow;
          assert_equals(frame_window.navigator.serviceWorker.controller, null,
                        'document should not have a controller');
          return frame_window.fetch_url('simple.txt');
        })
      .then(function(response) {
          assert_equals(response, 'a simple text file\n',
                        'requests should not be intercepted');
          frame.remove();
          t.done();
        })
      .catch(unreached_rejection(t));
  }, 'Unregister prevents control of subsequent navigations');

async_test(function(t) {
    var scope =
        'resources/scope/no-new-controllee-even-if-registration-is-still-used';
    var registration;

    service_worker_unregister_and_register(t, worker_url, scope)
      .then(function(r) {
          registration = r;
          return wait_for_state(t, r.installing, 'activated');
        })
      .then(function() {
          return with_iframe(scope);
        })
      .then(function(frame) {
          return registration.unregister();
        })
      .then(function() {
          return with_iframe(scope);
        })
      .then(function(frame) {
          assert_equals(frame.contentWindow.navigator.serviceWorker.controller,
                        null,
                        'document should not have a controller');
          frame.remove();
          t.done();
        })
      .catch(unreached_rejection(t));
  }, 'Unregister prevents new controllee even if registration is still in use');
</script>