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

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 = NULL;
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 // The id should match the window id of the browser instance that was passed
141 // to RunFunctionAndReturnResult.
142 EXPECT_EQ(new_id, GetInteger(result.get(), "id"));
143 ListValue* tabs = NULL;
144 EXPECT_FALSE(result.get()->GetList(keys::kTabsKey, &tabs));
145
146 // Get the current window using the old window and make the tabs populated.
147 result.reset(ToDictionary(
148 RunFunctionAndReturnResult(
149 new GetCurrentWindowFunction(),
150 "[{\"populate\": true}]",
151 browser())));
152
153 // The id should match the window id of the browser instance that was passed
154 // to RunFunctionAndReturnResult.
155 EXPECT_EQ(window_id, GetInteger(result.get(), "id"));
156 // "populate" was enabled so tabs should be populated.
157 EXPECT_TRUE(result.get()->GetList(keys::kTabsKey, &tabs));
158 }
159
160 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, GetLastFocusedWindow) {
161 // Create a new window which making it the "last focused" window.
162 // Note that "last focused" means the "top" most window.
163 Browser* new_browser = CreateBrowser(browser()->profile());
164 int focused_window_id = ExtensionTabUtil::GetWindowId(new_browser);
165
166 scoped_ptr<base::DictionaryValue> result(ToDictionary(
167 RunFunctionAndReturnResult(
168 new GetLastFocusedWindowFunction(),
169 "[]",
170 new_browser)));
171
172 // The id should always match the last focused window and does not depend
173 // on what was passed to RunFunctionAndReturnResult.
174 EXPECT_EQ(focused_window_id, GetInteger(result.get(), "id"));
175 ListValue* tabs = NULL;
176 EXPECT_FALSE(result.get()->GetList(keys::kTabsKey, &tabs));
177
178 result.reset(ToDictionary(
179 RunFunctionAndReturnResult(
180 new GetLastFocusedWindowFunction(),
181 "[{\"populate\": true}]",
182 browser())));
183
184 // The id should always match the last focused window and does not depend
185 // on what was passed to RunFunctionAndReturnResult.
186 EXPECT_EQ(focused_window_id, GetInteger(result.get(), "id"));
187 // "populate" was enabled so tabs should be populated.
188 EXPECT_TRUE(result.get()->GetList(keys::kTabsKey, &tabs));
189 }
190
191 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, GetAllWindows) {
192 const size_t NUM_WINDOWS = 5;
193 std::set<int> window_ids;
194 std::set<int> result_ids;
195 window_ids.insert(ExtensionTabUtil::GetWindowId(browser()));
196
197 for (uint i = 0; i < NUM_WINDOWS - 1; ++i) {
Aaron Boodman 2011/12/19 06:09:03 Dang, bad reviewer. This should be size_t to match
198 Browser* new_browser = CreateBrowser(browser()->profile());
199 window_ids.insert(ExtensionTabUtil::GetWindowId(new_browser));
200 }
201
202 scoped_ptr<base::ListValue> result(ToList(
203 RunFunctionAndReturnResult(
204 new GetAllWindowsFunction(),
205 "[]",
206 browser())));
207
208 ListValue* windows = result.get();
209 EXPECT_EQ(NUM_WINDOWS, windows->GetSize());
210 for (uint i = 0; i < NUM_WINDOWS; ++i) {
211 DictionaryValue* result_window = NULL;
212 EXPECT_TRUE(windows->GetDictionary(i, &result_window));
213 result_ids.insert(GetInteger(result_window, "id"));
214
215 // "populate" was not passed in so tabs are not populated.
216 ListValue* tabs = NULL;
217 EXPECT_FALSE(result_window->GetList(keys::kTabsKey, &tabs));
218 }
219 // The returned ids should contain all the current browser instance ids.
220 EXPECT_EQ(window_ids, result_ids);
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 = NULL;
233 EXPECT_TRUE(windows->GetDictionary(i, &result_window));
234 result_ids.insert(GetInteger(result_window, "id"));
235
236 // "populate" was enabled so tabs should be populated.
237 ListValue* tabs = NULL;
238 EXPECT_TRUE(result_window->GetList(keys::kTabsKey, &tabs));
239 }
240 // The returned ids should contain all the current browser instance ids.
241 EXPECT_EQ(window_ids, result_ids);
242 }
243
116 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, UpdateNoPermissions) { 244 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, UpdateNoPermissions) {
117 // The test empty extension has no permissions, therefore it should not get 245 // The test empty extension has no permissions, therefore it should not get
118 // tab data in the function result. 246 // tab data in the function result.
119 scoped_refptr<UpdateTabFunction> update_tab_function(new UpdateTabFunction()); 247 scoped_refptr<UpdateTabFunction> update_tab_function(new UpdateTabFunction());
120 scoped_refptr<Extension> empty_extension(CreateEmptyExtension()); 248 scoped_refptr<Extension> empty_extension(CreateEmptyExtension());
121 update_tab_function->set_extension(empty_extension.get()); 249 update_tab_function->set_extension(empty_extension.get());
122 // Without a callback the function will not generate a result. 250 // Without a callback the function will not generate a result.
123 update_tab_function->set_has_callback(true); 251 update_tab_function->set_has_callback(true);
124 252
125 scoped_ptr<base::Value> result(RunFunctionAndReturnResult( 253 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
209 // Force Incognito mode. 337 // Force Incognito mode.
210 IncognitoModePrefs::SetAvailability(browser()->profile()->GetPrefs(), 338 IncognitoModePrefs::SetAvailability(browser()->profile()->GetPrefs(),
211 IncognitoModePrefs::FORCED); 339 IncognitoModePrefs::FORCED);
212 340
213 // Run with an explicit "incognito" param. 341 // Run with an explicit "incognito" param.
214 EXPECT_TRUE(MatchPattern( 342 EXPECT_TRUE(MatchPattern(
215 RunFunctionAndReturnError( 343 RunFunctionAndReturnError(
216 new CreateWindowFunction(), 344 new CreateWindowFunction(),
217 kArgsWithExplicitIncognitoParam, 345 kArgsWithExplicitIncognitoParam,
218 browser()), 346 browser()),
219 extension_tabs_module_constants::kIncognitoModeIsForced)); 347 keys::kIncognitoModeIsForced));
220 348
221 // Now try opening a normal window from incognito window. 349 // Now try opening a normal window from incognito window.
222 Browser* incognito_browser = CreateIncognitoBrowser(); 350 Browser* incognito_browser = CreateIncognitoBrowser();
223 // Run with an explicit "incognito" param. 351 // Run with an explicit "incognito" param.
224 EXPECT_TRUE(MatchPattern( 352 EXPECT_TRUE(MatchPattern(
225 RunFunctionAndReturnError( 353 RunFunctionAndReturnError(
226 new CreateWindowFunction(), 354 new CreateWindowFunction(),
227 kArgsWithExplicitIncognitoParam, 355 kArgsWithExplicitIncognitoParam,
228 incognito_browser), 356 incognito_browser),
229 extension_tabs_module_constants::kIncognitoModeIsForced)); 357 keys::kIncognitoModeIsForced));
230 } 358 }
231 359
232 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, 360 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest,
233 DontCreateIncognitoWindowWhenIncognitoDisabled) { 361 DontCreateIncognitoWindowWhenIncognitoDisabled) {
234 static const char kArgs[] = 362 static const char kArgs[] =
235 "[{\"url\": \"about:blank\", \"incognito\": true }]"; 363 "[{\"url\": \"about:blank\", \"incognito\": true }]";
236 364
237 Browser* incognito_browser = CreateIncognitoBrowser(); 365 Browser* incognito_browser = CreateIncognitoBrowser();
238 // Disable Incognito mode. 366 // Disable Incognito mode.
239 IncognitoModePrefs::SetAvailability(browser()->profile()->GetPrefs(), 367 IncognitoModePrefs::SetAvailability(browser()->profile()->GetPrefs(),
240 IncognitoModePrefs::DISABLED); 368 IncognitoModePrefs::DISABLED);
241 // Run in normal window. 369 // Run in normal window.
242 EXPECT_TRUE(MatchPattern( 370 EXPECT_TRUE(MatchPattern(
243 RunFunctionAndReturnError( 371 RunFunctionAndReturnError(
244 new CreateWindowFunction(), 372 new CreateWindowFunction(),
245 kArgs, 373 kArgs,
246 browser()), 374 browser()),
247 extension_tabs_module_constants::kIncognitoModeIsDisabled)); 375 keys::kIncognitoModeIsDisabled));
248 376
249 // Run in incognito window. 377 // Run in incognito window.
250 EXPECT_TRUE(MatchPattern( 378 EXPECT_TRUE(MatchPattern(
251 RunFunctionAndReturnError( 379 RunFunctionAndReturnError(
252 new CreateWindowFunction(), 380 new CreateWindowFunction(),
253 kArgs, 381 kArgs,
254 incognito_browser), 382 incognito_browser),
255 extension_tabs_module_constants::kIncognitoModeIsDisabled)); 383 keys::kIncognitoModeIsDisabled));
256 } 384 }
257 385
258 #if defined(USE_AURA) 386 #if defined(USE_AURA)
259 // crbug.com/105173. 387 // crbug.com/105173.
260 #define MAYBE_InvalidUpdateWindowState DISABLED_InvalidUpdateWindowState 388 #define MAYBE_InvalidUpdateWindowState DISABLED_InvalidUpdateWindowState
261 #else 389 #else
262 #define MAYBE_InvalidUpdateWindowState InvalidUpdateWindowState 390 #define MAYBE_InvalidUpdateWindowState InvalidUpdateWindowState
263 #endif 391 #endif
264 392
265 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, MAYBE_InvalidUpdateWindowState) { 393 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, MAYBE_InvalidUpdateWindowState) {
266 static const char kArgsMinimizedWithFocus[] = 394 static const char kArgsMinimizedWithFocus[] =
267 "[%u, {\"state\": \"minimized\", \"focused\": true}]"; 395 "[%u, {\"state\": \"minimized\", \"focused\": true}]";
268 static const char kArgsMaximizedWithoutFocus[] = 396 static const char kArgsMaximizedWithoutFocus[] =
269 "[%u, {\"state\": \"maximized\", \"focused\": false}]"; 397 "[%u, {\"state\": \"maximized\", \"focused\": false}]";
270 static const char kArgsMinimizedWithBounds[] = 398 static const char kArgsMinimizedWithBounds[] =
271 "[%u, {\"state\": \"minimized\", \"width\": 500}]"; 399 "[%u, {\"state\": \"minimized\", \"width\": 500}]";
272 static const char kArgsMaximizedWithBounds[] = 400 static const char kArgsMaximizedWithBounds[] =
273 "[%u, {\"state\": \"maximized\", \"width\": 500}]"; 401 "[%u, {\"state\": \"maximized\", \"width\": 500}]";
274 int window_id = ExtensionTabUtil::GetWindowId(browser()); 402 int window_id = ExtensionTabUtil::GetWindowId(browser());
275 403
276 EXPECT_TRUE(MatchPattern( 404 EXPECT_TRUE(MatchPattern(
277 RunFunctionAndReturnError( 405 RunFunctionAndReturnError(
278 new UpdateWindowFunction(), 406 new UpdateWindowFunction(),
279 base::StringPrintf(kArgsMinimizedWithFocus, window_id), 407 base::StringPrintf(kArgsMinimizedWithFocus, window_id),
280 browser()), 408 browser()),
281 extension_tabs_module_constants::kInvalidWindowStateError)); 409 keys::kInvalidWindowStateError));
282 410
283 EXPECT_TRUE(MatchPattern( 411 EXPECT_TRUE(MatchPattern(
284 RunFunctionAndReturnError( 412 RunFunctionAndReturnError(
285 new UpdateWindowFunction(), 413 new UpdateWindowFunction(),
286 base::StringPrintf(kArgsMaximizedWithoutFocus, window_id), 414 base::StringPrintf(kArgsMaximizedWithoutFocus, window_id),
287 browser()), 415 browser()),
288 extension_tabs_module_constants::kInvalidWindowStateError)); 416 keys::kInvalidWindowStateError));
289 417
290 EXPECT_TRUE(MatchPattern( 418 EXPECT_TRUE(MatchPattern(
291 RunFunctionAndReturnError( 419 RunFunctionAndReturnError(
292 new UpdateWindowFunction(), 420 new UpdateWindowFunction(),
293 base::StringPrintf(kArgsMinimizedWithBounds, window_id), 421 base::StringPrintf(kArgsMinimizedWithBounds, window_id),
294 browser()), 422 browser()),
295 extension_tabs_module_constants::kInvalidWindowStateError)); 423 keys::kInvalidWindowStateError));
296 424
297 EXPECT_TRUE(MatchPattern( 425 EXPECT_TRUE(MatchPattern(
298 RunFunctionAndReturnError( 426 RunFunctionAndReturnError(
299 new UpdateWindowFunction(), 427 new UpdateWindowFunction(),
300 base::StringPrintf(kArgsMaximizedWithBounds, window_id), 428 base::StringPrintf(kArgsMaximizedWithBounds, window_id),
301 browser()), 429 browser()),
302 extension_tabs_module_constants::kInvalidWindowStateError)); 430 keys::kInvalidWindowStateError));
303 } 431 }
OLDNEW
« no previous file with comments | « chrome/browser/extensions/extension_tabs_module.cc ('k') | chrome/common/extensions/docs/samples.json » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698