Chromium Code Reviews| 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> |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 30 #include "chrome/browser/profiles/profile.h" | 30 #include "chrome/browser/profiles/profile.h" |
| 31 #include "chrome/browser/ui/browser.h" | 31 #include "chrome/browser/ui/browser.h" |
| 32 #include "chrome/browser/ui/browser_commands.h" | 32 #include "chrome/browser/ui/browser_commands.h" |
| 33 #include "chrome/browser/ui/browser_window.h" | 33 #include "chrome/browser/ui/browser_window.h" |
| 34 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 34 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 35 #include "chrome/browser/ui/views/frame/browser_view.h" | 35 #include "chrome/browser/ui/views/frame/browser_view.h" |
| 36 #include "chrome/browser/ui/zoom/chrome_zoom_level_prefs.h" | 36 #include "chrome/browser/ui/zoom/chrome_zoom_level_prefs.h" |
| 37 #include "chrome/test/base/ui_test_utils.h" | 37 #include "chrome/test/base/ui_test_utils.h" |
| 38 #include "components/prefs/pref_service.h" | 38 #include "components/prefs/pref_service.h" |
| 39 #include "content/public/browser/browser_context.h" | 39 #include "content/public/browser/browser_context.h" |
| 40 #include "content/public/browser/notification_service.h" | |
| 40 #include "content/public/browser/storage_partition.h" | 41 #include "content/public/browser/storage_partition.h" |
| 41 #include "content/public/common/page_zoom.h" | 42 #include "content/public/common/page_zoom.h" |
| 42 #include "content/public/common/url_constants.h" | 43 #include "content/public/common/url_constants.h" |
| 43 #include "extensions/browser/api_test_utils.h" | 44 #include "extensions/browser/api_test_utils.h" |
| 44 #include "extensions/browser/app_window/app_window.h" | 45 #include "extensions/browser/app_window/app_window.h" |
| 45 #include "extensions/browser/app_window/app_window_registry.h" | 46 #include "extensions/browser/app_window/app_window_registry.h" |
| 46 #include "extensions/browser/app_window/native_app_window.h" | 47 #include "extensions/browser/app_window/native_app_window.h" |
| 47 #include "extensions/common/manifest_constants.h" | 48 #include "extensions/common/manifest_constants.h" |
| 48 #include "extensions/common/test_util.h" | 49 #include "extensions/common/test_util.h" |
| 49 #include "extensions/test/extension_test_message_listener.h" | 50 #include "extensions/test/extension_test_message_listener.h" |
| (...skipping 1382 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1432 } | 1433 } |
| 1433 | 1434 |
| 1434 // Get discarded tabs after activating a discarded tab. | 1435 // Get discarded tabs after activating a discarded tab. |
| 1435 { | 1436 { |
| 1436 std::unique_ptr<base::ListValue> result( | 1437 std::unique_ptr<base::ListValue> result( |
| 1437 RunQueryFunction("[{\"discarded\": true}]")); | 1438 RunQueryFunction("[{\"discarded\": true}]")); |
| 1438 EXPECT_EQ(1u, result->GetSize()); | 1439 EXPECT_EQ(1u, result->GetSize()); |
| 1439 } | 1440 } |
| 1440 } | 1441 } |
| 1441 | 1442 |
| 1443 // Tests chrome.tabs.discard(tabId). | |
| 1444 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, DiscardWithId) { | |
| 1445 // Create an aditional tab. | |
| 1446 ui_test_utils::NavigateToURLWithDisposition( | |
| 1447 browser(), GURL(url::kAboutBlankURL), NEW_BACKGROUND_TAB, | |
| 1448 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); | |
| 1449 content::WebContents* web_contents = | |
| 1450 browser()->tab_strip_model()->GetWebContentsAt(1); | |
| 1451 | |
| 1452 // Set up the function with an extension. | |
| 1453 scoped_refptr<const Extension> extension = test_util::CreateEmptyExtension(); | |
| 1454 scoped_refptr<TabsDiscardFunction> discard(new TabsDiscardFunction()); | |
| 1455 discard->set_extension(extension.get()); | |
| 1456 | |
| 1457 // Run function passing the tab id as argument. | |
| 1458 int tab_id = ExtensionTabUtil::GetTabId(web_contents); | |
| 1459 std::unique_ptr<base::DictionaryValue> result( | |
| 1460 utils::ToDictionary(utils::RunFunctionAndReturnSingleResult( | |
| 1461 discard.get(), base::StringPrintf("[%u]", tab_id), browser()))); | |
| 1462 | |
| 1463 // Confirms that TabManager sees the tab as discarded. | |
| 1464 memory::TabManager* tab_manager = g_browser_process->GetTabManager(); | |
| 1465 web_contents = browser()->tab_strip_model()->GetWebContentsAt(1); | |
| 1466 EXPECT_TRUE(tab_manager->IsTabDiscarded(web_contents)); | |
| 1467 | |
| 1468 // Make sure the returned tab is the one discarded and | |
|
Devlin
2016/07/26 16:13:45
nit: line wrapping looks off here?
Anderson Silva
2016/07/26 21:02:52
Done.
| |
| 1469 // its discarded state is correct. | |
| 1470 tab_id = ExtensionTabUtil::GetTabId(web_contents); | |
| 1471 EXPECT_EQ(tab_id, api_test_utils::GetInteger(result.get(), "id")); | |
| 1472 EXPECT_TRUE(api_test_utils::GetBoolean(result.get(), "discarded")); | |
| 1473 } | |
| 1474 | |
| 1475 // Tests chrome.tabs.discard(tabId) with tab already discarded. | |
| 1476 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, DiscardTabAlreadyDiscarded) { | |
|
Devlin
2016/07/26 16:13:45
We could probably tack this on to the previous tes
Anderson Silva
2016/07/26 21:02:52
Done.
| |
| 1477 // Create an aditional tab. | |
| 1478 ui_test_utils::NavigateToURLWithDisposition( | |
| 1479 browser(), GURL(url::kAboutBlankURL), NEW_BACKGROUND_TAB, | |
| 1480 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); | |
| 1481 content::WebContents* web_contents = | |
| 1482 browser()->tab_strip_model()->GetWebContentsAt(1); | |
| 1483 | |
| 1484 // First discards the tab. | |
| 1485 EXPECT_TRUE( | |
| 1486 g_browser_process->GetTabManager()->DiscardTabByExtension(web_contents)); | |
| 1487 | |
| 1488 // Set up the function with an extension. | |
| 1489 scoped_refptr<const Extension> extension = test_util::CreateEmptyExtension(); | |
| 1490 scoped_refptr<TabsDiscardFunction> discard(new TabsDiscardFunction()); | |
| 1491 discard->set_extension(extension.get()); | |
| 1492 | |
| 1493 // Run function passing the tab id of the already discarded tab. | |
| 1494 web_contents = browser()->tab_strip_model()->GetWebContentsAt(1); | |
| 1495 int tab_id = ExtensionTabUtil::GetTabId(web_contents); | |
| 1496 std::string error = utils::RunFunctionAndReturnError( | |
| 1497 discard.get(), base::StringPrintf("[%u]", tab_id), browser()); | |
| 1498 | |
| 1499 // Check error message. | |
| 1500 EXPECT_TRUE(base::MatchPattern(error, keys::kCannotDiscardTab)); | |
| 1501 } | |
| 1502 | |
| 1503 // Tests chrome.tabs.discard(invalidId). | |
| 1504 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, DiscardWithInvalidId) { | |
| 1505 // Create an aditional tab. | |
| 1506 ui_test_utils::NavigateToURLWithDisposition( | |
| 1507 browser(), GURL(url::kAboutBlankURL), NEW_BACKGROUND_TAB, | |
| 1508 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); | |
| 1509 | |
| 1510 // Set up the function with an extension. | |
| 1511 scoped_refptr<const Extension> extension = test_util::CreateEmptyExtension(); | |
| 1512 scoped_refptr<TabsDiscardFunction> discard(new TabsDiscardFunction()); | |
| 1513 discard->set_extension(extension.get()); | |
| 1514 | |
| 1515 // Run function passing an invalid id as argument. | |
| 1516 int tab_invalid_id = ExtensionTabUtil::GetTabId( | |
| 1517 browser()->tab_strip_model()->GetWebContentsAt(0)); | |
| 1518 tab_invalid_id = std::max( | |
| 1519 tab_invalid_id, ExtensionTabUtil::GetTabId( | |
| 1520 browser()->tab_strip_model()->GetWebContentsAt(1))); | |
| 1521 tab_invalid_id++; | |
| 1522 | |
| 1523 std::string error = utils::RunFunctionAndReturnError( | |
| 1524 discard.get(), base::StringPrintf("[%u]", tab_invalid_id), browser()); | |
| 1525 | |
| 1526 // Discarded state should still be false as no tab was discarded. | |
| 1527 EXPECT_FALSE(g_browser_process->GetTabManager()->IsTabDiscarded( | |
| 1528 browser()->tab_strip_model()->GetWebContentsAt(1))); | |
| 1529 | |
| 1530 // Check error message. | |
| 1531 EXPECT_TRUE(base::MatchPattern(error, keys::kTabNotFoundError)); | |
| 1532 } | |
| 1533 | |
| 1534 // Tests chrome.tabs.discard(). | |
| 1535 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, DiscardWithoutId) { | |
| 1536 // Create an aditional tab. | |
| 1537 ui_test_utils::NavigateToURLWithDisposition( | |
| 1538 browser(), GURL(url::kAboutBlankURL), NEW_BACKGROUND_TAB, | |
| 1539 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); | |
| 1540 content::WebContents* web_contents = | |
| 1541 browser()->tab_strip_model()->GetWebContentsAt(1); | |
| 1542 | |
| 1543 // Set up the function with an extension. | |
| 1544 scoped_refptr<const Extension> extension = test_util::CreateEmptyExtension(); | |
| 1545 scoped_refptr<TabsDiscardFunction> discard(new TabsDiscardFunction()); | |
| 1546 discard->set_extension(extension.get()); | |
| 1547 | |
| 1548 // Disable protection time to discard the tab without passing id. | |
| 1549 memory::TabManager* tab_manager = g_browser_process->GetTabManager(); | |
| 1550 tab_manager->set_minimum_protection_time_for_tests( | |
| 1551 base::TimeDelta::FromSeconds(0)); | |
| 1552 | |
| 1553 // Run without passing an id. | |
| 1554 std::unique_ptr<base::DictionaryValue> result(utils::ToDictionary( | |
| 1555 utils::RunFunctionAndReturnSingleResult(discard.get(), "[]", browser()))); | |
| 1556 | |
| 1557 // Confirms that TabManager sees the tab as discarded. | |
| 1558 web_contents = browser()->tab_strip_model()->GetWebContentsAt(1); | |
| 1559 EXPECT_TRUE(tab_manager->IsTabDiscarded(web_contents)); | |
| 1560 | |
| 1561 // Make sure the returned tab is the one discarded and | |
|
Devlin
2016/07/26 16:13:45
wrapping looks off here, too
Anderson Silva
2016/07/26 21:02:52
Done.
| |
| 1562 // its discarded state is correct. | |
| 1563 EXPECT_EQ(ExtensionTabUtil::GetTabId(web_contents), | |
| 1564 api_test_utils::GetInteger(result.get(), "id")); | |
| 1565 EXPECT_TRUE(api_test_utils::GetBoolean(result.get(), "discarded")); | |
| 1566 } | |
| 1567 | |
| 1568 // Tests chrome.tabs.discard() without disabling protection time. | |
| 1569 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, DiscardNoTabProtection) { | |
| 1570 // Create an aditional tab. | |
| 1571 ui_test_utils::NavigateToURLWithDisposition( | |
| 1572 browser(), GURL(url::kAboutBlankURL), NEW_BACKGROUND_TAB, | |
| 1573 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); | |
| 1574 | |
| 1575 // Set up the function with an extension. | |
| 1576 scoped_refptr<const Extension> extension = test_util::CreateEmptyExtension(); | |
| 1577 scoped_refptr<TabsDiscardFunction> discard(new TabsDiscardFunction()); | |
| 1578 discard->set_extension(extension.get()); | |
| 1579 | |
| 1580 // Run without passing an id. In this case the tab | |
|
Devlin
2016/07/26 16:13:45
and here.
Anderson Silva
2016/07/26 21:02:52
Done.
| |
| 1581 // couldn't be discarded because of protection time. | |
| 1582 std::string error = | |
| 1583 utils::RunFunctionAndReturnError(discard.get(), "[]", browser()); | |
| 1584 | |
| 1585 // Discarded state should be false for both tabs as no tab was discarded. | |
| 1586 EXPECT_FALSE(g_browser_process->GetTabManager()->IsTabDiscarded( | |
| 1587 browser()->tab_strip_model()->GetWebContentsAt(1))); | |
| 1588 EXPECT_FALSE(g_browser_process->GetTabManager()->IsTabDiscarded( | |
| 1589 browser()->tab_strip_model()->GetWebContentsAt(0))); | |
| 1590 | |
| 1591 // Check error message. | |
| 1592 EXPECT_TRUE(base::MatchPattern(error, keys::kCannotFindTabToDiscard)); | |
| 1593 } | |
| 1594 | |
| 1442 // Tester class for the tabs.zoom* api functions. | 1595 // Tester class for the tabs.zoom* api functions. |
| 1443 class ExtensionTabsZoomTest : public ExtensionTabsTest { | 1596 class ExtensionTabsZoomTest : public ExtensionTabsTest { |
| 1444 public: | 1597 public: |
| 1445 void SetUpOnMainThread() override; | 1598 void SetUpOnMainThread() override; |
| 1446 | 1599 |
| 1447 // Runs chrome.tabs.setZoom(). | 1600 // Runs chrome.tabs.setZoom(). |
| 1448 bool RunSetZoom(int tab_id, double zoom_factor); | 1601 bool RunSetZoom(int tab_id, double zoom_factor); |
| 1449 | 1602 |
| 1450 // Runs chrome.tabs.getZoom(). | 1603 // Runs chrome.tabs.getZoom(). |
| 1451 testing::AssertionResult RunGetZoom(int tab_id, double* zoom_factor); | 1604 testing::AssertionResult RunGetZoom(int tab_id, double* zoom_factor); |
| (...skipping 413 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1865 EXPECT_TRUE( | 2018 EXPECT_TRUE( |
| 1866 base::MatchPattern(error, manifest_errors::kCannotAccessChromeUrl)); | 2019 base::MatchPattern(error, manifest_errors::kCannotAccessChromeUrl)); |
| 1867 | 2020 |
| 1868 // chrome.tabs.setZoomSettings(). | 2021 // chrome.tabs.setZoomSettings(). |
| 1869 error = RunSetZoomSettingsExpectError(tab_id, "manual", "per-tab"); | 2022 error = RunSetZoomSettingsExpectError(tab_id, "manual", "per-tab"); |
| 1870 EXPECT_TRUE( | 2023 EXPECT_TRUE( |
| 1871 base::MatchPattern(error, manifest_errors::kCannotAccessChromeUrl)); | 2024 base::MatchPattern(error, manifest_errors::kCannotAccessChromeUrl)); |
| 1872 } | 2025 } |
| 1873 | 2026 |
| 1874 } // namespace extensions | 2027 } // namespace extensions |
| OLD | NEW |