Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(461)

Side by Side Diff: chrome/test/data/extensions/api_test/webrequest/test_unload6.js

Issue 2003593002: Add tests for webRequest events and frame unload (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: battre's nits Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 const hostname1 = 'hostname-of-main-frame';
6 const hostname2 = 'origin-different-from-main-frame';
7 const hostname3 = 'origin-different-from-other-frames';
8
9 // Wait until |num| webRequest.onErrorOccurred sub_frame events have triggered
10 // and invoke |callback| with the results. We use this instead of the usual
11 // test framework because a frame may or may not have onBeforeRequest events,
12 // and at this point we are only interested in seeing whether the details in the
13 // onErrorOccurred event are valid.
14 function awaitOnErrorOccurred(num, callback) {
15 var callbackDone = chrome.test.callbackAdded();
16 var results = [];
17 chrome.webRequest.onErrorOccurred.addListener(function listener(details) {
18 delete details.requestId;
19 delete details.timeStamp;
20 details.frameId = normalizeFrameId(details.frameId);
21 details.parentFrameId = normalizeFrameId(details.parentFrameId);
22 results.push(details);
23
24 if (results.length === num) {
25 chrome.webRequest.onErrorOccurred.removeListener(listener);
26 callback(results);
27 callbackDone();
28 }
29 }, {
30 urls: ['<all_urls>'],
31 types: ['sub_frame'],
32 });
33 }
34
35 // Generate a deterministic frameId for a given frameId.
36 function normalizeFrameId(frameId) {
37 chrome.test.assertTrue(typeof frameId == 'number');
38 if (frameId === -1 || frameId === 0)
39 return frameId; // unknown or main frame.
40 if (!(frameId in normalizeFrameId.cached))
41 normalizeFrameId.cached[frameId] =
42 Object.keys(normalizeFrameId.cached).length + 1;
43 return normalizeFrameId.cached[frameId];
44 }
45 normalizeFrameId.cached = {};
46
47 runTests([
48 // Insert a frame from the same origin as the main frame, insert another frame
49 // from a different origin and immediately remove both.
50 function insertMultipleOriginFramesAndImmediatelyRemoveFrames() {
51 const mainUrl = getServerURL('empty.html', hostname1);
52 const frameUrl1 = getSlowURL(hostname1);
53 const frameUrl2 = getSlowURL(hostname2);
54
55 awaitOnErrorOccurred(2, function(results) {
56 // The order of the URLs doesn't matter.
57 var expectedUrls = [frameUrl1, frameUrl2].sort();
58 var actualUrls = results.map(r => r.url).sort();
59 chrome.test.assertEq(expectedUrls, actualUrls);
60 delete results[0].url;
61 delete results[1].url;
62
63 // The main purpose of this check is to see whether the frameId/tabId
64 // makes sense.
65 chrome.test.assertEq([{
66 method: 'GET',
67 frameId: 1,
68 parentFrameId: 0,
69 tabId,
70 type: 'sub_frame',
71 fromCache: false,
72 error: 'net::ERR_ABORTED',
73 }, {
74 method: 'GET',
75 frameId: 2,
76 parentFrameId: 0,
77 tabId,
78 type: 'sub_frame',
79 fromCache: false,
80 error: 'net::ERR_ABORTED',
81 }], results);
82 });
83
84 navigateAndWait(mainUrl, function() {
85 chrome.tabs.executeScript(tabId, {
86 code: `
87 var f1 = document.createElement('iframe');
88 f1.src = '${frameUrl1}';
89 var f2 = document.createElement('iframe');
90 f2.src = '${frameUrl2}';
91
92 document.body.appendChild(f1);
93 document.body.appendChild(f2);
94
95 f1.remove();
96 f2.remove();
97 `
98 });
99 });
100 },
101
102 // Insert a frame from yet another origin and immediately remove it.
103 // This test re-uses the tab from the previous test.
104 function insertCrossOriginFrameAndImmediatelyRemoveFrame() {
105 const frameUrl = getSlowURL(hostname3);
106
107 awaitOnErrorOccurred(1, function(results) {
108 chrome.test.assertEq([{
109 method: 'GET',
110 url: frameUrl,
111 frameId: 3,
112 parentFrameId: 0,
113 tabId,
114 type: 'sub_frame',
115 fromCache: false,
116 error: 'net::ERR_ABORTED',
117 }], results);
118 });
119
120 chrome.tabs.executeScript(tabId, {
121 code: `
122 var f = document.createElement('iframe');
123 f.src = '${frameUrl}';
124 document.body.appendChild(f);
125 f.remove();
126 `
127 });
128 },
129 ]);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698