OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 listenOnce = chrome.test.listenOnce; | 13 var listenOnce = chrome.test.listenOnce; |
13 | 14 |
14 var tabs = []; | 15 var tabs = []; |
16 var hangingTabProcess = -1; | |
15 | 17 |
16 function createTab(index, url) { | 18 function createTab(index, url) { |
17 chrome.tabs.create({"url": url}, pass(function(tab) { | 19 chrome.tabs.create({"url": url}, pass(function(tab) { |
18 tabs[index] = tab; | 20 tabs[index] = tab; |
19 })); | 21 })); |
20 } | 22 } |
21 | 23 |
22 var getProcessId = chrome.experimental.processes.getProcessIdForTab; | 24 var getProcessId = chrome.experimental.processes.getProcessIdForTab; |
23 | 25 |
24 function pageUrl(letter) { | 26 function pageUrl(letter) { |
25 return chrome.extension.getURL(letter + ".html"); | 27 return chrome.extension.getURL(letter + ".html"); |
26 } | 28 } |
27 | 29 |
30 function dumpProcess(process) { | |
31 console.log("id " + process.id); | |
32 console.log("osProcId " + process.osProcessId); | |
33 console.log("type " + process.type); | |
34 console.log("profile " + process.profile); | |
35 console.log("tabs " + process.tabs); | |
36 console.log("cpu " + process.cpu); | |
37 console.log("privMem " + process.privateMemory); | |
38 console.log("network " + process.network); | |
39 console.log("jsMemAlloc " + process.jsMemoryAllocated); | |
40 console.log("jsMemUsed " + process.jsMemoryUsed); | |
41 console.log("sqliteMem " + process.sqliteMemory); | |
42 console.log("fps " + process.fps); | |
43 if ("imageCache" in process) { | |
44 console.log("imageCache.size " + process.imageCache.size); | |
45 console.log("imageCache.liveSize " + process.imageCache.liveSize); | |
46 } | |
47 if ("scriptCache" in process) { | |
48 console.log("scriptCache.size " + process.scriptCache.size); | |
49 console.log("scriptCache.liveSize " + process.scriptCache.liveSize); | |
50 } | |
51 if ("cssCache" in process) { | |
52 console.log("cssCache.size " + process.cssCache.size); | |
53 console.log("cssCache .liveSize " + process.cssCache.liveSize); | |
54 } | |
55 } | |
56 | |
57 function validateProcessPropertiesOnUpdate(process) { | |
58 assertTrue("id" in process); | |
59 assertTrue("osProcessId" in process); | |
60 assertTrue("type" in process); | |
61 assertTrue("profile" in process); | |
62 assertTrue("tabs" in process); | |
63 assertTrue("cpu" in process); | |
64 assertTrue("network" in process); | |
Charlie Reis
2012/04/27 22:01:56
What about things like sqliteMemory? We should be
nasko
2012/04/30 18:05:19
Done.
| |
65 assertFalse("privateMemory" in process); | |
66 } | |
67 | |
68 function validateProcessPropertiesOnUpdateWithMemory(process) { | |
69 assertTrue("id" in process); | |
70 assertTrue("osProcessId" in process); | |
71 assertTrue("type" in process); | |
72 assertTrue("profile" in process); | |
73 assertTrue("tabs" in process); | |
74 assertTrue("cpu" in process); | |
75 assertTrue("network" in process); | |
Charlie Reis
2012/04/27 22:01:56
Could just call validateProcessPropertiesOnUpdate,
nasko
2012/04/30 18:05:19
I've rewritten the whole process validation.
| |
76 assertTrue("privateMemory" in process); | |
77 } | |
78 | |
79 function validateGetProcessInfo(process) { | |
80 assertTrue("id" in process); | |
81 assertTrue("osProcessId" in process); | |
82 assertTrue("type" in process); | |
83 assertTrue("profile" in process); | |
84 assertTrue("tabs" in process) | |
85 assertFalse("cpu" in process) | |
86 assertFalse("network" in process) | |
87 } | |
88 | |
28 chrome.test.runTests([ | 89 chrome.test.runTests([ |
29 function setupProcessTests() { | 90 function setupProcessTests() { |
30 // Open 4 tabs for test, then wait and create a 5th | 91 // Open 4 tabs for test, then wait and create a 5th |
31 createTab(0, "about:blank"); | 92 createTab(0, "about:blank"); |
32 createTab(1, pageUrl("a")); | 93 createTab(1, pageUrl("a")); |
33 createTab(2, pageUrl("b")); | 94 createTab(2, pageUrl("b")); |
34 createTab(3, "chrome://newtab/"); | 95 createTab(3, "chrome://newtab/"); |
35 | 96 |
36 // Wait for all loads to complete. | 97 // Wait for all loads to complete. |
37 var completedCount = 0; | 98 var completedCount = 0; |
(...skipping 30 matching lines...) Expand all Loading... | |
68 | 129 |
69 function extensionPagesShareProcess() { | 130 function extensionPagesShareProcess() { |
70 getProcessId(tabs[1].id, pass(function(pid1) { | 131 getProcessId(tabs[1].id, pass(function(pid1) { |
71 getProcessId(tabs[2].id, pass(function(pid2) { | 132 getProcessId(tabs[2].id, pass(function(pid2) { |
72 // Pages from same extension should share a process | 133 // Pages from same extension should share a process |
73 assertEq(pid1, pid2); | 134 assertEq(pid1, pid2); |
74 })); | 135 })); |
75 })); | 136 })); |
76 }, | 137 }, |
77 | 138 |
139 function extensionPagesMatchTabs() { | |
140 getProcessId(tabs[1].id, pass(function(pid1) { | |
141 getProcessId(tabs[2].id, pass(function(pid2) { | |
142 // Pages from same extension should share a process | |
143 assertEq(pid1, pid2); | |
144 chrome.experimental.processes.getProcessInfo(pid1, false, | |
145 function(pl1) { | |
146 chrome.experimental.processes.getProcessInfo(pid2, false, | |
147 function (pl2) { | |
148 var proc1 = pl1[pid1]; | |
149 var proc2 = pl2[pid2]; | |
150 assertTrue(proc1.tabs.length == proc2.tabs.length); | |
151 for (var i = 0; i < proc1.tabs.length; ++i) { | |
152 assertEq(proc1.tabs[i], proc2.tabs[i]); | |
153 } | |
154 }); | |
155 }); | |
156 })); | |
157 })); | |
158 }, | |
159 | |
78 function newTabPageInOwnProcess() { | 160 function newTabPageInOwnProcess() { |
79 getProcessId(tabs[0].id, pass(function(pid0) { | 161 getProcessId(tabs[0].id, pass(function(pid0) { |
80 getProcessId(tabs[3].id, pass(function(pid3) { | 162 getProcessId(tabs[3].id, pass(function(pid3) { |
81 // NTP should not share a process with current tabs | 163 // NTP should not share a process with current tabs |
82 assertTrue(pid0 != pid3); | 164 assertTrue(pid0 != pid3); |
83 })); | 165 })); |
84 })); | 166 })); |
85 }, | 167 }, |
86 | 168 |
87 function newTabPagesShareProcess() { | 169 function newTabPagesShareProcess() { |
88 getProcessId(tabs[3].id, pass(function(pid3) { | 170 getProcessId(tabs[3].id, pass(function(pid3) { |
89 getProcessId(tabs[4].id, pass(function(pid4) { | 171 getProcessId(tabs[4].id, pass(function(pid4) { |
90 // Multiple NTPs should share a process | 172 // Multiple NTPs should share a process |
91 assertEq(pid3, pid4); | 173 assertEq(pid3, pid4); |
92 })); | 174 })); |
93 })); | 175 })); |
94 }, | 176 }, |
95 | 177 |
96 function idsInUpdateEvent() { | 178 function idsInUpdateEvent() { |
97 listenOnce(chrome.experimental.processes.onUpdated, function(processes) { | 179 listenOnce(chrome.experimental.processes.onUpdated, function(processes) { |
98 // onUpdated should return a valid dictionary of processes, | 180 // onUpdated should return a valid dictionary of processes, |
99 // indexed by process ID. | 181 // indexed by process ID. |
100 var pids = Object.keys(processes); | 182 var pids = Object.keys(processes); |
101 // There should be at least 5 processes: 1 browser, 1 extension, and 3 | 183 // There should be at least 5 processes: 1 browser, 1 extension, and 3 |
102 // renderers (for the 5 tabs). | 184 // renderers (for the 5 tabs). |
103 assertTrue(pids.length >= 5); | 185 assertTrue(pids.length >= 5, "Unexpected size of pids"); |
104 | 186 |
105 // Should be able to look up process object by ID. | 187 // Should be able to look up process object by ID. |
106 assertTrue(processes[pids[0]].id == pids[0]); | 188 assertTrue(processes[pids[0]].id == pids[0]); |
107 assertTrue(processes[pids[0]].id != processes[pids[1]].id); | 189 assertTrue(processes[pids[0]].id != processes[pids[1]].id); |
108 | 190 |
109 getProcessId(tabs[0].id, pass(function(pidTab0) { | 191 getProcessId(tabs[0].id, pass(function(pidTab0) { |
110 // Process ID for tab 0 should be listed in pids. | 192 // Process ID for tab 0 should be listed in pids. |
111 assertTrue(processes[pidTab0] != undefined); | 193 assertTrue(processes[pidTab0] != undefined, "Undefined Process"); |
112 assertEq("renderer", processes[pidTab0].type); | 194 assertEq("renderer", processes[pidTab0].type, "Tab0 is not renderer"); |
113 })); | 195 })); |
114 }); | 196 }); |
115 }, | 197 }, |
116 | 198 |
117 function typesInUpdateEvent() { | 199 function typesInUpdateEvent() { |
118 listenOnce(chrome.experimental.processes.onUpdated, function(processes) { | 200 listenOnce(chrome.experimental.processes.onUpdated, function(processes) { |
119 // Check types: 1 browser, 3 renderers, and 1 extension | 201 // Check types: 1 browser, 3 renderers, and 1 extension |
120 var browserCount = 0; | 202 var browserCount = 0; |
121 var rendererCount = 0; | 203 var rendererCount = 0; |
122 var extensionCount = 0; | 204 var extensionCount = 0; |
(...skipping 16 matching lines...) Expand all Loading... | |
139 assertEq(1, browserCount); | 221 assertEq(1, browserCount); |
140 assertTrue(rendererCount >= 3); | 222 assertTrue(rendererCount >= 3); |
141 assertTrue(extensionCount >= 1); | 223 assertTrue(extensionCount >= 1); |
142 }); | 224 }); |
143 }, | 225 }, |
144 | 226 |
145 function propertiesOfProcesses() { | 227 function propertiesOfProcesses() { |
146 listenOnce(chrome.experimental.processes.onUpdated, function(processes) { | 228 listenOnce(chrome.experimental.processes.onUpdated, function(processes) { |
147 for (pid in processes) { | 229 for (pid in processes) { |
148 var process = processes[pid]; | 230 var process = processes[pid]; |
149 assertTrue("id" in process); | 231 validateProcessPropertiesOnUpdate(process); |
150 assertTrue("type" in process); | |
151 assertTrue("cpu" in process); | |
152 assertTrue("network" in process); | |
153 assertTrue("sharedMemory" in process); | |
154 assertTrue("privateMemory" in process); | |
155 } | 232 } |
156 }); | 233 }); |
157 }, | 234 }, |
158 | 235 |
236 function propertiesOfProcessesWithMemory() { | |
237 listenOnce(chrome.experimental.processes.onUpdatedWithMemory, | |
238 function(processes) { | |
239 for (pid in processes) { | |
240 var process = processes[pid]; | |
241 validateProcessPropertiesOnUpdateWithMemory(process); | |
242 } | |
243 }); | |
244 }, | |
245 | |
246 function terminateProcess() { | |
247 listenOnce(chrome.experimental.processes.onExited, | |
248 function(processId, type, code) { | |
249 assertTrue(processId > 0); | |
250 }); | |
251 getProcessId(tabs[4].id, function(pid0) { | |
252 chrome.experimental.processes.terminate(pid0, function(killed) { | |
253 chrome.test.assertTrue(killed); | |
254 }); | |
255 }); | |
256 }, | |
257 | |
258 function terminateProcessNonExisting() { | |
259 chrome.experimental.processes.terminate(31337, | |
260 fail("Process not found: 31337.")); | |
261 }, | |
262 | |
263 function testOnCreated() { | |
264 listenOnce(chrome.experimental.processes.onCreated, function(id, process) { | |
265 assertTrue("id" in process, "process doesn't have id property"); | |
266 assertTrue(id > 0, "id is not positive " + id); | |
267 }); | |
268 createTab(5, "chrome://newtab/"); | |
269 }, | |
270 | |
271 function testOnExited() { | |
272 listenOnce(chrome.experimental.processes.onExited, | |
273 function(processId, type, code) { | |
274 assertTrue(type >= 0 && type < 5); | |
275 }); | |
276 chrome.tabs.create({"url": "http://google.com/"}, pass(function(tab) { | |
277 chrome.tabs.remove(tab.id); | |
278 })); | |
279 }, | |
280 | |
281 function testGetProcessInfoList() { | |
282 chrome.experimental.processes.getProcessInfo([0, 2], false, | |
283 pass(function(processes) { | |
284 assertTrue(Object.keys(processes).length == 2); | |
285 })); | |
286 }, | |
287 | |
288 function testGetProcessInfoSingle() { | |
289 chrome.experimental.processes.getProcessInfo(0, false, | |
290 pass(function(processes) { | |
291 assertTrue(Object.keys(processes).length == 1); | |
292 })); | |
293 }, | |
294 | |
295 function testGetProcessInfoAll() { | |
296 chrome.experimental.processes.getProcessInfo([], false, | |
297 pass(function(processes) { | |
298 assertTrue(Object.keys(processes).length >= 1); | |
299 })); | |
300 }, | |
301 | |
302 function testGetProcessInfo() { | |
303 chrome.experimental.processes.getProcessInfo([], false, | |
304 pass(function(processes) { | |
305 for (pid in processes) { | |
306 var process = processes[pid]; | |
307 validateGetProcessInfo(process); | |
308 assertFalse("privateMemory" in process); | |
309 } | |
310 })); | |
311 }, | |
312 | |
313 function testGetProcessInfoWithMemory() { | |
314 chrome.experimental.processes.getProcessInfo(0, true, | |
315 pass(function(processes) { | |
316 for (pid in processes) { | |
317 var process = processes[pid]; | |
318 validateGetProcessInfo(process); | |
319 assertTrue("privateMemory" in process); | |
320 } | |
321 })); | |
322 }, | |
323 | |
324 function testOnUnresponsive() { | |
325 listenOnce(chrome.experimental.processes.onUnresponsive, | |
326 function(process) { | |
327 assertTrue(process.id == hangingTabProcess); | |
328 // actually kill the process, just to make sure it won't hang the test | |
329 chrome.experimental.processes.terminate(process.id, function(killed) { | |
330 chrome.test.assertTrue(killed); | |
331 }); | |
332 }); | |
333 chrome.tabs.create({"url": "chrome://hang" }, function(tab) { | |
334 getProcessId(tab.id, function(pid0) { | |
335 hangingTabProcess = pid0; | |
336 }); | |
337 chrome.tabs.update(tab.id, { "url": "chrome://flags" }); | |
338 }); | |
339 } | |
159 ]); | 340 ]); |
OLD | NEW |