summaryrefslogtreecommitdiffstats
path: root/dom/base/test/test_bug1222633_link_update.html
blob: a398f648b7688a2b388735aa320da74d4520e148 (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
118
119
120
121
122
123
124
125
126
127
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1222633
-->
<head>
  <title>Test for Bug 1222633</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1222633">Mozilla Bug 1222633</a>
<p id="display"></p>
<div id="content" style="display: none"></div>
<script class="testbody" type="text/javascript">

/** Test for Bug 1222633 **/

// Test changing as attribute from an empty value to "foo" to "image". The empty value and "foo" will
// not trigger any event and "image" should trigger an load event.
function testPreloadEventAsAttributeChange(url) {
  return new Promise((resolve) => {
    var link = document.createElement("LINK");
    link.setAttribute("rel", "preload");
    link.setAttribute("href", url);

    link.addEventListener("load", () => {
      ok(link.as == "image", "Only image will trigger a load event");
      link.remove();
      resolve();
    });
    link.addEventListener("error", () => {
      ok(false, "We should not get an error event.");
    });

    document.head.appendChild(link);
    link.setAttribute("as", "foo");
    link.setAttribute("as", "image");
  });
}

function testPreloadEventAttributeChange(url, attr, invalidValue, validValue) {
  return new Promise((resolve) => {
    var count = 0;
    var link = document.createElement("LINK");
    link.setAttribute("rel", "preload");
    link.setAttribute("href", url);
    link.setAttribute("as", "image");

    link.setAttribute(attr, invalidValue);

    let firedLoad = false;
    link.addEventListener("load", () => {
      ok(!firedLoad, "expecting first load event for " + url);
      firedLoad = true;
      link.remove();
      resolve();
    });
    link.addEventListener("error", () => {
      ok(false, "shouldn't fire error events");
    });
    document.head.appendChild(link);
    SimpleTest.executeSoon(function() {
      ok(!firedLoad, "Invalid value shouldn't fire load event");
      link.setAttribute(attr, validValue);
    })
  });
}

function testPreloadEventSetCrossOrigin(url) {
  return new Promise((resolve) => {
    var link = document.createElement("LINK");
    link.setAttribute("rel", "preload");
    link.setAttribute("href", url);
    link.setAttribute("as", "fetch");
    count = 0;

    link.addEventListener("load", () => {
      count++;
      if ((count == 1) || (count == 3) || (count == 5)) {
        ok(true, "expecting " + count + ". load event for " + url);
      } else {
        ok(false, "expecting " + count + ". event for " + url);
      }
      if ((count == 1) || (count == 3)) {
        link.setAttribute("crossorigin", "");
      } else {
        link.remove();
        resolve();
      }
    });

    link.addEventListener("error", () => {
      count++;
      if ((count == 2) || (count == 4)) {
        ok(true, "expecting " + count + ". error event for " + url);
      } else {
        ok(false, "expecting " + count + ". error event for " + url);
      }
      link.removeAttribute("crossorigin");
    });
    document.head.appendChild(link);
  });
}

const IMAGE_PATH = window.location.pathname.replace(/[^/]+$/, "file_mozfiledataurl_img.jpg");
const SJS_PATH = window.location.pathname.replace(/[^/]+$/, "file_bug1268962.sjs");
const SAME_ORIGIN = "http://mochi.test:8888";
const CROSS_ORIGIN = "http://example.com";

SimpleTest.waitForExplicitFinish();

// Test changing as parameter from a wrong to a correct one.
testPreloadEventAsAttributeChange(SAME_ORIGIN + IMAGE_PATH)
// Test changing type parameter from a wrong to a correct one for given as parameter.
.then(() => testPreloadEventAttributeChange(SAME_ORIGIN + IMAGE_PATH, "type", "text/vtt", "image/png"))
// Test changing media parameter from a wrong to a correct one.
.then(() => testPreloadEventAttributeChange(SAME_ORIGIN + IMAGE_PATH, "media", "foo", "all"))
// Test changing crossorigin parameter.
.then(() => testPreloadEventSetCrossOrigin(CROSS_ORIGIN + SJS_PATH + "?statusCode=404&cacheControl=max-age%3D120&allowOrigin=*"))

.catch((err) => ok(false, "promise rejected: " + err))
.then(() => SimpleTest.finish());

</script>
</body>
</html>