Index: content/browser/background_sync/background_sync_browsertest.cc |
diff --git a/content/browser/background_sync/background_sync_browsertest.cc b/content/browser/background_sync/background_sync_browsertest.cc |
index e3f8dc003a9f849e8f5b7d8ee1a4636e320c97ed..7c0d71b32e5a027c577a48d8d1b32184d40d8c6c 100644 |
--- a/content/browser/background_sync/background_sync_browsertest.cc |
+++ b/content/browser/background_sync/background_sync_browsertest.cc |
@@ -21,6 +21,7 @@ |
#include "content/public/browser/storage_partition.h" |
#include "content/public/browser/web_contents.h" |
#include "content/public/common/content_switches.h" |
+#include "content/public/test/background_sync_test_util.h" |
#include "content/public/test/browser_test_utils.h" |
#include "content/public/test/content_browser_test.h" |
#include "content/public/test/content_browser_test_utils.h" |
@@ -110,7 +111,8 @@ class BackgroundSyncBrowserTest : public ContentBrowserTest { |
~BackgroundSyncBrowserTest() override {} |
void SetUp() override { |
- BackgroundSyncNetworkObserver::SetIgnoreNetworkChangeNotifierForTests(true); |
+ BackgroundSyncTestUtil::SetIgnoreNetworkChangeNotifier(true); |
+ |
ContentBrowserTest::SetUp(); |
} |
@@ -128,6 +130,8 @@ class BackgroundSyncBrowserTest : public ContentBrowserTest { |
return GetStorage()->GetBackgroundSyncContext(); |
} |
+ WebContents* web_contents() { return shell_->web_contents(); } |
+ |
void SetUpCommandLine(base::CommandLine* command_line) override { |
// TODO(jkarlin): Remove this once background sync is no longer |
// experimental. |
@@ -143,7 +147,7 @@ class BackgroundSyncBrowserTest : public ContentBrowserTest { |
SetIncognitoMode(false); |
SetMaxSyncAttempts(1); |
- SetOnline(true); |
+ BackgroundSyncTestUtil::SetOnline(web_contents(), true); |
ASSERT_TRUE(LoadTestPage(kDefaultTestURL)); |
ContentBrowserTest::SetUpOnMainThread(); |
@@ -156,18 +160,10 @@ class BackgroundSyncBrowserTest : public ContentBrowserTest { |
} |
bool RunScript(const std::string& script, std::string* result) { |
- return content::ExecuteScriptAndExtractString(shell_->web_contents(), |
- script, result); |
+ return content::ExecuteScriptAndExtractString(web_contents(), script, |
+ result); |
} |
- // This runs asynchronously on the IO thread, but we don't need to wait for it |
- // to complete before running a background sync operation, since those also |
- // run on the IO thread. |
- void SetOnline(bool online); |
- void SetOnlineOnIOThread( |
- const scoped_refptr<BackgroundSyncContext>& sync_context, |
- bool online); |
- |
// Returns true if the one-shot sync with tag is currently pending. Fails |
// (assertion failure) if the tag isn't registered. |
bool OneShotPending(const std::string& tag); |
@@ -204,31 +200,6 @@ class BackgroundSyncBrowserTest : public ContentBrowserTest { |
DISALLOW_COPY_AND_ASSIGN(BackgroundSyncBrowserTest); |
}; |
-void BackgroundSyncBrowserTest::SetOnline(bool online) { |
- ASSERT_TRUE(shell_); |
- BrowserThread::PostTask( |
- BrowserThread::IO, FROM_HERE, |
- base::Bind(&BackgroundSyncBrowserTest::SetOnlineOnIOThread, |
- base::Unretained(this), base::Unretained(GetSyncContext()), |
- online)); |
- base::RunLoop().RunUntilIdle(); |
-} |
- |
-void BackgroundSyncBrowserTest::SetOnlineOnIOThread( |
- const scoped_refptr<BackgroundSyncContext>& sync_context, |
- bool online) { |
- BackgroundSyncManager* sync_manager = sync_context->background_sync_manager(); |
- BackgroundSyncNetworkObserver* network_observer = |
- sync_manager->GetNetworkObserverForTesting(); |
- if (online) { |
- network_observer->NotifyManagerIfNetworkChangedForTesting( |
- NetworkChangeNotifier::CONNECTION_WIFI); |
- } else { |
- network_observer->NotifyManagerIfNetworkChangedForTesting( |
- NetworkChangeNotifier::CONNECTION_NONE); |
- } |
-} |
- |
bool BackgroundSyncBrowserTest::OneShotPending(const std::string& tag) { |
bool is_pending; |
base::RunLoop run_loop; |
@@ -403,8 +374,7 @@ bool BackgroundSyncBrowserTest::RejectDelayedOneShot() { |
bool BackgroundSyncBrowserTest::NotifyWhenFinishedOneShot( |
const std::string& tag) { |
EXPECT_TRUE(content::ExecuteScript( |
- shell_->web_contents(), |
- BuildScriptString("notifyWhenFinishedOneShot", tag))); |
+ web_contents(), BuildScriptString("notifyWhenFinishedOneShot", tag))); |
return PopConsole(BuildExpectedResult(tag, "finished")); |
} |
@@ -450,13 +420,13 @@ IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, OneShotDelaysForNetwork) { |
EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. |
// Prevent firing by going offline. |
- SetOnline(false); |
+ BackgroundSyncTestUtil::SetOnline(web_contents(), false); |
EXPECT_TRUE(RegisterOneShot("foo")); |
EXPECT_TRUE(GetRegistrationOneShot("foo")); |
EXPECT_TRUE(OneShotPending("foo")); |
// Resume firing by going online. |
- SetOnline(true); |
+ BackgroundSyncTestUtil::SetOnline(web_contents(), true); |
EXPECT_TRUE(PopConsole("foo fired")); |
EXPECT_FALSE(GetRegistrationOneShot("foo")); |
} |
@@ -465,7 +435,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, WaitUntil) { |
EXPECT_TRUE(RegisterServiceWorker()); |
EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. |
- SetOnline(true); |
+ BackgroundSyncTestUtil::SetOnline(web_contents(), true); |
EXPECT_TRUE(RegisterOneShot("delay")); |
// Verify that it is firing. |
@@ -486,7 +456,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, WaitUntilReject) { |
EXPECT_TRUE(RegisterServiceWorker()); |
EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. |
- SetOnline(true); |
+ BackgroundSyncTestUtil::SetOnline(web_contents(), true); |
EXPECT_TRUE(RegisterOneShot("delay")); |
// Verify that it is firing. |
@@ -503,7 +473,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, Incognito) { |
EXPECT_TRUE(RegisterServiceWorker()); |
EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. |
- SetOnline(false); |
+ BackgroundSyncTestUtil::SetOnline(web_contents(), false); |
EXPECT_TRUE(RegisterOneShot("normal")); |
EXPECT_TRUE(OneShotPending("normal")); |
@@ -513,7 +483,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, Incognito) { |
// Tell the new network observer that we're offline (it initializes from |
// NetworkChangeNotifier::GetCurrentConnectionType() which is not mocked out |
// in this test). |
- SetOnline(false); |
+ BackgroundSyncTestUtil::SetOnline(web_contents(), false); |
EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); |
EXPECT_TRUE(RegisterServiceWorker()); |
@@ -539,7 +509,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, GetRegistrations) { |
std::vector<std::string> registered_tags; |
EXPECT_TRUE(GetRegistrationsOneShot(registered_tags)); |
- SetOnline(false); |
+ BackgroundSyncTestUtil::SetOnline(web_contents(), false); |
registered_tags.push_back("foo"); |
registered_tags.push_back("bar"); |
@@ -558,7 +528,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, |
std::vector<std::string> registered_tags; |
EXPECT_TRUE(GetRegistrationsOneShot(registered_tags)); |
- SetOnline(false); |
+ BackgroundSyncTestUtil::SetOnline(web_contents(), false); |
registered_tags.push_back("foo_sw"); |
registered_tags.push_back("bar_sw"); |
@@ -579,7 +549,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, |
std::vector<std::string> registered_tags; |
EXPECT_TRUE(GetRegistrationsOneShot(registered_tags)); |
- SetOnline(false); |
+ BackgroundSyncTestUtil::SetOnline(web_contents(), false); |
EXPECT_TRUE(RegisterOneShotFromServiceWorker("foo_sw")); |
EXPECT_TRUE(PopConsole("ok - foo_sw registered in SW")); |
@@ -590,7 +560,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, Unregister) { |
EXPECT_TRUE(RegisterServiceWorker()); |
EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. |
- SetOnline(false); |
+ BackgroundSyncTestUtil::SetOnline(web_contents(), false); |
EXPECT_TRUE(RegisterOneShot("foo")); |
EXPECT_TRUE(UnregisterOneShot("foo")); |
EXPECT_FALSE(GetRegistrationOneShot("foo")); |
@@ -600,7 +570,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, UnregisterTwice) { |
EXPECT_TRUE(RegisterServiceWorker()); |
EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. |
- SetOnline(false); |
+ BackgroundSyncTestUtil::SetOnline(web_contents(), false); |
EXPECT_TRUE(RegisterOneShot("foo")); |
EXPECT_TRUE(UnregisterOneShotTwice("foo")); |
EXPECT_FALSE(GetRegistrationOneShot("foo")); |
@@ -619,11 +589,11 @@ IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, |
EXPECT_TRUE(RegisterServiceWorker()); |
EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. |
- SetOnline(false); |
+ BackgroundSyncTestUtil::SetOnline(web_contents(), false); |
EXPECT_TRUE(RegisterOneShot("foo")); |
EXPECT_TRUE(NotifyWhenFinishedOneShot("foo")); |
- SetOnline(true); |
+ BackgroundSyncTestUtil::SetOnline(web_contents(), true); |
// The ordering of PopConsole messages tells us that the event fired |
// before finished resolved. |
EXPECT_TRUE(PopConsole("foo fired")); |
@@ -634,7 +604,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, CallFinishedBeforeSyncFails) { |
EXPECT_TRUE(RegisterServiceWorker()); |
EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. |
- SetOnline(true); |
+ BackgroundSyncTestUtil::SetOnline(web_contents(), true); |
EXPECT_TRUE(RegisterOneShot("delay")); |
EXPECT_FALSE(OneShotPending("delay")); |
EXPECT_TRUE(NotifyWhenFinishedOneShot("delay")); |
@@ -651,11 +621,11 @@ IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, |
EXPECT_TRUE(RegisterServiceWorker()); |
EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. |
- SetOnline(false); |
+ BackgroundSyncTestUtil::SetOnline(web_contents(), false); |
EXPECT_TRUE(RegisterOneShot("foo")); |
EXPECT_TRUE(StoreRegistrationOneShot("foo")); |
- SetOnline(true); |
+ BackgroundSyncTestUtil::SetOnline(web_contents(), true); |
EXPECT_TRUE(PopConsole("foo fired")); |
EXPECT_FALSE(GetRegistrationOneShot("foo")); |
EXPECT_TRUE(NotifyWhenFinishedImmediateOneShot("ok - foo result: true")); |
@@ -666,7 +636,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, |
EXPECT_TRUE(RegisterServiceWorker()); |
EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. |
- SetOnline(false); |
+ BackgroundSyncTestUtil::SetOnline(web_contents(), false); |
EXPECT_TRUE(RegisterOneShot("foo")); |
EXPECT_TRUE(StoreRegistrationOneShot("foo")); |
EXPECT_TRUE(UnregisterOneShot("foo")); |
@@ -678,7 +648,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, CallFinishedAfterSyncFails) { |
EXPECT_TRUE(RegisterServiceWorker()); |
EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. |
- SetOnline(true); |
+ BackgroundSyncTestUtil::SetOnline(web_contents(), true); |
EXPECT_TRUE(RegisterOneShot("delay")); |
EXPECT_FALSE(OneShotPending("delay")); |
EXPECT_TRUE(StoreRegistrationOneShot("delay")); |
@@ -696,7 +666,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, |
EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. |
// Prevent firing by going offline. |
- SetOnline(false); |
+ BackgroundSyncTestUtil::SetOnline(web_contents(), false); |
EXPECT_TRUE(RegisterOneShot("foo")); |
EXPECT_TRUE(GetRegistrationOneShot("foo")); |
EXPECT_TRUE(OneShotPending("foo")); |
@@ -718,7 +688,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, |
std::vector<std::string> registered_tags; |
EXPECT_TRUE(GetRegistrationsOneShot(registered_tags)); |
- SetOnline(false); |
+ BackgroundSyncTestUtil::SetOnline(web_contents(), false); |
EXPECT_TRUE(RegisterOneShotFromServiceWorker("foo_sw")); |
EXPECT_TRUE(PopConsole("ok - foo_sw registered in SW")); |
@@ -741,7 +711,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, |
std::vector<std::string> registered_tags; |
EXPECT_TRUE(GetRegistrationsOneShot(registered_tags)); |
- SetOnline(false); |
+ BackgroundSyncTestUtil::SetOnline(web_contents(), false); |
registered_tags.push_back("foo"); |
registered_tags.push_back("bar"); |
@@ -768,7 +738,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, |
EXPECT_TRUE(RegisterServiceWorker()); |
EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. |
- SetOnline(true); |
+ BackgroundSyncTestUtil::SetOnline(web_contents(), true); |
EXPECT_TRUE(RegisterOneShot("delay")); |
// Verify that it is firing. |