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

Side by Side Diff: chrome/browser/extensions/extension_tabs_test.cc

Issue 8969011: Added populate parameter to chrome.windows.get, chrome.windows.getCurrent, (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: '' Created 9 years 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
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 #include "chrome/browser/extensions/extension_tabs_module.h" 5 #include "chrome/browser/extensions/extension_tabs_module.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/memory/ref_counted.h" 9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
11 #include "base/stringprintf.h" 11 #include "base/stringprintf.h"
12 #include "base/string_util.h" 12 #include "base/string_util.h"
13 #include "base/values.h" 13 #include "base/values.h"
14 #include "chrome/browser/extensions/extension_function_test_utils.h" 14 #include "chrome/browser/extensions/extension_function_test_utils.h"
15 #include "chrome/browser/extensions/extension_tabs_module_constants.h" 15 #include "chrome/browser/extensions/extension_tabs_module_constants.h"
16 #include "chrome/browser/extensions/extension_tab_util.h" 16 #include "chrome/browser/extensions/extension_tab_util.h"
17 #include "chrome/browser/prefs/incognito_mode_prefs.h" 17 #include "chrome/browser/prefs/incognito_mode_prefs.h"
18 #include "chrome/browser/profiles/profile.h" 18 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/browser/ui/browser.h" 19 #include "chrome/browser/ui/browser.h"
20 #include "chrome/browser/ui/browser_window.h" 20 #include "chrome/browser/ui/browser_window.h"
21 #include "chrome/test/base/in_process_browser_test.h" 21 #include "chrome/test/base/in_process_browser_test.h"
22 #include "chrome/test/base/ui_test_utils.h" 22 #include "chrome/test/base/ui_test_utils.h"
23 #include "ui/gfx/rect.h" 23 #include "ui/gfx/rect.h"
24 24
25 using namespace extension_function_test_utils; 25 using namespace extension_function_test_utils;
26 namespace keys = extension_tabs_module_constants;
26 27
27 namespace { 28 namespace {
28 29
29 class ExtensionTabsTest : public InProcessBrowserTest { 30 class ExtensionTabsTest : public InProcessBrowserTest {
30 }; 31 };
31
32 } 32 }
33 33
34 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, GetWindow) { 34 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, GetWindow) {
35 int window_id = ExtensionTabUtil::GetWindowId(browser()); 35 int window_id = ExtensionTabUtil::GetWindowId(browser());
36 36
37 // Invalid window ID error. 37 // Invalid window ID error.
38 EXPECT_TRUE(MatchPattern( 38 EXPECT_TRUE(MatchPattern(
39 RunFunctionAndReturnError( 39 RunFunctionAndReturnError(
40 new GetWindowFunction(), 40 new GetWindowFunction(),
41 base::StringPrintf("[%u]", window_id + 1), 41 base::StringPrintf("[%u]", window_id + 1),
42 browser()), 42 browser()),
43 extension_tabs_module_constants::kWindowNotFoundError)); 43 keys::kWindowNotFoundError));
44 44
45 // Basic window details. 45 // Basic window details.
46 // Need GetRestoredBound instead of GetBounds. 46 // Need GetRestoredBound instead of GetBounds.
47 // See crbug.com/98759. 47 // See crbug.com/98759.
48 gfx::Rect bounds = browser()->window()->GetRestoredBounds(); 48 gfx::Rect bounds = browser()->window()->GetRestoredBounds();
49 49
50 scoped_ptr<base::DictionaryValue> result(ToDictionary( 50 scoped_ptr<base::DictionaryValue> result(ToDictionary(
51 RunFunctionAndReturnResult( 51 RunFunctionAndReturnResult(
52 new GetWindowFunction(), 52 new GetWindowFunction(),
53 base::StringPrintf("[%u]", window_id), 53 base::StringPrintf("[%u]", window_id),
54 browser()))); 54 browser())));
55 EXPECT_EQ(window_id, GetInteger(result.get(), "id")); 55 EXPECT_EQ(window_id, GetInteger(result.get(), "id"));
56 EXPECT_FALSE(GetBoolean(result.get(), "incognito")); 56 EXPECT_FALSE(GetBoolean(result.get(), "incognito"));
57 EXPECT_EQ("normal", GetString(result.get(), "type")); 57 EXPECT_EQ("normal", GetString(result.get(), "type"));
58 EXPECT_EQ(bounds.x(), GetInteger(result.get(), "left")); 58 EXPECT_EQ(bounds.x(), GetInteger(result.get(), "left"));
59 EXPECT_EQ(bounds.y(), GetInteger(result.get(), "top")); 59 EXPECT_EQ(bounds.y(), GetInteger(result.get(), "top"));
60 EXPECT_EQ(bounds.width(), GetInteger(result.get(), "width")); 60 EXPECT_EQ(bounds.width(), GetInteger(result.get(), "width"));
61 EXPECT_EQ(bounds.height(), GetInteger(result.get(), "height")); 61 EXPECT_EQ(bounds.height(), GetInteger(result.get(), "height"));
62 62
63 // With populate enabled
64 result.reset(ToDictionary(
65 RunFunctionAndReturnResult(
66 new GetWindowFunction(),
67 base::StringPrintf("[%u, {\"populate\": true}]", window_id),
68 browser())));
69
70 EXPECT_EQ(window_id, GetInteger(result.get(), "id"));
71 // populate was enabled so tabs should be populated
72 ListValue* tabs;
Aaron Boodman 2011/12/17 07:12:58 *twitch*
73 EXPECT_TRUE(result.get()->GetList(keys::kTabsKey, &tabs));
74
63 // TODO(aa): Can't assume window is focused. On mac, calling Activate() from a 75 // TODO(aa): Can't assume window is focused. On mac, calling Activate() from a
64 // browser test doesn't seem to do anything, so can't test the opposite 76 // browser test doesn't seem to do anything, so can't test the opposite
65 // either. 77 // either.
66 EXPECT_EQ(browser()->window()->IsActive(), 78 EXPECT_EQ(browser()->window()->IsActive(),
67 GetBoolean(result.get(), "focused")); 79 GetBoolean(result.get(), "focused"));
68 80
69 // TODO(aa): Minimized and maximized dimensions. Is there a way to set 81 // TODO(aa): Minimized and maximized dimensions. Is there a way to set
70 // minimize/maximize programmatically? 82 // minimize/maximize programmatically?
71 83
72 // Popup. 84 // Popup.
(...skipping 21 matching lines...) Expand all
94 // Incognito. 106 // Incognito.
95 Browser* incognito_browser = CreateIncognitoBrowser(); 107 Browser* incognito_browser = CreateIncognitoBrowser();
96 int incognito_window_id = ExtensionTabUtil::GetWindowId(incognito_browser); 108 int incognito_window_id = ExtensionTabUtil::GetWindowId(incognito_browser);
97 109
98 // Without "include_incognito". 110 // Without "include_incognito".
99 EXPECT_TRUE(MatchPattern( 111 EXPECT_TRUE(MatchPattern(
100 RunFunctionAndReturnError( 112 RunFunctionAndReturnError(
101 new GetWindowFunction(), 113 new GetWindowFunction(),
102 base::StringPrintf("[%u]", incognito_window_id), 114 base::StringPrintf("[%u]", incognito_window_id),
103 browser()), 115 browser()),
104 extension_tabs_module_constants::kWindowNotFoundError)); 116 keys::kWindowNotFoundError));
105 117
106 // With "include_incognito". 118 // With "include_incognito".
107 result.reset(ToDictionary( 119 result.reset(ToDictionary(
108 RunFunctionAndReturnResult( 120 RunFunctionAndReturnResult(
109 new GetWindowFunction(), 121 new GetWindowFunction(),
110 base::StringPrintf("[%u]", incognito_window_id), 122 base::StringPrintf("[%u]", incognito_window_id),
111 browser(), 123 browser(),
112 INCLUDE_INCOGNITO))); 124 INCLUDE_INCOGNITO)));
113 EXPECT_TRUE(GetBoolean(result.get(), "incognito")); 125 EXPECT_TRUE(GetBoolean(result.get(), "incognito"));
114 } 126 }
115 127
128 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, GetCurrentWindow) {
129 int window_id = ExtensionTabUtil::GetWindowId(browser());
130 Browser* new_browser = CreateBrowser(browser()->profile());
131 int new_id = ExtensionTabUtil::GetWindowId(new_browser);
132
133 // Get the current window using new_browser
134 scoped_ptr<base::DictionaryValue> result(ToDictionary(
135 RunFunctionAndReturnResult(
136 new GetCurrentWindowFunction(),
137 "[]",
138 new_browser)));
139
140 // id should match the window that was passed to RunFunction
Aaron Boodman 2011/12/17 07:12:58 Comment doesn't make sense. You mean something lik
Aaron Boodman 2011/12/17 07:12:58 Also, nit, but comments should generally be comple
Matt Tytel 2011/12/17 07:43:32 Here I want to distinguish between the functionali
Aaron Boodman 2011/12/17 08:02:40 Ok, fair enough. In that case, it might be a littl
141 EXPECT_EQ(new_id, GetInteger(result.get(), "id"));
142 ListValue* tabs;
Aaron Boodman 2011/12/17 07:12:58 !
143 EXPECT_FALSE(result.get()->GetList(keys::kTabsKey, &tabs));
144
145 // Get the current window using the old window and make the tabs populated
146 result.reset(ToDictionary(
147 RunFunctionAndReturnResult(
148 new GetCurrentWindowFunction(),
149 "[{\"populate\": true}]",
150 browser())));
151
152 // id should match the window that was passed to RunFunction
153 EXPECT_EQ(window_id, GetInteger(result.get(), "id"));
154 // populate was enabled so tabs should be populated
155 EXPECT_TRUE(result.get()->GetList(keys::kTabsKey, &tabs));
156 }
157
158 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, GetLastFocusedWindow) {
159 // Create a new window which making it the "last focused" window.
160 // Note that "last focused" means the "top" most window
Aaron Boodman 2011/12/17 07:12:58 Missing final period.
161 Browser* new_browser = CreateBrowser(browser()->profile());
162 int focus_id = ExtensionTabUtil::GetWindowId(new_browser);
Aaron Boodman 2011/12/17 07:12:58 focused_window_id?
163
164 scoped_ptr<base::DictionaryValue> result(ToDictionary(
165 RunFunctionAndReturnResult(
166 new GetLastFocusedWindowFunction(),
167 "[]",
168 new_browser)));
169
170 // id should match the last focused window
171 EXPECT_EQ(focus_id, GetInteger(result.get(), "id"));
172 ListValue* tabs;
Aaron Boodman 2011/12/17 07:12:58 !! (I'm going to stop pointing these out now)
173 EXPECT_FALSE(result.get()->GetList(keys::kTabsKey, &tabs));
174
175 result.reset(ToDictionary(
176 RunFunctionAndReturnResult(
177 new GetLastFocusedWindowFunction(),
178 "[{\"populate\": true}]",
179 browser())));
180
181 // id should match the last focused window, not the window it was called from
182 EXPECT_EQ(focus_id, GetInteger(result.get(), "id"));
183 // populate was enabled so tabs should be populated
184 EXPECT_TRUE(result.get()->GetList(keys::kTabsKey, &tabs));
185 }
186
187 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, GetAllWindows) {
188 const size_t NUM_WINDOWS = 5;
189 std::vector<int> window_ids;
190 std::vector<int> result_ids;
191 window_ids.push_back(ExtensionTabUtil::GetWindowId(browser()));
192
193 for (uint i = 0; i < NUM_WINDOWS - 1; ++i) {
194 Browser* bro = CreateBrowser(browser()->profile());
Aaron Boodman 2011/12/17 07:12:58 Avoid abbreviations. Just use the term "new_browse
Matt Tytel 2011/12/17 07:43:32 You got it bro ;)
195 window_ids.push_back(ExtensionTabUtil::GetWindowId(bro));
196 }
197 std::sort(window_ids.begin(), window_ids.end());
198
199 scoped_ptr<base::ListValue> result(ToList(
200 RunFunctionAndReturnResult(
201 new GetAllWindowsFunction(),
202 "[]",
203 browser())));
204
205 ListValue* windows = result.get();
206 EXPECT_EQ(NUM_WINDOWS, windows->GetSize());
207 for (uint i = 0; i < NUM_WINDOWS; ++i) {
208 DictionaryValue* result_window;
209 EXPECT_TRUE(windows->GetDictionary(i, &result_window));
210 result_ids.push_back(GetInteger(result_window, "id"));
211
212 // populate was not passed in so tabs are not populated
213 ListValue* tabs;
214 EXPECT_FALSE(result_window->GetList(keys::kTabsKey, &tabs));
215 }
216
217 std::sort(window_ids.begin(), window_ids.end());
Aaron Boodman 2011/12/17 07:12:58 You should just use std::set. They come with an eq
218 std::sort(result_ids.begin(), result_ids.end());
219 for (uint i = 0; i < NUM_WINDOWS; ++i)
220 EXPECT_EQ(window_ids[i], result_ids[i]);
221
222 result_ids.clear();
223 result.reset(ToList(
224 RunFunctionAndReturnResult(
225 new GetAllWindowsFunction(),
226 "[{\"populate\": true}]",
227 browser())));
228
229 windows = result.get();
230 EXPECT_EQ(NUM_WINDOWS, windows->GetSize());
231 for (uint i = 0; i < windows->GetSize(); ++i) {
232 DictionaryValue* result_window;
233 EXPECT_TRUE(windows->GetDictionary(i, &result_window));
234 result_ids.push_back(GetInteger(result_window, "id"));
235
236 // populate was enabled so tabs should be populated
237 ListValue* tabs;
238 EXPECT_TRUE(result_window->GetList(keys::kTabsKey, &tabs));
239 }
240
241 std::sort(result_ids.begin(), result_ids.end());
242 for (uint i = 0; i < NUM_WINDOWS; ++i)
243 EXPECT_EQ(window_ids[i], result_ids[i]);
244 }
245
116 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, UpdateNoPermissions) { 246 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, UpdateNoPermissions) {
117 // The test empty extension has no permissions, therefore it should not get 247 // The test empty extension has no permissions, therefore it should not get
118 // tab data in the function result. 248 // tab data in the function result.
119 scoped_refptr<UpdateTabFunction> update_tab_function(new UpdateTabFunction()); 249 scoped_refptr<UpdateTabFunction> update_tab_function(new UpdateTabFunction());
120 scoped_refptr<Extension> empty_extension(CreateEmptyExtension()); 250 scoped_refptr<Extension> empty_extension(CreateEmptyExtension());
121 update_tab_function->set_extension(empty_extension.get()); 251 update_tab_function->set_extension(empty_extension.get());
122 // Without a callback the function will not generate a result. 252 // Without a callback the function will not generate a result.
123 update_tab_function->set_has_callback(true); 253 update_tab_function->set_has_callback(true);
124 254
125 scoped_ptr<base::Value> result(RunFunctionAndReturnResult( 255 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
209 // Force Incognito mode. 339 // Force Incognito mode.
210 IncognitoModePrefs::SetAvailability(browser()->profile()->GetPrefs(), 340 IncognitoModePrefs::SetAvailability(browser()->profile()->GetPrefs(),
211 IncognitoModePrefs::FORCED); 341 IncognitoModePrefs::FORCED);
212 342
213 // Run with an explicit "incognito" param. 343 // Run with an explicit "incognito" param.
214 EXPECT_TRUE(MatchPattern( 344 EXPECT_TRUE(MatchPattern(
215 RunFunctionAndReturnError( 345 RunFunctionAndReturnError(
216 new CreateWindowFunction(), 346 new CreateWindowFunction(),
217 kArgsWithExplicitIncognitoParam, 347 kArgsWithExplicitIncognitoParam,
218 browser()), 348 browser()),
219 extension_tabs_module_constants::kIncognitoModeIsForced)); 349 keys::kIncognitoModeIsForced));
220 350
221 // Now try opening a normal window from incognito window. 351 // Now try opening a normal window from incognito window.
222 Browser* incognito_browser = CreateIncognitoBrowser(); 352 Browser* incognito_browser = CreateIncognitoBrowser();
223 // Run with an explicit "incognito" param. 353 // Run with an explicit "incognito" param.
224 EXPECT_TRUE(MatchPattern( 354 EXPECT_TRUE(MatchPattern(
225 RunFunctionAndReturnError( 355 RunFunctionAndReturnError(
226 new CreateWindowFunction(), 356 new CreateWindowFunction(),
227 kArgsWithExplicitIncognitoParam, 357 kArgsWithExplicitIncognitoParam,
228 incognito_browser), 358 incognito_browser),
229 extension_tabs_module_constants::kIncognitoModeIsForced)); 359 keys::kIncognitoModeIsForced));
230 } 360 }
231 361
232 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, 362 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest,
233 DontCreateIncognitoWindowWhenIncognitoDisabled) { 363 DontCreateIncognitoWindowWhenIncognitoDisabled) {
234 static const char kArgs[] = 364 static const char kArgs[] =
235 "[{\"url\": \"about:blank\", \"incognito\": true }]"; 365 "[{\"url\": \"about:blank\", \"incognito\": true }]";
236 366
237 Browser* incognito_browser = CreateIncognitoBrowser(); 367 Browser* incognito_browser = CreateIncognitoBrowser();
238 // Disable Incognito mode. 368 // Disable Incognito mode.
239 IncognitoModePrefs::SetAvailability(browser()->profile()->GetPrefs(), 369 IncognitoModePrefs::SetAvailability(browser()->profile()->GetPrefs(),
240 IncognitoModePrefs::DISABLED); 370 IncognitoModePrefs::DISABLED);
241 // Run in normal window. 371 // Run in normal window.
242 EXPECT_TRUE(MatchPattern( 372 EXPECT_TRUE(MatchPattern(
243 RunFunctionAndReturnError( 373 RunFunctionAndReturnError(
244 new CreateWindowFunction(), 374 new CreateWindowFunction(),
245 kArgs, 375 kArgs,
246 browser()), 376 browser()),
247 extension_tabs_module_constants::kIncognitoModeIsDisabled)); 377 keys::kIncognitoModeIsDisabled));
248 378
249 // Run in incognito window. 379 // Run in incognito window.
250 EXPECT_TRUE(MatchPattern( 380 EXPECT_TRUE(MatchPattern(
251 RunFunctionAndReturnError( 381 RunFunctionAndReturnError(
252 new CreateWindowFunction(), 382 new CreateWindowFunction(),
253 kArgs, 383 kArgs,
254 incognito_browser), 384 incognito_browser),
255 extension_tabs_module_constants::kIncognitoModeIsDisabled)); 385 keys::kIncognitoModeIsDisabled));
256 } 386 }
257 387
258 #if defined(USE_AURA) 388 #if defined(USE_AURA)
259 // crbug.com/105173. 389 // crbug.com/105173.
260 #define MAYBE_InvalidUpdateWindowState DISABLED_InvalidUpdateWindowState 390 #define MAYBE_InvalidUpdateWindowState DISABLED_InvalidUpdateWindowState
261 #else 391 #else
262 #define MAYBE_InvalidUpdateWindowState InvalidUpdateWindowState 392 #define MAYBE_InvalidUpdateWindowState InvalidUpdateWindowState
263 #endif 393 #endif
264 394
265 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, MAYBE_InvalidUpdateWindowState) { 395 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, MAYBE_InvalidUpdateWindowState) {
266 static const char kArgsMinimizedWithFocus[] = 396 static const char kArgsMinimizedWithFocus[] =
267 "[%u, {\"state\": \"minimized\", \"focused\": true}]"; 397 "[%u, {\"state\": \"minimized\", \"focused\": true}]";
268 static const char kArgsMaximizedWithoutFocus[] = 398 static const char kArgsMaximizedWithoutFocus[] =
269 "[%u, {\"state\": \"maximized\", \"focused\": false}]"; 399 "[%u, {\"state\": \"maximized\", \"focused\": false}]";
270 static const char kArgsMinimizedWithBounds[] = 400 static const char kArgsMinimizedWithBounds[] =
271 "[%u, {\"state\": \"minimized\", \"width\": 500}]"; 401 "[%u, {\"state\": \"minimized\", \"width\": 500}]";
272 static const char kArgsMaximizedWithBounds[] = 402 static const char kArgsMaximizedWithBounds[] =
273 "[%u, {\"state\": \"maximized\", \"width\": 500}]"; 403 "[%u, {\"state\": \"maximized\", \"width\": 500}]";
274 int window_id = ExtensionTabUtil::GetWindowId(browser()); 404 int window_id = ExtensionTabUtil::GetWindowId(browser());
275 405
276 EXPECT_TRUE(MatchPattern( 406 EXPECT_TRUE(MatchPattern(
277 RunFunctionAndReturnError( 407 RunFunctionAndReturnError(
278 new UpdateWindowFunction(), 408 new UpdateWindowFunction(),
279 base::StringPrintf(kArgsMinimizedWithFocus, window_id), 409 base::StringPrintf(kArgsMinimizedWithFocus, window_id),
280 browser()), 410 browser()),
281 extension_tabs_module_constants::kInvalidWindowStateError)); 411 keys::kInvalidWindowStateError));
282 412
283 EXPECT_TRUE(MatchPattern( 413 EXPECT_TRUE(MatchPattern(
284 RunFunctionAndReturnError( 414 RunFunctionAndReturnError(
285 new UpdateWindowFunction(), 415 new UpdateWindowFunction(),
286 base::StringPrintf(kArgsMaximizedWithoutFocus, window_id), 416 base::StringPrintf(kArgsMaximizedWithoutFocus, window_id),
287 browser()), 417 browser()),
288 extension_tabs_module_constants::kInvalidWindowStateError)); 418 keys::kInvalidWindowStateError));
289 419
290 EXPECT_TRUE(MatchPattern( 420 EXPECT_TRUE(MatchPattern(
291 RunFunctionAndReturnError( 421 RunFunctionAndReturnError(
292 new UpdateWindowFunction(), 422 new UpdateWindowFunction(),
293 base::StringPrintf(kArgsMinimizedWithBounds, window_id), 423 base::StringPrintf(kArgsMinimizedWithBounds, window_id),
294 browser()), 424 browser()),
295 extension_tabs_module_constants::kInvalidWindowStateError)); 425 keys::kInvalidWindowStateError));
296 426
297 EXPECT_TRUE(MatchPattern( 427 EXPECT_TRUE(MatchPattern(
298 RunFunctionAndReturnError( 428 RunFunctionAndReturnError(
299 new UpdateWindowFunction(), 429 new UpdateWindowFunction(),
300 base::StringPrintf(kArgsMaximizedWithBounds, window_id), 430 base::StringPrintf(kArgsMaximizedWithBounds, window_id),
301 browser()), 431 browser()),
302 extension_tabs_module_constants::kInvalidWindowStateError)); 432 keys::kInvalidWindowStateError));
303 } 433 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698