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

Side by Side Diff: chrome/test/data/extensions/api_test/processes/api/test.js

Issue 10175008: Improving the process model extension API (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Syncing to the latest tree after a week away. Created 8 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 | Annotate | Revision Log
« no previous file with comments | « chrome/common/extensions/docs/samples.json ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 validateProcessProperties(process, updating, memory_included) {
58 // Always present.
59 assertTrue("id" in process);
60 assertTrue("osProcessId" in process);
61 assertTrue("type" in process);
62 assertTrue("profile" in process);
63 assertTrue("tabs" in process);
64
65 // Present if onUpdate(WithMemory) listener is registered.
66 assertEq(("cpu" in process), updating);
67 assertEq(("network" in process), updating);
68 assertEq(("fps" in process), updating);
69
70 // Present if memory details are requested.
71 assertEq(("privateMemory" in process), memory_included);
72
73 // sqliteMemory is only reported for the browser process
74 if (process.type == "browser") {
75 assertEq(("sqliteMemory" in process), updating);
76 } else {
77 // The rest are not present in the browser process
78 assertEq(("jsMemoryAllocated" in process), updating);
79 assertEq(("jsMemoryUsed" in process), updating);
80 assertEq(("imageCache" in process), updating);
81 assertEq(("scriptCache" in process), updating);
82 assertEq(("cssCache" in process), updating);
83 }
84 }
85
28 chrome.test.runTests([ 86 chrome.test.runTests([
29 function setupProcessTests() { 87 function setupProcessTests() {
30 // Open 4 tabs for test, then wait and create a 5th 88 // Open 4 tabs for test, then wait and create a 5th
31 createTab(0, "about:blank"); 89 createTab(0, "about:blank");
32 createTab(1, pageUrl("a")); 90 createTab(1, pageUrl("a"));
33 createTab(2, pageUrl("b")); 91 createTab(2, pageUrl("b"));
34 createTab(3, "chrome://newtab/"); 92 createTab(3, "chrome://newtab/");
35 93
36 // Wait for all loads to complete. 94 // Wait for all loads to complete.
37 var completedCount = 0; 95 var completedCount = 0;
(...skipping 30 matching lines...) Expand all
68 126
69 function extensionPagesShareProcess() { 127 function extensionPagesShareProcess() {
70 getProcessId(tabs[1].id, pass(function(pid1) { 128 getProcessId(tabs[1].id, pass(function(pid1) {
71 getProcessId(tabs[2].id, pass(function(pid2) { 129 getProcessId(tabs[2].id, pass(function(pid2) {
72 // Pages from same extension should share a process 130 // Pages from same extension should share a process
73 assertEq(pid1, pid2); 131 assertEq(pid1, pid2);
74 })); 132 }));
75 })); 133 }));
76 }, 134 },
77 135
136 function extensionPagesMatchTabs() {
137 getProcessId(tabs[1].id, pass(function(pid1) {
138 getProcessId(tabs[2].id, pass(function(pid2) {
139 // Pages from same extension should share a process
140 assertEq(pid1, pid2);
141 chrome.experimental.processes.getProcessInfo(pid1, false,
142 function(pl1) {
143 chrome.experimental.processes.getProcessInfo(pid2, false,
144 function (pl2) {
145 var proc1 = pl1[pid1];
146 var proc2 = pl2[pid2];
147 assertTrue(proc1.tabs.length == proc2.tabs.length);
148 for (var i = 0; i < proc1.tabs.length; ++i) {
149 assertEq(proc1.tabs[i], proc2.tabs[i]);
150 }
151 });
152 });
153 }));
154 }));
155 },
156
78 function newTabPageInOwnProcess() { 157 function newTabPageInOwnProcess() {
79 getProcessId(tabs[0].id, pass(function(pid0) { 158 getProcessId(tabs[0].id, pass(function(pid0) {
80 getProcessId(tabs[3].id, pass(function(pid3) { 159 getProcessId(tabs[3].id, pass(function(pid3) {
81 // NTP should not share a process with current tabs 160 // NTP should not share a process with current tabs
82 assertTrue(pid0 != pid3); 161 assertTrue(pid0 != pid3);
83 })); 162 }));
84 })); 163 }));
85 }, 164 },
86 165
87 function newTabPagesShareProcess() { 166 function newTabPagesShareProcess() {
88 getProcessId(tabs[3].id, pass(function(pid3) { 167 getProcessId(tabs[3].id, pass(function(pid3) {
89 getProcessId(tabs[4].id, pass(function(pid4) { 168 getProcessId(tabs[4].id, pass(function(pid4) {
90 // Multiple NTPs should share a process 169 // Multiple NTPs should share a process
91 assertEq(pid3, pid4); 170 assertEq(pid3, pid4);
92 })); 171 }));
93 })); 172 }));
94 }, 173 },
95 174
96 function idsInUpdateEvent() { 175 function idsInUpdateEvent() {
97 listenOnce(chrome.experimental.processes.onUpdated, function(processes) { 176 listenOnce(chrome.experimental.processes.onUpdated, function(processes) {
98 // onUpdated should return a valid dictionary of processes, 177 // onUpdated should return a valid dictionary of processes,
99 // indexed by process ID. 178 // indexed by process ID.
100 var pids = Object.keys(processes); 179 var pids = Object.keys(processes);
101 // 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
102 // renderers (for the 5 tabs). 181 // renderers (for the 5 tabs).
103 assertTrue(pids.length >= 5); 182 assertTrue(pids.length >= 5, "Unexpected size of pids");
104 183
105 // Should be able to look up process object by ID. 184 // Should be able to look up process object by ID.
106 assertTrue(processes[pids[0]].id == pids[0]); 185 assertTrue(processes[pids[0]].id == pids[0]);
107 assertTrue(processes[pids[0]].id != processes[pids[1]].id); 186 assertTrue(processes[pids[0]].id != processes[pids[1]].id);
108 187
109 getProcessId(tabs[0].id, pass(function(pidTab0) { 188 getProcessId(tabs[0].id, pass(function(pidTab0) {
110 // Process ID for tab 0 should be listed in pids. 189 // Process ID for tab 0 should be listed in pids.
111 assertTrue(processes[pidTab0] != undefined); 190 assertTrue(processes[pidTab0] != undefined, "Undefined Process");
112 assertEq("renderer", processes[pidTab0].type); 191 assertEq("renderer", processes[pidTab0].type, "Tab0 is not renderer");
113 })); 192 }));
114 }); 193 });
115 }, 194 },
116 195
117 function typesInUpdateEvent() { 196 function typesInUpdateEvent() {
118 listenOnce(chrome.experimental.processes.onUpdated, function(processes) { 197 listenOnce(chrome.experimental.processes.onUpdated, function(processes) {
119 // Check types: 1 browser, 3 renderers, and 1 extension 198 // Check types: 1 browser, 3 renderers, and 1 extension
120 var browserCount = 0; 199 var browserCount = 0;
121 var rendererCount = 0; 200 var rendererCount = 0;
122 var extensionCount = 0; 201 var extensionCount = 0;
(...skipping 16 matching lines...) Expand all
139 assertEq(1, browserCount); 218 assertEq(1, browserCount);
140 assertTrue(rendererCount >= 3); 219 assertTrue(rendererCount >= 3);
141 assertTrue(extensionCount >= 1); 220 assertTrue(extensionCount >= 1);
142 }); 221 });
143 }, 222 },
144 223
145 function propertiesOfProcesses() { 224 function propertiesOfProcesses() {
146 listenOnce(chrome.experimental.processes.onUpdated, function(processes) { 225 listenOnce(chrome.experimental.processes.onUpdated, function(processes) {
147 for (pid in processes) { 226 for (pid in processes) {
148 var process = processes[pid]; 227 var process = processes[pid];
149 assertTrue("id" in process); 228 validateProcessProperties(process, true, false);
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 } 229 }
156 }); 230 });
157 }, 231 },
158 232
233 function propertiesOfProcessesWithMemory() {
234 listenOnce(chrome.experimental.processes.onUpdatedWithMemory,
235 function(processes) {
236 for (pid in processes) {
237 var process = processes[pid];
238 validateProcessProperties(process, true, true);
239 }
240 });
241 },
242
243 function terminateProcess() {
244 listenOnce(chrome.experimental.processes.onExited,
245 function(processId, type, code) {
246 assertTrue(processId > 0);
247 });
248 getProcessId(tabs[4].id, function(pid0) {
249 chrome.experimental.processes.terminate(pid0, function(killed) {
250 chrome.test.assertTrue(killed);
251 });
252 });
253 },
254
255 function terminateProcessNonExisting() {
256 chrome.experimental.processes.terminate(31337,
257 fail("Process not found: 31337."));
258 },
259
260 function testOnCreated() {
261 listenOnce(chrome.experimental.processes.onCreated, function(process) {
262 assertTrue("id" in process, "process doesn't have id property");
263 assertTrue(process.id > 0, "id is not positive " + process.id);
264 });
265 createTab(5, "chrome://newtab/");
266 },
267
268 function testOnExited() {
269 listenOnce(chrome.experimental.processes.onExited,
270 function(processId, type, code) {
271 assertTrue(type >= 0 && type < 5);
272 });
273 chrome.tabs.create({"url": "http://google.com/"}, pass(function(tab) {
274 chrome.tabs.remove(tab.id);
275 }));
276 },
277
278 function testGetProcessInfoList() {
279 getProcessId(tabs[0].id, pass(function(pidTab0) {
280 getProcessId(tabs[1].id, pass(function(pidTab1) {
281 chrome.experimental.processes.getProcessInfo([pidTab0, pidTab1], false,
282 pass(function(processes) {
283 assertTrue(Object.keys(processes).length == 2);
284 }));
285 }));
286 }));
287 },
288
289 function testGetProcessInfoSingle() {
290 chrome.experimental.processes.getProcessInfo(0, false,
291 pass(function(processes) {
292 assertTrue(Object.keys(processes).length == 1);
293 }));
294 },
295
296 function testGetProcessInfo() {
297 chrome.experimental.processes.getProcessInfo([], false,
298 pass(function(processes) {
299 assertTrue(Object.keys(processes).length >= 1);
300 for (pid in processes) {
301 var process = processes[pid];
302 validateProcessProperties(process, false, false);
303 assertFalse("privateMemory" in process);
304 }
305 }));
306 },
307
308 function testGetProcessInfoWithMemory() {
309 chrome.experimental.processes.getProcessInfo(0, true,
310 pass(function(processes) {
311 for (pid in processes) {
312 var process = processes[pid];
313 validateProcessProperties(process, false, true);
314 assertTrue("privateMemory" in process);
315 }
316 }));
317 },
318
319 function testOnUnresponsive() {
320 listenOnce(chrome.experimental.processes.onUnresponsive,
321 function(process) {
322 assertTrue(process.id == hangingTabProcess);
323 // actually kill the process, just to make sure it won't hang the test
324 chrome.experimental.processes.terminate(process.id, function(killed) {
325 chrome.test.assertTrue(killed);
326 });
327 });
328 chrome.tabs.create({"url": "chrome://hang" }, function(tab) {
329 getProcessId(tab.id, function(pid0) {
330 hangingTabProcess = pid0;
331 });
332 chrome.tabs.update(tab.id, { "url": "chrome://flags" });
333 });
334 }
159 ]); 335 ]);
OLDNEW
« no previous file with comments | « chrome/common/extensions/docs/samples.json ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698