blob: 83db391be091e1eb3cebca439af3b53ab6da4905 [file] [log] [blame]
Tim van der Lippe83f02be2020-01-23 11:11:401// Copyright 2017 The Chromium Authors. All rights reserved.
Blink Reformat4c46d092018-04-07 15:32:372// 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
10ApplicationTestRunner.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
19ApplicationTestRunner.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
30ApplicationTestRunner.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
40ApplicationTestRunner.swapFrameCache = function(frameId) {
41 const frame = TestRunner.resourceTreeModel.frameForId(frameId);
42 TestRunner.evaluateInPageAnonymously('swapFrameCache(unescape(\'' + escape(frame.name) + '\'))');
43};
44
45ApplicationTestRunner.dumpApplicationCache = function() {
46 ApplicationTestRunner.dumpApplicationCacheTree();
47 ApplicationTestRunner.dumpApplicationCacheModel();
48 TestRunner.addResult('');
49};
50
51ApplicationTestRunner.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
76ApplicationTestRunner.frameIdToString = function(frameId) {
Tim van der Lippe1d6e57a2019-09-30 11:55:3477 if (!ApplicationTestRunner.framesByFrameId) {
Blink Reformat4c46d092018-04-07 15:32:3778 ApplicationTestRunner.framesByFrameId = {};
Tim van der Lippe1d6e57a2019-09-30 11:55:3479 }
Blink Reformat4c46d092018-04-07 15:32:3780
81 let frame = TestRunner.resourceTreeModel.frameForId(frameId);
82
Tim van der Lippe1d6e57a2019-09-30 11:55:3483 if (!frame) {
Blink Reformat4c46d092018-04-07 15:32:3784 frame = ApplicationTestRunner.framesByFrameId[frameId];
Tim van der Lippe1d6e57a2019-09-30 11:55:3485 }
Blink Reformat4c46d092018-04-07 15:32:3786
87 ApplicationTestRunner.framesByFrameId[frameId] = frame;
88 return frame.name;
89};
90
91ApplicationTestRunner.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
102ApplicationTestRunner.dumpApplicationCacheModel = function() {
103 TestRunner.addResult('Dumping application cache model:');
104 const model = UI.panels.resources._sidebar._applicationCacheModel;
105 const frameIds = [];
106
Tim van der Lippe1d6e57a2019-09-30 11:55:34107 for (const frameId in model._manifestURLsByFrame) {
Blink Reformat4c46d092018-04-07 15:32:37108 frameIds.push(frameId);
Tim van der Lippe1d6e57a2019-09-30 11:55:34109 }
Blink Reformat4c46d092018-04-07 15:32:37110
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
132ApplicationTestRunner.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
146ApplicationTestRunner.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 Lippe1d6e57a2019-09-30 11:55:34159 if (!ApplicationTestRunner.applicationCacheStatusesRecords[frameId]) {
Blink Reformat4c46d092018-04-07 15:32:37160 ApplicationTestRunner.applicationCacheStatusesRecords[frameId] = [];
Tim van der Lippe1d6e57a2019-09-30 11:55:34161 }
Blink Reformat4c46d092018-04-07 15:32:37162
163 ApplicationTestRunner.applicationCacheStatusesRecords[frameId].push(record);
164
165 if (ApplicationTestRunner.awaitedFrameStatusEventsCount &&
166 ApplicationTestRunner.awaitedFrameStatusEventsCount[frameId]) {
167 ApplicationTestRunner.awaitedFrameStatusEventsCount[frameId].count--;
168
Tim van der Lippe1d6e57a2019-09-30 11:55:34169 if (!ApplicationTestRunner.awaitedFrameStatusEventsCount[frameId].count) {
Blink Reformat4c46d092018-04-07 15:32:37170 ApplicationTestRunner.awaitedFrameStatusEventsCount[frameId].callback();
Tim van der Lippe1d6e57a2019-09-30 11:55:34171 }
Blink Reformat4c46d092018-04-07 15:32:37172 }
173 }
174
175 TestRunner.addSniffer(Resources.ApplicationCacheModel.prototype, '_frameManifestUpdated', addRecord, true);
176};
177
178ApplicationTestRunner.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 Lippe1d6e57a2019-09-30 11:55:34187 if (!ApplicationTestRunner.awaitedFrameStatusEventsCount) {
Blink Reformat4c46d092018-04-07 15:32:37188 ApplicationTestRunner.awaitedFrameStatusEventsCount = {};
Tim van der Lippe1d6e57a2019-09-30 11:55:34189 }
Blink Reformat4c46d092018-04-07 15:32:37190
191 ApplicationTestRunner.awaitedFrameStatusEventsCount[frameId] = {count: eventsLeft, callback: callback};
192};
193
194TestRunner.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`);