Tim van der Lippe | 83f02be | 2020-01-23 11:11:40 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors. All rights reserved. |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | /** |
| 6 | * @fileoverview using private properties isn't a Closure violation in tests. |
| 7 | * @suppress {accessControls} |
| 8 | */ |
| 9 | |
| 10 | ApplicationTestRunner.createAndNavigateIFrame = function(url, callback) { |
| 11 | TestRunner.addSniffer(SDK.ResourceTreeModel.prototype, '_frameNavigated', frameNavigated); |
| 12 | TestRunner.evaluateInPageAnonymously('createAndNavigateIFrame(unescape(\'' + escape(url) + '\'))'); |
| 13 | |
| 14 | function frameNavigated(frame) { |
| 15 | callback(frame.id); |
| 16 | } |
| 17 | }; |
| 18 | |
| 19 | ApplicationTestRunner.navigateIFrame = function(frameId, url, callback) { |
| 20 | const frame = TestRunner.resourceTreeModel.frameForId(frameId); |
| 21 | TestRunner.evaluateInPageAnonymously( |
| 22 | 'navigateIFrame(unescape(\'' + escape(frame.name) + '\'), unescape(\'' + escape(url) + '\'))'); |
| 23 | TestRunner.addSniffer(SDK.ResourceTreeModel.prototype, '_frameNavigated', frameNavigated); |
| 24 | |
| 25 | function frameNavigated(frame) { |
| 26 | callback(frame.id); |
| 27 | } |
| 28 | }; |
| 29 | |
| 30 | ApplicationTestRunner.removeIFrame = function(frameId, callback) { |
| 31 | const frame = TestRunner.resourceTreeModel.frameForId(frameId); |
| 32 | TestRunner.evaluateInPageAnonymously('removeIFrame(unescape(\'' + escape(frame.name) + '\'))'); |
| 33 | TestRunner.addSniffer(SDK.ResourceTreeModel.prototype, '_frameDetached', frameDetached); |
| 34 | |
| 35 | function frameDetached(frame) { |
| 36 | callback(frame.id); |
| 37 | } |
| 38 | }; |
| 39 | |
| 40 | ApplicationTestRunner.swapFrameCache = function(frameId) { |
| 41 | const frame = TestRunner.resourceTreeModel.frameForId(frameId); |
| 42 | TestRunner.evaluateInPageAnonymously('swapFrameCache(unescape(\'' + escape(frame.name) + '\'))'); |
| 43 | }; |
| 44 | |
| 45 | ApplicationTestRunner.dumpApplicationCache = function() { |
| 46 | ApplicationTestRunner.dumpApplicationCacheTree(); |
| 47 | ApplicationTestRunner.dumpApplicationCacheModel(); |
| 48 | TestRunner.addResult(''); |
| 49 | }; |
| 50 | |
| 51 | ApplicationTestRunner.dumpApplicationCacheTree = function() { |
| 52 | TestRunner.addResult('Dumping application cache tree:'); |
| 53 | const applicationCacheTreeElement = UI.panels.resources._sidebar.applicationCacheListTreeElement; |
| 54 | |
| 55 | if (!applicationCacheTreeElement.childCount()) { |
| 56 | TestRunner.addResult(' (empty)'); |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | for (let i = 0; i < applicationCacheTreeElement.childCount(); ++i) { |
| 61 | const manifestTreeElement = applicationCacheTreeElement.childAt(i); |
| 62 | TestRunner.addResult(' Manifest URL: ' + manifestTreeElement.manifestURL); |
| 63 | |
| 64 | if (!manifestTreeElement.childCount()) { |
| 65 | TestRunner.addResult(' (no frames)'); |
| 66 | continue; |
| 67 | } |
| 68 | |
| 69 | for (let j = 0; j < manifestTreeElement.childCount(); ++j) { |
| 70 | const frameTreeElement = manifestTreeElement.childAt(j); |
| 71 | TestRunner.addResult(' Frame: ' + frameTreeElement.title); |
| 72 | } |
| 73 | } |
| 74 | }; |
| 75 | |
| 76 | ApplicationTestRunner.frameIdToString = function(frameId) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 77 | if (!ApplicationTestRunner.framesByFrameId) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 78 | ApplicationTestRunner.framesByFrameId = {}; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 79 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 80 | |
| 81 | let frame = TestRunner.resourceTreeModel.frameForId(frameId); |
| 82 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 83 | if (!frame) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 84 | frame = ApplicationTestRunner.framesByFrameId[frameId]; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 85 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 86 | |
| 87 | ApplicationTestRunner.framesByFrameId[frameId] = frame; |
| 88 | return frame.name; |
| 89 | }; |
| 90 | |
| 91 | ApplicationTestRunner.applicationCacheStatusToString = function(status) { |
| 92 | const statusInformation = {}; |
| 93 | statusInformation[applicationCache.UNCACHED] = 'UNCACHED'; |
| 94 | statusInformation[applicationCache.IDLE] = 'IDLE'; |
| 95 | statusInformation[applicationCache.CHECKING] = 'CHECKING'; |
| 96 | statusInformation[applicationCache.DOWNLOADING] = 'DOWNLOADING'; |
| 97 | statusInformation[applicationCache.UPDATEREADY] = 'UPDATEREADY'; |
| 98 | statusInformation[applicationCache.OBSOLETE] = 'OBSOLETE'; |
| 99 | return statusInformation[status] || statusInformation[applicationCache.UNCACHED]; |
| 100 | }; |
| 101 | |
| 102 | ApplicationTestRunner.dumpApplicationCacheModel = function() { |
| 103 | TestRunner.addResult('Dumping application cache model:'); |
| 104 | const model = UI.panels.resources._sidebar._applicationCacheModel; |
| 105 | const frameIds = []; |
| 106 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 107 | for (const frameId in model._manifestURLsByFrame) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 108 | frameIds.push(frameId); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 109 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 110 | |
| 111 | function compareFunc(a, b) { |
| 112 | return ApplicationTestRunner.frameIdToString(a).localeCompare(ApplicationTestRunner.frameIdToString(b)); |
| 113 | } |
| 114 | |
| 115 | frameIds.sort(compareFunc); |
| 116 | |
| 117 | if (!frameIds.length) { |
| 118 | TestRunner.addResult(' (empty)'); |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | for (let i = 0; i < frameIds.length; ++i) { |
| 123 | const frameId = frameIds[i]; |
| 124 | const manifestURL = model.frameManifestURL(frameId); |
| 125 | const status = model.frameManifestStatus(frameId); |
| 126 | TestRunner.addResult(' Frame: ' + ApplicationTestRunner.frameIdToString(frameId)); |
| 127 | TestRunner.addResult(' manifest url: ' + manifestURL); |
| 128 | TestRunner.addResult(' status: ' + ApplicationTestRunner.applicationCacheStatusToString(status)); |
| 129 | } |
| 130 | }; |
| 131 | |
| 132 | ApplicationTestRunner.waitForFrameManifestURLAndStatus = function(frameId, manifestURL, status, callback) { |
| 133 | const frameManifestStatus = UI.panels.resources._sidebar._applicationCacheModel.frameManifestStatus(frameId); |
| 134 | const frameManifestURL = UI.panels.resources._sidebar._applicationCacheModel.frameManifestURL(frameId); |
| 135 | |
| 136 | if (frameManifestStatus === status && frameManifestURL.indexOf(manifestURL) !== -1) { |
| 137 | callback(); |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | const handler = |
| 142 | ApplicationTestRunner.waitForFrameManifestURLAndStatus.bind(this, frameId, manifestURL, status, callback); |
| 143 | TestRunner.addSniffer(Resources.ApplicationCacheModel.prototype, '_frameManifestUpdated', handler); |
| 144 | }; |
| 145 | |
| 146 | ApplicationTestRunner.startApplicationCacheStatusesRecording = function() { |
| 147 | if (ApplicationTestRunner.applicationCacheStatusesRecords) { |
| 148 | ApplicationTestRunner.applicationCacheStatusesRecords = {}; |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | ApplicationTestRunner.applicationCacheStatusesRecords = {}; |
| 153 | |
| 154 | function addRecord(frameId, manifestURL, status) { |
| 155 | const record = {}; |
| 156 | record.manifestURL = manifestURL; |
| 157 | record.status = status; |
| 158 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 159 | if (!ApplicationTestRunner.applicationCacheStatusesRecords[frameId]) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 160 | ApplicationTestRunner.applicationCacheStatusesRecords[frameId] = []; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 161 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 162 | |
| 163 | ApplicationTestRunner.applicationCacheStatusesRecords[frameId].push(record); |
| 164 | |
| 165 | if (ApplicationTestRunner.awaitedFrameStatusEventsCount && |
| 166 | ApplicationTestRunner.awaitedFrameStatusEventsCount[frameId]) { |
| 167 | ApplicationTestRunner.awaitedFrameStatusEventsCount[frameId].count--; |
| 168 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 169 | if (!ApplicationTestRunner.awaitedFrameStatusEventsCount[frameId].count) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 170 | ApplicationTestRunner.awaitedFrameStatusEventsCount[frameId].callback(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 171 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
| 175 | TestRunner.addSniffer(Resources.ApplicationCacheModel.prototype, '_frameManifestUpdated', addRecord, true); |
| 176 | }; |
| 177 | |
| 178 | ApplicationTestRunner.ensureFrameStatusEventsReceived = function(frameId, count, callback) { |
| 179 | const records = ApplicationTestRunner.applicationCacheStatusesRecords[frameId] || []; |
| 180 | const eventsLeft = count - records.length; |
| 181 | |
| 182 | if (!eventsLeft) { |
| 183 | callback(); |
| 184 | return; |
| 185 | } |
| 186 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 187 | if (!ApplicationTestRunner.awaitedFrameStatusEventsCount) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 188 | ApplicationTestRunner.awaitedFrameStatusEventsCount = {}; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 189 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 190 | |
| 191 | ApplicationTestRunner.awaitedFrameStatusEventsCount[frameId] = {count: eventsLeft, callback: callback}; |
| 192 | }; |
| 193 | |
| 194 | TestRunner.deprecatedInitAsync(` |
| 195 | let framesCount = 0; |
| 196 | |
| 197 | function createAndNavigateIFrame(url) { |
| 198 | let iframe = document.createElement('iframe'); |
| 199 | iframe.src = url; |
| 200 | iframe.name = 'frame' + ++framesCount; |
| 201 | iframe.id = iframe.name; |
| 202 | document.body.appendChild(iframe); |
| 203 | } |
| 204 | |
| 205 | function removeIFrame(name) { |
| 206 | let iframe = document.querySelector('#' + name); |
| 207 | iframe.parentElement.removeChild(iframe); |
| 208 | } |
| 209 | |
| 210 | function navigateIFrame(name, url) { |
| 211 | let iframe = document.querySelector('#' + name); |
| 212 | iframe.src = url; |
| 213 | } |
| 214 | |
| 215 | function swapFrameCache(name) { |
| 216 | let iframe = document.querySelector('#' + name); |
| 217 | iframe.contentWindow.applicationCache.swapCache(); |
| 218 | } |
| 219 | `); |