| 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..9461cf71a51d3185777166057a0addc27c807095 100644
|
| --- a/chrome/browser/extensions/api/tabs/tabs_api_unittest.cc
|
| +++ b/chrome/browser/extensions/api/tabs/tabs_api_unittest.cc
|
| @@ -3,10 +3,13 @@
|
| // 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/api/tabs/tabs_constants.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 +18,14 @@
|
| #include "extensions/common/extension_builder.h"
|
| #include "extensions/common/test_util.h"
|
|
|
| +using memory::TabManager;
|
| +using content::WebContents;
|
| +
|
| namespace extensions {
|
|
|
| +namespace keys = tabs_constants;
|
| +namespace utils = extension_function_test_utils;
|
| +
|
| namespace {
|
|
|
| std::unique_ptr<base::ListValue> RunTabsQueryFunction(
|
| @@ -209,4 +218,150 @@ TEST_F(TabsApiUnitTest, QueryWithHostPermission) {
|
| }
|
| }
|
|
|
| +TEST_F(TabsApiUnitTest, QueryDiscarded) {
|
| + ASSERT_TRUE(g_browser_process && g_browser_process->GetTabManager());
|
| + memory::TabManager* tab_manager = g_browser_process->GetTabManager();
|
| +
|
| + // Create three tabs.
|
| + content::OpenURLParams params(GURL(url::kAboutBlankURL), content::Referrer(),
|
| + NEW_FOREGROUND_TAB, ui::PAGE_TRANSITION_LINK,
|
| + false);
|
| + content::WebContents* web_contents_a = browser()->OpenURL(params);
|
| + content::WebContents* web_contents_b = browser()->OpenURL(params);
|
| + browser()->OpenURL(params);
|
| +
|
| + // 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->GetSize());
|
| + }
|
| +
|
| + // 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->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->GetSize());
|
| + }
|
| +
|
| + TabStripModel* tab_strip_model = browser()->tab_strip_model();
|
| +
|
| + // 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->GetSize());
|
| +
|
| + // Make sure the returned tab is the correct one.
|
| + int tab_id_a =
|
| + ExtensionTabUtil::GetTabId(tab_strip_model->GetWebContentsAt(0));
|
| +
|
| + int id = -1;
|
| + base::Value* tab = nullptr;
|
| + EXPECT_TRUE(result->Get(0, &tab));
|
| + utils::ToDictionary(tab)->GetInteger(keys::kIdKey, &id);
|
| +
|
| + EXPECT_EQ(tab_id_a, id);
|
| + }
|
| +
|
| + // Discards another 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->GetSize());
|
| +
|
| + // Make sure the returned tab is the correct one.
|
| + int tab_id_c =
|
| + ExtensionTabUtil::GetTabId(tab_strip_model->GetWebContentsAt(2));
|
| +
|
| + int id = -1;
|
| + base::Value* tab = nullptr;
|
| + EXPECT_TRUE(result->Get(0, &tab));
|
| + utils::ToDictionary(tab)->GetInteger(keys::kIdKey, &id);
|
| +
|
| + EXPECT_EQ(tab_id_c, id);
|
| + }
|
| +
|
| + // 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->GetSize());
|
| + }
|
| +
|
| + // Activates the first tab.
|
| + tab_strip_model->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->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->GetSize());
|
| + }
|
| +
|
| + browser()->tab_strip_model()->CloseAllTabs();
|
| +}
|
| +
|
| } // namespace extensions
|
|
|