summaryrefslogtreecommitdiffstats
path: root/dom/base/test/test_bug704320_preload.html
blob: d7f2fc72d39bcc292691782027de48a8240333a4 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<!DOCTYPE HTML>
<html>
<!--
This is a spot check for whether the speculative parser reuses style, script or image loads after the referrer policy has changed.
https://bugzilla.mozilla.org/show_bug.cgi?id=704320
-->

<head>
  <meta charset="utf-8">
  <title>Test preloads for Bug 704320</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="application/javascript" src="referrerHelper.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>

<script type="application/javascript;version=1.7">

SimpleTest.waitForExplicitFinish();
var advance = function() { tests.next(); };

/**
 * This is the main test routine -- serialized by use of a generator.
 * It resets the counter, then performs two tests in sequence using
 * the same iframe.
 */
var tests = (function() {
  var iframe = document.getElementById("testframe");

  // reset the counter
  yield resetCounter();

  // load the first test frame
  // it will call back into this function via postMessage when it finishes loading.
  // and continue beyond the yield.
  yield iframe.src = 'file_bug704320_preload_noreuse.html';

  // check the first test
  yield checkResults(finalizePreloadNoreuse);

  // reset the counter
  yield resetCounter();

  // load the second test frame
  // it will call back into this function via postMessage when it finishes loading.
  // and continue beyond the yield.
  yield iframe.src = 'file_bug704320_preload_reuse.html';

  // check the second test
  yield checkResults(finalizePreloadReuse);

  // complete.  Be sure to yield so we don't call this twice.
  yield SimpleTest.finish();
})();

// Helper functions below.

/**
 * This checks the first test: a test where the preloads should not
 * be reused.  * we expect two requests for each image, script, js request
 * since the referrer policy changed after speculative loads were started.
 * Problem is that the "origin"/revised loads won't necessarily happen,
 * so we test for one or two loads (both are OK) and make the 'origin'
 * referrer optional.
 */
function finalizePreloadNoreuse(results) {
  console.log("<br/><pre>" + JSON.stringify(results) + "</pre>");
  var expected = {'css': {'count': 2, 'referrers': ['full', 'origin']},
                  'img': {'count': 2, 'referrers': ['full', 'origin']},
                  'js':  {'count': 2, 'referrers': ['full', 'origin']}};

  for (var x in expected) {
    ok(x in results, "some " + x + " loads required in results object.");

    ok(results[x].count == 1 || results[x].count == 2,
       "Expected 1-2  loads for " + x + " requests.");

    // the 'full' preload is optional, but required if count > 1
    if (results[x].count > 1) {
      ok(results[x].referrers.indexOf('full') >= 0,
         "More than one load for " + x + ", so expected an 'full' referrer preload.")
    }

    // 'origin' (final load) is required
    ok(results[x].referrers.indexOf('origin') >= 0,
       "One load for " + x + " should have had 'origin' referrer.");

    // no other values should be in the referrers.
    is(results[x].referrers.indexOf('none'), -1,
       "No loads for " + x + " should have a missing referrer.");
  }

  advance();
}

/**
 * This checks the second test: a test where preloads SHOULD be reused.
 * We expect one request for each image, script, js request since
 * the referrer policy does not change after speculative loads.
 */
function finalizePreloadReuse(results) {
  var expected = {'css': {'count': 1, 'referrers': ['origin']},
                  'img': {'count': 1, 'referrers': ['origin']},
                  'js':  {'count': 1, 'referrers': ['origin']}};

  for (var x in expected) {
    ok(x in results, "some " + x + " loads required in results object.");

    is(results[x].count, expected[x].count,
       "Expected " + expected[x].count + " loads for " + x + " requests.");

    // 'origin' is required
    ok(results[x].referrers.indexOf('origin') >= 0,
       "One load for " + x + " should have had 'origin' referrer.");

    // no other values should be in the referrers.
    is(results[x].referrers.indexOf('none'), -1,
       "No loads for " + x + " should have a missing referrer.");
    is(results[x].referrers.indexOf('full'), -1,
       "No loads for " + x + " should have an 'full' referrer.")
  }

  advance();
}


/**
 * Grabs the results via XHR and passes to checker.
 */
function checkResults(checker) {
  doXHR('/tests/dom/base/test/bug704320_counter.sjs?results',
        function(xhr) {
          checker(JSON.parse(xhr.responseText));
        },
        function(xhr) {
          ok(false, "Can't get results from the counter server.");
        });
}

</script>
</head>

<body onload="tests.next();">
  <iframe id="testframe"></iframe>

</body>
</html>