Commit 279406a6 authored by Hailey Wang's avatar Hailey Wang Committed by Commit Bot

[PM] Add browsertests for BackgroundTabLoadingPolicy

Add a more throughout browsertest in addition to the basic browsertest.

Bug: 1059341
Change-Id: I0c1b5facf5d77914777462f69083070c01a2ad03
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2149634
Commit-Queue: Hailey Wang <haileywang@google.com>
Reviewed-by: default avatarSébastien Marchand <sebmarchand@chromium.org>
Reviewed-by: default avatarFrançois Doray <fdoray@chromium.org>
Cr-Commit-Position: refs/heads/master@{#759433}
parent f3756a6d
......@@ -4,6 +4,7 @@
#include "chrome/browser/performance_manager/graph/policies/background_tab_loading_policy.h"
#include "base/test/bind_test_util.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/browser/performance_manager/graph/policies/policy_features.h"
#include "chrome/browser/ui/browser.h"
......@@ -12,6 +13,7 @@
#include "chrome/common/buildflags.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/performance_manager/public/performance_manager.h"
#include "content/public/browser/notification_types.h"
namespace performance_manager {
......@@ -19,8 +21,7 @@ namespace performance_manager {
class BackgroundTabLoadingBrowserTest : public InProcessBrowserTest {
public:
BackgroundTabLoadingBrowserTest() {
base::test::ScopedFeatureList features;
features.InitAndEnableFeature(
features_.InitAndEnableFeature(
performance_manager::features::
kBackgroundTabLoadingFromPerformanceManager);
url_ = ui_test_utils::GetTestUrl(
......@@ -29,6 +30,16 @@ class BackgroundTabLoadingBrowserTest : public InProcessBrowserTest {
}
~BackgroundTabLoadingBrowserTest() override = default;
#if BUILDFLAG(ENABLE_SESSION_SERVICE)
void SetDefaultPropertiesForTesting(
policies::BackgroundTabLoadingPolicy* policy) {
// Set a value explicitly for MaxSimultaneousLoad threshold to avoid a
// dependency on the number of cores of the machine on which the test runs.
policy->SetMaxSimultaneousLoadsForTesting(1);
policy->SetFreeMemoryForTesting(150);
}
#endif
protected:
// Adds tabs to the given browser, all navigated to |url_|.
void AddNTabsToBrowser(Browser* browser, int number_of_tabs_to_add) {
......@@ -54,7 +65,26 @@ class BackgroundTabLoadingBrowserTest : public InProcessBrowserTest {
EXPECT_TRUE(content::WaitForLoadStop(tab));
}
void SetUpOnMainThread() override {
InProcessBrowserTest::SetUpOnMainThread();
// Configure BackgroundTabLoadingPolicy for the tests.
PerformanceManager::CallOnGraph(
FROM_HERE, base::BindLambdaForTesting([]() {
// Set a value explicitly for thresholds that depends on system
// information, to avoid flakiness when tests run in different
// environments.
policies::BackgroundTabLoadingPolicy* policy =
policies::BackgroundTabLoadingPolicy::GetInstance();
EXPECT_TRUE(policy);
policy->SetMaxSimultaneousLoadsForTesting(1);
policy->SetFreeMemoryForTesting(policies::BackgroundTabLoadingPolicy::
kDesiredAmountOfFreeMemoryMb);
}));
}
GURL url_;
base::test::ScopedFeatureList features_;
};
#if BUILDFLAG(ENABLE_SESSION_SERVICE)
......@@ -62,20 +92,21 @@ IN_PROC_BROWSER_TEST_F(BackgroundTabLoadingBrowserTest, RestoreTab) {
ui_test_utils::NavigateToURLWithDisposition(
browser(), url_, WindowOpenDisposition::NEW_WINDOW,
ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER);
Browser* restored_browser = BrowserList::GetInstance()->get(1);
Browser* browser_to_restore = BrowserList::GetInstance()->get(1);
// Add tabs and close browser.
const int kDesiredNumberOfTabs = 3;
AddNTabsToBrowser(
restored_browser,
kDesiredNumberOfTabs - restored_browser->tab_strip_model()->count());
EXPECT_EQ(kDesiredNumberOfTabs, restored_browser->tab_strip_model()->count());
CloseBrowserSynchronously(restored_browser);
browser_to_restore,
kDesiredNumberOfTabs - browser_to_restore->tab_strip_model()->count());
EXPECT_EQ(kDesiredNumberOfTabs,
browser_to_restore->tab_strip_model()->count());
CloseBrowserSynchronously(browser_to_restore);
// Restore recently closed window.
chrome::OpenWindowWithRestoredTabs(browser()->profile());
ASSERT_EQ(2U, BrowserList::GetInstance()->size());
restored_browser = BrowserList::GetInstance()->get(1);
Browser* restored_browser = BrowserList::GetInstance()->get(1);
EXPECT_EQ(kDesiredNumberOfTabs, restored_browser->tab_strip_model()->count());
EXPECT_EQ(kDesiredNumberOfTabs - 1,
......@@ -87,6 +118,48 @@ IN_PROC_BROWSER_TEST_F(BackgroundTabLoadingBrowserTest, RestoreTab) {
restored_browser->tab_strip_model()->GetWebContentsAt(i));
}
}
IN_PROC_BROWSER_TEST_F(BackgroundTabLoadingBrowserTest,
RestoredTabsAreLoadedGradually) {
ui_test_utils::NavigateToURLWithDisposition(
browser(), url_, WindowOpenDisposition::NEW_WINDOW,
ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER);
Browser* browser_to_restore = BrowserList::GetInstance()->get(1);
// Add tabs and close browser.
const int kDesiredNumberOfTabs =
policies::BackgroundTabLoadingPolicy::kMaxTabsToLoad + 1;
AddNTabsToBrowser(
browser_to_restore,
kDesiredNumberOfTabs - browser_to_restore->tab_strip_model()->count());
EXPECT_EQ(kDesiredNumberOfTabs,
browser_to_restore->tab_strip_model()->count());
CloseBrowserSynchronously(browser_to_restore);
// Restore recently closed window.
chrome::OpenWindowWithRestoredTabs(browser()->profile());
ASSERT_EQ(2U, BrowserList::GetInstance()->size());
Browser* restored_browser = BrowserList::GetInstance()->get(1);
EXPECT_EQ(kDesiredNumberOfTabs, restored_browser->tab_strip_model()->count());
EXPECT_EQ(kDesiredNumberOfTabs - 1,
restored_browser->tab_strip_model()->active_index());
// These tabs should be loaded by BackgroundTabLoadingPolicy.
EnsureTabFinishedRestoring(
restored_browser->tab_strip_model()->GetWebContentsAt(
kDesiredNumberOfTabs - 1));
for (int i = 0; i < kDesiredNumberOfTabs - 2; i++) {
EnsureTabFinishedRestoring(
restored_browser->tab_strip_model()->GetWebContentsAt(i));
}
// This tab shouldn't want to be loaded.
auto* contents = restored_browser->tab_strip_model()->GetWebContentsAt(
kDesiredNumberOfTabs - 2);
EXPECT_FALSE(contents->IsLoading());
EXPECT_TRUE(contents->GetController().NeedsReload());
}
#endif
} // namespace performance_manager
......@@ -18,6 +18,10 @@ namespace mechanism {
class PageLoader;
} // namespace mechanism
FORWARD_DECLARE_TEST(BackgroundTabLoadingBrowserTest,
RestoredTabsAreLoadedGradually);
class BackgroundTabLoadingBrowserTest;
namespace policies {
// This policy manages loading of background tabs created by session restore. It
......@@ -52,6 +56,8 @@ class BackgroundTabLoadingPolicy : public GraphOwned,
static BackgroundTabLoadingPolicy* GetInstance();
private:
friend class ::performance_manager::BackgroundTabLoadingBrowserTest;
// Holds a handful of data about a tab which is used to prioritize it during
// session restore.
struct PageNodeToLoadData {
......@@ -204,6 +210,9 @@ class BackgroundTabLoadingPolicy : public GraphOwned,
ShouldLoad_FreeMemory);
FRIEND_TEST_ALL_PREFIXES(BackgroundTabLoadingPolicyTest, ShouldLoad_OldTab);
FRIEND_TEST_ALL_PREFIXES(BackgroundTabLoadingPolicyTest, OnMemoryPressure);
FRIEND_TEST_ALL_PREFIXES(
::performance_manager::BackgroundTabLoadingBrowserTest,
RestoredTabsAreLoadedGradually);
};
} // namespace policies
......
......@@ -50,8 +50,7 @@ class BackgroundTabLoadingPolicyTest : public GraphTestHarness {
mock_loader_ = mock_loader.get();
policy_->SetMockLoaderForTesting(std::move(mock_loader));
// Set a value explicitly for MaxSimultaneousLoad threshold to avoid a
// dependency on the number of cores of the machine on which the test runs.
// Set a value explicitly for thresholds that depends on system information.
policy_->SetMaxSimultaneousLoadsForTesting(4);
policy_->SetFreeMemoryForTesting(150);
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment