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

Unified Diff: chrome/browser/extensions/api/tabs/tabs_api_unittest.cc

Issue 2067033002: Discarded property on chrome.tabs.Tab and chrome.tabs.query() (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: browsertest to unittest Created 4 years, 5 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/browser/extensions/api/tabs/tabs_api.cc ('k') | chrome/browser/extensions/extension_tab_util.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/extensions/api/tabs/tabs_api_unittest.cc
diff --git a/chrome/browser/extensions/api/tabs/tabs_api_unittest.cc b/chrome/browser/extensions/api/tabs/tabs_api_unittest.cc
index f8edf1b93711c07c14c804347652f5ab19d5c6fb..e085a54ee8b09cbe8e5b45c171506c6082defcb5 100644
--- a/chrome/browser/extensions/api/tabs/tabs_api_unittest.cc
+++ b/chrome/browser/extensions/api/tabs/tabs_api_unittest.cc
@@ -3,10 +3,12 @@
// found in the LICENSE file.
#include "base/strings/utf_string_conversions.h"
+#include "chrome/browser/browser_process.h"
#include "chrome/browser/extensions/api/tabs/tabs_api.h"
#include "chrome/browser/extensions/extension_function_test_utils.h"
#include "chrome/browser/extensions/extension_service_test_base.h"
#include "chrome/browser/extensions/extension_tab_util.h"
+#include "chrome/browser/memory/tab_manager.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/test/base/test_browser_window.h"
@@ -15,8 +17,13 @@
#include "extensions/common/extension_builder.h"
#include "extensions/common/test_util.h"
+using memory::TabManager;
+using content::WebContents;
+
namespace extensions {
+namespace utils = extension_function_test_utils;
+
namespace {
std::unique_ptr<base::ListValue> RunTabsQueryFunction(
@@ -209,4 +216,129 @@ TEST_F(TabsApiUnitTest, QueryWithHostPermission) {
}
}
+TEST_F(TabsApiUnitTest, QueryDiscarded) {
Devlin 2016/07/11 16:32:28 It doesn't look like this test has been formatted
Anderson Silva 2016/07/11 19:47:51 Done.
+ ASSERT_TRUE(g_browser_process && g_browser_process->GetTabManager());
+ memory::TabManager* tab_manager = g_browser_process->GetTabManager();
+
+ // Create three tabs.
+ content::OpenURLParams params_1(GURL(url::kAboutBlankURL),
Devlin 2016/07/11 16:32:28 optional nit: I'd also probably just inline these,
Anderson Silva 2016/07/11 19:47:51 Acknowledged.
+ content::Referrer(), NEW_BACKGROUND_TAB,
+ ui::PAGE_TRANSITION_LINK, false);
+ content::OpenURLParams params_2(GURL(url::kAboutBlankURL),
+ content::Referrer(), NEW_FOREGROUND_TAB,
+ ui::PAGE_TRANSITION_LINK, false);
+ content::WebContents* web_contents_a = browser()->OpenURL(params_1);
+ content::WebContents* web_contents_b = browser()->OpenURL(params_1);
+ browser()->OpenURL(params_2);
+
+ // Get non-discarded tabs.
+ {
+ const char* kQueryInfo = "[{\"discarded\": false}]";
+ scoped_refptr<TabsQueryFunction> function = new TabsQueryFunction();
+ function->set_extension(test_util::CreateEmptyExtension().get());
+ std::unique_ptr<base::ListValue> result(
+ utils::ToList(utils::RunFunctionAndReturnSingleResult(
+ function.get(), kQueryInfo, browser())));
+
+ EXPECT_EQ(3u, result.get()->GetSize());
Devlin 2016/07/11 16:32:28 unique_ptr::operator-> forwards to the underlying
Anderson Silva 2016/07/11 19:47:51 Done.
+ }
+
+ // Get discarded tabs.
+ {
+ const char* kQueryInfo = "[{\"discarded\": true}]";
+ scoped_refptr<TabsQueryFunction> function = new TabsQueryFunction();
+ function->set_extension(test_util::CreateEmptyExtension().get());
+ std::unique_ptr<base::ListValue> result(
+ utils::ToList(utils::RunFunctionAndReturnSingleResult(
+ function.get(), kQueryInfo, browser())));
+
+ EXPECT_EQ(0u, result.get()->GetSize());
+ }
+
+ // Discards one tab.
+ EXPECT_TRUE(
+ tab_manager->DiscardTabById(reinterpret_cast<int64_t>(web_contents_a)));
+
+ // Get non-discarded tabs after discarding one tab.
+ {
+ const char* kQueryInfo = "[{\"discarded\": false}]";
+ scoped_refptr<TabsQueryFunction> function = new TabsQueryFunction();
+ function->set_extension(test_util::CreateEmptyExtension().get());
+ std::unique_ptr<base::ListValue> result(
+ utils::ToList(utils::RunFunctionAndReturnSingleResult(
+ function.get(), kQueryInfo, browser())));
+
+ EXPECT_EQ(2u, result.get()->GetSize());
Devlin 2016/07/11 16:32:28 It might be worth checking that the tabs you get a
Anderson Silva 2016/07/11 19:47:51 Done. Checked in 2 results.
+ }
+
+ // Get discarded tabs after discarding one tab.
+ {
+ const char* kQueryInfo = "[{\"discarded\": true}]";
+ scoped_refptr<TabsQueryFunction> function = new TabsQueryFunction();
+ function->set_extension(test_util::CreateEmptyExtension().get());
+ std::unique_ptr<base::ListValue> result(
+ utils::ToList(utils::RunFunctionAndReturnSingleResult(
+ function.get(), kQueryInfo, browser())));
+
+ EXPECT_EQ(1u, result.get()->GetSize());
+ }
+
+ // Discards the other created tab.
+ EXPECT_TRUE(
+ tab_manager->DiscardTabById(reinterpret_cast<int64_t>(web_contents_b)));
+
+ // Get non-discarded tabs after discarding two created tabs.
+ {
+ const char* kQueryInfo = "[{\"discarded\": false}]";
+ scoped_refptr<TabsQueryFunction> function = new TabsQueryFunction();
+ function->set_extension(test_util::CreateEmptyExtension().get());
+ std::unique_ptr<base::ListValue> result(
+ utils::ToList(utils::RunFunctionAndReturnSingleResult(
+ function.get(), kQueryInfo, browser())));
+
+ EXPECT_EQ(1u, result.get()->GetSize());
+ }
+
+ // Get discarded tabs after discarding two created tabs.
+ {
+ const char* kQueryInfo = "[{\"discarded\": true}]";
+ scoped_refptr<TabsQueryFunction> function = new TabsQueryFunction();
+ function->set_extension(test_util::CreateEmptyExtension().get());
+ std::unique_ptr<base::ListValue> result(
+ utils::ToList(utils::RunFunctionAndReturnSingleResult(
+ function.get(), kQueryInfo, browser())));
+
+ EXPECT_EQ(2u, result.get()->GetSize());
+ }
+
+ auto tsm = browser()->tab_strip_model();
Devlin 2016/07/11 16:32:28 style guide says to avoid using abbreviation like
Anderson Silva 2016/07/11 19:47:51 Done.
+ tsm->ActivateTabAt(0, false);
+
+ // Get non-discarded tabs after activating a discarded tab.
+ {
+ const char* kQueryInfo = "[{\"discarded\": false}]";
+ scoped_refptr<TabsQueryFunction> function = new TabsQueryFunction();
+ function->set_extension(test_util::CreateEmptyExtension().get());
+ std::unique_ptr<base::ListValue> result(
+ utils::ToList(utils::RunFunctionAndReturnSingleResult(
+ function.get(), kQueryInfo, browser())));
+
+ EXPECT_EQ(2u, result.get()->GetSize());
+ }
+
+ // Get discarded tabs after activating a discarded tab.
+ {
+ const char* kQueryInfo = "[{\"discarded\": true}]";
+ scoped_refptr<TabsQueryFunction> function = new TabsQueryFunction();
+ function->set_extension(test_util::CreateEmptyExtension().get());
+ std::unique_ptr<base::ListValue> result(
+ utils::ToList(utils::RunFunctionAndReturnSingleResult(
+ function.get(), kQueryInfo, browser())));
+
+ EXPECT_EQ(1u, result.get()->GetSize());
+ }
+
+ browser()->tab_strip_model()->CloseAllTabs();
Devlin 2016/07/11 16:32:28 Is this necessary?
Anderson Silva 2016/07/11 19:47:51 Yes. Otherwise we crash at tear down.
Anderson Silva 2016/07/11 19:50:52 Yes. Because there's a check to make sure that Tab
+}
+
} // namespace extensions
« no previous file with comments | « chrome/browser/extensions/api/tabs/tabs_api.cc ('k') | chrome/browser/extensions/extension_tab_util.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698