OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/api/tabs/tabs.h" | 5 #include "chrome/browser/extensions/api/tabs/tabs.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" |
(...skipping 18 matching lines...) Expand all Loading... |
29 namespace { | 29 namespace { |
30 | 30 |
31 class ExtensionTabsTest : public InProcessBrowserTest { | 31 class ExtensionTabsTest : public InProcessBrowserTest { |
32 }; | 32 }; |
33 } | 33 } |
34 | 34 |
35 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, GetWindow) { | 35 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, GetWindow) { |
36 int window_id = ExtensionTabUtil::GetWindowId(browser()); | 36 int window_id = ExtensionTabUtil::GetWindowId(browser()); |
37 | 37 |
38 // Invalid window ID error. | 38 // Invalid window ID error. |
| 39 scoped_refptr<GetWindowFunction> function = new GetWindowFunction(); |
39 EXPECT_TRUE(MatchPattern( | 40 EXPECT_TRUE(MatchPattern( |
40 utils::RunFunctionAndReturnError( | 41 utils::RunFunctionAndReturnError( |
41 new GetWindowFunction(), | 42 function.get(), |
42 base::StringPrintf("[%u]", window_id + 1), | 43 base::StringPrintf("[%u]", window_id + 1), |
43 browser()), | 44 browser()), |
44 keys::kWindowNotFoundError)); | 45 keys::kWindowNotFoundError)); |
45 | 46 |
46 // Basic window details. | 47 // Basic window details. |
47 gfx::Rect bounds; | 48 gfx::Rect bounds; |
48 if (browser()->window()->IsMinimized()) | 49 if (browser()->window()->IsMinimized()) |
49 bounds = browser()->window()->GetRestoredBounds(); | 50 bounds = browser()->window()->GetRestoredBounds(); |
50 else | 51 else |
51 bounds = browser()->window()->GetBounds(); | 52 bounds = browser()->window()->GetBounds(); |
52 | 53 |
| 54 function = new GetWindowFunction(); |
53 scoped_ptr<base::DictionaryValue> result(utils::ToDictionary( | 55 scoped_ptr<base::DictionaryValue> result(utils::ToDictionary( |
54 utils::RunFunctionAndReturnSingleResult( | 56 utils::RunFunctionAndReturnSingleResult( |
55 new GetWindowFunction(), | 57 function.get(), |
56 base::StringPrintf("[%u]", window_id), | 58 base::StringPrintf("[%u]", window_id), |
57 browser()))); | 59 browser()))); |
58 EXPECT_EQ(window_id, utils::GetInteger(result.get(), "id")); | 60 EXPECT_EQ(window_id, utils::GetInteger(result.get(), "id")); |
59 EXPECT_FALSE(utils::GetBoolean(result.get(), "incognito")); | 61 EXPECT_FALSE(utils::GetBoolean(result.get(), "incognito")); |
60 EXPECT_EQ("normal", utils::GetString(result.get(), "type")); | 62 EXPECT_EQ("normal", utils::GetString(result.get(), "type")); |
61 EXPECT_EQ(bounds.x(), utils::GetInteger(result.get(), "left")); | 63 EXPECT_EQ(bounds.x(), utils::GetInteger(result.get(), "left")); |
62 EXPECT_EQ(bounds.y(), utils::GetInteger(result.get(), "top")); | 64 EXPECT_EQ(bounds.y(), utils::GetInteger(result.get(), "top")); |
63 EXPECT_EQ(bounds.width(), utils::GetInteger(result.get(), "width")); | 65 EXPECT_EQ(bounds.width(), utils::GetInteger(result.get(), "width")); |
64 EXPECT_EQ(bounds.height(), utils::GetInteger(result.get(), "height")); | 66 EXPECT_EQ(bounds.height(), utils::GetInteger(result.get(), "height")); |
65 | 67 |
66 // With "populate" enabled. | 68 // With "populate" enabled. |
| 69 function = new GetWindowFunction(); |
67 result.reset(utils::ToDictionary( | 70 result.reset(utils::ToDictionary( |
68 utils::RunFunctionAndReturnSingleResult( | 71 utils::RunFunctionAndReturnSingleResult( |
69 new GetWindowFunction(), | 72 function.get(), |
70 base::StringPrintf("[%u, {\"populate\": true}]", window_id), | 73 base::StringPrintf("[%u, {\"populate\": true}]", window_id), |
71 browser()))); | 74 browser()))); |
72 | 75 |
73 EXPECT_EQ(window_id, utils::GetInteger(result.get(), "id")); | 76 EXPECT_EQ(window_id, utils::GetInteger(result.get(), "id")); |
74 // "populate" was enabled so tabs should be populated. | 77 // "populate" was enabled so tabs should be populated. |
75 ListValue* tabs = NULL; | 78 ListValue* tabs = NULL; |
76 EXPECT_TRUE(result.get()->GetList(keys::kTabsKey, &tabs)); | 79 EXPECT_TRUE(result.get()->GetList(keys::kTabsKey, &tabs)); |
77 | 80 |
78 // TODO(aa): Can't assume window is focused. On mac, calling Activate() from a | 81 // TODO(aa): Can't assume window is focused. On mac, calling Activate() from a |
79 // browser test doesn't seem to do anything, so can't test the opposite | 82 // browser test doesn't seem to do anything, so can't test the opposite |
80 // either. | 83 // either. |
81 EXPECT_EQ(browser()->window()->IsActive(), | 84 EXPECT_EQ(browser()->window()->IsActive(), |
82 utils::GetBoolean(result.get(), "focused")); | 85 utils::GetBoolean(result.get(), "focused")); |
83 | 86 |
84 // TODO(aa): Minimized and maximized dimensions. Is there a way to set | 87 // TODO(aa): Minimized and maximized dimensions. Is there a way to set |
85 // minimize/maximize programmatically? | 88 // minimize/maximize programmatically? |
86 | 89 |
87 // Popup. | 90 // Popup. |
88 Browser* popup_browser = new Browser( | 91 Browser* popup_browser = new Browser( |
89 Browser::CreateParams(Browser::TYPE_POPUP, browser()->profile())); | 92 Browser::CreateParams(Browser::TYPE_POPUP, browser()->profile())); |
| 93 function = new GetWindowFunction(); |
90 result.reset(utils::ToDictionary( | 94 result.reset(utils::ToDictionary( |
91 utils::RunFunctionAndReturnSingleResult( | 95 utils::RunFunctionAndReturnSingleResult( |
92 new GetWindowFunction(), | 96 function.get(), |
93 base::StringPrintf( | 97 base::StringPrintf( |
94 "[%u]", ExtensionTabUtil::GetWindowId(popup_browser)), | 98 "[%u]", ExtensionTabUtil::GetWindowId(popup_browser)), |
95 browser()))); | 99 browser()))); |
96 EXPECT_EQ("popup", utils::GetString(result.get(), "type")); | 100 EXPECT_EQ("popup", utils::GetString(result.get(), "type")); |
97 | 101 |
98 // Panel. | 102 // Panel. |
99 Browser* panel_browser = new Browser( | 103 Browser* panel_browser = new Browser( |
100 Browser::CreateParams(Browser::TYPE_PANEL, browser()->profile())); | 104 Browser::CreateParams(Browser::TYPE_PANEL, browser()->profile())); |
| 105 function = new GetWindowFunction(); |
101 result.reset(utils::ToDictionary( | 106 result.reset(utils::ToDictionary( |
102 utils::RunFunctionAndReturnSingleResult( | 107 utils::RunFunctionAndReturnSingleResult( |
103 new GetWindowFunction(), | 108 function.get(), |
104 base::StringPrintf( | 109 base::StringPrintf( |
105 "[%u]", ExtensionTabUtil::GetWindowId(panel_browser)), | 110 "[%u]", ExtensionTabUtil::GetWindowId(panel_browser)), |
106 browser()))); | 111 browser()))); |
107 EXPECT_EQ("panel", utils::GetString(result.get(), "type")); | 112 EXPECT_EQ("panel", utils::GetString(result.get(), "type")); |
108 | 113 |
109 // Incognito. | 114 // Incognito. |
110 Browser* incognito_browser = CreateIncognitoBrowser(); | 115 Browser* incognito_browser = CreateIncognitoBrowser(); |
111 int incognito_window_id = ExtensionTabUtil::GetWindowId(incognito_browser); | 116 int incognito_window_id = ExtensionTabUtil::GetWindowId(incognito_browser); |
112 | 117 |
113 // Without "include_incognito". | 118 // Without "include_incognito". |
| 119 function = new GetWindowFunction(); |
114 EXPECT_TRUE(MatchPattern( | 120 EXPECT_TRUE(MatchPattern( |
115 utils::RunFunctionAndReturnError( | 121 utils::RunFunctionAndReturnError( |
116 new GetWindowFunction(), | 122 function.get(), |
117 base::StringPrintf("[%u]", incognito_window_id), | 123 base::StringPrintf("[%u]", incognito_window_id), |
118 browser()), | 124 browser()), |
119 keys::kWindowNotFoundError)); | 125 keys::kWindowNotFoundError)); |
120 | 126 |
121 // With "include_incognito". | 127 // With "include_incognito". |
| 128 function = new GetWindowFunction(); |
122 result.reset(utils::ToDictionary( | 129 result.reset(utils::ToDictionary( |
123 utils::RunFunctionAndReturnSingleResult( | 130 utils::RunFunctionAndReturnSingleResult( |
124 new GetWindowFunction(), | 131 function.get(), |
125 base::StringPrintf("[%u]", incognito_window_id), | 132 base::StringPrintf("[%u]", incognito_window_id), |
126 browser(), | 133 browser(), |
127 utils::INCLUDE_INCOGNITO))); | 134 utils::INCLUDE_INCOGNITO))); |
128 EXPECT_TRUE(utils::GetBoolean(result.get(), "incognito")); | 135 EXPECT_TRUE(utils::GetBoolean(result.get(), "incognito")); |
129 } | 136 } |
130 | 137 |
131 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, GetCurrentWindow) { | 138 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, GetCurrentWindow) { |
132 int window_id = ExtensionTabUtil::GetWindowId(browser()); | 139 int window_id = ExtensionTabUtil::GetWindowId(browser()); |
133 Browser* new_browser = CreateBrowser(browser()->profile()); | 140 Browser* new_browser = CreateBrowser(browser()->profile()); |
134 int new_id = ExtensionTabUtil::GetWindowId(new_browser); | 141 int new_id = ExtensionTabUtil::GetWindowId(new_browser); |
135 | 142 |
136 // Get the current window using new_browser. | 143 // Get the current window using new_browser. |
| 144 scoped_refptr<GetCurrentWindowFunction> function = |
| 145 new GetCurrentWindowFunction(); |
137 scoped_ptr<base::DictionaryValue> result(utils::ToDictionary( | 146 scoped_ptr<base::DictionaryValue> result(utils::ToDictionary( |
138 utils::RunFunctionAndReturnSingleResult( | 147 utils::RunFunctionAndReturnSingleResult(function.get(), |
139 new GetCurrentWindowFunction(), | 148 "[]", |
140 "[]", | 149 new_browser))); |
141 new_browser))); | |
142 | 150 |
143 // The id should match the window id of the browser instance that was passed | 151 // The id should match the window id of the browser instance that was passed |
144 // to RunFunctionAndReturnSingleResult. | 152 // to RunFunctionAndReturnSingleResult. |
145 EXPECT_EQ(new_id, utils::GetInteger(result.get(), "id")); | 153 EXPECT_EQ(new_id, utils::GetInteger(result.get(), "id")); |
146 ListValue* tabs = NULL; | 154 ListValue* tabs = NULL; |
147 EXPECT_FALSE(result.get()->GetList(keys::kTabsKey, &tabs)); | 155 EXPECT_FALSE(result.get()->GetList(keys::kTabsKey, &tabs)); |
148 | 156 |
149 // Get the current window using the old window and make the tabs populated. | 157 // Get the current window using the old window and make the tabs populated. |
| 158 function = new GetCurrentWindowFunction(); |
150 result.reset(utils::ToDictionary( | 159 result.reset(utils::ToDictionary( |
151 utils::RunFunctionAndReturnSingleResult( | 160 utils::RunFunctionAndReturnSingleResult(function.get(), |
152 new GetCurrentWindowFunction(), | 161 "[{\"populate\": true}]", |
153 "[{\"populate\": true}]", | 162 browser()))); |
154 browser()))); | |
155 | 163 |
156 // The id should match the window id of the browser instance that was passed | 164 // The id should match the window id of the browser instance that was passed |
157 // to RunFunctionAndReturnSingleResult. | 165 // to RunFunctionAndReturnSingleResult. |
158 EXPECT_EQ(window_id, utils::GetInteger(result.get(), "id")); | 166 EXPECT_EQ(window_id, utils::GetInteger(result.get(), "id")); |
159 // "populate" was enabled so tabs should be populated. | 167 // "populate" was enabled so tabs should be populated. |
160 EXPECT_TRUE(result.get()->GetList(keys::kTabsKey, &tabs)); | 168 EXPECT_TRUE(result.get()->GetList(keys::kTabsKey, &tabs)); |
161 } | 169 } |
162 | 170 |
163 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, GetLastFocusedWindow) { | 171 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, GetLastFocusedWindow) { |
164 // Create a new window which making it the "last focused" window. | 172 // Create a new window which making it the "last focused" window. |
165 // Note that "last focused" means the "top" most window. | 173 // Note that "last focused" means the "top" most window. |
166 Browser* new_browser = CreateBrowser(browser()->profile()); | 174 Browser* new_browser = CreateBrowser(browser()->profile()); |
167 int focused_window_id = ExtensionTabUtil::GetWindowId(new_browser); | 175 int focused_window_id = ExtensionTabUtil::GetWindowId(new_browser); |
168 | 176 |
| 177 scoped_refptr<GetLastFocusedWindowFunction> function = |
| 178 new GetLastFocusedWindowFunction(); |
169 scoped_ptr<base::DictionaryValue> result(utils::ToDictionary( | 179 scoped_ptr<base::DictionaryValue> result(utils::ToDictionary( |
170 utils::RunFunctionAndReturnSingleResult( | 180 utils::RunFunctionAndReturnSingleResult(function.get(), |
171 new GetLastFocusedWindowFunction(), | 181 "[]", |
172 "[]", | 182 new_browser))); |
173 new_browser))); | |
174 | 183 |
175 // The id should always match the last focused window and does not depend | 184 // The id should always match the last focused window and does not depend |
176 // on what was passed to RunFunctionAndReturnSingleResult. | 185 // on what was passed to RunFunctionAndReturnSingleResult. |
177 EXPECT_EQ(focused_window_id, utils::GetInteger(result.get(), "id")); | 186 EXPECT_EQ(focused_window_id, utils::GetInteger(result.get(), "id")); |
178 ListValue* tabs = NULL; | 187 ListValue* tabs = NULL; |
179 EXPECT_FALSE(result.get()->GetList(keys::kTabsKey, &tabs)); | 188 EXPECT_FALSE(result.get()->GetList(keys::kTabsKey, &tabs)); |
180 | 189 |
| 190 function = new GetLastFocusedWindowFunction(); |
181 result.reset(utils::ToDictionary( | 191 result.reset(utils::ToDictionary( |
182 utils::RunFunctionAndReturnSingleResult( | 192 utils::RunFunctionAndReturnSingleResult(function.get(), |
183 new GetLastFocusedWindowFunction(), | 193 "[{\"populate\": true}]", |
184 "[{\"populate\": true}]", | 194 browser()))); |
185 browser()))); | |
186 | 195 |
187 // The id should always match the last focused window and does not depend | 196 // The id should always match the last focused window and does not depend |
188 // on what was passed to RunFunctionAndReturnSingleResult. | 197 // on what was passed to RunFunctionAndReturnSingleResult. |
189 EXPECT_EQ(focused_window_id, utils::GetInteger(result.get(), "id")); | 198 EXPECT_EQ(focused_window_id, utils::GetInteger(result.get(), "id")); |
190 // "populate" was enabled so tabs should be populated. | 199 // "populate" was enabled so tabs should be populated. |
191 EXPECT_TRUE(result.get()->GetList(keys::kTabsKey, &tabs)); | 200 EXPECT_TRUE(result.get()->GetList(keys::kTabsKey, &tabs)); |
192 } | 201 } |
193 | 202 |
194 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, GetAllWindows) { | 203 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, GetAllWindows) { |
195 const size_t NUM_WINDOWS = 5; | 204 const size_t NUM_WINDOWS = 5; |
196 std::set<int> window_ids; | 205 std::set<int> window_ids; |
197 std::set<int> result_ids; | 206 std::set<int> result_ids; |
198 window_ids.insert(ExtensionTabUtil::GetWindowId(browser())); | 207 window_ids.insert(ExtensionTabUtil::GetWindowId(browser())); |
199 | 208 |
200 for (size_t i = 0; i < NUM_WINDOWS - 1; ++i) { | 209 for (size_t i = 0; i < NUM_WINDOWS - 1; ++i) { |
201 Browser* new_browser = CreateBrowser(browser()->profile()); | 210 Browser* new_browser = CreateBrowser(browser()->profile()); |
202 window_ids.insert(ExtensionTabUtil::GetWindowId(new_browser)); | 211 window_ids.insert(ExtensionTabUtil::GetWindowId(new_browser)); |
203 } | 212 } |
204 | 213 |
| 214 scoped_refptr<GetAllWindowsFunction> function = new GetAllWindowsFunction(); |
205 scoped_ptr<base::ListValue> result(utils::ToList( | 215 scoped_ptr<base::ListValue> result(utils::ToList( |
206 utils::RunFunctionAndReturnSingleResult( | 216 utils::RunFunctionAndReturnSingleResult(function.get(), |
207 new GetAllWindowsFunction(), | 217 "[]", |
208 "[]", | 218 browser()))); |
209 browser()))); | |
210 | 219 |
211 ListValue* windows = result.get(); | 220 ListValue* windows = result.get(); |
212 EXPECT_EQ(NUM_WINDOWS, windows->GetSize()); | 221 EXPECT_EQ(NUM_WINDOWS, windows->GetSize()); |
213 for (size_t i = 0; i < NUM_WINDOWS; ++i) { | 222 for (size_t i = 0; i < NUM_WINDOWS; ++i) { |
214 DictionaryValue* result_window = NULL; | 223 DictionaryValue* result_window = NULL; |
215 EXPECT_TRUE(windows->GetDictionary(i, &result_window)); | 224 EXPECT_TRUE(windows->GetDictionary(i, &result_window)); |
216 result_ids.insert(utils::GetInteger(result_window, "id")); | 225 result_ids.insert(utils::GetInteger(result_window, "id")); |
217 | 226 |
218 // "populate" was not passed in so tabs are not populated. | 227 // "populate" was not passed in so tabs are not populated. |
219 ListValue* tabs = NULL; | 228 ListValue* tabs = NULL; |
220 EXPECT_FALSE(result_window->GetList(keys::kTabsKey, &tabs)); | 229 EXPECT_FALSE(result_window->GetList(keys::kTabsKey, &tabs)); |
221 } | 230 } |
222 // The returned ids should contain all the current browser instance ids. | 231 // The returned ids should contain all the current browser instance ids. |
223 EXPECT_EQ(window_ids, result_ids); | 232 EXPECT_EQ(window_ids, result_ids); |
224 | 233 |
225 result_ids.clear(); | 234 result_ids.clear(); |
| 235 function = new GetAllWindowsFunction(); |
226 result.reset(utils::ToList( | 236 result.reset(utils::ToList( |
227 utils::RunFunctionAndReturnSingleResult( | 237 utils::RunFunctionAndReturnSingleResult(function.get(), |
228 new GetAllWindowsFunction(), | 238 "[{\"populate\": true}]", |
229 "[{\"populate\": true}]", | 239 browser()))); |
230 browser()))); | |
231 | 240 |
232 windows = result.get(); | 241 windows = result.get(); |
233 EXPECT_EQ(NUM_WINDOWS, windows->GetSize()); | 242 EXPECT_EQ(NUM_WINDOWS, windows->GetSize()); |
234 for (size_t i = 0; i < windows->GetSize(); ++i) { | 243 for (size_t i = 0; i < windows->GetSize(); ++i) { |
235 DictionaryValue* result_window = NULL; | 244 DictionaryValue* result_window = NULL; |
236 EXPECT_TRUE(windows->GetDictionary(i, &result_window)); | 245 EXPECT_TRUE(windows->GetDictionary(i, &result_window)); |
237 result_ids.insert(utils::GetInteger(result_window, "id")); | 246 result_ids.insert(utils::GetInteger(result_window, "id")); |
238 | 247 |
239 // "populate" was enabled so tabs should be populated. | 248 // "populate" was enabled so tabs should be populated. |
240 ListValue* tabs = NULL; | 249 ListValue* tabs = NULL; |
(...skipping 21 matching lines...) Expand all Loading... |
262 } | 271 } |
263 | 272 |
264 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, | 273 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, |
265 DefaultToIncognitoWhenItIsForced) { | 274 DefaultToIncognitoWhenItIsForced) { |
266 static const char kArgsWithoutExplicitIncognitoParam[] = | 275 static const char kArgsWithoutExplicitIncognitoParam[] = |
267 "[{\"url\": \"about:blank\"}]"; | 276 "[{\"url\": \"about:blank\"}]"; |
268 // Force Incognito mode. | 277 // Force Incognito mode. |
269 IncognitoModePrefs::SetAvailability(browser()->profile()->GetPrefs(), | 278 IncognitoModePrefs::SetAvailability(browser()->profile()->GetPrefs(), |
270 IncognitoModePrefs::FORCED); | 279 IncognitoModePrefs::FORCED); |
271 // Run without an explicit "incognito" param. | 280 // Run without an explicit "incognito" param. |
| 281 scoped_refptr<CreateWindowFunction> function = new CreateWindowFunction(); |
272 scoped_ptr<base::DictionaryValue> result(utils::ToDictionary( | 282 scoped_ptr<base::DictionaryValue> result(utils::ToDictionary( |
273 utils::RunFunctionAndReturnSingleResult( | 283 utils::RunFunctionAndReturnSingleResult( |
274 new CreateWindowFunction(), | 284 function.get(), |
275 kArgsWithoutExplicitIncognitoParam, | 285 kArgsWithoutExplicitIncognitoParam, |
276 browser(), | 286 browser(), |
277 utils::INCLUDE_INCOGNITO))); | 287 utils::INCLUDE_INCOGNITO))); |
278 | 288 |
279 // Make sure it is a new(different) window. | 289 // Make sure it is a new(different) window. |
280 EXPECT_NE(ExtensionTabUtil::GetWindowId(browser()), | 290 EXPECT_NE(ExtensionTabUtil::GetWindowId(browser()), |
281 utils::GetInteger(result.get(), "id")); | 291 utils::GetInteger(result.get(), "id")); |
282 // ... and it is incognito. | 292 // ... and it is incognito. |
283 EXPECT_TRUE(utils::GetBoolean(result.get(), "incognito")); | 293 EXPECT_TRUE(utils::GetBoolean(result.get(), "incognito")); |
284 | 294 |
285 // Now try creating a window from incognito window. | 295 // Now try creating a window from incognito window. |
286 Browser* incognito_browser = CreateIncognitoBrowser(); | 296 Browser* incognito_browser = CreateIncognitoBrowser(); |
287 // Run without an explicit "incognito" param. | 297 // Run without an explicit "incognito" param. |
| 298 function = new CreateWindowFunction(); |
288 result.reset(utils::ToDictionary( | 299 result.reset(utils::ToDictionary( |
289 utils::RunFunctionAndReturnSingleResult( | 300 utils::RunFunctionAndReturnSingleResult( |
290 new CreateWindowFunction(), | 301 function.get(), |
291 kArgsWithoutExplicitIncognitoParam, | 302 kArgsWithoutExplicitIncognitoParam, |
292 incognito_browser, | 303 incognito_browser, |
293 utils::INCLUDE_INCOGNITO))); | 304 utils::INCLUDE_INCOGNITO))); |
294 // Make sure it is a new(different) window. | 305 // Make sure it is a new(different) window. |
295 EXPECT_NE(ExtensionTabUtil::GetWindowId(incognito_browser), | 306 EXPECT_NE(ExtensionTabUtil::GetWindowId(incognito_browser), |
296 utils::GetInteger(result.get(), "id")); | 307 utils::GetInteger(result.get(), "id")); |
297 // ... and it is incognito. | 308 // ... and it is incognito. |
298 EXPECT_TRUE(utils::GetBoolean(result.get(), "incognito")); | 309 EXPECT_TRUE(utils::GetBoolean(result.get(), "incognito")); |
299 } | 310 } |
300 | 311 |
301 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, | 312 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, |
302 DefaultToIncognitoWhenItIsForcedAndNoArgs) { | 313 DefaultToIncognitoWhenItIsForcedAndNoArgs) { |
303 static const char kEmptyArgs[] = "[]"; | 314 static const char kEmptyArgs[] = "[]"; |
304 // Force Incognito mode. | 315 // Force Incognito mode. |
305 IncognitoModePrefs::SetAvailability(browser()->profile()->GetPrefs(), | 316 IncognitoModePrefs::SetAvailability(browser()->profile()->GetPrefs(), |
306 IncognitoModePrefs::FORCED); | 317 IncognitoModePrefs::FORCED); |
307 // Run without an explicit "incognito" param. | 318 // Run without an explicit "incognito" param. |
| 319 scoped_refptr<CreateWindowFunction> function = new CreateWindowFunction(); |
308 scoped_ptr<base::DictionaryValue> result(utils::ToDictionary( | 320 scoped_ptr<base::DictionaryValue> result(utils::ToDictionary( |
309 utils::RunFunctionAndReturnSingleResult( | 321 utils::RunFunctionAndReturnSingleResult(function.get(), |
310 new CreateWindowFunction(), | 322 kEmptyArgs, |
311 kEmptyArgs, | 323 browser(), |
312 browser(), | 324 utils::INCLUDE_INCOGNITO))); |
313 utils::INCLUDE_INCOGNITO))); | |
314 | 325 |
315 // Make sure it is a new(different) window. | 326 // Make sure it is a new(different) window. |
316 EXPECT_NE(ExtensionTabUtil::GetWindowId(browser()), | 327 EXPECT_NE(ExtensionTabUtil::GetWindowId(browser()), |
317 utils::GetInteger(result.get(), "id")); | 328 utils::GetInteger(result.get(), "id")); |
318 // ... and it is incognito. | 329 // ... and it is incognito. |
319 EXPECT_TRUE(utils::GetBoolean(result.get(), "incognito")); | 330 EXPECT_TRUE(utils::GetBoolean(result.get(), "incognito")); |
320 | 331 |
321 // Now try creating a window from incognito window. | 332 // Now try creating a window from incognito window. |
322 Browser* incognito_browser = CreateIncognitoBrowser(); | 333 Browser* incognito_browser = CreateIncognitoBrowser(); |
323 // Run without an explicit "incognito" param. | 334 // Run without an explicit "incognito" param. |
| 335 function = new CreateWindowFunction(); |
324 result.reset(utils::ToDictionary( | 336 result.reset(utils::ToDictionary( |
325 utils::RunFunctionAndReturnSingleResult( | 337 utils::RunFunctionAndReturnSingleResult(function.get(), |
326 new CreateWindowFunction(), | 338 kEmptyArgs, |
327 kEmptyArgs, | 339 incognito_browser, |
328 incognito_browser, | 340 utils::INCLUDE_INCOGNITO))); |
329 utils::INCLUDE_INCOGNITO))); | |
330 // Make sure it is a new(different) window. | 341 // Make sure it is a new(different) window. |
331 EXPECT_NE(ExtensionTabUtil::GetWindowId(incognito_browser), | 342 EXPECT_NE(ExtensionTabUtil::GetWindowId(incognito_browser), |
332 utils::GetInteger(result.get(), "id")); | 343 utils::GetInteger(result.get(), "id")); |
333 // ... and it is incognito. | 344 // ... and it is incognito. |
334 EXPECT_TRUE(utils::GetBoolean(result.get(), "incognito")); | 345 EXPECT_TRUE(utils::GetBoolean(result.get(), "incognito")); |
335 } | 346 } |
336 | 347 |
337 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, | 348 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, |
338 DontCreateNormalWindowWhenIncognitoForced) { | 349 DontCreateNormalWindowWhenIncognitoForced) { |
339 static const char kArgsWithExplicitIncognitoParam[] = | 350 static const char kArgsWithExplicitIncognitoParam[] = |
340 "[{\"url\": \"about:blank\", \"incognito\": false }]"; | 351 "[{\"url\": \"about:blank\", \"incognito\": false }]"; |
341 // Force Incognito mode. | 352 // Force Incognito mode. |
342 IncognitoModePrefs::SetAvailability(browser()->profile()->GetPrefs(), | 353 IncognitoModePrefs::SetAvailability(browser()->profile()->GetPrefs(), |
343 IncognitoModePrefs::FORCED); | 354 IncognitoModePrefs::FORCED); |
344 | 355 |
345 // Run with an explicit "incognito" param. | 356 // Run with an explicit "incognito" param. |
| 357 scoped_refptr<CreateWindowFunction> function = new CreateWindowFunction(); |
346 EXPECT_TRUE(MatchPattern( | 358 EXPECT_TRUE(MatchPattern( |
347 utils::RunFunctionAndReturnError( | 359 utils::RunFunctionAndReturnError(function.get(), |
348 new CreateWindowFunction(), | 360 kArgsWithExplicitIncognitoParam, |
349 kArgsWithExplicitIncognitoParam, | 361 browser()), |
350 browser()), | |
351 keys::kIncognitoModeIsForced)); | 362 keys::kIncognitoModeIsForced)); |
352 | 363 |
353 // Now try opening a normal window from incognito window. | 364 // Now try opening a normal window from incognito window. |
354 Browser* incognito_browser = CreateIncognitoBrowser(); | 365 Browser* incognito_browser = CreateIncognitoBrowser(); |
355 // Run with an explicit "incognito" param. | 366 // Run with an explicit "incognito" param. |
| 367 function = new CreateWindowFunction(); |
356 EXPECT_TRUE(MatchPattern( | 368 EXPECT_TRUE(MatchPattern( |
357 utils::RunFunctionAndReturnError( | 369 utils::RunFunctionAndReturnError(function.get(), |
358 new CreateWindowFunction(), | 370 kArgsWithExplicitIncognitoParam, |
359 kArgsWithExplicitIncognitoParam, | 371 incognito_browser), |
360 incognito_browser), | |
361 keys::kIncognitoModeIsForced)); | 372 keys::kIncognitoModeIsForced)); |
362 } | 373 } |
363 | 374 |
364 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, | 375 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, |
365 DontCreateIncognitoWindowWhenIncognitoDisabled) { | 376 DontCreateIncognitoWindowWhenIncognitoDisabled) { |
366 static const char kArgs[] = | 377 static const char kArgs[] = |
367 "[{\"url\": \"about:blank\", \"incognito\": true }]"; | 378 "[{\"url\": \"about:blank\", \"incognito\": true }]"; |
368 | 379 |
369 Browser* incognito_browser = CreateIncognitoBrowser(); | 380 Browser* incognito_browser = CreateIncognitoBrowser(); |
370 // Disable Incognito mode. | 381 // Disable Incognito mode. |
371 IncognitoModePrefs::SetAvailability(browser()->profile()->GetPrefs(), | 382 IncognitoModePrefs::SetAvailability(browser()->profile()->GetPrefs(), |
372 IncognitoModePrefs::DISABLED); | 383 IncognitoModePrefs::DISABLED); |
373 // Run in normal window. | 384 // Run in normal window. |
| 385 scoped_refptr<CreateWindowFunction> function = new CreateWindowFunction(); |
374 EXPECT_TRUE(MatchPattern( | 386 EXPECT_TRUE(MatchPattern( |
375 utils::RunFunctionAndReturnError( | 387 utils::RunFunctionAndReturnError(function.get(), |
376 new CreateWindowFunction(), | 388 kArgs, |
377 kArgs, | 389 browser()), |
378 browser()), | |
379 keys::kIncognitoModeIsDisabled)); | 390 keys::kIncognitoModeIsDisabled)); |
380 | 391 |
381 // Run in incognito window. | 392 // Run in incognito window. |
| 393 function = new CreateWindowFunction(); |
382 EXPECT_TRUE(MatchPattern( | 394 EXPECT_TRUE(MatchPattern( |
383 utils::RunFunctionAndReturnError( | 395 utils::RunFunctionAndReturnError(function.get(), |
384 new CreateWindowFunction(), | 396 kArgs, |
385 kArgs, | 397 incognito_browser), |
386 incognito_browser), | |
387 keys::kIncognitoModeIsDisabled)); | 398 keys::kIncognitoModeIsDisabled)); |
388 } | 399 } |
389 | 400 |
390 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, QueryCurrentWindowTabs) { | 401 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, QueryCurrentWindowTabs) { |
391 const size_t kExtraWindows = 3; | 402 const size_t kExtraWindows = 3; |
392 for (size_t i = 0; i < kExtraWindows; ++i) | 403 for (size_t i = 0; i < kExtraWindows; ++i) |
393 CreateBrowser(browser()->profile()); | 404 CreateBrowser(browser()->profile()); |
394 | 405 |
395 GURL url; | 406 GURL url; |
396 AddTabAtIndexToBrowser(browser(), 0, url, content::PAGE_TRANSITION_LINK); | 407 AddTabAtIndexToBrowser(browser(), 0, url, content::PAGE_TRANSITION_LINK); |
397 int window_id = ExtensionTabUtil::GetWindowId(browser()); | 408 int window_id = ExtensionTabUtil::GetWindowId(browser()); |
398 | 409 |
399 // Get tabs in the 'current' window called from non-focused browser. | 410 // Get tabs in the 'current' window called from non-focused browser. |
| 411 scoped_refptr<QueryTabsFunction> function = new QueryTabsFunction(); |
400 scoped_ptr<base::ListValue> result(utils::ToList( | 412 scoped_ptr<base::ListValue> result(utils::ToList( |
401 utils::RunFunctionAndReturnSingleResult( | 413 utils::RunFunctionAndReturnSingleResult(function.get(), |
402 new QueryTabsFunction(), | 414 "[{\"currentWindow\":true}]", |
403 "[{\"currentWindow\":true}]", | 415 browser()))); |
404 browser()))); | |
405 | 416 |
406 ListValue* result_tabs = result.get(); | 417 ListValue* result_tabs = result.get(); |
407 // We should have one initial tab and one added tab. | 418 // We should have one initial tab and one added tab. |
408 EXPECT_EQ(2u, result_tabs->GetSize()); | 419 EXPECT_EQ(2u, result_tabs->GetSize()); |
409 for (size_t i = 0; i < result_tabs->GetSize(); ++i) { | 420 for (size_t i = 0; i < result_tabs->GetSize(); ++i) { |
410 DictionaryValue* result_tab = NULL; | 421 DictionaryValue* result_tab = NULL; |
411 EXPECT_TRUE(result_tabs->GetDictionary(i, &result_tab)); | 422 EXPECT_TRUE(result_tabs->GetDictionary(i, &result_tab)); |
412 EXPECT_EQ(window_id, utils::GetInteger(result_tab, keys::kWindowIdKey)); | 423 EXPECT_EQ(window_id, utils::GetInteger(result_tab, keys::kWindowIdKey)); |
413 } | 424 } |
414 | 425 |
415 // Get tabs NOT in the 'current' window called from non-focused browser. | 426 // Get tabs NOT in the 'current' window called from non-focused browser. |
| 427 function = new QueryTabsFunction(); |
416 result.reset(utils::ToList( | 428 result.reset(utils::ToList( |
417 utils::RunFunctionAndReturnSingleResult( | 429 utils::RunFunctionAndReturnSingleResult(function.get(), |
418 new QueryTabsFunction(), | 430 "[{\"currentWindow\":false}]", |
419 "[{\"currentWindow\":false}]", | 431 browser()))); |
420 browser()))); | |
421 | 432 |
422 result_tabs = result.get(); | 433 result_tabs = result.get(); |
423 // We should have one tab for each extra window. | 434 // We should have one tab for each extra window. |
424 EXPECT_EQ(kExtraWindows, result_tabs->GetSize()); | 435 EXPECT_EQ(kExtraWindows, result_tabs->GetSize()); |
425 for (size_t i = 0; i < kExtraWindows; ++i) { | 436 for (size_t i = 0; i < kExtraWindows; ++i) { |
426 DictionaryValue* result_tab = NULL; | 437 DictionaryValue* result_tab = NULL; |
427 EXPECT_TRUE(result_tabs->GetDictionary(i, &result_tab)); | 438 EXPECT_TRUE(result_tabs->GetDictionary(i, &result_tab)); |
428 EXPECT_NE(window_id, utils::GetInteger(result_tab, keys::kWindowIdKey)); | 439 EXPECT_NE(window_id, utils::GetInteger(result_tab, keys::kWindowIdKey)); |
429 } | 440 } |
430 } | 441 } |
(...skipping 13 matching lines...) Expand all Loading... |
444 #endif | 455 #endif |
445 | 456 |
446 // Needed on Mac and Linux so that the BrowserWindow::IsActive calls work. | 457 // Needed on Mac and Linux so that the BrowserWindow::IsActive calls work. |
447 content::RunAllPendingInMessageLoop(); | 458 content::RunAllPendingInMessageLoop(); |
448 | 459 |
449 GURL url; | 460 GURL url; |
450 AddTabAtIndexToBrowser(focused_window, 0, url, content::PAGE_TRANSITION_LINK); | 461 AddTabAtIndexToBrowser(focused_window, 0, url, content::PAGE_TRANSITION_LINK); |
451 int focused_window_id = ExtensionTabUtil::GetWindowId(focused_window); | 462 int focused_window_id = ExtensionTabUtil::GetWindowId(focused_window); |
452 | 463 |
453 // Get tabs in the 'last focused' window called from non-focused browser. | 464 // Get tabs in the 'last focused' window called from non-focused browser. |
| 465 scoped_refptr<QueryTabsFunction> function = new QueryTabsFunction(); |
454 scoped_ptr<base::ListValue> result(utils::ToList( | 466 scoped_ptr<base::ListValue> result(utils::ToList( |
455 utils::RunFunctionAndReturnSingleResult( | 467 utils::RunFunctionAndReturnSingleResult(function.get(), |
456 new QueryTabsFunction(), | 468 "[{\"lastFocusedWindow\":true}]", |
457 "[{\"lastFocusedWindow\":true}]", | 469 browser()))); |
458 browser()))); | |
459 | 470 |
460 ListValue* result_tabs = result.get(); | 471 ListValue* result_tabs = result.get(); |
461 // We should have one initial tab and one added tab. | 472 // We should have one initial tab and one added tab. |
462 EXPECT_EQ(2u, result_tabs->GetSize()); | 473 EXPECT_EQ(2u, result_tabs->GetSize()); |
463 for (size_t i = 0; i < result_tabs->GetSize(); ++i) { | 474 for (size_t i = 0; i < result_tabs->GetSize(); ++i) { |
464 DictionaryValue* result_tab = NULL; | 475 DictionaryValue* result_tab = NULL; |
465 EXPECT_TRUE(result_tabs->GetDictionary(i, &result_tab)); | 476 EXPECT_TRUE(result_tabs->GetDictionary(i, &result_tab)); |
466 EXPECT_EQ(focused_window_id, utils::GetInteger(result_tab, | 477 EXPECT_EQ(focused_window_id, utils::GetInteger(result_tab, |
467 keys::kWindowIdKey)); | 478 keys::kWindowIdKey)); |
468 } | 479 } |
469 | 480 |
470 // Get tabs NOT in the 'last focused' window called from the focused browser. | 481 // Get tabs NOT in the 'last focused' window called from the focused browser. |
| 482 function = new QueryTabsFunction(); |
471 result.reset(utils::ToList( | 483 result.reset(utils::ToList( |
472 utils::RunFunctionAndReturnSingleResult( | 484 utils::RunFunctionAndReturnSingleResult(function.get(), |
473 new QueryTabsFunction(), | 485 "[{\"lastFocusedWindow\":false}]", |
474 "[{\"lastFocusedWindow\":false}]", | 486 browser()))); |
475 browser()))); | |
476 | 487 |
477 result_tabs = result.get(); | 488 result_tabs = result.get(); |
478 // We should get one tab for each extra window and one for the initial window. | 489 // We should get one tab for each extra window and one for the initial window. |
479 EXPECT_EQ(kExtraWindows + 1, result_tabs->GetSize()); | 490 EXPECT_EQ(kExtraWindows + 1, result_tabs->GetSize()); |
480 for (size_t i = 0; i < result_tabs->GetSize(); ++i) { | 491 for (size_t i = 0; i < result_tabs->GetSize(); ++i) { |
481 DictionaryValue* result_tab = NULL; | 492 DictionaryValue* result_tab = NULL; |
482 EXPECT_TRUE(result_tabs->GetDictionary(i, &result_tab)); | 493 EXPECT_TRUE(result_tabs->GetDictionary(i, &result_tab)); |
483 EXPECT_NE(focused_window_id, utils::GetInteger(result_tab, | 494 EXPECT_NE(focused_window_id, utils::GetInteger(result_tab, |
484 keys::kWindowIdKey)); | 495 keys::kWindowIdKey)); |
485 } | 496 } |
(...skipping 22 matching lines...) Expand all Loading... |
508 browser()))); | 519 browser()))); |
509 | 520 |
510 EXPECT_NE(window_id, utils::GetInteger(result.get(), "windowId")); | 521 EXPECT_NE(window_id, utils::GetInteger(result.get(), "windowId")); |
511 } | 522 } |
512 | 523 |
513 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, InvalidUpdateWindowState) { | 524 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, InvalidUpdateWindowState) { |
514 int window_id = ExtensionTabUtil::GetWindowId(browser()); | 525 int window_id = ExtensionTabUtil::GetWindowId(browser()); |
515 | 526 |
516 static const char kArgsMinimizedWithFocus[] = | 527 static const char kArgsMinimizedWithFocus[] = |
517 "[%u, {\"state\": \"minimized\", \"focused\": true}]"; | 528 "[%u, {\"state\": \"minimized\", \"focused\": true}]"; |
| 529 scoped_refptr<UpdateWindowFunction> function = new UpdateWindowFunction(); |
518 EXPECT_TRUE(MatchPattern( | 530 EXPECT_TRUE(MatchPattern( |
519 utils::RunFunctionAndReturnError( | 531 utils::RunFunctionAndReturnError( |
520 new UpdateWindowFunction(), | 532 function.get(), |
521 base::StringPrintf(kArgsMinimizedWithFocus, window_id), | 533 base::StringPrintf(kArgsMinimizedWithFocus, window_id), |
522 browser()), | 534 browser()), |
523 keys::kInvalidWindowStateError)); | 535 keys::kInvalidWindowStateError)); |
524 | 536 |
525 static const char kArgsMaximizedWithoutFocus[] = | 537 static const char kArgsMaximizedWithoutFocus[] = |
526 "[%u, {\"state\": \"maximized\", \"focused\": false}]"; | 538 "[%u, {\"state\": \"maximized\", \"focused\": false}]"; |
| 539 function = new UpdateWindowFunction(); |
527 EXPECT_TRUE(MatchPattern( | 540 EXPECT_TRUE(MatchPattern( |
528 utils::RunFunctionAndReturnError( | 541 utils::RunFunctionAndReturnError( |
529 new UpdateWindowFunction(), | 542 function.get(), |
530 base::StringPrintf(kArgsMaximizedWithoutFocus, window_id), | 543 base::StringPrintf(kArgsMaximizedWithoutFocus, window_id), |
531 browser()), | 544 browser()), |
532 keys::kInvalidWindowStateError)); | 545 keys::kInvalidWindowStateError)); |
533 | 546 |
534 static const char kArgsMinimizedWithBounds[] = | 547 static const char kArgsMinimizedWithBounds[] = |
535 "[%u, {\"state\": \"minimized\", \"width\": 500}]"; | 548 "[%u, {\"state\": \"minimized\", \"width\": 500}]"; |
| 549 function = new UpdateWindowFunction(); |
536 EXPECT_TRUE(MatchPattern( | 550 EXPECT_TRUE(MatchPattern( |
537 utils::RunFunctionAndReturnError( | 551 utils::RunFunctionAndReturnError( |
538 new UpdateWindowFunction(), | 552 function.get(), |
539 base::StringPrintf(kArgsMinimizedWithBounds, window_id), | 553 base::StringPrintf(kArgsMinimizedWithBounds, window_id), |
540 browser()), | 554 browser()), |
541 keys::kInvalidWindowStateError)); | 555 keys::kInvalidWindowStateError)); |
542 | 556 |
543 static const char kArgsMaximizedWithBounds[] = | 557 static const char kArgsMaximizedWithBounds[] = |
544 "[%u, {\"state\": \"maximized\", \"width\": 500}]"; | 558 "[%u, {\"state\": \"maximized\", \"width\": 500}]"; |
| 559 function = new UpdateWindowFunction(); |
545 EXPECT_TRUE(MatchPattern( | 560 EXPECT_TRUE(MatchPattern( |
546 utils::RunFunctionAndReturnError( | 561 utils::RunFunctionAndReturnError( |
547 new UpdateWindowFunction(), | 562 function.get(), |
548 base::StringPrintf(kArgsMaximizedWithBounds, window_id), | 563 base::StringPrintf(kArgsMaximizedWithBounds, window_id), |
549 browser()), | 564 browser()), |
550 keys::kInvalidWindowStateError)); | 565 keys::kInvalidWindowStateError)); |
551 } | 566 } |
OLD | NEW |