Commit 38a39f60 authored by Nicolas Ouellet-Payeur's avatar Nicolas Ouellet-Payeur Committed by Commit Bot

Revert "[BrowserSwitcher] Deflake BrowserSwitcherServiceTest"

This reverts commit 54aa10ad.

Reason for revert: crbug.com/1073010

Original change's description:
> [BrowserSwitcher] Deflake BrowserSwitcherServiceTest
> 
> Previously, the tests waited for action_timeout(), to wait for LBS to
> load the rules and write some files. As expected, this is fairly flaky,
> and it slows down the tests.
> 
> Now, `BrowserSwitcherService' exposes a new event for testing, which
> fires once LBS has loaded the rules. This should reduce flakes by
> listening for the event instead of using an unreliable timeout.
> 
> Also rewrote some tests in a synchronous style, which improves
> readability.
> 
> Bug: 1044619, 1070400
> Change-Id: If1a2093cfe3bface73a46896ff2bc95b80ab2f2e
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2152885
> Commit-Queue: Nicolas Ouellet-Payeur <nicolaso@chromium.org>
> Reviewed-by: Julian Pastarmov <pastarmovj@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#760531}

TBR=pastarmovj@chromium.org,nicolaso@chromium.org

Change-Id: If573be05ce1a3d4a34449bd6038f16129cb4d1fa
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Bug: 1044619, 1070400
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2159313Reviewed-by: default avatarNicolas Ouellet-Payeur <nicolaso@chromium.org>
Commit-Queue: Nicolas Ouellet-Payeur <nicolaso@chromium.org>
Cr-Commit-Position: refs/heads/master@{#760985}
parent 920d8f93
......@@ -239,11 +239,6 @@ void BrowserSwitcherService::Init() {
StartDownload(fetch_delay());
}
void BrowserSwitcherService::OnAllRulesetsLoadedForTesting(
base::OnceCallback<void()> cb) {
all_rulesets_loaded_callback_for_testing_ = std::move(cb);
}
void BrowserSwitcherService::StartDownload(base::TimeDelta delay) {
// This destroys the previous XmlDownloader, which cancels any scheduled
// refresh operations.
......@@ -324,8 +319,6 @@ void BrowserSwitcherService::LoadRulesFromPrefs() {
void BrowserSwitcherService::OnAllRulesetsParsed() {
callback_list_.Notify(this);
if (all_rulesets_loaded_callback_for_testing_)
std::move(all_rulesets_loaded_callback_for_testing_).Run();
}
std::unique_ptr<BrowserSwitcherService::CallbackSubscription>
......
......@@ -124,8 +124,6 @@ class BrowserSwitcherService : public KeyedService {
explicit BrowserSwitcherService(Profile* profile);
~BrowserSwitcherService() override;
virtual void Init();
// KeyedService:
void Shutdown() override;
......@@ -151,10 +149,7 @@ class BrowserSwitcherService : public KeyedService {
// happens.
virtual void LoadRulesFromPrefs();
// Called after all XML rulesets finished downloading, and the rules are
// applied. The XML is downloaded asynchronously, so browser tests use this
// event to check that they applied correctly.
void OnAllRulesetsLoadedForTesting(base::OnceCallback<void()> callback);
void Init();
protected:
virtual void OnAllRulesetsParsed();
......@@ -199,8 +194,6 @@ class BrowserSwitcherService : public KeyedService {
// CallbackList for OnAllRulesetsParsed() listeners.
base::CallbackList<AllRulesetsParsedCallbackSignature> callback_list_;
base::OnceCallback<void()> all_rulesets_loaded_callback_for_testing_;
// Per-profile helpers.
std::unique_ptr<AlternativeBrowserDriver> driver_;
std::unique_ptr<BrowserSwitcherSitelist> sitelist_;
......
......@@ -30,6 +30,8 @@
#include "url/gurl.h"
#if defined(OS_WIN)
#include "base/files/scoped_temp_dir.h"
#include "base/path_service.h"
#include "chrome/browser/browser_switcher/browser_switcher_policy_migrator.h"
#include "chrome/browser/browser_switcher/browser_switcher_service_win.h"
#include "chrome/browser/extensions/extension_service.h"
......@@ -102,15 +104,14 @@ class BrowserSwitcherServiceTest : public InProcessBrowserTest {
.WillRepeatedly(testing::Return(true));
policy::BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_);
BrowserSwitcherService::SetFetchDelayForTesting(base::TimeDelta());
BrowserSwitcherService::SetRefreshDelayForTesting(base::TimeDelta());
BrowserSwitcherService::SetRefreshDelayForTesting(action_timeout() * 3 / 2);
#if defined(OS_WIN)
ASSERT_TRUE(fake_appdata_dir_.CreateUniqueTempDir());
#endif
}
void SetUpOnMainThread() override {
#if defined(OS_WIN)
fake_appdata_dir_ =
browser()->profile()->GetPath().AppendASCII("FakeAppData");
ASSERT_TRUE(DirectoryExists(fake_appdata_dir_) ||
CreateDirectory(fake_appdata_dir_));
BrowserSwitcherServiceFactory::GetInstance()->SetTestingFactory(
browser()->profile(),
base::BindRepeating(
......@@ -142,48 +143,20 @@ class BrowserSwitcherServiceTest : public InProcessBrowserTest {
base::RunLoop().RunUntilIdle();
}
void WaitForRefresh() {
base::RunLoop run_loop;
GetService()->OnAllRulesetsLoadedForTesting(run_loop.QuitClosure());
run_loop.Run();
}
void WaitForActionTimeout() {
base::RunLoop run_loop;
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE, run_loop.QuitClosure(), TestTimeouts::action_timeout());
run_loop.Run();
}
BrowserSwitcherService* GetService() {
return BrowserSwitcherServiceFactory::GetForBrowserContext(
browser()->profile());
}
policy::MockConfigurationPolicyProvider& policy_provider() {
return provider_;
}
#if defined(OS_WIN)
BrowserSwitcherServiceWin* GetServiceWin() {
return static_cast<BrowserSwitcherServiceWin*>(GetService());
base::TimeDelta action_timeout() {
// Makes the tests a little less slow.
return TestTimeouts::action_timeout() / 2;
}
void WaitForCacheFile() {
base::RunLoop run_loop;
GetServiceWin()->OnCacheFileUpdatedForTesting(run_loop.QuitClosure());
run_loop.Run();
}
void WaitForSitelistCacheFile() {
base::RunLoop run_loop;
GetServiceWin()->OnSitelistCacheFileUpdatedForTesting(
run_loop.QuitClosure());
run_loop.Run();
#if defined(OS_WIN)
const base::FilePath& appdata_dir() const {
return fake_appdata_dir_.GetPath();
}
const base::FilePath& appdata_dir() const { return fake_appdata_dir_; }
const base::FilePath cache_dir() const {
return appdata_dir().AppendASCII("Google").AppendASCII("BrowserSwitcher");
}
......@@ -201,7 +174,7 @@ class BrowserSwitcherServiceTest : public InProcessBrowserTest {
policy::MockConfigurationPolicyProvider provider_;
#if defined(OS_WIN)
base::FilePath fake_appdata_dir_;
base::ScopedTempDir fake_appdata_dir_;
#endif
};
......@@ -220,9 +193,18 @@ IN_PROC_BROWSER_TEST_F(BrowserSwitcherServiceTest, ExternalSitelistInvalidUrl) {
&fetch_happened));
// Execute everything and make sure we didn't get to the fetch step.
GetService();
WaitForActionTimeout();
EXPECT_FALSE(fetch_happened);
BrowserSwitcherServiceFactory::GetForBrowserContext(browser()->profile());
base::RunLoop run_loop;
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::BindOnce(
[](bool* happened, base::OnceClosure quit) {
EXPECT_FALSE(*happened);
std::move(quit).Run();
},
&fetch_happened, run_loop.QuitClosure()),
action_timeout());
run_loop.Run();
}
IN_PROC_BROWSER_TEST_F(BrowserSwitcherServiceTest,
......@@ -246,17 +228,32 @@ IN_PROC_BROWSER_TEST_F(BrowserSwitcherServiceTest,
&counter));
// Execute everything and make sure the rules are applied correctly.
auto* service = GetService();
WaitForRefresh();
EXPECT_FALSE(ShouldSwitch(service, GURL("http://google.com/")));
EXPECT_TRUE(ShouldSwitch(service, GURL("http://docs.google.com/")));
EXPECT_FALSE(ShouldSwitch(service, GURL("http://yahoo.com/")));
WaitForRefresh();
EXPECT_FALSE(ShouldSwitch(service, GURL("http://google.com/")));
EXPECT_FALSE(ShouldSwitch(service, GURL("http://docs.google.com/")));
EXPECT_TRUE(ShouldSwitch(service, GURL("http://yahoo.com/")));
auto* service =
BrowserSwitcherServiceFactory::GetForBrowserContext(browser()->profile());
base::RunLoop run_loop;
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::BindOnce(
[](BrowserSwitcherService* service) {
EXPECT_FALSE(ShouldSwitch(service, GURL("http://google.com/")));
EXPECT_TRUE(ShouldSwitch(service, GURL("http://docs.google.com/")));
EXPECT_FALSE(ShouldSwitch(service, GURL("http://yahoo.com/")));
},
service),
action_timeout());
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::BindOnce(
[](BrowserSwitcherService* service, base::OnceClosure quit) {
EXPECT_FALSE(ShouldSwitch(service, GURL("http://google.com/")));
EXPECT_FALSE(
ShouldSwitch(service, GURL("http://docs.google.com/")));
EXPECT_TRUE(ShouldSwitch(service, GURL("http://yahoo.com/")));
std::move(quit).Run();
},
service, run_loop.QuitClosure()),
action_timeout() * 2);
run_loop.Run();
}
IN_PROC_BROWSER_TEST_F(BrowserSwitcherServiceTest,
......@@ -279,16 +276,31 @@ IN_PROC_BROWSER_TEST_F(BrowserSwitcherServiceTest,
&counter));
// Execute everything and make sure the rules are applied correctly.
auto* service = GetService();
WaitForRefresh();
EXPECT_FALSE(ShouldSwitch(service, GURL("http://google.com/")));
EXPECT_FALSE(ShouldSwitch(service, GURL("http://docs.google.com/")));
EXPECT_FALSE(ShouldSwitch(service, GURL("http://yahoo.com/")));
WaitForRefresh();
EXPECT_FALSE(ShouldSwitch(service, GURL("http://google.com/")));
EXPECT_TRUE(ShouldSwitch(service, GURL("http://docs.google.com/")));
auto* service =
BrowserSwitcherServiceFactory::GetForBrowserContext(browser()->profile());
base::RunLoop run_loop;
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::BindOnce(
[](BrowserSwitcherService* service) {
EXPECT_FALSE(ShouldSwitch(service, GURL("http://google.com/")));
EXPECT_FALSE(
ShouldSwitch(service, GURL("http://docs.google.com/")));
EXPECT_FALSE(ShouldSwitch(service, GURL("http://yahoo.com/")));
},
service),
action_timeout());
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::BindOnce(
[](BrowserSwitcherService* service, base::OnceClosure quit) {
EXPECT_FALSE(ShouldSwitch(service, GURL("http://google.com/")));
EXPECT_TRUE(ShouldSwitch(service, GURL("http://docs.google.com/")));
std::move(quit).Run();
},
service, run_loop.QuitClosure()),
action_timeout() * 2);
run_loop.Run();
}
IN_PROC_BROWSER_TEST_F(BrowserSwitcherServiceTest,
......@@ -299,16 +311,43 @@ IN_PROC_BROWSER_TEST_F(BrowserSwitcherServiceTest,
content::URLLoaderInterceptor interceptor(
base::BindRepeating(ReturnValidXml));
auto* service = GetService();
EXPECT_FALSE(ShouldSwitch(service, GURL("http://docs.google.com/")));
SetExternalUrl(kAValidUrl);
WaitForRefresh();
EXPECT_TRUE(ShouldSwitch(service, GURL("http://docs.google.com/")));
SetExternalUrl(kAnInvalidUrl);
EXPECT_FALSE(ShouldSwitch(service, GURL("http://docs.google.com/")));
auto* service =
BrowserSwitcherServiceFactory::GetForBrowserContext(browser()->profile());
base::RunLoop run_loop;
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::BindOnce(
[](BrowserSwitcherService* service,
BrowserSwitcherServiceTest* test) {
EXPECT_FALSE(
ShouldSwitch(service, GURL("http://docs.google.com/")));
// This will cause the sitelist to be downloaded.
test->SetExternalUrl(kAValidUrl);
},
service, this),
action_timeout());
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::BindOnce(
[](BrowserSwitcherService* service,
BrowserSwitcherServiceTest* test) {
EXPECT_TRUE(ShouldSwitch(service, GURL("http://docs.google.com/")));
// This will cause the sitelist to be cleared again.
test->SetExternalUrl(kAnInvalidUrl);
},
service, this),
action_timeout() * 2);
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::BindOnce(
[](BrowserSwitcherService* service, base::OnceClosure quit) {
EXPECT_FALSE(
ShouldSwitch(service, GURL("http://docs.google.com/")));
std::move(quit).Run();
},
service, run_loop.QuitClosure()),
action_timeout() * 3);
run_loop.Run();
}
IN_PROC_BROWSER_TEST_F(BrowserSwitcherServiceTest, ExternalFileUrl) {
......@@ -322,11 +361,20 @@ IN_PROC_BROWSER_TEST_F(BrowserSwitcherServiceTest, ExternalFileUrl) {
SetExternalUrl(net::FilePathToFileURL(sitelist_path).spec());
// Execute everything and make sure the rules are applied correctly.
auto* service = GetService();
WaitForRefresh();
EXPECT_FALSE(ShouldSwitch(service, GURL("http://google.com/")));
EXPECT_TRUE(ShouldSwitch(service, GURL("http://docs.google.com/")));
auto* service =
BrowserSwitcherServiceFactory::GetForBrowserContext(browser()->profile());
base::RunLoop run_loop;
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::BindOnce(
[](BrowserSwitcherService* service, base::OnceClosure quit) {
EXPECT_FALSE(ShouldSwitch(service, GURL("http://google.com/")));
EXPECT_TRUE(ShouldSwitch(service, GURL("http://docs.google.com/")));
std::move(quit).Run();
},
service, run_loop.QuitClosure()),
action_timeout());
run_loop.Run();
}
IN_PROC_BROWSER_TEST_F(BrowserSwitcherServiceTest,
......@@ -337,11 +385,21 @@ IN_PROC_BROWSER_TEST_F(BrowserSwitcherServiceTest,
base::BindRepeating(FailToDownload));
// Execute everything and make sure no rules are applied.
auto* service = GetService();
WaitForRefresh();
EXPECT_FALSE(ShouldSwitch(service, GURL("http://google.com/")));
EXPECT_FALSE(ShouldSwitch(service, GURL("http://docs.google.com/")));
auto* service =
BrowserSwitcherServiceFactory::GetForBrowserContext(browser()->profile());
base::RunLoop run_loop;
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::BindOnce(
[](BrowserSwitcherService* service, base::OnceClosure quit) {
EXPECT_FALSE(ShouldSwitch(service, GURL("http://google.com/")));
EXPECT_FALSE(
ShouldSwitch(service, GURL("http://docs.google.com/")));
std::move(quit).Run();
},
service, run_loop.QuitClosure()),
action_timeout());
run_loop.Run();
}
IN_PROC_BROWSER_TEST_F(BrowserSwitcherServiceTest,
......@@ -359,9 +417,18 @@ IN_PROC_BROWSER_TEST_F(BrowserSwitcherServiceTest,
&fetch_happened));
// Execute everything and make sure we didn't get to the fetch step.
GetService();
WaitForActionTimeout();
EXPECT_FALSE(fetch_happened);
BrowserSwitcherServiceFactory::GetForBrowserContext(browser()->profile());
base::RunLoop run_loop;
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::BindOnce(
[](bool* happened, base::OnceClosure quit) {
EXPECT_FALSE(*happened);
std::move(quit).Run();
},
&fetch_happened, run_loop.QuitClosure()),
action_timeout());
run_loop.Run();
}
IN_PROC_BROWSER_TEST_F(BrowserSwitcherServiceTest,
......@@ -381,11 +448,22 @@ IN_PROC_BROWSER_TEST_F(BrowserSwitcherServiceTest,
base::BindRepeating(ReturnValidXml));
// Execute everything and make sure the rules are applied correctly.
auto* service = GetService();
WaitForRefresh();
EXPECT_TRUE(ShouldSwitch(service, GURL("http://google.com/")));
EXPECT_FALSE(ShouldSwitch(service, GURL("http://docs.google.com/")));
EXPECT_TRUE(ShouldSwitch(service, GURL("http://yahoo.com/")));
auto* service =
BrowserSwitcherServiceFactory::GetForBrowserContext(browser()->profile());
base::RunLoop run_loop;
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::BindOnce(
[](BrowserSwitcherService* service, base::OnceClosure quit) {
EXPECT_TRUE(ShouldSwitch(service, GURL("http://google.com/")));
EXPECT_FALSE(
ShouldSwitch(service, GURL("http://docs.google.com/")));
EXPECT_TRUE(ShouldSwitch(service, GURL("http://yahoo.com/")));
std::move(quit).Run();
},
service, run_loop.QuitClosure()),
action_timeout());
run_loop.Run();
}
IN_PROC_BROWSER_TEST_F(BrowserSwitcherServiceTest,
......@@ -396,11 +474,21 @@ IN_PROC_BROWSER_TEST_F(BrowserSwitcherServiceTest,
base::BindRepeating(&ReturnValidXml));
// Execute everything and make sure the rules are applied correctly.
auto* service = GetService();
WaitForRefresh();
EXPECT_FALSE(ShouldSwitch(service, GURL("http://google.com/")));
EXPECT_TRUE(ShouldSwitch(service, GURL("http://docs.google.com/")));
EXPECT_FALSE(ShouldSwitch(service, GURL("http://yahoo.com/")));
auto* service =
BrowserSwitcherServiceFactory::GetForBrowserContext(browser()->profile());
base::RunLoop run_loop;
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::BindOnce(
[](BrowserSwitcherService* service, base::OnceClosure quit) {
EXPECT_FALSE(ShouldSwitch(service, GURL("http://google.com/")));
EXPECT_TRUE(ShouldSwitch(service, GURL("http://docs.google.com/")));
EXPECT_FALSE(ShouldSwitch(service, GURL("http://yahoo.com/")));
std::move(quit).Run();
},
service, run_loop.QuitClosure()),
TestTimeouts::action_timeout());
run_loop.Run();
}
IN_PROC_BROWSER_TEST_F(BrowserSwitcherServiceTest,
......@@ -411,9 +499,11 @@ IN_PROC_BROWSER_TEST_F(BrowserSwitcherServiceTest,
base::BindRepeating(&ReturnValidXml));
// Execute everything and make sure the rules are applied correctly.
auto* service = GetService();
auto* service =
BrowserSwitcherServiceFactory::GetForBrowserContext(browser()->profile());
// No timeout here, since we're checking that the rules get applied *before*
// downloading.
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(ShouldSwitch(service, GURL("http://google.com/")));
EXPECT_TRUE(ShouldSwitch(service, GURL("http://docs.google.com/")));
EXPECT_FALSE(ShouldSwitch(service, GURL("http://yahoo.com/")));
......@@ -436,9 +526,18 @@ IN_PROC_BROWSER_TEST_F(BrowserSwitcherServiceTest, IeemSitelistInvalidUrl) {
&fetch_happened));
// Execute everything and make sure we didn't get to the fetch step.
GetService();
WaitForActionTimeout();
EXPECT_FALSE(fetch_happened);
BrowserSwitcherServiceFactory::GetForBrowserContext(browser()->profile());
base::RunLoop run_loop;
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::BindOnce(
[](bool* happened, base::OnceClosure quit) {
EXPECT_FALSE(*happened);
std::move(quit).Run();
},
&fetch_happened, run_loop.QuitClosure()),
action_timeout());
run_loop.Run();
}
IN_PROC_BROWSER_TEST_F(BrowserSwitcherServiceTest,
......@@ -450,13 +549,25 @@ IN_PROC_BROWSER_TEST_F(BrowserSwitcherServiceTest,
base::BindRepeating(&ReturnValidXml));
// Execute everything and make sure the rules are applied correctly.
auto* service = GetService();
WaitForRefresh();
EXPECT_FALSE(ShouldSwitch(service, GURL("http://google.com/")));
EXPECT_TRUE(ShouldSwitch(service, GURL("http://docs.google.com/")));
auto* service =
BrowserSwitcherServiceFactory::GetForBrowserContext(browser()->profile());
base::RunLoop run_loop;
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::BindOnce(
[](BrowserSwitcherService* service, base::OnceClosure quit) {
EXPECT_FALSE(ShouldSwitch(service, GURL("http://google.com/")));
EXPECT_TRUE(ShouldSwitch(service, GURL("http://docs.google.com/")));
std::move(quit).Run();
},
service, run_loop.QuitClosure()),
action_timeout());
run_loop.Run();
}
IN_PROC_BROWSER_TEST_F(BrowserSwitcherServiceTest, IeemIgnoresFailedDownload) {
// Disabled test due to flaky failures on Win 7: crbug.com/1044619
IN_PROC_BROWSER_TEST_F(BrowserSwitcherServiceTest,
DISABLED_IeemIgnoresFailedDownload) {
SetUseIeSitelist(true);
BrowserSwitcherServiceWin::SetIeemSitelistUrlForTesting(kAValidUrl);
......@@ -464,11 +575,21 @@ IN_PROC_BROWSER_TEST_F(BrowserSwitcherServiceTest, IeemIgnoresFailedDownload) {
base::BindRepeating(FailToDownload));
// Execute everything and make sure no rules are applied.
auto* service = GetService();
WaitForRefresh();
EXPECT_FALSE(ShouldSwitch(service, GURL("http://google.com/")));
EXPECT_FALSE(ShouldSwitch(service, GURL("http://docs.google.com/")));
auto* service =
BrowserSwitcherServiceFactory::GetForBrowserContext(browser()->profile());
base::RunLoop run_loop;
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::BindOnce(
[](BrowserSwitcherService* service, base::OnceClosure quit) {
EXPECT_FALSE(ShouldSwitch(service, GURL("http://google.com/")));
EXPECT_FALSE(
ShouldSwitch(service, GURL("http://docs.google.com/")));
std::move(quit).Run();
},
service, run_loop.QuitClosure()),
action_timeout());
run_loop.Run();
}
IN_PROC_BROWSER_TEST_F(BrowserSwitcherServiceTest, IeemIgnoresNonManagedPref) {
......@@ -485,9 +606,18 @@ IN_PROC_BROWSER_TEST_F(BrowserSwitcherServiceTest, IeemIgnoresNonManagedPref) {
&fetch_happened));
// Execute everything and make sure we didn't get to the fetch step.
GetService();
WaitForActionTimeout();
EXPECT_FALSE(fetch_happened);
BrowserSwitcherServiceFactory::GetForBrowserContext(browser()->profile());
base::RunLoop run_loop;
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::BindOnce(
[](bool* happened, base::OnceClosure quit) {
EXPECT_FALSE(*happened);
std::move(quit).Run();
},
&fetch_happened, run_loop.QuitClosure()),
action_timeout());
run_loop.Run();
}
IN_PROC_BROWSER_TEST_F(BrowserSwitcherServiceTest, IeemListensForPrefChanges) {
......@@ -498,15 +628,43 @@ IN_PROC_BROWSER_TEST_F(BrowserSwitcherServiceTest, IeemListensForPrefChanges) {
content::URLLoaderInterceptor interceptor(
base::BindRepeating(ReturnValidXml));
auto* service = GetService();
EXPECT_FALSE(ShouldSwitch(service, GURL("http://docs.google.com/")));
SetUseIeSitelist(true);
WaitForRefresh();
EXPECT_TRUE(ShouldSwitch(service, GURL("http://docs.google.com/")));
SetUseIeSitelist(false);
EXPECT_FALSE(ShouldSwitch(service, GURL("http://docs.google.com/")));
auto* service =
BrowserSwitcherServiceFactory::GetForBrowserContext(browser()->profile());
base::RunLoop run_loop;
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::BindOnce(
[](BrowserSwitcherService* service,
BrowserSwitcherServiceTest* test) {
EXPECT_FALSE(
ShouldSwitch(service, GURL("http://docs.google.com/")));
// This will cause the sitelist to be downloaded.
test->SetUseIeSitelist(true);
},
service, this),
action_timeout());
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::BindOnce(
[](BrowserSwitcherService* service,
BrowserSwitcherServiceTest* test) {
EXPECT_TRUE(ShouldSwitch(service, GURL("http://docs.google.com/")));
// This will cause the sitelist to be cleared again.
test->SetUseIeSitelist(false);
},
service, this),
action_timeout() * 2);
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::BindOnce(
[](BrowserSwitcherService* service, base::OnceClosure quit) {
EXPECT_FALSE(
ShouldSwitch(service, GURL("http://docs.google.com/")));
std::move(quit).Run();
},
service, run_loop.QuitClosure()),
action_timeout() * 3);
run_loop.Run();
}
IN_PROC_BROWSER_TEST_F(BrowserSwitcherServiceTest, WritesPrefsToCacheFile) {
......@@ -536,27 +694,43 @@ IN_PROC_BROWSER_TEST_F(BrowserSwitcherServiceTest, WritesPrefsToCacheFile) {
base::RunLoop().RunUntilIdle();
// Execute everything and check "cache.dat" file contents.
GetService();
WaitForCacheFile();
const char expected_output[] =
"1\n"
"IExplore.exe\n"
"--bogus-flag\n"
"chrome.exe\n"
"--force-dark-mode\n"
"1\n"
"example.com\n"
"1\n"
"foo.example.com\n";
base::ScopedAllowBlockingForTesting allow_blocking;
std::string output;
EXPECT_TRUE(base::ReadFileToString(cache_file_path(), &output));
EXPECT_EQ(expected_output, output);
// Check that sitelistcache.dat doesn't exist.
EXPECT_FALSE(base::PathExists(sitelist_cache_file_path()));
BrowserSwitcherServiceFactory::GetForBrowserContext(browser()->profile());
base::RunLoop run_loop;
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::BindOnce(
[](base::FilePath cache_file_path,
base::FilePath sitelist_cache_file_path, base::OnceClosure quit) {
base::ScopedAllowBlockingForTesting allow_blocking;
base::File file(cache_file_path,
base::File::FLAG_OPEN | base::File::FLAG_READ);
ASSERT_TRUE(file.IsValid());
const char expected_output[] =
"1\n"
"IExplore.exe\n"
"--bogus-flag\n"
"chrome.exe\n"
"--force-dark-mode\n"
"1\n"
"example.com\n"
"1\n"
"foo.example.com\n";
std::unique_ptr<char[]> buffer(new char[file.GetLength() + 1]);
buffer.get()[file.GetLength()] = '\0';
file.Read(0, buffer.get(), file.GetLength());
EXPECT_EQ(std::string(expected_output), std::string(buffer.get()));
// Check that sitelistcache.dat doesn't exist.
EXPECT_FALSE(base::PathExists(sitelist_cache_file_path));
std::move(quit).Run();
},
cache_file_path(), sitelist_cache_file_path(),
run_loop.QuitClosure()),
action_timeout());
run_loop.Run();
}
IN_PROC_BROWSER_TEST_F(BrowserSwitcherServiceTest, WritesSitelistsToCacheFile) {
......@@ -595,38 +769,51 @@ IN_PROC_BROWSER_TEST_F(BrowserSwitcherServiceTest, WritesSitelistsToCacheFile) {
// Execute everything and check "cache.dat" file contents. It should
// contain the *union* of both sitelists, not just one of them.
GetService();
// LBS will write to cache.dat twice: once before downloading the XML files,
// and then once after. We're interested in the second state, so wait for 2
// writes to cache.dat.
WaitForCacheFile();
WaitForCacheFile();
base::FilePath expected_chrome_path;
base::FilePath::CharType chrome_path[MAX_PATH];
BrowserSwitcherServiceFactory::GetForBrowserContext(browser()->profile());
base::RunLoop run_loop;
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::BindOnce(
[](base::FilePath cache_file_path,
base::FilePath sitelist_cache_file_path, base::OnceClosure quit) {
base::ScopedAllowBlockingForTesting allow_blocking;
base::File file(cache_file_path,
base::File::FLAG_OPEN | base::File::FLAG_READ);
ASSERT_TRUE(file.IsValid());
base::FilePath expected_chrome_path;
base::FilePath::CharType chrome_path[MAX_PATH];
#if defined(OS_WIN)
::GetModuleFileName(nullptr, chrome_path, ARRAYSIZE(chrome_path));
expected_chrome_path = base::FilePath(chrome_path);
::GetModuleFileName(NULL, chrome_path, ARRAYSIZE(chrome_path));
expected_chrome_path = base::FilePath(chrome_path);
#endif
std::string expected_output = base::StringPrintf(
"1\n"
"\n"
"\n"
"%s\n"
"\n"
"2\n"
"docs.google.com\n"
"yahoo.com\n"
"1\n"
"greylist.invalid.com\n",
expected_chrome_path.MaybeAsASCII().c_str());
std::string output;
EXPECT_TRUE(base::ReadFileToString(cache_file_path(), &output));
EXPECT_EQ(expected_output, output);
// Check that sitelistcache.dat doesn't exist.
EXPECT_FALSE(base::PathExists(sitelist_cache_file_path()));
std::string expected_output = base::StringPrintf(
"1\n"
"\n"
"\n"
"%s\n"
"\n"
"2\n"
"docs.google.com\n"
"yahoo.com\n"
"1\n"
"greylist.invalid.com\n",
expected_chrome_path.MaybeAsASCII().c_str());
std::unique_ptr<char[]> buffer(new char[file.GetLength() + 1]);
buffer.get()[file.GetLength()] = '\0';
file.Read(0, buffer.get(), file.GetLength());
EXPECT_EQ(expected_output, std::string(buffer.get()));
// Check that sitelistcache.dat doesn't exist.
EXPECT_FALSE(base::PathExists(sitelist_cache_file_path));
std::move(quit).Run();
},
cache_file_path(), sitelist_cache_file_path(),
run_loop.QuitClosure()),
action_timeout());
run_loop.Run();
}
IN_PROC_BROWSER_TEST_F(BrowserSwitcherServiceTest,
......@@ -638,15 +825,19 @@ IN_PROC_BROWSER_TEST_F(BrowserSwitcherServiceTest,
base::BindRepeating(&ReturnValidXml));
// Execute everything and check "cache.dat" file contents.
GetService();
// LBS will write to cache.dat twice: once before downloading the XML files,
// and then once after. We're interested in the second state, so wait for 2
// writes to cache.dat.
WaitForCacheFile();
WaitForCacheFile();
base::ScopedAllowBlockingForTesting allow_blocking;
ASSERT_TRUE(base::PathExists(cache_file_path()));
BrowserSwitcherServiceFactory::GetForBrowserContext(browser()->profile());
base::RunLoop run_loop;
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::BindOnce(
[](base::FilePath cache_file_path, base::OnceClosure quit) {
base::ScopedAllowBlockingForTesting allow_blocking;
ASSERT_TRUE(base::PathExists(base::FilePath(cache_file_path)));
std::move(quit).Run();
},
cache_file_path(), run_loop.QuitClosure()),
action_timeout());
run_loop.Run();
}
IN_PROC_BROWSER_TEST_F(BrowserSwitcherServiceTest, CacheFileCorrectOnStartup) {
......@@ -661,32 +852,45 @@ IN_PROC_BROWSER_TEST_F(BrowserSwitcherServiceTest, CacheFileCorrectOnStartup) {
base::BindRepeating(&ReturnValidXml));
// Execute everything and check "cache.dat" file contents.
GetService();
WaitForCacheFile();
base::ScopedAllowBlockingForTesting allow_blocking;
base::File file(cache_file_path(),
base::File::FLAG_OPEN | base::File::FLAG_READ);
ASSERT_TRUE(base::PathExists(cache_file_path()));
ASSERT_TRUE(file.IsValid());
base::FilePath expected_chrome_path;
base::FilePath::CharType chrome_path[MAX_PATH];
::GetModuleFileName(nullptr, chrome_path, ARRAYSIZE(chrome_path));
expected_chrome_path = base::FilePath(chrome_path);
std::string expected_output = base::StringPrintf(
"1\n"
"\n"
"\n"
"%s\n"
"\n"
"1\n"
"docs.google.com\n"
"0\n",
expected_chrome_path.MaybeAsASCII().c_str());
std::string output;
EXPECT_TRUE(base::ReadFileToString(cache_file_path(), &output));
EXPECT_EQ(expected_output, output);
BrowserSwitcherServiceFactory::GetForBrowserContext(browser()->profile());
base::RunLoop run_loop;
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::BindOnce(
[](base::FilePath cache_file_path,
base::FilePath sitelist_cache_file_path, base::OnceClosure quit) {
base::ScopedAllowBlockingForTesting allow_blocking;
base::File file(cache_file_path,
base::File::FLAG_OPEN | base::File::FLAG_READ);
ASSERT_TRUE(file.IsValid());
base::FilePath expected_chrome_path;
base::FilePath::CharType chrome_path[MAX_PATH];
#if defined(OS_WIN)
::GetModuleFileName(NULL, chrome_path, ARRAYSIZE(chrome_path));
expected_chrome_path = base::FilePath(chrome_path);
#endif
std::string expected_output = base::StringPrintf(
"1\n"
"\n"
"\n"
"%s\n"
"\n"
"1\n"
"docs.google.com\n"
"0\n",
expected_chrome_path.MaybeAsASCII().c_str());
std::unique_ptr<char[]> buffer(new char[file.GetLength() + 1]);
buffer.get()[file.GetLength()] = '\0';
file.Read(0, buffer.get(), file.GetLength());
EXPECT_EQ(expected_output, std::string(buffer.get()));
std::move(quit).Run();
},
cache_file_path(), sitelist_cache_file_path(),
run_loop.QuitClosure()),
action_timeout());
run_loop.Run();
}
IN_PROC_BROWSER_TEST_F(BrowserSwitcherServiceTest,
......@@ -703,33 +907,43 @@ IN_PROC_BROWSER_TEST_F(BrowserSwitcherServiceTest,
ASSERT_TRUE(base::PathExists(sitelist_cache_file_path()));
// Check that "sitelistcache.dat" got cleaned up on startup.
GetService();
WaitForSitelistCacheFile();
EXPECT_FALSE(base::PathExists(sitelist_cache_file_path()));
BrowserSwitcherServiceFactory::GetForBrowserContext(browser()->profile());
base::RunLoop run_loop;
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::BindOnce(
[](base::FilePath sitelist_cache_file_path, base::OnceClosure quit) {
EXPECT_FALSE(base::PathExists(sitelist_cache_file_path));
std::move(quit).Run();
},
sitelist_cache_file_path(), run_loop.QuitClosure()),
action_timeout());
run_loop.Run();
}
IN_PROC_BROWSER_TEST_F(BrowserSwitcherServiceTest, WritesNothingIfDisabled) {
base::ScopedAllowBlockingForTesting allow_blocking;
// No policies configured.
// Check that "cache.dat" and "sitelistcache.dat" don't exist when LBS is not
// configured.
auto* service = GetServiceWin();
// Need to initialize both RunLoops at the same time to avoid deadlocks
// depending on which is written first (cache.dat or sitelistcache.dat).
base::RunLoop cache_run_loop;
base::RunLoop sitelist_cache_run_loop;
service->OnCacheFileUpdatedForTesting(cache_run_loop.QuitClosure());
service->OnSitelistCacheFileUpdatedForTesting(
sitelist_cache_run_loop.QuitClosure());
cache_run_loop.Run();
sitelist_cache_run_loop.Run();
base::ScopedAllowBlockingForTesting allow_blocking;
EXPECT_FALSE(base::PathExists(cache_dir()));
EXPECT_FALSE(base::PathExists(cache_file_path()));
EXPECT_FALSE(base::PathExists(sitelist_cache_file_path()));
BrowserSwitcherServiceFactory::GetForBrowserContext(browser()->profile());
base::RunLoop run_loop;
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::BindOnce(
[](base::FilePath cache_dir, base::FilePath cache_file_path,
base::FilePath sitelist_cache_file_path, base::OnceClosure quit) {
EXPECT_FALSE(base::PathExists(cache_dir));
EXPECT_FALSE(base::PathExists(cache_file_path));
EXPECT_FALSE(base::PathExists(sitelist_cache_file_path));
std::move(quit).Run();
},
cache_dir(), cache_file_path(), sitelist_cache_file_path(),
run_loop.QuitClosure()),
action_timeout());
run_loop.Run();
}
IN_PROC_BROWSER_TEST_F(BrowserSwitcherServiceTest,
......@@ -759,11 +973,22 @@ IN_PROC_BROWSER_TEST_F(BrowserSwitcherServiceTest,
ASSERT_TRUE(base::PathExists(cache_file_path()));
ASSERT_TRUE(base::PathExists(sitelist_cache_file_path()));
GetService();
WaitForActionTimeout();
EXPECT_TRUE(base::PathExists(cache_dir()));
EXPECT_TRUE(base::PathExists(cache_file_path()));
EXPECT_TRUE(base::PathExists(sitelist_cache_file_path()));
BrowserSwitcherServiceFactory::GetForBrowserContext(browser()->profile());
base::RunLoop run_loop;
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::BindOnce(
[](base::FilePath cache_dir, base::FilePath cache_file_path,
base::FilePath sitelist_cache_file_path, base::OnceClosure quit) {
EXPECT_TRUE(base::PathExists(cache_dir));
EXPECT_TRUE(base::PathExists(cache_file_path));
EXPECT_TRUE(base::PathExists(sitelist_cache_file_path));
std::move(quit).Run();
},
cache_dir(), cache_file_path(), sitelist_cache_file_path(),
run_loop.QuitClosure()),
action_timeout());
run_loop.Run();
}
#endif
......
......@@ -27,8 +27,6 @@
#include "chrome/browser/browser_switcher/browser_switcher_sitelist.h"
#include "chrome/browser/browser_switcher/ieem_sitelist_parser.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "extensions/browser/extension_registry.h"
namespace browser_switcher {
......@@ -138,7 +136,9 @@ BrowserSwitcherServiceWin::BrowserSwitcherServiceWin(
cache_dir_for_testing_(std::move(cache_dir_for_testing)),
sequenced_task_runner_(base::ThreadPool::CreateSequencedTaskRunner(
{base::MayBlock(), base::TaskPriority::BEST_EFFORT,
base::TaskShutdownBehavior::BLOCK_SHUTDOWN})) {}
base::TaskShutdownBehavior::BLOCK_SHUTDOWN})) {
UpdateAllCacheFiles();
}
BrowserSwitcherServiceWin::~BrowserSwitcherServiceWin() = default;
......@@ -166,16 +166,14 @@ std::vector<RulesetSource> BrowserSwitcherServiceWin::GetRulesetSources() {
return sources;
}
void BrowserSwitcherServiceWin::Init() {
BrowserSwitcherService::Init();
UpdateAllCacheFiles();
}
void BrowserSwitcherServiceWin::LoadRulesFromPrefs() {
BrowserSwitcherService::LoadRulesFromPrefs();
if (prefs().UseIeSitelist())
sitelist()->SetIeemSitelist(
ParsedXml(prefs().GetCachedIeemSitelist(), base::nullopt));
if (!prefs().IsEnabled())
return;
SavePrefsToFile();
}
base::FilePath BrowserSwitcherServiceWin::GetCacheDir() {
......@@ -234,51 +232,25 @@ void BrowserSwitcherServiceWin::OnIeemSitelistParsed(ParsedXml xml) {
}
}
void BrowserSwitcherServiceWin::CacheFileUpdated() {
if (cache_file_updated_callback_for_testing_)
std::move(cache_file_updated_callback_for_testing_).Run();
}
void BrowserSwitcherServiceWin::SitelistCacheFileUpdated() {
if (sitelist_cache_file_updated_callback_for_testing_)
std::move(sitelist_cache_file_updated_callback_for_testing_).Run();
}
void BrowserSwitcherServiceWin::OnCacheFileUpdatedForTesting(
base::OnceClosure cb) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
cache_file_updated_callback_for_testing_ = std::move(cb);
}
void BrowserSwitcherServiceWin::OnSitelistCacheFileUpdatedForTesting(
base::OnceClosure cb) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
sitelist_cache_file_updated_callback_for_testing_ = std::move(cb);
}
void BrowserSwitcherServiceWin::DeletePrefsFile() {
void BrowserSwitcherServiceWin::SavePrefsToFile() {
DCHECK(prefs().IsEnabled());
base::FilePath path = GetCacheDir();
if (path.empty())
return;
path = path.AppendASCII("cache.dat");
sequenced_task_runner_->PostTaskAndReply(
FROM_HERE, base::BindOnce(&RemoveFile, std::move(path)),
base::BindOnce(&BrowserSwitcherServiceWin::CacheFileUpdated,
base::Unretained(this)));
sequenced_task_runner_->PostTask(
FROM_HERE,
base::BindOnce(&SaveDataToFile, SerializeCacheFile(prefs(), sitelist()),
std::move(path)));
}
void BrowserSwitcherServiceWin::SavePrefsToFile() {
DCHECK(prefs().IsEnabled());
void BrowserSwitcherServiceWin::DeletePrefsFile() {
base::FilePath path = GetCacheDir();
if (path.empty())
return;
path = path.AppendASCII("cache.dat");
sequenced_task_runner_->PostTaskAndReply(
FROM_HERE,
base::BindOnce(&SaveDataToFile, SerializeCacheFile(prefs(), sitelist()),
std::move(path)),
base::BindOnce(&BrowserSwitcherServiceWin::CacheFileUpdated,
base::Unretained(this)));
sequenced_task_runner_->PostTask(
FROM_HERE, base::BindOnce(&RemoveFile, std::move(path)));
}
void BrowserSwitcherServiceWin::DeleteSitelistCacheFile() {
......@@ -286,10 +258,8 @@ void BrowserSwitcherServiceWin::DeleteSitelistCacheFile() {
if (path.empty())
return;
path = path.AppendASCII("sitelistcache.dat");
sequenced_task_runner_->PostTaskAndReply(
FROM_HERE, base::BindOnce(&RemoveFile, std::move(path)),
base::BindOnce(&BrowserSwitcherServiceWin::SitelistCacheFileUpdated,
base::Unretained(this)));
sequenced_task_runner_->PostTask(
FROM_HERE, base::BindOnce(&RemoveFile, std::move(path)));
}
void BrowserSwitcherServiceWin::UpdateAllCacheFiles() {
......
......@@ -22,8 +22,6 @@ class BrowserSwitcherServiceWin : public BrowserSwitcherService {
base::FilePath cache_dir_for_testing = base::FilePath());
~BrowserSwitcherServiceWin() override;
void Init() override;
static void SetIeemSitelistUrlForTesting(const std::string& url);
// BrowserSwitcherService:
......@@ -31,9 +29,6 @@ class BrowserSwitcherServiceWin : public BrowserSwitcherService {
void LoadRulesFromPrefs() override;
void OnCacheFileUpdatedForTesting(base::OnceClosure cb);
void OnSitelistCacheFileUpdatedForTesting(base::OnceClosure cb);
protected:
// BrowserSwitcherService:
void OnAllRulesetsParsed() override;
......@@ -64,16 +59,11 @@ class BrowserSwitcherServiceWin : public BrowserSwitcherService {
// extension, or from a previous Chrome version. Called during initialization.
void DeleteSitelistCacheFile();
void CacheFileUpdated();
void SitelistCacheFileUpdated();
// Updates or cleans up cache.dat and sitelistcache.dat, based on whether
// BrowserSwitcher is enabled or disabled.
void UpdateAllCacheFiles();
base::FilePath cache_dir_for_testing_;
base::OnceClosure cache_file_updated_callback_for_testing_;
base::OnceClosure sitelist_cache_file_updated_callback_for_testing_;
scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner_;
......
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