Chromium Code Reviews| Index: chrome/browser/extensions/api/tabs/tabs_test.cc |
| diff --git a/chrome/browser/extensions/api/tabs/tabs_test.cc b/chrome/browser/extensions/api/tabs/tabs_test.cc |
| index 1bc1eac1ae248a56c861da8de03788acb98ff51c..618f28a60e9dc5680e73940ba15bd2bcfeebce4d 100644 |
| --- a/chrome/browser/extensions/api/tabs/tabs_test.cc |
| +++ b/chrome/browser/extensions/api/tabs/tabs_test.cc |
| @@ -37,6 +37,7 @@ |
| #include "chrome/test/base/ui_test_utils.h" |
| #include "components/prefs/pref_service.h" |
| #include "content/public/browser/browser_context.h" |
| +#include "content/public/browser/notification_service.h" |
| #include "content/public/browser/storage_partition.h" |
| #include "content/public/common/page_zoom.h" |
| #include "content/public/common/url_constants.h" |
| @@ -1439,6 +1440,100 @@ IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, DiscardedProperty) { |
| } |
| } |
| +// Tests chrome.tabs.discard(tabId). |
| +IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, DiscardWithId) { |
| + // Create an aditional tab. |
| + content::OpenURLParams params(GURL(url::kAboutBlankURL), content::Referrer(), |
| + NEW_BACKGROUND_TAB, ui::PAGE_TRANSITION_LINK, |
| + false); |
| + content::WebContents* web_contents = browser()->OpenURL(params); |
|
Devlin
2016/07/21 15:39:21
We should probably wait for load stop here. ui_te
Anderson Silva
2016/07/21 20:43:04
Done.
Had to use NavigateToURLWithDisposition() in
|
| + |
| + // Set up the function with an extension. |
| + scoped_refptr<const Extension> extension = test_util::CreateEmptyExtension(); |
| + scoped_refptr<TabsDiscardFunction> discard(new TabsDiscardFunction()); |
| + discard->set_extension(extension.get()); |
| + discard->set_has_callback(true); |
|
Devlin
2016/07/21 15:39:21
Is this necessary?
Anderson Silva
2016/07/21 20:43:04
Not anymore, because we are always returning resul
|
| + |
| + // Run function passing the tab id as argument. |
| + int tab_id = ExtensionTabUtil::GetTabId(web_contents); |
| + utils::RunFunction(discard.get(), base::StringPrintf("[%u]", tab_id), |
| + browser(), utils::NONE); |
| + |
| + // Make sure the result was successfull. |
| + const base::Value* result_success; |
| + discard->GetResultList()->Get(1, &result_success); |
|
Devlin
2016/07/21 15:39:21
Couldn't we just use ListValue::GetBoolean()?
Anderson Silva
2016/07/21 20:43:04
Yes, we probably could. I didn't see we had that.
|
| + bool success = false; |
| + result_success->GetAsBoolean(&success); |
| + EXPECT_TRUE(success); |
| + |
| + // Confirms that TabManager sees the the tab as discarded. |
|
Devlin
2016/07/21 15:39:21
typo: 'the the'
Anderson Silva
2016/07/21 20:43:04
Done.
|
| + memory::TabManager* tab_manager = g_browser_process->GetTabManager(); |
| + ASSERT_TRUE(tab_manager); |
| + web_contents = browser()->tab_strip_model()->GetWebContentsAt(1); |
| + EXPECT_TRUE(tab_manager->IsTabDiscarded(web_contents)); |
| + |
| + // Make sure the returned tab is the one discarded and |
| + // its discarded state is correct. |
| + const base::Value* result_tab; |
| + discard->GetResultList()->Get(0, &result_tab); |
|
Devlin
2016/07/21 15:39:21
here, too - why not just GetDictionary?
Anderson Silva
2016/07/21 20:43:04
Done. Same.
|
| + std::unique_ptr<base::DictionaryValue> result_dict( |
| + utils::ToDictionary(result_tab->DeepCopy())); |
| + |
| + tab_id = ExtensionTabUtil::GetTabId(web_contents); |
| + EXPECT_EQ(tab_id, api_test_utils::GetInteger(result_dict.get(), "id")); |
| + EXPECT_TRUE(api_test_utils::GetBoolean(result_dict.get(), "discarded")); |
| +} |
| + |
| +// Tests chrome.tabs.discard(). |
| +IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, DiscardWithoutId) { |
| + // Create an aditional tab. |
| + content::WindowedNotificationObserver load1( |
| + content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
| + content::NotificationService::AllSources()); |
| + content::OpenURLParams params(GURL(url::kAboutBlankURL), content::Referrer(), |
| + NEW_BACKGROUND_TAB, ui::PAGE_TRANSITION_LINK, |
| + false); |
| + content::WebContents* web_contents = browser()->OpenURL(params); |
|
Devlin
2016/07/21 15:39:21
here too, prefer NavigateToURL so that we know the
Anderson Silva
2016/07/21 20:43:04
Done.
|
| + load1.Wait(); |
| + |
| + // Set up the function with an extension. |
| + scoped_refptr<const Extension> extension = test_util::CreateEmptyExtension(); |
| + scoped_refptr<TabsDiscardFunction> discard(new TabsDiscardFunction()); |
| + discard->set_extension(extension.get()); |
| + discard->set_has_callback(true); |
| + |
| + // Disable protection time to discard the tab without passing id. |
| + memory::TabManager* tab_manager = g_browser_process->GetTabManager(); |
| + ASSERT_TRUE(tab_manager); |
| + tab_manager->set_minimum_protection_time_for_tests( |
| + base::TimeDelta::FromSeconds(0)); |
| + |
| + // Run without passing an id. |
| + utils::RunFunction(discard.get(), "[]", browser(), utils::NONE); |
| + |
| + // Make sure the result was successfull. |
|
Devlin
2016/07/21 15:39:21
s/successfull/successful
Anderson Silva
2016/07/21 20:43:04
Done.
|
| + const base::Value* result_success; |
| + discard->GetResultList()->Get(1, &result_success); |
| + bool success = false; |
| + result_success->GetAsBoolean(&success); |
| + EXPECT_TRUE(success); |
| + |
| + // Confirms that TabManager sees the tab as discarded. |
| + web_contents = browser()->tab_strip_model()->GetWebContentsAt(1); |
| + EXPECT_TRUE(tab_manager->IsTabDiscarded(web_contents)); |
| + |
| + // Make sure the returned tab is the one discarded and |
| + // its discarded state is correct. |
| + const base::Value* result_tab; |
| + discard->GetResultList()->Get(0, &result_tab); |
| + std::unique_ptr<base::DictionaryValue> result_dict( |
| + utils::ToDictionary(result_tab->DeepCopy())); |
| + |
| + EXPECT_EQ(ExtensionTabUtil::GetTabId(web_contents), |
| + api_test_utils::GetInteger(result_dict.get(), "id")); |
| + EXPECT_TRUE(api_test_utils::GetBoolean(result_dict.get(), "discarded")); |
| +} |
| + |
| // Tester class for the tabs.zoom* api functions. |
| class ExtensionTabsZoomTest : public ExtensionTabsTest { |
| public: |