OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Processes API test for Chrome. | 5 // Processes API test for Chrome. |
6 // browser_tests.exe --gtest_filter=ExtensionApiTest.Processes | 6 // browser_tests.exe --gtest_filter=ExtensionApiTest.Processes |
7 | 7 |
8 var pass = chrome.test.callbackPass; | 8 var pass = chrome.test.callbackPass; |
9 var fail = chrome.test.callbackFail; | 9 var fail = chrome.test.callbackFail; |
10 var assertEq = chrome.test.assertEq; | 10 var assertEq = chrome.test.assertEq; |
11 var assertTrue = chrome.test.assertTrue; | 11 var assertTrue = chrome.test.assertTrue; |
12 var assertFalse = chrome.test.assertFalse; | 12 var assertFalse = chrome.test.assertFalse; |
13 var listenOnce = chrome.test.listenOnce; | 13 var listenOnce = chrome.test.listenOnce; |
14 | 14 |
15 var tabs = []; | 15 var tabs = []; |
16 var hangingTabProcess = -1; | 16 var hangingTabProcess = -1; |
17 | 17 |
18 function createTab(index, url) { | 18 function createTab(index, url) { |
19 chrome.tabs.create({"url": url}, pass(function(tab) { | 19 chrome.tabs.create({"url": url}, pass(function(tab) { |
20 tabs[index] = tab; | 20 tabs[index] = tab; |
21 })); | 21 })); |
22 } | 22 } |
23 | 23 |
24 var getProcessId = chrome.experimental.processes.getProcessIdForTab; | 24 var getProcessId = chrome.processes.getProcessIdForTab; |
25 | 25 |
26 function pageUrl(letter) { | 26 function pageUrl(letter) { |
27 return chrome.extension.getURL(letter + ".html"); | 27 return chrome.extension.getURL(letter + ".html"); |
28 } | 28 } |
29 | 29 |
30 function dumpProcess(process) { | 30 function dumpProcess(process) { |
31 console.log("id " + process.id); | 31 console.log("id " + process.id); |
32 console.log("osProcId " + process.osProcessId); | 32 console.log("osProcId " + process.osProcessId); |
33 console.log("type " + process.type); | 33 console.log("type " + process.type); |
34 console.log("profile " + process.profile); | 34 console.log("profile " + process.profile); |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 assertEq(pid1, pid2); | 131 assertEq(pid1, pid2); |
132 })); | 132 })); |
133 })); | 133 })); |
134 }, | 134 }, |
135 | 135 |
136 function extensionPagesMatchTabs() { | 136 function extensionPagesMatchTabs() { |
137 getProcessId(tabs[1].id, pass(function(pid1) { | 137 getProcessId(tabs[1].id, pass(function(pid1) { |
138 getProcessId(tabs[2].id, pass(function(pid2) { | 138 getProcessId(tabs[2].id, pass(function(pid2) { |
139 // Pages from same extension should share a process | 139 // Pages from same extension should share a process |
140 assertEq(pid1, pid2); | 140 assertEq(pid1, pid2); |
141 chrome.experimental.processes.getProcessInfo(pid1, false, | 141 chrome.processes.getProcessInfo(pid1, false, |
142 function(pl1) { | 142 function(pl1) { |
143 chrome.experimental.processes.getProcessInfo(pid2, false, | 143 chrome.processes.getProcessInfo(pid2, false, |
144 function (pl2) { | 144 function (pl2) { |
145 var proc1 = pl1[pid1]; | 145 var proc1 = pl1[pid1]; |
146 var proc2 = pl2[pid2]; | 146 var proc2 = pl2[pid2]; |
147 assertTrue(proc1.tabs.length == proc2.tabs.length); | 147 assertTrue(proc1.tabs.length == proc2.tabs.length); |
148 for (var i = 0; i < proc1.tabs.length; ++i) { | 148 for (var i = 0; i < proc1.tabs.length; ++i) { |
149 assertEq(proc1.tabs[i], proc2.tabs[i]); | 149 assertEq(proc1.tabs[i], proc2.tabs[i]); |
150 } | 150 } |
151 }); | 151 }); |
152 }); | 152 }); |
153 })); | 153 })); |
(...skipping 12 matching lines...) Expand all Loading... |
166 function newTabPagesShareProcess() { | 166 function newTabPagesShareProcess() { |
167 getProcessId(tabs[3].id, pass(function(pid3) { | 167 getProcessId(tabs[3].id, pass(function(pid3) { |
168 getProcessId(tabs[4].id, pass(function(pid4) { | 168 getProcessId(tabs[4].id, pass(function(pid4) { |
169 // Multiple NTPs should share a process | 169 // Multiple NTPs should share a process |
170 assertEq(pid3, pid4); | 170 assertEq(pid3, pid4); |
171 })); | 171 })); |
172 })); | 172 })); |
173 }, | 173 }, |
174 | 174 |
175 function idsInUpdateEvent() { | 175 function idsInUpdateEvent() { |
176 listenOnce(chrome.experimental.processes.onUpdated, function(processes) { | 176 listenOnce(chrome.processes.onUpdated, function(processes) { |
177 // onUpdated should return a valid dictionary of processes, | 177 // onUpdated should return a valid dictionary of processes, |
178 // indexed by process ID. | 178 // indexed by process ID. |
179 var pids = Object.keys(processes); | 179 var pids = Object.keys(processes); |
180 // There should be at least 5 processes: 1 browser, 1 extension, and 3 | 180 // There should be at least 5 processes: 1 browser, 1 extension, and 3 |
181 // renderers (for the 5 tabs). | 181 // renderers (for the 5 tabs). |
182 assertTrue(pids.length >= 5, "Unexpected size of pids"); | 182 assertTrue(pids.length >= 5, "Unexpected size of pids"); |
183 | 183 |
184 // Should be able to look up process object by ID. | 184 // Should be able to look up process object by ID. |
185 assertTrue(processes[pids[0]].id == pids[0]); | 185 assertTrue(processes[pids[0]].id == pids[0]); |
186 assertTrue(processes[pids[0]].id != processes[pids[1]].id); | 186 assertTrue(processes[pids[0]].id != processes[pids[1]].id); |
187 | 187 |
188 getProcessId(tabs[0].id, pass(function(pidTab0) { | 188 getProcessId(tabs[0].id, pass(function(pidTab0) { |
189 // Process ID for tab 0 should be listed in pids. | 189 // Process ID for tab 0 should be listed in pids. |
190 assertTrue(processes[pidTab0] != undefined, "Undefined Process"); | 190 assertTrue(processes[pidTab0] != undefined, "Undefined Process"); |
191 assertEq("renderer", processes[pidTab0].type, "Tab0 is not renderer"); | 191 assertEq("renderer", processes[pidTab0].type, "Tab0 is not renderer"); |
192 })); | 192 })); |
193 }); | 193 }); |
194 }, | 194 }, |
195 | 195 |
196 function typesInUpdateEvent() { | 196 function typesInUpdateEvent() { |
197 listenOnce(chrome.experimental.processes.onUpdated, function(processes) { | 197 listenOnce(chrome.processes.onUpdated, function(processes) { |
198 // Check types: 1 browser, 3 renderers, and 1 extension | 198 // Check types: 1 browser, 3 renderers, and 1 extension |
199 var browserCount = 0; | 199 var browserCount = 0; |
200 var rendererCount = 0; | 200 var rendererCount = 0; |
201 var extensionCount = 0; | 201 var extensionCount = 0; |
202 var otherCount = 0; | 202 var otherCount = 0; |
203 for (pid in processes) { | 203 for (pid in processes) { |
204 switch (processes[pid].type) { | 204 switch (processes[pid].type) { |
205 case "browser": | 205 case "browser": |
206 browserCount++; | 206 browserCount++; |
207 break; | 207 break; |
208 case "renderer": | 208 case "renderer": |
209 rendererCount++; | 209 rendererCount++; |
210 break; | 210 break; |
211 case "extension": | 211 case "extension": |
212 extensionCount++; | 212 extensionCount++; |
213 break; | 213 break; |
214 default: | 214 default: |
215 otherCount++; | 215 otherCount++; |
216 } | 216 } |
217 } | 217 } |
218 assertEq(1, browserCount); | 218 assertEq(1, browserCount); |
219 assertTrue(rendererCount >= 3); | 219 assertTrue(rendererCount >= 3); |
220 assertTrue(extensionCount >= 1); | 220 assertTrue(extensionCount >= 1); |
221 }); | 221 }); |
222 }, | 222 }, |
223 | 223 |
224 function propertiesOfProcesses() { | 224 function propertiesOfProcesses() { |
225 listenOnce(chrome.experimental.processes.onUpdated, function(processes) { | 225 listenOnce(chrome.processes.onUpdated, function(processes) { |
226 for (pid in processes) { | 226 for (pid in processes) { |
227 var process = processes[pid]; | 227 var process = processes[pid]; |
228 validateProcessProperties(process, true, false); | 228 validateProcessProperties(process, true, false); |
229 } | 229 } |
230 }); | 230 }); |
231 }, | 231 }, |
232 | 232 |
233 function propertiesOfProcessesWithMemory() { | 233 function propertiesOfProcessesWithMemory() { |
234 listenOnce(chrome.experimental.processes.onUpdatedWithMemory, | 234 listenOnce(chrome.processes.onUpdatedWithMemory, |
235 function(processes) { | 235 function(processes) { |
236 for (pid in processes) { | 236 for (pid in processes) { |
237 var process = processes[pid]; | 237 var process = processes[pid]; |
238 validateProcessProperties(process, true, true); | 238 validateProcessProperties(process, true, true); |
239 } | 239 } |
240 }); | 240 }); |
241 }, | 241 }, |
242 | 242 |
243 function terminateProcess() { | 243 function terminateProcess() { |
244 listenOnce(chrome.experimental.processes.onExited, | 244 listenOnce(chrome.processes.onExited, |
245 function(processId, type, code) { | 245 function(processId, type, code) { |
246 assertTrue(processId > 0); | 246 assertTrue(processId > 0); |
247 }); | 247 }); |
248 getProcessId(tabs[4].id, function(pid0) { | 248 getProcessId(tabs[4].id, function(pid0) { |
249 chrome.experimental.processes.terminate(pid0, function(killed) { | 249 chrome.processes.terminate(pid0, function(killed) { |
250 chrome.test.assertTrue(killed); | 250 chrome.test.assertTrue(killed); |
251 }); | 251 }); |
252 }); | 252 }); |
253 }, | 253 }, |
254 | 254 |
255 function terminateProcessNonExisting() { | 255 function terminateProcessNonExisting() { |
256 chrome.experimental.processes.terminate(31337, | 256 chrome.processes.terminate(31337, fail("Process not found: 31337.")); |
257 fail("Process not found: 31337.")); | |
258 }, | 257 }, |
259 | 258 |
260 function testOnCreated() { | 259 function testOnCreated() { |
261 listenOnce(chrome.experimental.processes.onCreated, function(process) { | 260 listenOnce(chrome.processes.onCreated, function(process) { |
262 assertTrue("id" in process, "process doesn't have id property"); | 261 assertTrue("id" in process, "process doesn't have id property"); |
263 assertTrue(process.id > 0, "id is not positive " + process.id); | 262 assertTrue(process.id > 0, "id is not positive " + process.id); |
264 }); | 263 }); |
265 createTab(5, "chrome://newtab/"); | 264 createTab(5, "chrome://newtab/"); |
266 }, | 265 }, |
267 | 266 |
268 function testOnExited() { | 267 function testOnExited() { |
269 listenOnce(chrome.experimental.processes.onExited, | 268 listenOnce(chrome.processes.onExited, |
270 function(processId, type, code) { | 269 function(processId, type, code) { |
271 assertTrue(type >= 0 && type < 5); | 270 assertTrue(type >= 0 && type < 5); |
272 }); | 271 }); |
273 chrome.tabs.create({"url": "http://google.com/"}, pass(function(tab) { | 272 chrome.tabs.create({"url": "http://google.com/"}, pass(function(tab) { |
274 chrome.tabs.remove(tab.id); | 273 chrome.tabs.remove(tab.id); |
275 })); | 274 })); |
276 }, | 275 }, |
277 | 276 |
278 function testGetProcessInfoList() { | 277 function testGetProcessInfoList() { |
279 getProcessId(tabs[0].id, pass(function(pidTab0) { | 278 getProcessId(tabs[0].id, pass(function(pidTab0) { |
280 getProcessId(tabs[1].id, pass(function(pidTab1) { | 279 getProcessId(tabs[1].id, pass(function(pidTab1) { |
281 chrome.experimental.processes.getProcessInfo([pidTab0, pidTab1], false, | 280 chrome.processes.getProcessInfo([pidTab0, pidTab1], false, |
282 pass(function(processes) { | 281 pass(function(processes) { |
283 assertTrue(Object.keys(processes).length == 2); | 282 assertTrue(Object.keys(processes).length == 2); |
284 })); | 283 })); |
285 })); | 284 })); |
286 })); | 285 })); |
287 }, | 286 }, |
288 | 287 |
289 function testGetProcessInfoSingle() { | 288 function testGetProcessInfoSingle() { |
290 chrome.experimental.processes.getProcessInfo(0, false, | 289 chrome.processes.getProcessInfo(0, false, pass(function(processes) { |
291 pass(function(processes) { | |
292 assertTrue(Object.keys(processes).length == 1); | 290 assertTrue(Object.keys(processes).length == 1); |
293 })); | 291 })); |
294 }, | 292 }, |
295 | 293 |
296 function testGetProcessInfo() { | 294 function testGetProcessInfo() { |
297 chrome.experimental.processes.getProcessInfo([], false, | 295 chrome.processes.getProcessInfo([], false, pass(function(processes) { |
298 pass(function(processes) { | |
299 assertTrue(Object.keys(processes).length >= 1); | 296 assertTrue(Object.keys(processes).length >= 1); |
300 for (pid in processes) { | 297 for (pid in processes) { |
301 var process = processes[pid]; | 298 var process = processes[pid]; |
302 validateProcessProperties(process, false, false); | 299 validateProcessProperties(process, false, false); |
303 assertFalse("privateMemory" in process); | 300 assertFalse("privateMemory" in process); |
304 } | 301 } |
305 })); | 302 })); |
306 }, | 303 }, |
307 | 304 |
308 function testGetProcessInfoWithMemory() { | 305 function testGetProcessInfoWithMemory() { |
309 chrome.experimental.processes.getProcessInfo(0, true, | 306 chrome.processes.getProcessInfo(0, true, pass(function(processes) { |
310 pass(function(processes) { | |
311 for (pid in processes) { | 307 for (pid in processes) { |
312 var process = processes[pid]; | 308 var process = processes[pid]; |
313 validateProcessProperties(process, false, true); | 309 validateProcessProperties(process, false, true); |
314 assertTrue("privateMemory" in process); | 310 assertTrue("privateMemory" in process); |
315 } | 311 } |
316 })); | 312 })); |
317 }, | 313 }, |
318 | 314 |
319 function testOnUnresponsive() { | 315 function testOnUnresponsive() { |
320 listenOnce(chrome.experimental.processes.onUnresponsive, | 316 listenOnce(chrome.processes.onUnresponsive, function(process) { |
321 function(process) { | |
322 assertTrue(process.id == hangingTabProcess); | 317 assertTrue(process.id == hangingTabProcess); |
323 // actually kill the process, just to make sure it won't hang the test | 318 // actually kill the process, just to make sure it won't hang the test |
324 chrome.experimental.processes.terminate(process.id, function(killed) { | 319 chrome.processes.terminate(process.id, function(killed) { |
325 chrome.test.assertTrue(killed); | 320 chrome.test.assertTrue(killed); |
326 }); | 321 }); |
327 }); | 322 }); |
328 chrome.tabs.create({"url": "chrome://hang" }, function(tab) { | 323 chrome.tabs.create({"url": "chrome://hang" }, function(tab) { |
329 getProcessId(tab.id, function(pid0) { | 324 getProcessId(tab.id, function(pid0) { |
330 hangingTabProcess = pid0; | 325 hangingTabProcess = pid0; |
331 }); | 326 }); |
332 chrome.tabs.update(tab.id, { "url": "chrome://flags" }); | 327 chrome.tabs.update(tab.id, { "url": "chrome://flags" }); |
333 }); | 328 }); |
334 } | 329 } |
335 ]); | 330 ]); |
OLD | NEW |