Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | <!-- |
michael@0 | 2 | Any copyright is dedicated to the Public Domain. |
michael@0 | 3 | http://creativecommons.org/publicdomain/zero/1.0/ |
michael@0 | 4 | --> |
michael@0 | 5 | |
michael@0 | 6 | <!-- |
michael@0 | 7 | This file contains test for the Resource Timing and Performance Timeline APIs. |
michael@0 | 8 | The test starts by checking that all the entries were added to the performance |
michael@0 | 9 | object. |
michael@0 | 10 | The next step is to check that the "performance" object and its "getEntries()" |
michael@0 | 11 | methods are available. We check all the 3 methods: getEntries, |
michael@0 | 12 | getEntriesByName() and getEntriesByType(). |
michael@0 | 13 | |
michael@0 | 14 | As a next step, we check that the entries contain the correct information |
michael@0 | 15 | ("checkEntries()" method). |
michael@0 | 16 | The test checks that the entries contain all the required members, that the |
michael@0 | 17 | timings are sorted properly and that the entries were returned in |
michael@0 | 18 | chronological order with respect to startTime. In "checkEntries()", it is also |
michael@0 | 19 | checked if the order of the entries is the expected order (the expected order |
michael@0 | 20 | is hard-coded here). |
michael@0 | 21 | The last test from the "checkEntries()" method will verify the iframe case: |
michael@0 | 22 | the iframe must be added as an entry to this window's performance object, |
michael@0 | 23 | while the image from the iframe should not be added here. |
michael@0 | 24 | |
michael@0 | 25 | Next tests will check the Performance API extensions introduced by the |
michael@0 | 26 | resource timing: window.performance.setResourceTimingBufferSize(1) and |
michael@0 | 27 | window.performance.clearResourceTimings(); |
michael@0 | 28 | |
michael@0 | 29 | The last tests will verify that the xhr resources are also added as entries |
michael@0 | 30 | to our performance object. |
michael@0 | 31 | |
michael@0 | 32 | Meanwhile, the iframe from the page will get loaded |
michael@0 | 33 | (resource_timing_iframe.html). |
michael@0 | 34 | The iframe contains a second script that will do some tests, as well, plus |
michael@0 | 35 | an image - its own resource. |
michael@0 | 36 | The script from the iframe will check that the iframe itself was not added |
michael@0 | 37 | as an entry (to itself). Also, it will check that its image was added as |
michael@0 | 38 | entry to the iframe's performance object. |
michael@0 | 39 | The last check is a double check: check that no subdocuments were added as |
michael@0 | 40 | entries for this iframe's performance object. |
michael@0 | 41 | The parent's (this window) "ok_wrapper()" method will be called once the tests |
michael@0 | 42 | are completed. |
michael@0 | 43 | --> |
michael@0 | 44 | |
michael@0 | 45 | <!DOCTYPE html> |
michael@0 | 46 | <html> |
michael@0 | 47 | <head> |
michael@0 | 48 | <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> |
michael@0 | 49 | <script type="application/javascript"> |
michael@0 | 50 | |
michael@0 | 51 | var mainWindowTestsDone = false; |
michael@0 | 52 | var iframeTestsDone = false; |
michael@0 | 53 | |
michael@0 | 54 | function ok(cond, message) { |
michael@0 | 55 | window.opener.ok(cond, message) |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | function is(received, expected, message) { |
michael@0 | 59 | window.opener.is(received, expected, message); |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | function isnot(received, notExpected, message) { |
michael@0 | 63 | window.opener.isnot(received, notExpected, message); |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | window.onload = function() { |
michael@0 | 67 | ok(!!window.performance, "Performance object should exist"); |
michael@0 | 68 | ok(!!window.performance.getEntries, "Performance.getEntries() should exist"); |
michael@0 | 69 | ok(!!window.performance.getEntriesByName, "Performance.getEntriesByName() should exist"); |
michael@0 | 70 | ok(!!window.performance.getEntriesByType, "Performance.getEntriesByType() should exist"); |
michael@0 | 71 | |
michael@0 | 72 | // Here, we should have 6 entries (1 css, 3 png, 1 html) since the image was loaded. |
michael@0 | 73 | is(window.performance.getEntries().length, 5, "Performance.getEntries() returned wrong number of entries."); |
michael@0 | 74 | |
michael@0 | 75 | ok(!!window.performance.getEntriesByType("resource").length, |
michael@0 | 76 | "Performance.getEntriesByType() should return some results"); |
michael@0 | 77 | ok(!!window.performance.getEntriesByName("http://example.com/tests/image/test/mochitest/blue.png").length, |
michael@0 | 78 | "Performance.getEntriesByName() should return some results"); |
michael@0 | 79 | |
michael@0 | 80 | // Checks that two calls for "getEntries()" return a different array with the same |
michael@0 | 81 | // entries. |
michael@0 | 82 | ok(window.performance.getEntries() !== window.performance.getEntries(), |
michael@0 | 83 | "getEntries() should return a different array object every time."); |
michael@0 | 84 | ok(function (array1, array2) { |
michael@0 | 85 | if (array1.length != array2.length) { |
michael@0 | 86 | return false; |
michael@0 | 87 | } |
michael@0 | 88 | for (var i = 0 ; i < array1.length ; i++) { |
michael@0 | 89 | if (array1[i] !== array2[i]) { |
michael@0 | 90 | return false; |
michael@0 | 91 | } |
michael@0 | 92 | } |
michael@0 | 93 | return true; |
michael@0 | 94 | }(window.performance.getEntries(), window.performance.getEntries()), |
michael@0 | 95 | "The arrays should have the same entries."); |
michael@0 | 96 | |
michael@0 | 97 | checkEntries(window.performance.getEntries()); |
michael@0 | 98 | |
michael@0 | 99 | window.performance.setResourceTimingBufferSize(1); |
michael@0 | 100 | is(window.performance.getEntries().length, 5, "No entries should be " + |
michael@0 | 101 | "removed when setResourceTimingBufferSize is called."); |
michael@0 | 102 | |
michael@0 | 103 | window.performance.setResourceTimingBufferSize(4); |
michael@0 | 104 | is(window.performance.getEntries().length, 5, "No entries should be " + |
michael@0 | 105 | "removed when setResourceTimingBufferSize is called."); |
michael@0 | 106 | |
michael@0 | 107 | window.performance.setResourceTimingBufferSize(1); |
michael@0 | 108 | window.performance.clearResourceTimings(); |
michael@0 | 109 | is(window.performance.getEntries().length, 0, "All the entries should " + |
michael@0 | 110 | "be removed when when clearResourceTimings is being called."); |
michael@0 | 111 | |
michael@0 | 112 | makeXhr("test-data.json", firstCheck); |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | function checkEntries(anEntryList) { |
michael@0 | 116 | // Check that all the entries have all the properties. |
michael@0 | 117 | for (var i = 0 ; i < anEntryList.length ; i++) { |
michael@0 | 118 | var entry = anEntryList[i]; |
michael@0 | 119 | |
michael@0 | 120 | ok(!!entry, "PerformanceEntry should not be null"); |
michael@0 | 121 | ok(!!entry.name, "PerformanceEntry.name should be valid."); |
michael@0 | 122 | ok(entry.startTime > 0, "PerformanceEntry.startTime should be grater than 0"); |
michael@0 | 123 | |
michael@0 | 124 | // The entries list should be in chronological order with respect to startTime |
michael@0 | 125 | if (i > 0) { |
michael@0 | 126 | ok(anEntryList[i - 1].startTime <= anEntryList[i].startTime, |
michael@0 | 127 | "Entries list should be in chronological order with respect to startTime."); |
michael@0 | 128 | } |
michael@0 | 129 | |
michael@0 | 130 | // Check that each entry has all the properties and that the timings were |
michael@0 | 131 | // returned in the expected order. |
michael@0 | 132 | if ("initiatorType" in entry) { |
michael@0 | 133 | ok("redirectStart" in entry, "PerformanceEntry.redirectStart should be part of PerformanceEntry"); |
michael@0 | 134 | ok("redirectEnd" in entry, "PerformanceEntry.redirectEnd should be part of PerformanceEntry"); |
michael@0 | 135 | ok("fetchStart" in entry, "PerformanceEntry.fetchStart should be part of PerformanceEntry"); |
michael@0 | 136 | ok("domainLookupStart" in entry, "PerformanceEntry.domainLookupStart should be part of PerformanceEntry"); |
michael@0 | 137 | ok("domainLookupEnd" in entry, "PerformanceEntry.domainLookupEnd should be part of PerformanceEntry"); |
michael@0 | 138 | ok("connectStart" in entry, "PerformanceEntry.connectStart should be part of PerformanceEntry"); |
michael@0 | 139 | ok("connectEnd" in entry, "PerformanceEntry.connectEnd should be part of PerformanceEntry"); |
michael@0 | 140 | ok("secureConnectionStart" in entry, "PerformanceEntry.secureConnectionStart should be part of PerformanceEntry"); |
michael@0 | 141 | ok("requestStart" in entry, "PerformanceEntry.requestStart should be part of PerformanceEntry"); |
michael@0 | 142 | ok("responseStart" in entry, "PerformanceEntry.responseStart should be part of PerformanceEntry"); |
michael@0 | 143 | ok("responseEnd" in entry, "PerformanceEntry.responseEnd should be part of PerformanceEntry"); |
michael@0 | 144 | |
michael@0 | 145 | // Check that timings are in proper order |
michael@0 | 146 | sequence = ['startTime', 'redirectStart', 'redirectEnd', 'fetchStart', |
michael@0 | 147 | 'domainLookupStart', 'domainLookupEnd', 'connectStart', |
michael@0 | 148 | 'connectEnd', 'requestStart', 'responseStart', 'responseEnd']; |
michael@0 | 149 | for (var j = 1; j < sequence.length; ++j) { |
michael@0 | 150 | var prop = sequence[j]; |
michael@0 | 151 | var prevProp = sequence[j-1]; |
michael@0 | 152 | ok(entry[prevProp] <= entry[prop], |
michael@0 | 153 | ['Expected ', prevProp, ' to happen before ', prop, |
michael@0 | 154 | ', got ', prevProp, ' = ', entry[prevProp], |
michael@0 | 155 | ', ', prop, ' = ', entry[prop]].join('')); |
michael@0 | 156 | } |
michael@0 | 157 | } |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | // Check that the entries have the expected initiator type. We can't check |
michael@0 | 161 | // the order (the order might depend on the platform the tests are running). |
michael@0 | 162 | allResources = { |
michael@0 | 163 | "http://mochi.test:8888/tests/SimpleTest/test.css" : "link", |
michael@0 | 164 | "http://example.com/tests/image/test/mochitest/blue.png" : "img", |
michael@0 | 165 | "http://example.com/tests/image/test/mochitest/red.png" : "object", |
michael@0 | 166 | "http://example.com/tests/image/test/mochitest/big.png" : "embed", |
michael@0 | 167 | "http://mochi.test:8888/tests/dom/tests/mochitest/general/resource_timing_iframe.html" : "subdocument"}; |
michael@0 | 168 | |
michael@0 | 169 | for (resourceName in allResources) { |
michael@0 | 170 | // Check that we have a resource with the specific name. |
michael@0 | 171 | namedEntries = window.performance.getEntriesByName(resourceName); |
michael@0 | 172 | ok (!!namedEntries && (namedEntries.length == 1), |
michael@0 | 173 | "An entry with the name '" + resourceName + "' should be available"); |
michael@0 | 174 | |
michael@0 | 175 | // Double check for the entry name. |
michael@0 | 176 | is (namedEntries[0].name, resourceName, "The resource name is invalid"); |
michael@0 | 177 | |
michael@0 | 178 | // Check the initiator type. |
michael@0 | 179 | is (namedEntries[0].initiatorType, allResources[resourceName], |
michael@0 | 180 | "The initiator type for " + resourceName + " is invalid"); |
michael@0 | 181 | } |
michael@0 | 182 | |
michael@0 | 183 | // Check that the iframe's image was NOT added as an entry to this window's performance entry. |
michael@0 | 184 | ok(!window.performance.getEntriesByName("http://example.com/tests/image/test/mochitest/damon.jpg").length, |
michael@0 | 185 | "http://example.com/tests/image/test/mochitest/damon.jpg should be a valid entry name"); |
michael@0 | 186 | } |
michael@0 | 187 | |
michael@0 | 188 | function firstCheck() { |
michael@0 | 189 | is(window.performance.getEntries().length, 1, "The first xhr entry was not added."); |
michael@0 | 190 | is(window.performance.getEntries()[0].initiatorType, "xmlhttprequest", |
michael@0 | 191 | "The initiatorType is incorect for this entry"); |
michael@0 | 192 | makeXhr("test-data2.json", secondCheck); |
michael@0 | 193 | } |
michael@0 | 194 | |
michael@0 | 195 | function secondCheck() { |
michael@0 | 196 | // Since the buffer max size was set to '1', 'peformance.getEntries()' should |
michael@0 | 197 | // return only '1' entry (first xhr results). |
michael@0 | 198 | is(window.performance.getEntries().length, 1, "The second xhr entry should not be " + |
michael@0 | 199 | "returned since the buffer size was set to 1."); |
michael@0 | 200 | isnot(window.performance.getEntries()[0].name, "http://mochi.test:8888/tests/dom/tests/mochitest/general/test-data2.json", |
michael@0 | 201 | "We returned the second xhr instead of the first one"); |
michael@0 | 202 | finishTest(); |
michael@0 | 203 | } |
michael@0 | 204 | |
michael@0 | 205 | function finishTest() { |
michael@0 | 206 | // Check if all the tests are completed. |
michael@0 | 207 | if (iframeTestsDone) { |
michael@0 | 208 | window.opener.finishTests(); |
michael@0 | 209 | } else { |
michael@0 | 210 | mainWindowTestsDone = true; |
michael@0 | 211 | } |
michael@0 | 212 | } |
michael@0 | 213 | |
michael@0 | 214 | function makeXhr(aUrl, aCallback) { |
michael@0 | 215 | var xmlhttp = new XMLHttpRequest(); |
michael@0 | 216 | xmlhttp.onload = aCallback; |
michael@0 | 217 | xmlhttp.open("get", aUrl, true); |
michael@0 | 218 | xmlhttp.send(); |
michael@0 | 219 | } |
michael@0 | 220 | |
michael@0 | 221 | function checkArraysHaveSameElementsInSameOrder(array1, array2) { |
michael@0 | 222 | if (array1.length != array2.length) { |
michael@0 | 223 | return false; |
michael@0 | 224 | } |
michael@0 | 225 | for (var i = 0 ; i < array1.length ; i++) { |
michael@0 | 226 | if (array1[i] !== array2[i]) { |
michael@0 | 227 | return false; |
michael@0 | 228 | } |
michael@0 | 229 | } |
michael@0 | 230 | return true; |
michael@0 | 231 | } |
michael@0 | 232 | |
michael@0 | 233 | function iframeTestsCompleted() { |
michael@0 | 234 | if (mainWindowTestsDone) { |
michael@0 | 235 | window.opener.finishTests(); |
michael@0 | 236 | } |
michael@0 | 237 | else { |
michael@0 | 238 | iframeTestsDone = true; |
michael@0 | 239 | } |
michael@0 | 240 | } |
michael@0 | 241 | |
michael@0 | 242 | </script> |
michael@0 | 243 | </head> |
michael@0 | 244 | <body> |
michael@0 | 245 | <a target="_blank" |
michael@0 | 246 | href="https://bugzilla.mozilla.org/show_bug.cgi?id=822480" |
michael@0 | 247 | title="Add resource timing API."> |
michael@0 | 248 | Bug #822480 - Add in the Resource Timing API |
michael@0 | 249 | </a> |
michael@0 | 250 | <p id="display"></p> |
michael@0 | 251 | <div id="content"> |
michael@0 | 252 | <img src="http://example.com/tests/image/test/mochitest/blue.png"> |
michael@0 | 253 | <object data="http://example.com/tests/image/test/mochitest/red.png" type="image/png"/> |
michael@0 | 254 | <embed src="http://example.com/tests/image/test/mochitest/big.png" type="image/png"/> |
michael@0 | 255 | <iframe sandbox="allow-same-origin allow-scripts" id="if_2" src="resource_timing_iframe.html" height="10" width="10"></iframe> |
michael@0 | 256 | </div> |
michael@0 | 257 | </body> |
michael@0 | 258 | </html> |