summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/clear-site-data/resource.html
blob: a966cb95aaf1c11c08e817471321f46bb2b45697 (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
<!DOCTYPE html>
<html>
  <head>
    <script src="/resources/testharness.js"></script>
    <script src="/resources/testharnessreport.js"></script>
    <script src="support/test_utils.sub.js"></script>
  </head>

  <body>
    <script>
      /**
       * @typedef{TestCase}
       * @type{object}
       * @property{string} frame The scheme of the url of the iframe.
       * @property{string} resource The scheme of the resource in the iframe.
       * @property{boolean} deleted Whether it is expected that Clear-Site-Data
       *     will be respected when loading |resource| in |frame|.
       */
      var TestCase;

      /** Array<TestCase> Test cases. */
      var test_cases = [
        { "frame": "https", "resource": "https", "deleted": true },
        { "frame": "http", "resource": "https", "deleted": true },
        { "frame": "https", "resource": "http", "deleted": false },
        { "frame": "http", "resource": "http", "deleted": false },
      ];

      /**
       * @param TestCase test_case The test case that is tested.
       * @param Dict.<string, boolean> report A map between a datatype name and
       *     whether it is empty.
       */
      function verifyDatatypes(test_case, report) {
        if (test_case.deleted) {
          assert_true(
              report["storage"],
              "Storage should have been cleared.");
        } else {
          assert_false(
              report["storage"],
              "Storage should NOT have been cleared.");
        }
      }

      test_cases.forEach(function(test_case) {
        var test_name =
            test_case.resource + " resource on a " + test_case.frame + " page";

        promise_test(function(test) {
          return new Promise(function(resolve_test, reject_test) {
            TestUtils.populateDatatypes()
                .then(function() {
                  // Navigate to a page with a resource that is loaded with
                  // the Clear-Site-Data header, then verify that storage
                  // has been deleted if and only if the resource was loaded
                  // via HTTPS.
                  return new Promise(function(resolve, reject) {
                    window.addEventListener("message", resolve);
                    var iframe = document.createElement("iframe");
                    iframe.src = TestUtils.getPageWithResourceUrl(
                        test_case.frame, test_case.resource);
                    document.body.appendChild(iframe);
                  }).then(function(messageEvent) {
                    verifyDatatypes(test_case, messageEvent.data);
                    resolve_test();
                  });
                });
          });
        }, test_name);
      });
    </script>
  </body>
</html>