summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/service-workers/service-worker/client-navigate.https.html
blob: a505e8722791b58d77b130b62e7448879ebfa4f1 (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
109
110
111
112
113
114
115
116
117
<!doctype html>
<meta charset=utf-8>
<title>Service Worker: WindowClient.navigate</title>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script src="resources/get-host-info.sub.js"></script>
<script src="resources/test-helpers.sub.js"></script>
<script>
  function wait_for_message(msg) {
    return new Promise(function(resolve, reject) {
      var get_message_data = function get_message_data(e) {
        window.removeEventListener("message", get_message_data);
        resolve(e.data);
      }
      window.addEventListener("message", get_message_data, false);
    });
  }

  function run_test(controller, clientId, test) {
    return new Promise(function(resolve, reject) {
      var channel = new MessageChannel();
      channel.port1.onmessage = function(e) {
        resolve(e.data);
      };
      var message = {
        port: channel.port2,
        test: test,
        clientId: clientId,
      };
      controller.postMessage(
        message, [channel.port2]);
    });
  }

  promise_test(function(t) {
    var worker = "resources/client-navigate-worker.js";
    var scope = "resources/client-navigate-frame.html";
    var controller, frame, clientId;

    return service_worker_unregister_and_register(t, worker, scope)
      .then(reg => wait_for_state(t, reg.installing, "activated"))
      .then(___ => with_iframe(scope))
      .then(f => {
        frame = f;
        controller = frame.contentWindow.navigator.serviceWorker.controller;
        fetch_tests_from_worker(controller);
        return wait_for_message()
      })
      .then(({id}) => clientId = id)
      .then(___ => run_test(controller, clientId, "test_client_navigate_success"))
      .then(({result, url}) => {
        assert_equals(result, "test_client_navigate_success");
        assert_equals(
          url, new URL("resources/client-navigated-frame.html",
                       location).toString());
        assert_equals(
          frame.contentWindow.location.href,
          new URL("resources/client-navigated-frame.html",
                  location).toString());
      })
      .catch(unreached_rejection(t))
      .then(___ => service_worker_unregister(t, scope));
  }, "Frame location should update on successful navigation");

  promise_test(function(t) {
    var worker = "resources/client-navigate-worker.js";
    var scope = "resources/client-navigate-frame.html";
    var controller, frame, clientId;

    return service_worker_unregister_and_register(t, worker, scope)
      .then(reg => wait_for_state(t, reg.installing, "activated"))
      .then(___ => with_iframe(scope))
      .then(f => {
        frame = f;
        controller = frame.contentWindow.navigator.serviceWorker.controller;
        fetch_tests_from_worker(controller);
        return wait_for_message()
      })
      .then(({id}) => clientId = id)
      .then(___ => run_test(controller, clientId, "test_client_navigate_redirect"))
      .then(({result, url}) => {
        assert_equals(result, "test_client_navigate_redirect");
        assert_equals(url, "");
        assert_throws(null, function() { return frame.contentWindow.location.href });
      })
      .catch(unreached_rejection(t))
      .then(___ => service_worker_unregister(t, scope));
  }, "Frame location should not be accessible after redirect");

  promise_test(function(t) {
    var worker = "resources/client-navigate-worker.js";
    var scope = "resources/client-navigate-frame.html";
    var controller, frame, clientId;

    return service_worker_unregister_and_register(t, worker, scope)
      .then(reg => wait_for_state(t, reg.installing, "activated"))
      .then(___ => with_iframe(scope))
      .then(f => {
        frame = f;
        controller = frame.contentWindow.navigator.serviceWorker.controller;
        fetch_tests_from_worker(controller);
        return wait_for_message()
      })
      .then(({id}) => clientId = id)
      .then(___ => run_test(controller, clientId, "test_client_navigate_failure"))
      .then(({result, url}) => {
        assert_equals(result, "test_client_navigate_failure");
        assert_equals(
          frame.contentWindow.location.href,
          new URL("resources/client-navigate-frame.html",
                  location).toString());
        frame.contentWindow.document.body.style = "background-color: green"
      })
      .catch(unreached_rejection(t))
      .then(___ => service_worker_unregister(t, scope));
  }, "Frame location should not update on failed navigation");
</script>