| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "base/strings/utf_string_conversions.h" | 5 #include "base/strings/utf_string_conversions.h" |
| 6 #include "chrome/browser/browser_process.h" |
| 6 #include "chrome/browser/extensions/api/tabs/tabs_api.h" | 7 #include "chrome/browser/extensions/api/tabs/tabs_api.h" |
| 8 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" |
| 7 #include "chrome/browser/extensions/extension_function_test_utils.h" | 9 #include "chrome/browser/extensions/extension_function_test_utils.h" |
| 8 #include "chrome/browser/extensions/extension_service_test_base.h" | 10 #include "chrome/browser/extensions/extension_service_test_base.h" |
| 9 #include "chrome/browser/extensions/extension_tab_util.h" | 11 #include "chrome/browser/extensions/extension_tab_util.h" |
| 12 #include "chrome/browser/memory/tab_manager.h" |
| 10 #include "chrome/browser/ui/browser.h" | 13 #include "chrome/browser/ui/browser.h" |
| 11 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 14 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 12 #include "chrome/test/base/test_browser_window.h" | 15 #include "chrome/test/base/test_browser_window.h" |
| 13 #include "content/public/browser/navigation_entry.h" | 16 #include "content/public/browser/navigation_entry.h" |
| 14 #include "content/public/test/web_contents_tester.h" | 17 #include "content/public/test/web_contents_tester.h" |
| 15 #include "extensions/common/extension_builder.h" | 18 #include "extensions/common/extension_builder.h" |
| 16 #include "extensions/common/test_util.h" | 19 #include "extensions/common/test_util.h" |
| 17 | 20 |
| 21 using memory::TabManager; |
| 22 using content::WebContents; |
| 23 |
| 18 namespace extensions { | 24 namespace extensions { |
| 19 | 25 |
| 26 namespace keys = tabs_constants; |
| 27 namespace utils = extension_function_test_utils; |
| 28 |
| 20 namespace { | 29 namespace { |
| 21 | 30 |
| 22 std::unique_ptr<base::ListValue> RunTabsQueryFunction( | 31 std::unique_ptr<base::ListValue> RunTabsQueryFunction( |
| 23 Browser* browser, | 32 Browser* browser, |
| 24 const Extension* extension, | 33 const Extension* extension, |
| 25 const std::string& query_info) { | 34 const std::string& query_info) { |
| 26 scoped_refptr<TabsQueryFunction> function(new TabsQueryFunction()); | 35 scoped_refptr<TabsQueryFunction> function(new TabsQueryFunction()); |
| 27 function->set_extension(extension); | 36 function->set_extension(extension); |
| 28 std::unique_ptr<base::Value> value( | 37 std::unique_ptr<base::Value> value( |
| 29 extension_function_test_utils::RunFunctionAndReturnSingleResult( | 38 extension_function_test_utils::RunFunctionAndReturnSingleResult( |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 int first_tab_id = -1; | 211 int first_tab_id = -1; |
| 203 ASSERT_TRUE(first_tab_info->GetInteger("id", &first_tab_id)); | 212 ASSERT_TRUE(first_tab_info->GetInteger("id", &first_tab_id)); |
| 204 EXPECT_TRUE(ContainsValue(expected_tabs_ids, first_tab_id)); | 213 EXPECT_TRUE(ContainsValue(expected_tabs_ids, first_tab_id)); |
| 205 | 214 |
| 206 int third_tab_id = -1; | 215 int third_tab_id = -1; |
| 207 ASSERT_TRUE(third_tab_info->GetInteger("id", &third_tab_id)); | 216 ASSERT_TRUE(third_tab_info->GetInteger("id", &third_tab_id)); |
| 208 EXPECT_TRUE(ContainsValue(expected_tabs_ids, third_tab_id)); | 217 EXPECT_TRUE(ContainsValue(expected_tabs_ids, third_tab_id)); |
| 209 } | 218 } |
| 210 } | 219 } |
| 211 | 220 |
| 221 TEST_F(TabsApiUnitTest, QueryDiscarded) { |
| 222 ASSERT_TRUE(g_browser_process && g_browser_process->GetTabManager()); |
| 223 memory::TabManager* tab_manager = g_browser_process->GetTabManager(); |
| 224 |
| 225 // Create three tabs. |
| 226 content::OpenURLParams params(GURL(url::kAboutBlankURL), content::Referrer(), |
| 227 NEW_FOREGROUND_TAB, ui::PAGE_TRANSITION_LINK, |
| 228 false); |
| 229 content::WebContents* web_contents_a = browser()->OpenURL(params); |
| 230 content::WebContents* web_contents_b = browser()->OpenURL(params); |
| 231 browser()->OpenURL(params); |
| 232 |
| 233 // Get non-discarded tabs. |
| 234 { |
| 235 const char* kQueryInfo = "[{\"discarded\": false}]"; |
| 236 scoped_refptr<TabsQueryFunction> function = new TabsQueryFunction(); |
| 237 function->set_extension(test_util::CreateEmptyExtension().get()); |
| 238 std::unique_ptr<base::ListValue> result( |
| 239 utils::ToList(utils::RunFunctionAndReturnSingleResult( |
| 240 function.get(), kQueryInfo, browser()))); |
| 241 |
| 242 EXPECT_EQ(3u, result->GetSize()); |
| 243 } |
| 244 |
| 245 // Get discarded tabs. |
| 246 { |
| 247 const char* kQueryInfo = "[{\"discarded\": true}]"; |
| 248 scoped_refptr<TabsQueryFunction> function = new TabsQueryFunction(); |
| 249 function->set_extension(test_util::CreateEmptyExtension().get()); |
| 250 std::unique_ptr<base::ListValue> result( |
| 251 utils::ToList(utils::RunFunctionAndReturnSingleResult( |
| 252 function.get(), kQueryInfo, browser()))); |
| 253 |
| 254 EXPECT_EQ(0u, result->GetSize()); |
| 255 } |
| 256 |
| 257 // Discards one tab. |
| 258 EXPECT_TRUE( |
| 259 tab_manager->DiscardTabById(reinterpret_cast<int64_t>(web_contents_a))); |
| 260 |
| 261 // Get non-discarded tabs after discarding one tab. |
| 262 { |
| 263 const char* kQueryInfo = "[{\"discarded\": false}]"; |
| 264 scoped_refptr<TabsQueryFunction> function = new TabsQueryFunction(); |
| 265 function->set_extension(test_util::CreateEmptyExtension().get()); |
| 266 std::unique_ptr<base::ListValue> result( |
| 267 utils::ToList(utils::RunFunctionAndReturnSingleResult( |
| 268 function.get(), kQueryInfo, browser()))); |
| 269 |
| 270 EXPECT_EQ(2u, result->GetSize()); |
| 271 } |
| 272 |
| 273 TabStripModel* tab_strip_model = browser()->tab_strip_model(); |
| 274 |
| 275 // Get discarded tabs after discarding one tab. |
| 276 { |
| 277 const char* kQueryInfo = "[{\"discarded\": true}]"; |
| 278 scoped_refptr<TabsQueryFunction> function = new TabsQueryFunction(); |
| 279 function->set_extension(test_util::CreateEmptyExtension().get()); |
| 280 std::unique_ptr<base::ListValue> result( |
| 281 utils::ToList(utils::RunFunctionAndReturnSingleResult( |
| 282 function.get(), kQueryInfo, browser()))); |
| 283 |
| 284 EXPECT_EQ(1u, result->GetSize()); |
| 285 |
| 286 // Make sure the returned tab is the correct one. |
| 287 int tab_id_a = |
| 288 ExtensionTabUtil::GetTabId(tab_strip_model->GetWebContentsAt(0)); |
| 289 |
| 290 int id = -1; |
| 291 base::Value* tab = nullptr; |
| 292 EXPECT_TRUE(result->Get(0, &tab)); |
| 293 utils::ToDictionary(tab)->GetInteger(keys::kIdKey, &id); |
| 294 |
| 295 EXPECT_EQ(tab_id_a, id); |
| 296 } |
| 297 |
| 298 // Discards another created tab. |
| 299 EXPECT_TRUE( |
| 300 tab_manager->DiscardTabById(reinterpret_cast<int64_t>(web_contents_b))); |
| 301 |
| 302 // Get non-discarded tabs after discarding two created tabs. |
| 303 { |
| 304 const char* kQueryInfo = "[{\"discarded\": false}]"; |
| 305 scoped_refptr<TabsQueryFunction> function = new TabsQueryFunction(); |
| 306 function->set_extension(test_util::CreateEmptyExtension().get()); |
| 307 std::unique_ptr<base::ListValue> result( |
| 308 utils::ToList(utils::RunFunctionAndReturnSingleResult( |
| 309 function.get(), kQueryInfo, browser()))); |
| 310 |
| 311 EXPECT_EQ(1u, result->GetSize()); |
| 312 |
| 313 // Make sure the returned tab is the correct one. |
| 314 int tab_id_c = |
| 315 ExtensionTabUtil::GetTabId(tab_strip_model->GetWebContentsAt(2)); |
| 316 |
| 317 int id = -1; |
| 318 base::Value* tab = nullptr; |
| 319 EXPECT_TRUE(result->Get(0, &tab)); |
| 320 utils::ToDictionary(tab)->GetInteger(keys::kIdKey, &id); |
| 321 |
| 322 EXPECT_EQ(tab_id_c, id); |
| 323 } |
| 324 |
| 325 // Get discarded tabs after discarding two created tabs. |
| 326 { |
| 327 const char* kQueryInfo = "[{\"discarded\": true}]"; |
| 328 scoped_refptr<TabsQueryFunction> function = new TabsQueryFunction(); |
| 329 function->set_extension(test_util::CreateEmptyExtension().get()); |
| 330 std::unique_ptr<base::ListValue> result( |
| 331 utils::ToList(utils::RunFunctionAndReturnSingleResult( |
| 332 function.get(), kQueryInfo, browser()))); |
| 333 |
| 334 EXPECT_EQ(2u, result->GetSize()); |
| 335 } |
| 336 |
| 337 // Activates the first tab. |
| 338 tab_strip_model->ActivateTabAt(0, false); |
| 339 |
| 340 // Get non-discarded tabs after activating a discarded tab. |
| 341 { |
| 342 const char* kQueryInfo = "[{\"discarded\": false}]"; |
| 343 scoped_refptr<TabsQueryFunction> function = new TabsQueryFunction(); |
| 344 function->set_extension(test_util::CreateEmptyExtension().get()); |
| 345 std::unique_ptr<base::ListValue> result( |
| 346 utils::ToList(utils::RunFunctionAndReturnSingleResult( |
| 347 function.get(), kQueryInfo, browser()))); |
| 348 |
| 349 EXPECT_EQ(2u, result->GetSize()); |
| 350 } |
| 351 |
| 352 // Get discarded tabs after activating a discarded tab. |
| 353 { |
| 354 const char* kQueryInfo = "[{\"discarded\": true}]"; |
| 355 scoped_refptr<TabsQueryFunction> function = new TabsQueryFunction(); |
| 356 function->set_extension(test_util::CreateEmptyExtension().get()); |
| 357 std::unique_ptr<base::ListValue> result( |
| 358 utils::ToList(utils::RunFunctionAndReturnSingleResult( |
| 359 function.get(), kQueryInfo, browser()))); |
| 360 |
| 361 EXPECT_EQ(1u, result->GetSize()); |
| 362 } |
| 363 |
| 364 browser()->tab_strip_model()->CloseAllTabs(); |
| 365 } |
| 366 |
| 212 } // namespace extensions | 367 } // namespace extensions |
| OLD | NEW |