| 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 <limits.h> | 5 #include <limits.h> |
| 6 #include <stddef.h> | 6 #include <stddef.h> |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <string> | 10 #include <string> |
| 11 | 11 |
| 12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/strings/pattern.h" | 13 #include "base/strings/pattern.h" |
| 14 #include "base/strings/string_split.h" | 14 #include "base/strings/string_split.h" |
| 15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
| 16 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
| 17 #include "base/values.h" | 17 #include "base/values.h" |
| 18 #include "build/build_config.h" | 18 #include "build/build_config.h" |
| 19 #include "chrome/browser/apps/app_browsertest_util.h" | 19 #include "chrome/browser/apps/app_browsertest_util.h" |
| 20 #include "chrome/browser/browser_process.h" |
| 20 #include "chrome/browser/devtools/devtools_window_testing.h" | 21 #include "chrome/browser/devtools/devtools_window_testing.h" |
| 21 #include "chrome/browser/extensions/api/tabs/tabs_api.h" | 22 #include "chrome/browser/extensions/api/tabs/tabs_api.h" |
| 22 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" | 23 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" |
| 23 #include "chrome/browser/extensions/extension_apitest.h" | 24 #include "chrome/browser/extensions/extension_apitest.h" |
| 24 #include "chrome/browser/extensions/extension_function_test_utils.h" | 25 #include "chrome/browser/extensions/extension_function_test_utils.h" |
| 25 #include "chrome/browser/extensions/extension_tab_util.h" | 26 #include "chrome/browser/extensions/extension_tab_util.h" |
| 26 #include "chrome/browser/extensions/window_controller.h" | 27 #include "chrome/browser/extensions/window_controller.h" |
| 28 #include "chrome/browser/memory/tab_manager.h" |
| 27 #include "chrome/browser/prefs/incognito_mode_prefs.h" | 29 #include "chrome/browser/prefs/incognito_mode_prefs.h" |
| 28 #include "chrome/browser/profiles/profile.h" | 30 #include "chrome/browser/profiles/profile.h" |
| 29 #include "chrome/browser/ui/browser.h" | 31 #include "chrome/browser/ui/browser.h" |
| 30 #include "chrome/browser/ui/browser_commands.h" | 32 #include "chrome/browser/ui/browser_commands.h" |
| 31 #include "chrome/browser/ui/browser_window.h" | 33 #include "chrome/browser/ui/browser_window.h" |
| 32 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 34 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 33 #include "chrome/browser/ui/views/frame/browser_view.h" | 35 #include "chrome/browser/ui/views/frame/browser_view.h" |
| 34 #include "chrome/browser/ui/zoom/chrome_zoom_level_prefs.h" | 36 #include "chrome/browser/ui/zoom/chrome_zoom_level_prefs.h" |
| 35 #include "chrome/test/base/ui_test_utils.h" | 37 #include "chrome/test/base/ui_test_utils.h" |
| 36 #include "components/prefs/pref_service.h" | 38 #include "components/prefs/pref_service.h" |
| (...skipping 1268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1305 EXPECT_TRUE(base::MatchPattern( | 1307 EXPECT_TRUE(base::MatchPattern( |
| 1306 utils::RunFunctionAndReturnError( | 1308 utils::RunFunctionAndReturnError( |
| 1307 function.get(), base::StringPrintf("[%u, {\"code\": \"true\"}]", | 1309 function.get(), base::StringPrintf("[%u, {\"code\": \"true\"}]", |
| 1308 api::windows::WINDOW_ID_CURRENT), | 1310 api::windows::WINDOW_ID_CURRENT), |
| 1309 DevToolsWindowTesting::Get(devtools)->browser()), | 1311 DevToolsWindowTesting::Get(devtools)->browser()), |
| 1310 manifest_errors::kCannotAccessPageWithUrl)); | 1312 manifest_errors::kCannotAccessPageWithUrl)); |
| 1311 | 1313 |
| 1312 DevToolsWindowTesting::CloseDevToolsWindowSync(devtools); | 1314 DevToolsWindowTesting::CloseDevToolsWindowSync(devtools); |
| 1313 } | 1315 } |
| 1314 | 1316 |
| 1317 // TODO(georgesak): change this browsertest to an unittest. |
| 1318 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, DiscardedProperty) { |
| 1319 ASSERT_TRUE(g_browser_process && g_browser_process->GetTabManager()); |
| 1320 memory::TabManager* tab_manager = g_browser_process->GetTabManager(); |
| 1321 |
| 1322 // Create two aditional tabs. |
| 1323 content::OpenURLParams params(GURL(url::kAboutBlankURL), content::Referrer(), |
| 1324 NEW_BACKGROUND_TAB, ui::PAGE_TRANSITION_LINK, |
| 1325 false); |
| 1326 content::WebContents* web_contents_a = browser()->OpenURL(params); |
| 1327 content::WebContents* web_contents_b = browser()->OpenURL(params); |
| 1328 |
| 1329 // Set up query function with an extension. |
| 1330 scoped_refptr<const Extension> extension = test_util::CreateEmptyExtension(); |
| 1331 auto RunQueryFunction = [this, &extension](const char* query_info) { |
| 1332 scoped_refptr<TabsQueryFunction> function = new TabsQueryFunction(); |
| 1333 function->set_extension(extension.get()); |
| 1334 return utils::ToList(utils::RunFunctionAndReturnSingleResult( |
| 1335 function.get(), query_info, browser())); |
| 1336 }; |
| 1337 |
| 1338 // Get non-discarded tabs. |
| 1339 { |
| 1340 std::unique_ptr<base::ListValue> result( |
| 1341 RunQueryFunction("[{\"discarded\": false}]")); |
| 1342 |
| 1343 // The two created plus the default tab. |
| 1344 EXPECT_EQ(3u, result->GetSize()); |
| 1345 } |
| 1346 |
| 1347 // Get discarded tabs. |
| 1348 { |
| 1349 std::unique_ptr<base::ListValue> result( |
| 1350 RunQueryFunction("[{\"discarded\": true}]")); |
| 1351 EXPECT_EQ(0u, result->GetSize()); |
| 1352 } |
| 1353 |
| 1354 TabStripModel* tab_strip_model = browser()->tab_strip_model(); |
| 1355 |
| 1356 // Creates Tab object to ensure the property is correct for the extension. |
| 1357 std::unique_ptr<api::tabs::Tab> tab_object_a = |
| 1358 ExtensionTabUtil::CreateTabObject(web_contents_a, tab_strip_model, 0); |
| 1359 EXPECT_FALSE(tab_object_a->discarded); |
| 1360 |
| 1361 // Discards one tab. |
| 1362 EXPECT_TRUE( |
| 1363 tab_manager->DiscardTabById(reinterpret_cast<int64_t>(web_contents_a))); |
| 1364 web_contents_a = tab_strip_model->GetWebContentsAt(1); |
| 1365 |
| 1366 // Make sure the property is changed accordingly after discarding the tab. |
| 1367 tab_object_a = |
| 1368 ExtensionTabUtil::CreateTabObject(web_contents_a, tab_strip_model, 0); |
| 1369 EXPECT_TRUE(tab_object_a->discarded); |
| 1370 |
| 1371 // Get non-discarded tabs after discarding one tab. |
| 1372 { |
| 1373 std::unique_ptr<base::ListValue> result( |
| 1374 RunQueryFunction("[{\"discarded\": false}]")); |
| 1375 EXPECT_EQ(2u, result->GetSize()); |
| 1376 } |
| 1377 |
| 1378 // Get discarded tabs after discarding one tab. |
| 1379 { |
| 1380 std::unique_ptr<base::ListValue> result( |
| 1381 RunQueryFunction("[{\"discarded\": true}]")); |
| 1382 EXPECT_EQ(1u, result->GetSize()); |
| 1383 |
| 1384 // Make sure the returned tab is the correct one. |
| 1385 int tab_id_a = ExtensionTabUtil::GetTabId(web_contents_a); |
| 1386 |
| 1387 int id = -1; |
| 1388 base::Value* tab = nullptr; |
| 1389 EXPECT_TRUE(result->Get(0, &tab)); |
| 1390 utils::ToDictionary(tab)->GetInteger(keys::kIdKey, &id); |
| 1391 |
| 1392 EXPECT_EQ(tab_id_a, id); |
| 1393 } |
| 1394 |
| 1395 // Discards another created tab. |
| 1396 EXPECT_TRUE( |
| 1397 tab_manager->DiscardTabById(reinterpret_cast<int64_t>(web_contents_b))); |
| 1398 |
| 1399 // Get non-discarded tabs after discarding two created tabs. |
| 1400 { |
| 1401 std::unique_ptr<base::ListValue> result( |
| 1402 RunQueryFunction("[{\"discarded\": false}]")); |
| 1403 EXPECT_EQ(1u, result->GetSize()); |
| 1404 |
| 1405 // Make sure the returned tab is the correct one. |
| 1406 int tab_id_c = |
| 1407 ExtensionTabUtil::GetTabId(tab_strip_model->GetWebContentsAt(0)); |
| 1408 |
| 1409 int id = -1; |
| 1410 base::Value* tab = nullptr; |
| 1411 EXPECT_TRUE(result->Get(0, &tab)); |
| 1412 utils::ToDictionary(tab)->GetInteger(keys::kIdKey, &id); |
| 1413 |
| 1414 EXPECT_EQ(tab_id_c, id); |
| 1415 } |
| 1416 |
| 1417 // Get discarded tabs after discarding two created tabs. |
| 1418 { |
| 1419 std::unique_ptr<base::ListValue> result( |
| 1420 RunQueryFunction("[{\"discarded\": true}]")); |
| 1421 EXPECT_EQ(2u, result->GetSize()); |
| 1422 } |
| 1423 |
| 1424 // Activates the first created tab. |
| 1425 tab_strip_model->ActivateTabAt(1, false); |
| 1426 |
| 1427 // Get non-discarded tabs after activating a discarded tab. |
| 1428 { |
| 1429 std::unique_ptr<base::ListValue> result( |
| 1430 RunQueryFunction("[{\"discarded\": false}]")); |
| 1431 EXPECT_EQ(2u, result->GetSize()); |
| 1432 } |
| 1433 |
| 1434 // Get discarded tabs after activating a discarded tab. |
| 1435 { |
| 1436 std::unique_ptr<base::ListValue> result( |
| 1437 RunQueryFunction("[{\"discarded\": true}]")); |
| 1438 EXPECT_EQ(1u, result->GetSize()); |
| 1439 } |
| 1440 } |
| 1441 |
| 1315 // Tester class for the tabs.zoom* api functions. | 1442 // Tester class for the tabs.zoom* api functions. |
| 1316 class ExtensionTabsZoomTest : public ExtensionTabsTest { | 1443 class ExtensionTabsZoomTest : public ExtensionTabsTest { |
| 1317 public: | 1444 public: |
| 1318 void SetUpOnMainThread() override; | 1445 void SetUpOnMainThread() override; |
| 1319 | 1446 |
| 1320 // Runs chrome.tabs.setZoom(). | 1447 // Runs chrome.tabs.setZoom(). |
| 1321 bool RunSetZoom(int tab_id, double zoom_factor); | 1448 bool RunSetZoom(int tab_id, double zoom_factor); |
| 1322 | 1449 |
| 1323 // Runs chrome.tabs.getZoom(). | 1450 // Runs chrome.tabs.getZoom(). |
| 1324 testing::AssertionResult RunGetZoom(int tab_id, double* zoom_factor); | 1451 testing::AssertionResult RunGetZoom(int tab_id, double* zoom_factor); |
| (...skipping 413 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1738 EXPECT_TRUE( | 1865 EXPECT_TRUE( |
| 1739 base::MatchPattern(error, manifest_errors::kCannotAccessChromeUrl)); | 1866 base::MatchPattern(error, manifest_errors::kCannotAccessChromeUrl)); |
| 1740 | 1867 |
| 1741 // chrome.tabs.setZoomSettings(). | 1868 // chrome.tabs.setZoomSettings(). |
| 1742 error = RunSetZoomSettingsExpectError(tab_id, "manual", "per-tab"); | 1869 error = RunSetZoomSettingsExpectError(tab_id, "manual", "per-tab"); |
| 1743 EXPECT_TRUE( | 1870 EXPECT_TRUE( |
| 1744 base::MatchPattern(error, manifest_errors::kCannotAccessChromeUrl)); | 1871 base::MatchPattern(error, manifest_errors::kCannotAccessChromeUrl)); |
| 1745 } | 1872 } |
| 1746 | 1873 |
| 1747 } // namespace extensions | 1874 } // namespace extensions |
| OLD | NEW |