1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/tests/mochitest/general/resource_timing_main_test.html Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,258 @@ 1.4 +<!-- 1.5 + Any copyright is dedicated to the Public Domain. 1.6 + http://creativecommons.org/publicdomain/zero/1.0/ 1.7 +--> 1.8 + 1.9 +<!-- 1.10 + This file contains test for the Resource Timing and Performance Timeline APIs. 1.11 + The test starts by checking that all the entries were added to the performance 1.12 + object. 1.13 + The next step is to check that the "performance" object and its "getEntries()" 1.14 + methods are available. We check all the 3 methods: getEntries, 1.15 + getEntriesByName() and getEntriesByType(). 1.16 + 1.17 + As a next step, we check that the entries contain the correct information 1.18 + ("checkEntries()" method). 1.19 + The test checks that the entries contain all the required members, that the 1.20 + timings are sorted properly and that the entries were returned in 1.21 + chronological order with respect to startTime. In "checkEntries()", it is also 1.22 + checked if the order of the entries is the expected order (the expected order 1.23 + is hard-coded here). 1.24 + The last test from the "checkEntries()" method will verify the iframe case: 1.25 + the iframe must be added as an entry to this window's performance object, 1.26 + while the image from the iframe should not be added here. 1.27 + 1.28 + Next tests will check the Performance API extensions introduced by the 1.29 + resource timing: window.performance.setResourceTimingBufferSize(1) and 1.30 + window.performance.clearResourceTimings(); 1.31 + 1.32 + The last tests will verify that the xhr resources are also added as entries 1.33 + to our performance object. 1.34 + 1.35 + Meanwhile, the iframe from the page will get loaded 1.36 + (resource_timing_iframe.html). 1.37 + The iframe contains a second script that will do some tests, as well, plus 1.38 + an image - its own resource. 1.39 + The script from the iframe will check that the iframe itself was not added 1.40 + as an entry (to itself). Also, it will check that its image was added as 1.41 + entry to the iframe's performance object. 1.42 + The last check is a double check: check that no subdocuments were added as 1.43 + entries for this iframe's performance object. 1.44 + The parent's (this window) "ok_wrapper()" method will be called once the tests 1.45 + are completed. 1.46 +--> 1.47 + 1.48 +<!DOCTYPE html> 1.49 +<html> 1.50 +<head> 1.51 + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> 1.52 + <script type="application/javascript"> 1.53 + 1.54 +var mainWindowTestsDone = false; 1.55 +var iframeTestsDone = false; 1.56 + 1.57 +function ok(cond, message) { 1.58 + window.opener.ok(cond, message) 1.59 +} 1.60 + 1.61 +function is(received, expected, message) { 1.62 + window.opener.is(received, expected, message); 1.63 +} 1.64 + 1.65 +function isnot(received, notExpected, message) { 1.66 + window.opener.isnot(received, notExpected, message); 1.67 +} 1.68 + 1.69 +window.onload = function() { 1.70 + ok(!!window.performance, "Performance object should exist"); 1.71 + ok(!!window.performance.getEntries, "Performance.getEntries() should exist"); 1.72 + ok(!!window.performance.getEntriesByName, "Performance.getEntriesByName() should exist"); 1.73 + ok(!!window.performance.getEntriesByType, "Performance.getEntriesByType() should exist"); 1.74 + 1.75 + // Here, we should have 6 entries (1 css, 3 png, 1 html) since the image was loaded. 1.76 + is(window.performance.getEntries().length, 5, "Performance.getEntries() returned wrong number of entries."); 1.77 + 1.78 + ok(!!window.performance.getEntriesByType("resource").length, 1.79 + "Performance.getEntriesByType() should return some results"); 1.80 + ok(!!window.performance.getEntriesByName("http://example.com/tests/image/test/mochitest/blue.png").length, 1.81 + "Performance.getEntriesByName() should return some results"); 1.82 + 1.83 + // Checks that two calls for "getEntries()" return a different array with the same 1.84 + // entries. 1.85 + ok(window.performance.getEntries() !== window.performance.getEntries(), 1.86 + "getEntries() should return a different array object every time."); 1.87 + ok(function (array1, array2) { 1.88 + if (array1.length != array2.length) { 1.89 + return false; 1.90 + } 1.91 + for (var i = 0 ; i < array1.length ; i++) { 1.92 + if (array1[i] !== array2[i]) { 1.93 + return false; 1.94 + } 1.95 + } 1.96 + return true; 1.97 + }(window.performance.getEntries(), window.performance.getEntries()), 1.98 + "The arrays should have the same entries."); 1.99 + 1.100 + checkEntries(window.performance.getEntries()); 1.101 + 1.102 + window.performance.setResourceTimingBufferSize(1); 1.103 + is(window.performance.getEntries().length, 5, "No entries should be " + 1.104 + "removed when setResourceTimingBufferSize is called."); 1.105 + 1.106 + window.performance.setResourceTimingBufferSize(4); 1.107 + is(window.performance.getEntries().length, 5, "No entries should be " + 1.108 + "removed when setResourceTimingBufferSize is called."); 1.109 + 1.110 + window.performance.setResourceTimingBufferSize(1); 1.111 + window.performance.clearResourceTimings(); 1.112 + is(window.performance.getEntries().length, 0, "All the entries should " + 1.113 + "be removed when when clearResourceTimings is being called."); 1.114 + 1.115 + makeXhr("test-data.json", firstCheck); 1.116 +} 1.117 + 1.118 +function checkEntries(anEntryList) { 1.119 + // Check that all the entries have all the properties. 1.120 + for (var i = 0 ; i < anEntryList.length ; i++) { 1.121 + var entry = anEntryList[i]; 1.122 + 1.123 + ok(!!entry, "PerformanceEntry should not be null"); 1.124 + ok(!!entry.name, "PerformanceEntry.name should be valid."); 1.125 + ok(entry.startTime > 0, "PerformanceEntry.startTime should be grater than 0"); 1.126 + 1.127 + // The entries list should be in chronological order with respect to startTime 1.128 + if (i > 0) { 1.129 + ok(anEntryList[i - 1].startTime <= anEntryList[i].startTime, 1.130 + "Entries list should be in chronological order with respect to startTime."); 1.131 + } 1.132 + 1.133 + // Check that each entry has all the properties and that the timings were 1.134 + // returned in the expected order. 1.135 + if ("initiatorType" in entry) { 1.136 + ok("redirectStart" in entry, "PerformanceEntry.redirectStart should be part of PerformanceEntry"); 1.137 + ok("redirectEnd" in entry, "PerformanceEntry.redirectEnd should be part of PerformanceEntry"); 1.138 + ok("fetchStart" in entry, "PerformanceEntry.fetchStart should be part of PerformanceEntry"); 1.139 + ok("domainLookupStart" in entry, "PerformanceEntry.domainLookupStart should be part of PerformanceEntry"); 1.140 + ok("domainLookupEnd" in entry, "PerformanceEntry.domainLookupEnd should be part of PerformanceEntry"); 1.141 + ok("connectStart" in entry, "PerformanceEntry.connectStart should be part of PerformanceEntry"); 1.142 + ok("connectEnd" in entry, "PerformanceEntry.connectEnd should be part of PerformanceEntry"); 1.143 + ok("secureConnectionStart" in entry, "PerformanceEntry.secureConnectionStart should be part of PerformanceEntry"); 1.144 + ok("requestStart" in entry, "PerformanceEntry.requestStart should be part of PerformanceEntry"); 1.145 + ok("responseStart" in entry, "PerformanceEntry.responseStart should be part of PerformanceEntry"); 1.146 + ok("responseEnd" in entry, "PerformanceEntry.responseEnd should be part of PerformanceEntry"); 1.147 + 1.148 + // Check that timings are in proper order 1.149 + sequence = ['startTime', 'redirectStart', 'redirectEnd', 'fetchStart', 1.150 + 'domainLookupStart', 'domainLookupEnd', 'connectStart', 1.151 + 'connectEnd', 'requestStart', 'responseStart', 'responseEnd']; 1.152 + for (var j = 1; j < sequence.length; ++j) { 1.153 + var prop = sequence[j]; 1.154 + var prevProp = sequence[j-1]; 1.155 + ok(entry[prevProp] <= entry[prop], 1.156 + ['Expected ', prevProp, ' to happen before ', prop, 1.157 + ', got ', prevProp, ' = ', entry[prevProp], 1.158 + ', ', prop, ' = ', entry[prop]].join('')); 1.159 + } 1.160 + } 1.161 + } 1.162 + 1.163 + // Check that the entries have the expected initiator type. We can't check 1.164 + // the order (the order might depend on the platform the tests are running). 1.165 + allResources = { 1.166 + "http://mochi.test:8888/tests/SimpleTest/test.css" : "link", 1.167 + "http://example.com/tests/image/test/mochitest/blue.png" : "img", 1.168 + "http://example.com/tests/image/test/mochitest/red.png" : "object", 1.169 + "http://example.com/tests/image/test/mochitest/big.png" : "embed", 1.170 + "http://mochi.test:8888/tests/dom/tests/mochitest/general/resource_timing_iframe.html" : "subdocument"}; 1.171 + 1.172 + for (resourceName in allResources) { 1.173 + // Check that we have a resource with the specific name. 1.174 + namedEntries = window.performance.getEntriesByName(resourceName); 1.175 + ok (!!namedEntries && (namedEntries.length == 1), 1.176 + "An entry with the name '" + resourceName + "' should be available"); 1.177 + 1.178 + // Double check for the entry name. 1.179 + is (namedEntries[0].name, resourceName, "The resource name is invalid"); 1.180 + 1.181 + // Check the initiator type. 1.182 + is (namedEntries[0].initiatorType, allResources[resourceName], 1.183 + "The initiator type for " + resourceName + " is invalid"); 1.184 + } 1.185 + 1.186 + // Check that the iframe's image was NOT added as an entry to this window's performance entry. 1.187 + ok(!window.performance.getEntriesByName("http://example.com/tests/image/test/mochitest/damon.jpg").length, 1.188 + "http://example.com/tests/image/test/mochitest/damon.jpg should be a valid entry name"); 1.189 +} 1.190 + 1.191 +function firstCheck() { 1.192 + is(window.performance.getEntries().length, 1, "The first xhr entry was not added."); 1.193 + is(window.performance.getEntries()[0].initiatorType, "xmlhttprequest", 1.194 + "The initiatorType is incorect for this entry"); 1.195 + makeXhr("test-data2.json", secondCheck); 1.196 +} 1.197 + 1.198 +function secondCheck() { 1.199 + // Since the buffer max size was set to '1', 'peformance.getEntries()' should 1.200 + // return only '1' entry (first xhr results). 1.201 + is(window.performance.getEntries().length, 1, "The second xhr entry should not be " + 1.202 + "returned since the buffer size was set to 1."); 1.203 + isnot(window.performance.getEntries()[0].name, "http://mochi.test:8888/tests/dom/tests/mochitest/general/test-data2.json", 1.204 + "We returned the second xhr instead of the first one"); 1.205 + finishTest(); 1.206 +} 1.207 + 1.208 +function finishTest() { 1.209 + // Check if all the tests are completed. 1.210 + if (iframeTestsDone) { 1.211 + window.opener.finishTests(); 1.212 + } else { 1.213 + mainWindowTestsDone = true; 1.214 + } 1.215 +} 1.216 + 1.217 +function makeXhr(aUrl, aCallback) { 1.218 + var xmlhttp = new XMLHttpRequest(); 1.219 + xmlhttp.onload = aCallback; 1.220 + xmlhttp.open("get", aUrl, true); 1.221 + xmlhttp.send(); 1.222 +} 1.223 + 1.224 +function checkArraysHaveSameElementsInSameOrder(array1, array2) { 1.225 + if (array1.length != array2.length) { 1.226 + return false; 1.227 + } 1.228 + for (var i = 0 ; i < array1.length ; i++) { 1.229 + if (array1[i] !== array2[i]) { 1.230 + return false; 1.231 + } 1.232 + } 1.233 + return true; 1.234 +} 1.235 + 1.236 +function iframeTestsCompleted() { 1.237 + if (mainWindowTestsDone) { 1.238 + window.opener.finishTests(); 1.239 + } 1.240 + else { 1.241 + iframeTestsDone = true; 1.242 + } 1.243 +} 1.244 + 1.245 +</script> 1.246 +</head> 1.247 +<body> 1.248 + <a target="_blank" 1.249 + href="https://bugzilla.mozilla.org/show_bug.cgi?id=822480" 1.250 + title="Add resource timing API."> 1.251 + Bug #822480 - Add in the Resource Timing API 1.252 + </a> 1.253 + <p id="display"></p> 1.254 + <div id="content"> 1.255 + <img src="http://example.com/tests/image/test/mochitest/blue.png"> 1.256 + <object data="http://example.com/tests/image/test/mochitest/red.png" type="image/png"/> 1.257 + <embed src="http://example.com/tests/image/test/mochitest/big.png" type="image/png"/> 1.258 + <iframe sandbox="allow-same-origin allow-scripts" id="if_2" src="resource_timing_iframe.html" height="10" width="10"></iframe> 1.259 + </div> 1.260 +</body> 1.261 +</html>