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

Side by Side Diff: chrome/browser/extensions/api/tabs/tabs_test.cc

Issue 2205523002: AutoDiscardable property support on Chrome Extensions Tabs API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fixed nits Created 4 years, 4 months 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
OLDNEW
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 1558 matching lines...) Expand 10 before | Expand all | Expand 10 after
1569 // Discarded state should be false for both tabs as no tab was discarded. 1569 // Discarded state should be false for both tabs as no tab was discarded.
1570 EXPECT_FALSE(g_browser_process->GetTabManager()->IsTabDiscarded( 1570 EXPECT_FALSE(g_browser_process->GetTabManager()->IsTabDiscarded(
1571 browser()->tab_strip_model()->GetWebContentsAt(1))); 1571 browser()->tab_strip_model()->GetWebContentsAt(1)));
1572 EXPECT_FALSE(g_browser_process->GetTabManager()->IsTabDiscarded( 1572 EXPECT_FALSE(g_browser_process->GetTabManager()->IsTabDiscarded(
1573 browser()->tab_strip_model()->GetWebContentsAt(0))); 1573 browser()->tab_strip_model()->GetWebContentsAt(0)));
1574 1574
1575 // Check error message. 1575 // Check error message.
1576 EXPECT_TRUE(base::MatchPattern(error, keys::kCannotFindTabToDiscard)); 1576 EXPECT_TRUE(base::MatchPattern(error, keys::kCannotFindTabToDiscard));
1577 } 1577 }
1578 1578
1579 IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, AutoDiscardableProperty) {
1580 // Create two aditional tabs.
1581 content::OpenURLParams params(GURL(url::kAboutBlankURL), content::Referrer(),
1582 NEW_BACKGROUND_TAB, ui::PAGE_TRANSITION_LINK,
1583 false);
1584 content::WebContents* web_contents_a = browser()->OpenURL(params);
1585 content::WebContents* web_contents_b = browser()->OpenURL(params);
1586
1587 // Creates Tab object to ensure the property is correct for the extension.
1588 TabStripModel* tab_strip_model = browser()->tab_strip_model();
1589 std::unique_ptr<api::tabs::Tab> tab_object_a =
1590 ExtensionTabUtil::CreateTabObject(web_contents_a, tab_strip_model, 0);
1591 EXPECT_TRUE(tab_object_a->auto_discardable);
1592
1593 // Set up query and update functions with the extension.
1594 scoped_refptr<const Extension> extension = test_util::CreateEmptyExtension();
1595 auto RunQueryFunction = [this, &extension](const char* query_info) {
1596 scoped_refptr<TabsQueryFunction> function = new TabsQueryFunction();
1597 function->set_extension(extension.get());
1598 return utils::ToList(utils::RunFunctionAndReturnSingleResult(
1599 function.get(), query_info, browser()));
1600 };
1601 auto RunUpdateFunction = [this, &extension](std::string update_info) {
1602 scoped_refptr<TabsUpdateFunction> function = new TabsUpdateFunction();
1603 function->set_extension(extension.get());
1604 return utils::ToDictionary(utils::RunFunctionAndReturnSingleResult(
1605 function.get(), update_info, browser()));
1606 };
1607
1608 // Queries and results used.
1609 const char* kAutoDiscardableQueryInfo = "[{\"autoDiscardable\": true}]";
1610 const char* kNonAutoDiscardableQueryInfo = "[{\"autoDiscardable\": false}]";
1611 std::unique_ptr<base::ListValue> query_result;
1612 std::unique_ptr<base::DictionaryValue> update_result;
1613
1614 // Get auto-discardable tabs. Returns all since tabs are auto-discardable
1615 // by default.
1616 query_result.reset(RunQueryFunction(kAutoDiscardableQueryInfo));
1617 EXPECT_EQ(3u, query_result->GetSize());
1618
1619 // Get non auto-discardable tabs.
1620 query_result.reset(RunQueryFunction(kNonAutoDiscardableQueryInfo));
1621 EXPECT_EQ(0u, query_result->GetSize());
1622
1623 // Update the auto-discardable state of web contents A.
1624 int tab_id_a = ExtensionTabUtil::GetTabId(web_contents_a);
1625 update_result.reset(RunUpdateFunction(
1626 base::StringPrintf("[%u, {\"autoDiscardable\": false}]", tab_id_a)));
1627 EXPECT_EQ(tab_id_a, api_test_utils::GetInteger(update_result.get(), "id"));
1628 EXPECT_FALSE(
1629 api_test_utils::GetBoolean(update_result.get(), "autoDiscardable"));
1630
1631 // Make sure the property is changed accordingly after updating the tab.
1632 tab_object_a =
1633 ExtensionTabUtil::CreateTabObject(web_contents_a, tab_strip_model, 0);
1634 EXPECT_FALSE(tab_object_a->auto_discardable);
1635
1636 // Get auto-discardable tabs after changing the status of web contents A.
1637 query_result.reset(RunQueryFunction(kAutoDiscardableQueryInfo));
1638 EXPECT_EQ(2u, query_result->GetSize());
1639
1640 // Get non auto-discardable tabs after changing the status of web contents A.
1641 query_result.reset(RunQueryFunction(kNonAutoDiscardableQueryInfo));
1642 EXPECT_EQ(1u, query_result->GetSize());
1643
1644 // Make sure the returned tab is the correct one.
1645 int id = -1;
1646 base::Value* tab = nullptr;
1647 EXPECT_TRUE(query_result->Get(0, &tab));
1648 utils::ToDictionary(tab)->GetInteger(keys::kIdKey, &id);
1649 EXPECT_EQ(tab_id_a, id);
1650
1651 // Update the auto-discardable state of web contents B.
1652 int tab_id_b = ExtensionTabUtil::GetTabId(web_contents_b);
1653 update_result.reset(RunUpdateFunction(
1654 base::StringPrintf("[%u, {\"autoDiscardable\": false}]", tab_id_b)));
1655 EXPECT_EQ(tab_id_b, api_test_utils::GetInteger(update_result.get(), "id"));
1656 EXPECT_FALSE(
1657 api_test_utils::GetBoolean(update_result.get(), "autoDiscardable"));
1658
1659 // Get auto-discardable tabs after changing the status of both created tabs.
1660 query_result.reset(RunQueryFunction(kAutoDiscardableQueryInfo));
1661 EXPECT_EQ(1u, query_result->GetSize());
1662
1663 // Make sure the returned tab is the correct one.
1664 id = -1;
1665 tab = nullptr;
1666 EXPECT_TRUE(query_result->Get(0, &tab));
1667 utils::ToDictionary(tab)->GetInteger(keys::kIdKey, &id);
1668 EXPECT_EQ(ExtensionTabUtil::GetTabId(tab_strip_model->GetWebContentsAt(0)),
1669 id);
1670
1671 // Get auto-discardable tabs after changing the status of both created tabs.
1672 query_result.reset(RunQueryFunction(kNonAutoDiscardableQueryInfo));
1673 EXPECT_EQ(2u, query_result->GetSize());
1674
1675 // Resets the first tab back to auto-discardable.
1676 update_result.reset(RunUpdateFunction(
1677 base::StringPrintf("[%u, {\"autoDiscardable\": true}]", tab_id_a)));
1678 EXPECT_EQ(tab_id_a, api_test_utils::GetInteger(update_result.get(), "id"));
1679 EXPECT_TRUE(
1680 api_test_utils::GetBoolean(update_result.get(), "autoDiscardable"));
1681
1682 // Get auto-discardable tabs after resetting the status of web contents A.
1683 query_result.reset(RunQueryFunction(kAutoDiscardableQueryInfo));
1684 EXPECT_EQ(2u, query_result->GetSize());
1685
1686 // Get non auto-discardable tabs after resetting the status of web contents A.
1687 query_result.reset(RunQueryFunction(kNonAutoDiscardableQueryInfo));
1688 EXPECT_EQ(1u, query_result->GetSize());
1689 }
1690
1579 // Tester class for the tabs.zoom* api functions. 1691 // Tester class for the tabs.zoom* api functions.
1580 class ExtensionTabsZoomTest : public ExtensionTabsTest { 1692 class ExtensionTabsZoomTest : public ExtensionTabsTest {
1581 public: 1693 public:
1582 void SetUpOnMainThread() override; 1694 void SetUpOnMainThread() override;
1583 1695
1584 // Runs chrome.tabs.setZoom(). 1696 // Runs chrome.tabs.setZoom().
1585 bool RunSetZoom(int tab_id, double zoom_factor); 1697 bool RunSetZoom(int tab_id, double zoom_factor);
1586 1698
1587 // Runs chrome.tabs.getZoom(). 1699 // Runs chrome.tabs.getZoom().
1588 testing::AssertionResult RunGetZoom(int tab_id, double* zoom_factor); 1700 testing::AssertionResult RunGetZoom(int tab_id, double* zoom_factor);
(...skipping 413 matching lines...) Expand 10 before | Expand all | Expand 10 after
2002 EXPECT_TRUE( 2114 EXPECT_TRUE(
2003 base::MatchPattern(error, manifest_errors::kCannotAccessChromeUrl)); 2115 base::MatchPattern(error, manifest_errors::kCannotAccessChromeUrl));
2004 2116
2005 // chrome.tabs.setZoomSettings(). 2117 // chrome.tabs.setZoomSettings().
2006 error = RunSetZoomSettingsExpectError(tab_id, "manual", "per-tab"); 2118 error = RunSetZoomSettingsExpectError(tab_id, "manual", "per-tab");
2007 EXPECT_TRUE( 2119 EXPECT_TRUE(
2008 base::MatchPattern(error, manifest_errors::kCannotAccessChromeUrl)); 2120 base::MatchPattern(error, manifest_errors::kCannotAccessChromeUrl));
2009 } 2121 }
2010 2122
2011 } // namespace extensions 2123 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/tabs/tabs_event_router.cc ('k') | chrome/browser/extensions/extension_tab_util.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698