| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <set> | 5 #include <set> |
| 6 #include <string> | 6 #include <string> |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/strings/string_split.h" | 10 #include "base/strings/string_split.h" |
| 11 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 12 #include "base/task_runner_util.h" | 12 #include "base/task_runner_util.h" |
| 13 #include "content/browser/background_sync/background_sync_manager.h" | 13 #include "content/browser/background_sync/background_sync_manager.h" |
| 14 #include "content/browser/background_sync/background_sync_network_observer.h" |
| 14 #include "content/browser/background_sync/background_sync_registration_handle.h" | 15 #include "content/browser/background_sync/background_sync_registration_handle.h" |
| 15 #include "content/browser/background_sync/background_sync_status.h" | 16 #include "content/browser/background_sync/background_sync_status.h" |
| 16 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 17 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 17 #include "content/browser/service_worker/service_worker_registration.h" | 18 #include "content/browser/service_worker/service_worker_registration.h" |
| 18 #include "content/public/browser/background_sync_context.h" | 19 #include "content/public/browser/background_sync_context.h" |
| 19 #include "content/public/browser/browser_context.h" | 20 #include "content/public/browser/browser_context.h" |
| 20 #include "content/public/browser/storage_partition.h" | 21 #include "content/public/browser/storage_partition.h" |
| 21 #include "content/public/browser/web_contents.h" | 22 #include "content/public/browser/web_contents.h" |
| 22 #include "content/public/common/content_switches.h" | 23 #include "content/public/common/content_switches.h" |
| 23 #include "content/public/test/browser_test_utils.h" | 24 #include "content/public/test/browser_test_utils.h" |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 const scoped_refptr<BackgroundSyncContext> sync_context, | 86 const scoped_refptr<BackgroundSyncContext> sync_context, |
| 86 const scoped_refptr<ServiceWorkerContextWrapper> sw_context, | 87 const scoped_refptr<ServiceWorkerContextWrapper> sw_context, |
| 87 const std::string& tag, | 88 const std::string& tag, |
| 88 const GURL& url, | 89 const GURL& url, |
| 89 const base::Callback<void(bool)>& callback) { | 90 const base::Callback<void(bool)>& callback) { |
| 90 sw_context->FindRegistrationForDocument( | 91 sw_context->FindRegistrationForDocument( |
| 91 url, base::Bind(&OneShotPendingDidGetSWRegistration, sync_context, tag, | 92 url, base::Bind(&OneShotPendingDidGetSWRegistration, sync_context, tag, |
| 92 callback)); | 93 callback)); |
| 93 } | 94 } |
| 94 | 95 |
| 96 } // namespace |
| 97 |
| 95 class BackgroundSyncBrowserTest : public ContentBrowserTest { | 98 class BackgroundSyncBrowserTest : public ContentBrowserTest { |
| 96 public: | 99 public: |
| 97 BackgroundSyncBrowserTest() {} | 100 BackgroundSyncBrowserTest() {} |
| 98 ~BackgroundSyncBrowserTest() override {} | 101 ~BackgroundSyncBrowserTest() override {} |
| 99 | 102 |
| 100 void SetUp() override { | 103 void SetUp() override { |
| 101 NetworkChangeNotifier::SetTestNotificationsOnly(true); | 104 BackgroundSyncNetworkObserver::SetIgnoreNetworkChangeNotifierForTests(true); |
| 102 | |
| 103 #if defined(OS_CHROMEOS) | |
| 104 // ChromeOS's NetworkChangeNotifier doesn't get created in | |
| 105 // content_browsertests, so make one now. | |
| 106 net::NetworkChangeNotifier::CreateMock(); | |
| 107 #endif | |
| 108 | |
| 109 ContentBrowserTest::SetUp(); | 105 ContentBrowserTest::SetUp(); |
| 110 } | 106 } |
| 111 | 107 |
| 112 void SetIncognitoMode(bool incognito) { | 108 void SetIncognitoMode(bool incognito) { |
| 113 shell_ = incognito ? CreateOffTheRecordBrowser() : shell(); | 109 shell_ = incognito ? CreateOffTheRecordBrowser() : shell(); |
| 114 } | 110 } |
| 115 | 111 |
| 112 BackgroundSyncContext* GetSyncContextFromShell(Shell* shell) { |
| 113 StoragePartition* storage = BrowserContext::GetDefaultStoragePartition( |
| 114 shell_->web_contents()->GetBrowserContext()); |
| 115 return storage->GetBackgroundSyncContext(); |
| 116 } |
| 117 |
| 116 void SetUpCommandLine(base::CommandLine* command_line) override { | 118 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 117 // TODO(jkarlin): Remove this once background sync is no longer | 119 // TODO(jkarlin): Remove this once background sync is no longer |
| 118 // experimental. | 120 // experimental. |
| 119 command_line->AppendSwitch( | 121 command_line->AppendSwitch( |
| 120 switches::kEnableExperimentalWebPlatformFeatures); | 122 switches::kEnableExperimentalWebPlatformFeatures); |
| 121 } | 123 } |
| 122 | 124 |
| 123 void SetUpOnMainThread() override { | 125 void SetUpOnMainThread() override { |
| 124 https_server_.reset(new net::SpawnedTestServer( | 126 https_server_.reset(new net::SpawnedTestServer( |
| 125 net::SpawnedTestServer::TYPE_HTTPS, | 127 net::SpawnedTestServer::TYPE_HTTPS, |
| 126 net::BaseTestServer::SSLOptions( | 128 net::BaseTestServer::SSLOptions( |
| 127 net::BaseTestServer::SSLOptions::CERT_OK), | 129 net::BaseTestServer::SSLOptions::CERT_OK), |
| 128 base::FilePath(FILE_PATH_LITERAL("content/test/data/")))); | 130 base::FilePath(FILE_PATH_LITERAL("content/test/data/")))); |
| 129 ASSERT_TRUE(https_server_->Start()); | 131 ASSERT_TRUE(https_server_->Start()); |
| 130 | 132 |
| 133 SetIncognitoMode(false); |
| 134 |
| 131 SetOnline(true); | 135 SetOnline(true); |
| 132 | 136 |
| 133 SetIncognitoMode(false); | |
| 134 | |
| 135 ASSERT_TRUE(LoadTestPage(kDefaultTestURL)); | 137 ASSERT_TRUE(LoadTestPage(kDefaultTestURL)); |
| 136 | 138 |
| 137 ContentBrowserTest::SetUpOnMainThread(); | 139 ContentBrowserTest::SetUpOnMainThread(); |
| 138 } | 140 } |
| 139 | 141 |
| 140 void TearDownOnMainThread() override { https_server_.reset(); } | 142 void TearDownOnMainThread() override { https_server_.reset(); } |
| 141 | 143 |
| 142 bool LoadTestPage(const std::string& path) { | 144 bool LoadTestPage(const std::string& path) { |
| 143 return NavigateToURL(shell_, https_server_->GetURL(path)); | 145 return NavigateToURL(shell_, https_server_->GetURL(path)); |
| 144 } | 146 } |
| 145 | 147 |
| 146 bool RunScript(const std::string& script, std::string* result) { | 148 bool RunScript(const std::string& script, std::string* result) { |
| 147 return content::ExecuteScriptAndExtractString(shell_->web_contents(), | 149 return content::ExecuteScriptAndExtractString(shell_->web_contents(), |
| 148 script, result); | 150 script, result); |
| 149 } | 151 } |
| 150 | 152 |
| 153 // This runs asynchronously on the IO thread, but we don't need to wait for it |
| 154 // to complete before running a background sync operation, since those also |
| 155 // run on the IO thread. |
| 151 void SetOnline(bool online); | 156 void SetOnline(bool online); |
| 157 void SetOnlineOnIOThread( |
| 158 const scoped_refptr<BackgroundSyncContext>& sync_context, |
| 159 bool online); |
| 152 | 160 |
| 153 // Returns true if the one-shot sync with tag is currently pending. Fails | 161 // Returns true if the one-shot sync with tag is currently pending. Fails |
| 154 // (assertion failure) if the tag isn't registered. | 162 // (assertion failure) if the tag isn't registered. |
| 155 bool OneShotPending(const std::string& tag); | 163 bool OneShotPending(const std::string& tag); |
| 156 | 164 |
| 157 bool PopConsole(const std::string& expected_msg); | 165 bool PopConsole(const std::string& expected_msg); |
| 158 bool RegisterServiceWorker(); | 166 bool RegisterServiceWorker(); |
| 159 bool RegisterOneShot(const std::string& tag); | 167 bool RegisterOneShot(const std::string& tag); |
| 160 bool UnregisterOneShot(const std::string& tag); | 168 bool UnregisterOneShot(const std::string& tag); |
| 161 bool UnregisterOneShotTwice(const std::string& tag); | 169 bool UnregisterOneShotTwice(const std::string& tag); |
| 162 bool GetRegistrationOneShot(const std::string& tag); | 170 bool GetRegistrationOneShot(const std::string& tag); |
| 163 bool GetRegistrationsOneShot(const std::vector<std::string>& expected_tags); | 171 bool GetRegistrationsOneShot(const std::vector<std::string>& expected_tags); |
| 164 bool CompleteDelayedOneShot(); | 172 bool CompleteDelayedOneShot(); |
| 165 bool RejectDelayedOneShot(); | 173 bool RejectDelayedOneShot(); |
| 166 bool NotifyWhenDoneOneShot(const std::string& tag); | 174 bool NotifyWhenDoneOneShot(const std::string& tag); |
| 167 bool NotifyWhenDoneImmediateOneShot(const std::string& expected_msg); | 175 bool NotifyWhenDoneImmediateOneShot(const std::string& expected_msg); |
| 168 bool StoreRegistrationOneShot(const std::string& tag); | 176 bool StoreRegistrationOneShot(const std::string& tag); |
| 169 | 177 |
| 170 private: | 178 private: |
| 171 scoped_ptr<net::SpawnedTestServer> https_server_; | 179 scoped_ptr<net::SpawnedTestServer> https_server_; |
| 172 Shell* shell_ = nullptr; | 180 Shell* shell_ = nullptr; |
| 173 | 181 |
| 174 DISALLOW_COPY_AND_ASSIGN(BackgroundSyncBrowserTest); | 182 DISALLOW_COPY_AND_ASSIGN(BackgroundSyncBrowserTest); |
| 175 }; | 183 }; |
| 176 | 184 |
| 177 void BackgroundSyncBrowserTest::SetOnline(bool online) { | 185 void BackgroundSyncBrowserTest::SetOnline(bool online) { |
| 178 if (online) { | 186 ASSERT_TRUE(shell_); |
| 179 NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests( | 187 BrowserThread::PostTask( |
| 180 NetworkChangeNotifier::CONNECTION_WIFI); | 188 BrowserThread::IO, FROM_HERE, |
| 181 } else { | 189 base::Bind(&BackgroundSyncBrowserTest::SetOnlineOnIOThread, |
| 182 NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests( | 190 base::Unretained(this), |
| 183 NetworkChangeNotifier::CONNECTION_NONE); | 191 base::Unretained(GetSyncContextFromShell(shell_)), online)); |
| 184 } | |
| 185 base::RunLoop().RunUntilIdle(); | 192 base::RunLoop().RunUntilIdle(); |
| 186 } | 193 } |
| 187 | 194 |
| 195 void BackgroundSyncBrowserTest::SetOnlineOnIOThread( |
| 196 const scoped_refptr<BackgroundSyncContext>& sync_context, |
| 197 bool online) { |
| 198 BackgroundSyncManager* sync_manager = sync_context->background_sync_manager(); |
| 199 BackgroundSyncNetworkObserver* network_observer = |
| 200 sync_manager->GetNetworkObserverForTesting(); |
| 201 if (online) { |
| 202 network_observer->NotifyManagerIfNetworkChanged( |
| 203 NetworkChangeNotifier::CONNECTION_WIFI); |
| 204 } else { |
| 205 network_observer->NotifyManagerIfNetworkChanged( |
| 206 NetworkChangeNotifier::CONNECTION_NONE); |
| 207 } |
| 208 } |
| 209 |
| 188 bool BackgroundSyncBrowserTest::OneShotPending(const std::string& tag) { | 210 bool BackgroundSyncBrowserTest::OneShotPending(const std::string& tag) { |
| 189 bool is_pending; | 211 bool is_pending; |
| 190 base::RunLoop run_loop; | 212 base::RunLoop run_loop; |
| 191 | 213 |
| 192 StoragePartition* storage = BrowserContext::GetDefaultStoragePartition( | 214 StoragePartition* storage = BrowserContext::GetDefaultStoragePartition( |
| 193 shell_->web_contents()->GetBrowserContext()); | 215 shell_->web_contents()->GetBrowserContext()); |
| 194 BackgroundSyncContext* sync_context = storage->GetBackgroundSyncContext(); | 216 BackgroundSyncContext* sync_context = storage->GetBackgroundSyncContext(); |
| 195 ServiceWorkerContextWrapper* service_worker_context = | 217 ServiceWorkerContextWrapper* service_worker_context = |
| 196 static_cast<ServiceWorkerContextWrapper*>( | 218 static_cast<ServiceWorkerContextWrapper*>( |
| 197 storage->GetServiceWorkerContext()); | 219 storage->GetServiceWorkerContext()); |
| (...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 506 SetOnline(true); | 528 SetOnline(true); |
| 507 EXPECT_TRUE(RegisterOneShot("delay")); | 529 EXPECT_TRUE(RegisterOneShot("delay")); |
| 508 EXPECT_FALSE(OneShotPending("delay")); | 530 EXPECT_FALSE(OneShotPending("delay")); |
| 509 EXPECT_TRUE(StoreRegistrationOneShot("delay")); | 531 EXPECT_TRUE(StoreRegistrationOneShot("delay")); |
| 510 | 532 |
| 511 EXPECT_TRUE(RejectDelayedOneShot()); | 533 EXPECT_TRUE(RejectDelayedOneShot()); |
| 512 EXPECT_TRUE(PopConsole("ok - delay rejected")); | 534 EXPECT_TRUE(PopConsole("ok - delay rejected")); |
| 513 EXPECT_TRUE(NotifyWhenDoneImmediateOneShot("ok - delay result: false")); | 535 EXPECT_TRUE(NotifyWhenDoneImmediateOneShot("ok - delay result: false")); |
| 514 } | 536 } |
| 515 | 537 |
| 516 } // namespace | |
| 517 | |
| 518 } // namespace content | 538 } // namespace content |
| OLD | NEW |