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

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

Issue 199091: Update ExtensionApiTest.Tabs and re-enable (Closed)
Patch Set: remove comment Created 11 years, 3 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
« no previous file with comments | « chrome/renderer/resources/extension_apitest.js ('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 // tabs api test 1 // tabs api test
2 // browser_tests.exe --gtest_filter=ExtensionApiTest.Tabs 2 // browser_tests.exe --gtest_filter=ExtensionApiTest.Tabs
3 3
4 // We have a bunch of places where we need to remember some state from one 4 // We have a bunch of places where we need to remember some state from one
5 // test (or setup code) to subsequent tests. 5 // test (or setup code) to subsequent tests.
6 var firstWindowId = null; 6 var firstWindowId = null;
7 var secondWindowId = null; 7 var secondWindowId = null;
8 var firstTabIndex = null; 8 var firstTabIndex = null;
9 var testTabId = null; 9 var testTabId = null;
10 10
11 var moveWindow1 = null; 11 var moveWindow1 = null;
12 var moveWindow2 = null; 12 var moveWindow2 = null;
13 var moveTabIds = {}; 13 var moveTabIds = {};
14 14
15 var testCallback = chrome.test.testCallback; 15 var pass = chrome.test.callbackPass;
16 var assertEq = chrome.test.assertEq; 16 var assertEq = chrome.test.assertEq;
17 var assertTrue = chrome.test.assertTrue; 17 var assertTrue = chrome.test.assertTrue;
18 18
19 chrome.test.runTests([ 19 chrome.test.runTests([
20 function getSelected() { 20 function getSelected() {
21 chrome.tabs.getSelected(null, testCallback(true, function(tab) { 21 chrome.tabs.getSelected(null, pass(function(tab) {
22 assertEq("about:blank", tab.url); 22 assertEq("about:blank", tab.url);
23 assertEq("about:blank", tab.title); 23 assertEq("about:blank", tab.title);
24 firstWindowId = tab.windowId; 24 firstWindowId = tab.windowId;
25 firstTabIndex = tab.index; 25 firstTabIndex = tab.index;
26 })); 26 }));
27 }, 27 },
28 28
29 function openNewTab() { 29 function openNewTab() {
30 // TODO(asargent) Add more tests for the following cases: 30 // TODO(asargent) Add more tests for the following cases:
31 // 1) two windows open. create tab in "other" window. 31 // 1) two windows open. create tab in "other" window.
32 // 2) don't pass windowId. confirm created in "this" window. 32 // 2) don't pass windowId. confirm created in "this" window.
33 // 3) pass index. confirm placed at correct index position. 33 // 3) pass index. confirm placed at correct index position.
34 // 4) pass selected. confirm is selected. 34 // 4) pass selected. confirm is selected.
35 chrome.tabs.create({"windowId" : firstWindowId, "selected" : false}, 35 chrome.tabs.create({"windowId" : firstWindowId, "selected" : false},
36 testCallback(true, function(tab){ 36 pass(function(tab){
37 assertTrue(tab.index > firstTabIndex); 37 assertTrue(tab.index > firstTabIndex);
38 assertEq(firstWindowId, tab.windowId); 38 assertEq(firstWindowId, tab.windowId);
39 assertEq(false, tab.selected); 39 assertEq(false, tab.selected);
40 assertEq("chrome://newtab/", tab.url); 40 assertEq("chrome://newtab/", tab.url);
41 })); 41 }));
42 }, 42 },
43 43
44 // Setup a new window for later tests, and open some tabs in the 44 // Setup a new window for later tests, and open some tabs in the
45 // first and second windows. 45 // first and second windows.
46 function createWindow() { 46 function createWindow() {
47 // TODO(asargent) Add more tests for: 47 // TODO(asargent) Add more tests for:
48 // 1) window sizing/positioning. 48 // 1) window sizing/positioning.
49 // 2) passed url (relative & absolute) 49 // 2) passed url (relative & absolute)
50 chrome.windows.create({}, testCallback(false, function(win) { 50 chrome.windows.create({}, pass(function(win) {
51 assertTrue(win.id > 0); 51 assertTrue(win.id > 0);
52 secondWindowId = win.id; 52 secondWindowId = win.id;
53 // Create first window.
53 chrome.tabs.create({"windowId" : firstWindowId, "url" : "chrome://a"}, 54 chrome.tabs.create({"windowId" : firstWindowId, "url" : "chrome://a"},
54 testCallback(false, null)); 55 pass(function() {
55 chrome.tabs.create({"windowId" : secondWindowId, "url" : "chrome://b"}, 56 // Create second window.
56 testCallback(true, null)); 57 chrome.tabs.create({"windowId" : secondWindowId, "url" : "chrome://b"},
58 pass());
59 }));
57 })); 60 }));
58 }, 61 },
59 62
60 function getAllFirstWindow() { 63 function getAllFirstWindow() {
61 // TODO(asargent) Add test for passing null for windowId - this should 64 // TODO(asargent) Add test for passing null for windowId - this should
62 // default to the "current" window. 65 // default to the "current" window.
63 chrome.tabs.getAllInWindow(firstWindowId, 66 chrome.tabs.getAllInWindow(firstWindowId,
64 testCallback(true, function(tabs) { 67 pass(function(tabs) {
65 assertEq(3, tabs.length); 68 assertEq(3, tabs.length);
66 for (var i = 0; i < tabs.length; i++) { 69 for (var i = 0; i < tabs.length; i++) {
67 assertEq(firstWindowId, tabs[i].windowId); 70 assertEq(firstWindowId, tabs[i].windowId);
68 assertEq(i, tabs[i].index); 71 assertEq(i, tabs[i].index);
69 72
70 // The most recent tab should be selected 73 // The most recent tab should be selected
71 assertEq((i == 2), tabs[i].selected); 74 assertEq((i == 2), tabs[i].selected);
72 } 75 }
73 assertEq("about:blank", tabs[0].url); 76 assertEq("about:blank", tabs[0].url);
74 assertEq("chrome://newtab/", tabs[1].url); 77 assertEq("chrome://newtab/", tabs[1].url);
75 assertEq("chrome://a/", tabs[2].url); 78 assertEq("chrome://a/", tabs[2].url);
76 testTabId = tabs[2].id; 79 testTabId = tabs[2].id;
77 })); 80 }));
78 }, 81 },
79 82
80 function getAllSecondWindow() { 83 function getAllSecondWindow() {
81 chrome.tabs.getAllInWindow(secondWindowId, 84 chrome.tabs.getAllInWindow(secondWindowId,
82 testCallback(true, function(tabs) { 85 pass(function(tabs) {
83 assertEq(2, tabs.length); 86 assertEq(2, tabs.length);
84 for (var i = 0; i < tabs.length; i++) { 87 for (var i = 0; i < tabs.length; i++) {
85 assertEq(secondWindowId, tabs[i].windowId); 88 assertEq(secondWindowId, tabs[i].windowId);
86 assertEq(i, tabs[i].index); 89 assertEq(i, tabs[i].index);
87 } 90 }
88 assertEq("chrome://newtab/", tabs[0].url); 91 assertEq("chrome://newtab/", tabs[0].url);
89 assertEq("chrome://b/", tabs[1].url); 92 assertEq("chrome://b/", tabs[1].url);
90 })); 93 }));
91 }, 94 },
92 95
93 function update() { 96 function updateUrl() {
94 chrome.tabs.update(testTabId, {"selected":true, "url": "chrome://c"}, 97 chrome.tabs.get(testTabId, pass(function(tab) {
95 testCallback(false, function(){ 98 assertEq("chrome://a/", tab.url);
96 chrome.tabs.getSelected(firstWindowId, testCallback(true, function(tab) { 99 // Update url.
97 assertEq(testTabId, tab.id); 100 chrome.tabs.update(testTabId, {"url": "chrome://c"},
98 assertEq("chrome://c/", tab.url); 101 pass(function(){
99 assertEq(true, tab.selected); 102 // Check url.
103 chrome.tabs.get(testTabId, pass(function(tab) {
104 assertEq("chrome://c/", tab.url);
105 }));
106 }));
107 }));
108 },
109
110 function updateSelect() {
111 chrome.tabs.getAllInWindow(firstWindowId, pass(function(tabs) {
112 assertEq(false, tabs[1].selected);
113 assertEq(true, tabs[2].selected);
114 // Select tab[1].
115 chrome.tabs.update(tabs[1].id, {selected: true},
116 pass(function(){
117 // Check update of tab[1].
118 chrome.tabs.getAllInWindow(firstWindowId, pass(function(tabs) {
119 assertEq(true, tabs[1].selected);
120 assertEq(false, tabs[2].selected);
121 // Select tab[2].
122 chrome.tabs.update(tabs[2].id, {selected: true},
123 pass(function(){
124 // Check update of tab[2].
125 chrome.tabs.getAllInWindow(firstWindowId, pass(function(tabs) {
126 assertEq(false, tabs[1].selected);
127 assertEq(true, tabs[2].selected);
128 }));
129 }));
130 }));
100 })); 131 }));
101 })); 132 }));
102 }, 133 },
103 134
104 // Create 2 new windows, close existing windows. 135 // Create 2 new windows, close existing windows.
105 function moveTabsSetup1() { 136 function moveTabsSetup1() {
106 chrome.windows.create({}, testCallback(false, function(win) { 137 chrome.windows.create({}, pass(function(win1) {
107 moveWindow1 = win.id; 138 moveWindow1 = win1.id;
139 chrome.windows.create({}, pass(function(win2) {
140 moveWindow2 = win2.id;
141 chrome.windows.remove(firstWindowId, pass(function(){
142 chrome.windows.remove(secondWindowId, pass());
143 }));
144 }));
108 })); 145 }));
109 chrome.windows.create({}, testCallback(false, function(win) {
110 moveWindow2 = win.id;
111 }));
112 chrome.windows.remove(firstWindowId, testCallback(false, null));
113 chrome.windows.remove(secondWindowId, testCallback(true, null));
114 }, 146 },
115 147
116 // Create a bunch of tabs and record the resulting ids. 148 // Create a bunch of tabs and record the resulting ids.
117 function moveTabsSetup2() { 149 function moveTabsSetup2() {
118 var letters = ['a', 'b', 'c', 'd', 'e']; 150 var letters = ['a', 'b', 'c', 'd', 'e'];
119 for (var i in letters) { 151 for (var i in letters) {
120 chrome.tabs.create({"windowId": moveWindow1, 152 chrome.tabs.create({"windowId": moveWindow1,
121 "url": "chrome://" + letters[i]}, 153 "url": "chrome://" + letters[i]},
122 testCallback(false, function(tab) { 154 pass(function(tab) {
123 var letter = tab.url[tab.url.length-2]; 155 var letter = tab.url[tab.url.length-2];
124 moveTabIds[letter] = tab.id; 156 moveTabIds[letter] = tab.id;
125 if (tab.url == "chrome://e/") { 157
126 chrome.test.succeed(); 158 // Assert on last callback that tabs were added in the order we created
159 // them.
160 if (letter == 'e') {
161 chrome.tabs.getAllInWindow(moveWindow1, pass(function(tabs) {
162 assertEq(6, tabs.length);
163 assertEq("chrome://newtab/", tabs[0].url);
164 assertEq("chrome://a/", tabs[1].url);
165 assertEq("chrome://b/", tabs[2].url);
166 assertEq("chrome://c/", tabs[3].url);
167 assertEq("chrome://d/", tabs[4].url);
168 assertEq("chrome://e/", tabs[5].url);
169 }));
127 } 170 }
128 })); 171 }));
129 } 172 }
130 }, 173 },
131 174
132 // Do a series of moves so that we get the following 175 // Do a series of moves so that we get the following
133 // 176 //
134 // Before: 177 // Before:
135 // Window1: (newtab),a,b,c,d,e 178 // Window1: (newtab),a,b,c,d,e
136 // Window2: (newtab) 179 // Window2: (newtab)
137 // 180 //
138 // After: 181 // After:
139 // Window1: (newtab),a,e,c 182 // Window1: (newtab),a,e,c
140 // Window2: b,(newtab),d 183 // Window2: b,(newtab),d
141 function moveTabs() { 184 function moveTabs() {
142 chrome.tabs.move(moveTabIds['b'], {"windowId": moveWindow2, "index": 0}, 185 chrome.tabs.move(moveTabIds['b'], {"windowId": moveWindow2, "index": 0},
143 testCallback(false, null)); 186 pass(function() {
144 chrome.tabs.move(moveTabIds['e'], {"index": 2}, 187 chrome.tabs.move(moveTabIds['e'], {"index": 2},
145 testCallback(false, null)); 188 pass(function() {
146 chrome.tabs.move(moveTabIds['d'], {"windowId": moveWindow2, "index": 2}, 189 chrome.tabs.move(moveTabIds['d'], {"windowId": moveWindow2,
147 testCallback(true, null)); 190 "index": 2}, pass(function() {}));
191 }));
192 }));
148 }, 193 },
149 194
150 // Check that the tab/window state is what we expect after doing moves. 195 // Check that the tab/window state is what we expect after doing moves.
151 function moveTabsCheck() { 196 function moveTabsCheck() {
152 chrome.tabs.getAllInWindow(moveWindow1, testCallback(false, function(tabs) { 197 chrome.tabs.getAllInWindow(moveWindow1, pass(function(tabs) {
153 assertEq(4, tabs.length); 198 assertEq(4, tabs.length);
154 assertEq("chrome://newtab/", tabs[0].url); 199 assertEq("chrome://newtab/", tabs[0].url);
155 assertEq("chrome://a/", tabs[1].url); 200 assertEq("chrome://a/", tabs[1].url);
156 assertEq("chrome://e/", tabs[2].url); 201 assertEq("chrome://e/", tabs[2].url);
157 assertEq("chrome://c/", tabs[3].url); 202 assertEq("chrome://c/", tabs[3].url);
158 })); 203
159 chrome.tabs.getAllInWindow(moveWindow2, testCallback(true, function(tabs) { 204 chrome.tabs.getAllInWindow(moveWindow2, pass(function(tabs) {
160 assertEq(3, tabs.length); 205 assertEq(3, tabs.length);
161 assertEq("chrome://b/", tabs[0].url); 206 assertEq("chrome://b/", tabs[0].url);
162 assertEq("chrome://newtab/", tabs[1].url); 207 assertEq("chrome://newtab/", tabs[1].url);
163 assertEq("chrome://d/", tabs[2].url); 208 assertEq("chrome://d/", tabs[2].url);
209 }));
164 })); 210 }));
165 }, 211 },
166 212
167 function remove() { 213 function remove() {
168 chrome.tabs.remove(moveTabIds["d"], testCallback(false, function() { 214 chrome.tabs.remove(moveTabIds["d"], pass(function() {
169 chrome.tabs.getAllInWindow(moveWindow2, 215 chrome.tabs.getAllInWindow(moveWindow2,
170 testCallback(true, function(tabs) { 216 pass(function(tabs) {
171 assertEq(2, tabs.length); 217 assertEq(2, tabs.length);
172 assertEq("chrome://b/", tabs[0].url); 218 assertEq("chrome://b/", tabs[0].url);
173 assertEq("chrome://newtab/", tabs[1].url); 219 assertEq("chrome://newtab/", tabs[1].url);
174 })); 220 }));
175 })); 221 }));
176 }, 222 },
177 223
178 function detectLanguage() { 224 function detectLanguage() {
179 chrome.tabs.getAllInWindow(moveWindow1, testCallback(false, function(tabs) { 225 chrome.tabs.getAllInWindow(moveWindow1, pass(function(tabs) {
180 chrome.tabs.detectLanguage(tabs[0].id, testCallback(true, function(lang) { 226 chrome.tabs.detectLanguage(tabs[0].id, pass(function(lang) {
181 assertEq("en", lang); 227 assertEq("en", lang);
182 })); 228 }));
183 })); 229 }));
184 }, 230 },
185 231
186 function captureVisibleTab() { 232 function captureVisibleTab() {
187 // Grab an image for each of our two windows. 233 // Take First Capture
188 var firstImage;
189 var secondImage;
190 chrome.tabs.captureVisibleTab(moveWindow1, 234 chrome.tabs.captureVisibleTab(moveWindow1,
191 testCallback(false, function(url) { 235 pass(function(window1Url) {
192 assertEq("string", typeof(url)); 236 assertEq("string", typeof(window1Url));
193 assertTrue(url.length > 0); 237 assertTrue(window1Url.length > 0);
194 firstImage = url; 238
195 })); 239 // Take Second Capture
196 chrome.tabs.captureVisibleTab(moveWindow2, 240 chrome.tabs.captureVisibleTab(moveWindow2,
197 testCallback(false, function(url) { 241 pass(function(window2Url) {
198 assertEq("string", typeof(url)); 242 assertEq("string", typeof(window2Url));
199 assertTrue(url.length > 0); 243 assertTrue(window2Url.length > 0);
200 secondImage = url; 244 assertTrue(window1Url != window2Url);
201 assertTrue(firstImage != secondImage); 245
202 })); 246 // Now pass null for windowId - it should come back with something
203 247 // equal to either the first or second window. This is nondeterministic
204 // Now pass null for windowId - it should come back with something 248 // depending on whether you let chrome stay focused, or click
205 // equal to either the first or second window. This is nondeterministic 249 // focus away (or are running on the try/build servers).
206 // depending on whether you let chrome stay focused, or click 250 chrome.tabs.captureVisibleTab(null, pass(function(url) {
207 // focus away (or are running on the try/build servers). 251 assertEq("string", typeof(url));
208 chrome.tabs.captureVisibleTab(null, testCallback(true, function(url) { 252 assertTrue(url.length > 0);
209 assertEq("string", typeof(url)); 253 assertTrue(url == window1Url || url == window2Url);
210 assertTrue(url.length > 0); 254 }));
211 assertTrue(url == firstImage || url == secondImage); 255 }));
212 })); 256 }));
213 }, 257 },
214 258
215 function onCreated() { 259 function onCreated() {
216 var callbackSuccess = false; 260 chrome.test.listenOnce(chrome.tabs.onCreated, function(tab) {
217 var listener = function(tab) {
218 assertEq("chrome://f/", tab.url); 261 assertEq("chrome://f/", tab.url);
219 callbackSuccess = true; 262 });
220 }; 263
221 chrome.tabs.onCreated.addListener(listener);
222 chrome.tabs.create({"windowId": moveWindow1, "url": "chrome://f", 264 chrome.tabs.create({"windowId": moveWindow1, "url": "chrome://f",
223 "selected": true}, testCallback(true, function(tab) { 265 "selected": true}, pass(function(tab) {}));
224 chrome.tabs.onCreated.removeListener(listener);
225 assertTrue(callbackSuccess);
226 }));
227 }, 266 },
228 267
229 function onUpdated() { 268 function onUpdated() {
230 var listener = function(tabid, info) { 269 var listener = chrome.test.listenForever(chrome.tabs.onUpdated,
231 if (tabid != moveTabIds['a']) { 270 function(tabid, info) {
232 return; // ignore updates for tabs we weren't interested in 271 if (tabid == moveTabIds['a'] && info.status == "complete") {
272 listener.doneListening();
273 }
233 } 274 }
234 if (info.status == "complete") { 275 );
235 chrome.tabs.onUpdated.removeListener(listener); 276
236 chrome.test.succeed();
237 }
238 };
239 chrome.tabs.onUpdated.addListener(listener);
240 chrome.tabs.update(moveTabIds['a'], {"url": "chrome://aa"}, 277 chrome.tabs.update(moveTabIds['a'], {"url": "chrome://aa"},
241 testCallback(false, null)); 278 pass());
242 }, 279 },
243 280
244 function onMoved() { 281 function onMoved() {
245 var listener = function(tabid, info) { 282 chrome.test.listenOnce(chrome.tabs.onMoved, function(tabid, info) {
246 chrome.tabs.onMoved.removeListener(listener);
247 assertEq(moveTabIds['a'], tabid); 283 assertEq(moveTabIds['a'], tabid);
248 chrome.test.succeed(); 284 });
249 }; 285
250 chrome.tabs.onMoved.addListener(listener); 286 chrome.tabs.move(moveTabIds['a'], {"index": 0}, pass());
251 chrome.tabs.move(moveTabIds['a'], {"index": 0}, testCallback(false, null));
252 }, 287 },
253 288
254 function onSelectionChanged() { 289 function onSelectionChanged() {
255 var listener = function(tabid, info) { 290 chrome.test.listenOnce(chrome.tabs.onSelectionChanged,
256 chrome.tabs.onSelectionChanged.removeListener(listener); 291 function(tabid, info) {
257 assertEq(moveTabIds['c'], tabid); 292 assertEq(moveTabIds['c'], tabid);
258 chrome.test.succeed(); 293 }
259 }; 294 );
260 chrome.tabs.onSelectionChanged.addListener(listener); 295
261 chrome.tabs.update(moveTabIds['c'], {"selected": true}, 296 chrome.tabs.update(moveTabIds['c'], {"selected": true},
262 testCallback(false, null)); 297 pass());
263 }, 298 },
264 299
265 function onRemoved() { 300 function onRemoved() {
266 var listener = function(tabid) { 301 chrome.test.listenOnce(chrome.tabs.onRemoved, function(tabid) {
267 chrome.tabs.onRemoved.removeListener(listener);
268 assertEq(moveTabIds['c'], tabid); 302 assertEq(moveTabIds['c'], tabid);
269 chrome.test.succeed(); 303 });
270 }; 304
271 chrome.tabs.onRemoved.addListener(listener); 305 chrome.tabs.remove(moveTabIds['c'], pass());
272 chrome.tabs.remove(moveTabIds['c'], testCallback(false, null));
273 } 306 }
274 307
275 // TODO(asargent) We still need to add tests for the following: 308 // TODO(asargent) We still need to add tests for the following:
276 // Methods: 309 // Methods:
277 // -chrome.tabs.connect 310 // -chrome.tabs.connect
278 // Events: 311 // Events:
279 // -chrome.tabs.onAttached 312 // -chrome.tabs.onAttached
280 // -chrome.tabs.onDetched 313 // -chrome.tabs.onDetched
281 // 314 //
282 // Also, it would be an improvement to check the captureVisibleTab results 315 // Also, it would be an improvement to check the captureVisibleTab results
283 // against a known-good result. 316 // against a known-good result.
284 ]); 317 ]);
OLDNEW
« no previous file with comments | « chrome/renderer/resources/extension_apitest.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698