OLD | NEW |
1 <script> | 1 <script> |
2 var expectedEventData; | 2 var expectedEventData; |
3 var capturedEventData; | 3 var capturedEventData; |
4 | 4 |
5 function expect(data) { | 5 function expect(data) { |
6 expectedEventData = data; | 6 expectedEventData = data; |
7 capturedEventData = []; | 7 capturedEventData = []; |
8 } | 8 } |
9 | 9 |
10 function checkExpectations() { | 10 function checkExpectations() { |
11 if (capturedEventData.length < expectedEventData.length) { | 11 if (capturedEventData.length < expectedEventData.length) { |
12 return; | 12 return; |
13 } | 13 } |
14 chrome.test.assertEq(JSON.stringify(expectedEventData), | 14 chrome.test.assertEq(JSON.stringify(expectedEventData), |
15 JSON.stringify(capturedEventData)); | 15 JSON.stringify(capturedEventData)); |
16 chrome.test.succeed(); | 16 chrome.test.succeed(); |
17 } | 17 } |
18 | 18 |
19 chrome.experimental.webNavigation.onBeforeNavigate.addListener( | 19 chrome.experimental.webNavigation.onCommitted.addListener(function(details) { |
20 function(details) { | 20 console.log('---onCommitted: ' + details.url); |
21 console.log('---onBeforeNavigate: ' + details.url); | |
22 // normalize details. | 21 // normalize details. |
23 details.timeStamp = 0; | 22 details.timeStamp = 0; |
24 if (details.frameId != 0) { | 23 if (details.frameId != 0) { |
25 details.frameId = 1; | 24 details.frameId = 1; |
26 } | 25 } |
27 capturedEventData.push(["onBeforeNavigate", details]); | 26 capturedEventData.push(details); |
28 checkExpectations(); | |
29 }); | |
30 | |
31 chrome.experimental.webNavigation.onCommitted.addListener(function(details) { | |
32 console.log('---onCommitted: ' + details.url); | |
33 // normalize details. | |
34 details.timeStamp = 0; | |
35 if (details.frameId != 0) { | |
36 details.frameId = 1; | |
37 } | |
38 capturedEventData.push(["onCommitted", details]); | |
39 checkExpectations(); | |
40 }); | |
41 | |
42 chrome.experimental.webNavigation.onErrorOccurred.addListener( | |
43 function(details) { | |
44 console.log('---onErrorOccurred: ' + details.url); | |
45 // normalize details. | |
46 details.timeStamp = 0; | |
47 if (details.frameId != 0) { | |
48 details.frameId = 1; | |
49 } | |
50 capturedEventData.push(["onErrorOccurred", details]); | |
51 checkExpectations(); | 27 checkExpectations(); |
52 }); | 28 }); |
53 | 29 |
54 var getURL = chrome.extension.getURL; | 30 var getURL = chrome.extension.getURL; |
55 chrome.tabs.getSelected(null, function(tab) { | 31 chrome.tabs.getSelected(null, function(tab) { |
56 var tabId = tab.id; | 32 var tabId = tab.id; |
57 | 33 |
58 chrome.test.runTests([ | 34 chrome.test.runTests([ |
59 /* Navigates to an URL */ | 35 /* Navigates to an URL */ |
60 function simpleLoad() { | 36 function simpleLoad() { |
61 expect([ | 37 expect([ |
62 [ "onBeforeNavigate", | 38 { frameId: 0, |
63 { frameId: 0, | 39 tabId: tabId, |
64 requestId: 0, | 40 timeStamp: 0, |
65 tabId: tabId, | 41 transitionQualifiers: "", |
66 timeStamp: 0, | 42 transitionType: "link", |
67 url: getURL('simpleLoad/a.html') }], | 43 url: getURL('simpleLoad/a.html') }]); |
68 [ "onCommitted", | 44 chrome.tabs.update(tabId, { url: getURL('simpleLoad/a.html') }); |
69 { frameId: 0, | 45 }, |
70 tabId: tabId, | |
71 timeStamp: 0, | |
72 transitionQualifiers: "", | |
73 transitionType: "link", | |
74 url: getURL('simpleLoad/a.html') }]]); | |
75 chrome.tabs.update(tabId, { url: getURL('simpleLoad/a.html') }); | |
76 }, | |
77 | 46 |
78 /* Navigates to a.html that redirects to b.html (using javascript) | 47 /* Navigates to a.html that redirects to b.html (using javascript) |
79 after a delay of 500ms, so the initial navigation is completed and | 48 after a delay of 500ms, so the initial navigation is completed and |
80 the redirection is marked as client_redirect */ | 49 the redirection is marked as client_redirect */ |
81 function clientRedirect() { | 50 function clientRedirect() { |
82 expect([ | 51 expect([ |
83 [ "onBeforeNavigate", | 52 { frameId: 0, |
84 { frameId: 0, | 53 tabId: tabId, |
85 requestId: 0, | 54 timeStamp: 0, |
86 tabId: tabId, | 55 transitionQualifiers: "", |
87 timeStamp: 0, | 56 transitionType: "link", |
88 url: getURL('clientRedirect/a.html') }], | 57 url: getURL('clientRedirect/a.html') }, |
89 [ "onCommitted", | 58 { frameId: 0, |
90 { frameId: 0, | 59 tabId: tabId, |
91 tabId: tabId, | 60 timeStamp: 0, |
92 timeStamp: 0, | 61 transitionQualifiers: "client_redirect", |
93 transitionQualifiers: "", | 62 transitionType: "link", |
94 transitionType: "link", | 63 url: getURL('clientRedirect/b.html') }]); |
95 url: getURL('clientRedirect/a.html') }], | 64 chrome.tabs.update(tabId, { url: getURL('clientRedirect/a.html') }); |
96 [ "onBeforeNavigate", | 65 }, |
97 { frameId: 0, | |
98 requestId: 0, | |
99 tabId: tabId, | |
100 timeStamp: 0, | |
101 url: getURL('clientRedirect/b.html') }], | |
102 [ "onCommitted", | |
103 { frameId: 0, | |
104 tabId: tabId, | |
105 timeStamp: 0, | |
106 transitionQualifiers: "client_redirect", | |
107 transitionType: "link", | |
108 url: getURL('clientRedirect/b.html') }]]); | |
109 chrome.tabs.update(tabId, { url: getURL('clientRedirect/a.html') }); | |
110 }, | |
111 | 66 |
112 /* First navigates to a.html which redirects to to b.html which uses | 67 /* First navigates to a.html which redirects to to b.html which uses |
113 history.back() to navigate back to a.html */ | 68 history.back() to navigate back to a.html */ |
114 function forwardBack() { | 69 function forwardBack() { |
115 expect([ | 70 expect([ |
116 [ "onBeforeNavigate", | 71 { frameId: 0, |
117 { frameId: 0, | 72 tabId: tabId, |
118 requestId: 0, | 73 timeStamp: 0, |
119 tabId: tabId, | 74 transitionQualifiers: "", |
120 timeStamp: 0, | 75 transitionType: "link", |
121 url: getURL('forwardBack/a.html') }], | 76 url: getURL('forwardBack/a.html') }, |
122 [ "onCommitted", | 77 { frameId: 0, |
123 { frameId: 0, | 78 tabId: tabId, |
124 tabId: tabId, | 79 timeStamp: 0, |
125 timeStamp: 0, | 80 transitionQualifiers: "client_redirect", |
126 transitionQualifiers: "", | 81 transitionType: "link", |
127 transitionType: "link", | 82 url: getURL('forwardBack/b.html') }, |
128 url: getURL('forwardBack/a.html') }], | 83 { frameId: 0, |
129 [ "onBeforeNavigate", | 84 tabId: tabId, |
130 { frameId: 0, | 85 timeStamp: 0, |
131 requestId: 0, | 86 transitionQualifiers: "forward_back", |
132 tabId: tabId, | 87 transitionType: "link", |
133 timeStamp: 0, | 88 url: getURL('forwardBack/a.html') }]); |
134 url: getURL('forwardBack/b.html') }], | 89 chrome.tabs.update(tabId, { url: getURL('forwardBack/a.html') }); |
135 [ "onCommitted", | 90 }, |
136 { frameId: 0, | |
137 tabId: tabId, | |
138 timeStamp: 0, | |
139 transitionQualifiers: "client_redirect", | |
140 transitionType: "link", | |
141 url: getURL('forwardBack/b.html') }], | |
142 [ "onBeforeNavigate", | |
143 { frameId: 0, | |
144 requestId: 0, | |
145 tabId: tabId, | |
146 timeStamp: 0, | |
147 url: getURL('forwardBack/a.html') }], | |
148 [ "onCommitted", | |
149 { frameId: 0, | |
150 tabId: tabId, | |
151 timeStamp: 0, | |
152 transitionQualifiers: "forward_back", | |
153 transitionType: "link", | |
154 url: getURL('forwardBack/a.html') }]]); | |
155 chrome.tabs.update(tabId, { url: getURL('forwardBack/a.html') }); | |
156 }, | |
157 | 91 |
158 /* Navigates to a.html which includes b.html as an iframe. b.html | 92 /* Navigates to a.html which includes b.html as an iframe. b.html |
159 redirects to c.html. */ | 93 redirects to c.html. Note that all navigation entries are for |
160 function iframe() { | 94 a.html. Also, b.html does not generate a navigation entry. */ |
161 expect([ | 95 function iframe() { |
162 [ "onBeforeNavigate", | 96 expect([ |
163 { frameId: 0, | 97 { frameId: 0, |
164 requestId: 0, | 98 tabId: tabId, |
165 tabId: tabId, | 99 timeStamp: 0, |
166 timeStamp: 0, | 100 transitionQualifiers: "", |
167 url: getURL('iframe/a.html') }], | 101 transitionType: "link", |
168 [ "onCommitted", | 102 url: getURL('iframe/a.html') }, |
169 { frameId: 0, | 103 { frameId: 1, |
170 tabId: tabId, | 104 tabId: tabId, |
171 timeStamp: 0, | 105 timeStamp: 0, |
172 transitionQualifiers: "", | 106 transitionQualifiers: "", |
173 transitionType: "link", | 107 transitionType: "link", |
174 url: getURL('iframe/a.html') }], | 108 url: getURL('iframe/a.html') }]); |
175 [ "onBeforeNavigate", | 109 chrome.tabs.update(tabId, { url: getURL('iframe/a.html') }); |
176 { frameId: 0, | 110 }, |
177 requestId: 0, | 111 ]); |
178 tabId: tabId, | |
179 timeStamp: 0, | |
180 url: getURL('iframe/b.html') }], | |
181 [ "onCommitted", | |
182 { frameId: 0, | |
183 tabId: tabId, | |
184 timeStamp: 0, | |
185 transitionQualifiers: "", | |
186 transitionType: "auto_subframe", | |
187 url: getURL('iframe/b.html') }], | |
188 [ "onBeforeNavigate", | |
189 { frameId: 0, | |
190 requestId: 0, | |
191 tabId: tabId, | |
192 timeStamp: 0, | |
193 url: getURL('iframe/c.html') }], | |
194 [ "onCommitted", | |
195 { frameId: 0, | |
196 tabId: tabId, | |
197 timeStamp: 0, | |
198 transitionQualifiers: "", | |
199 transitionType: "manual_subframe", | |
200 url: getURL('iframe/c.html') }]]); | |
201 chrome.tabs.update(tabId, { url: getURL('iframe/a.html') }); | |
202 }, | |
203 | |
204 /* Navigates to a non-existant page. */ | |
205 function nonExistant() { | |
206 expect([ | |
207 [ "onBeforeNavigate", | |
208 { frameId: 0, | |
209 requestId: 0, | |
210 tabId: tabId, | |
211 timeStamp: 0, | |
212 url: getURL('nonexistant.html') }], | |
213 [ "onErrorOccurred", | |
214 { error: "net::ERR_FILE_NOT_FOUND", | |
215 frameId: 0, | |
216 tabId: tabId, | |
217 timeStamp: 0, | |
218 url: getURL('nonexistant.html') }], | |
219 [ "onBeforeNavigate", | |
220 { frameId: 0, | |
221 requestId: 0, | |
222 tabId: tabId, | |
223 timeStamp: 0, | |
224 url: "chrome://chromewebdata/"}], | |
225 [ "onCommitted", | |
226 { frameId: 0, | |
227 tabId: tabId, | |
228 timeStamp: 0, | |
229 transitionQualifiers: "", | |
230 transitionType: "link", | |
231 url: getURL('nonexistant.html') }]]); | |
232 chrome.tabs.update(tabId, { url: getURL('nonexistant.html') }); | |
233 }, | |
234 ]); | |
235 }); | 112 }); |
236 </script> | 113 </script> |
OLD | NEW |