Commit 54aa10ad authored by Nicolas Ouellet-Payeur's avatar Nicolas Ouellet-Payeur Committed by Commit Bot

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