| 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 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 net::NetworkChangeNotifier::CreateMock(); | 106 net::NetworkChangeNotifier::CreateMock(); |
| 106 #endif | 107 #endif |
| 107 | 108 |
| 108 ContentBrowserTest::SetUp(); | 109 ContentBrowserTest::SetUp(); |
| 109 } | 110 } |
| 110 | 111 |
| 111 void SetIncognitoMode(bool incognito) { | 112 void SetIncognitoMode(bool incognito) { |
| 112 shell_ = incognito ? CreateOffTheRecordBrowser() : shell(); | 113 shell_ = incognito ? CreateOffTheRecordBrowser() : shell(); |
| 113 } | 114 } |
| 114 | 115 |
| 116 BackgroundSyncContext* GetSyncContextFromShell(Shell* shell) { |
| 117 StoragePartition* storage = BrowserContext::GetDefaultStoragePartition( |
| 118 shell_->web_contents()->GetBrowserContext()); |
| 119 return storage->GetBackgroundSyncContext(); |
| 120 } |
| 121 |
| 115 void SetUpCommandLine(base::CommandLine* command_line) override { | 122 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 116 // TODO(jkarlin): Remove this once background sync is no longer | 123 // TODO(jkarlin): Remove this once background sync is no longer |
| 117 // experimental. | 124 // experimental. |
| 118 command_line->AppendSwitch( | 125 command_line->AppendSwitch( |
| 119 switches::kEnableExperimentalWebPlatformFeatures); | 126 switches::kEnableExperimentalWebPlatformFeatures); |
| 120 } | 127 } |
| 121 | 128 |
| 122 void SetUpOnMainThread() override { | 129 void SetUpOnMainThread() override { |
| 123 https_server_.reset(new net::SpawnedTestServer( | 130 https_server_.reset(new net::SpawnedTestServer( |
| 124 net::SpawnedTestServer::TYPE_HTTPS, | 131 net::SpawnedTestServer::TYPE_HTTPS, |
| 125 net::BaseTestServer::SSLOptions( | 132 net::BaseTestServer::SSLOptions( |
| 126 net::BaseTestServer::SSLOptions::CERT_OK), | 133 net::BaseTestServer::SSLOptions::CERT_OK), |
| 127 base::FilePath(FILE_PATH_LITERAL("content/test/data/")))); | 134 base::FilePath(FILE_PATH_LITERAL("content/test/data/")))); |
| 128 ASSERT_TRUE(https_server_->Start()); | 135 ASSERT_TRUE(https_server_->Start()); |
| 129 | 136 |
| 137 SetIncognitoMode(false); |
| 138 |
| 130 SetOnline(true); | 139 SetOnline(true); |
| 131 | 140 |
| 132 SetIncognitoMode(false); | |
| 133 | |
| 134 ASSERT_TRUE(LoadTestPage(kDefaultTestURL)); | 141 ASSERT_TRUE(LoadTestPage(kDefaultTestURL)); |
| 135 | 142 |
| 136 ContentBrowserTest::SetUpOnMainThread(); | 143 ContentBrowserTest::SetUpOnMainThread(); |
| 137 } | 144 } |
| 138 | 145 |
| 139 void TearDownOnMainThread() override { https_server_.reset(); } | 146 void TearDownOnMainThread() override { https_server_.reset(); } |
| 140 | 147 |
| 141 bool LoadTestPage(const std::string& path) { | 148 bool LoadTestPage(const std::string& path) { |
| 142 return NavigateToURL(shell_, https_server_->GetURL(path)); | 149 return NavigateToURL(shell_, https_server_->GetURL(path)); |
| 143 } | 150 } |
| 144 | 151 |
| 145 bool RunScript(const std::string& script, std::string* result) { | 152 bool RunScript(const std::string& script, std::string* result) { |
| 146 return content::ExecuteScriptAndExtractString(shell_->web_contents(), | 153 return content::ExecuteScriptAndExtractString(shell_->web_contents(), |
| 147 script, result); | 154 script, result); |
| 148 } | 155 } |
| 149 | 156 |
| 150 void SetOnline(bool online); | 157 void SetOnline(bool online); |
| 158 void SetOnlineOnIOThread( |
| 159 const scoped_refptr<BackgroundSyncContext>& sync_context, |
| 160 bool online); |
| 151 | 161 |
| 152 // Returns true if the one-shot sync with tag is currently pending. Fails | 162 // Returns true if the one-shot sync with tag is currently pending. Fails |
| 153 // (assertion failure) if the tag isn't registered. | 163 // (assertion failure) if the tag isn't registered. |
| 154 bool OneShotPending(const std::string& tag); | 164 bool OneShotPending(const std::string& tag); |
| 155 | 165 |
| 156 bool PopConsole(const std::string& expected_msg); | 166 bool PopConsole(const std::string& expected_msg); |
| 157 bool RegisterServiceWorker(); | 167 bool RegisterServiceWorker(); |
| 158 bool RegisterOneShot(const std::string& tag); | 168 bool RegisterOneShot(const std::string& tag); |
| 159 bool UnregisterOneShot(const std::string& tag); | 169 bool UnregisterOneShot(const std::string& tag); |
| 160 bool UnregisterOneShotTwice(const std::string& tag); | 170 bool UnregisterOneShotTwice(const std::string& tag); |
| 161 bool GetRegistrationOneShot(const std::string& tag); | 171 bool GetRegistrationOneShot(const std::string& tag); |
| 162 bool GetRegistrationsOneShot(const std::vector<std::string>& expected_tags); | 172 bool GetRegistrationsOneShot(const std::vector<std::string>& expected_tags); |
| 163 bool CompleteDelayedOneShot(); | 173 bool CompleteDelayedOneShot(); |
| 164 bool RejectDelayedOneShot(); | 174 bool RejectDelayedOneShot(); |
| 165 | 175 |
| 166 private: | 176 private: |
| 167 scoped_ptr<net::SpawnedTestServer> https_server_; | 177 scoped_ptr<net::SpawnedTestServer> https_server_; |
| 168 Shell* shell_ = nullptr; | 178 Shell* shell_ = nullptr; |
| 169 | 179 |
| 170 DISALLOW_COPY_AND_ASSIGN(BackgroundSyncBrowserTest); | 180 DISALLOW_COPY_AND_ASSIGN(BackgroundSyncBrowserTest); |
| 171 }; | 181 }; |
| 172 | 182 |
| 173 void BackgroundSyncBrowserTest::SetOnline(bool online) { | 183 void BackgroundSyncBrowserTest::SetOnline(bool online) { |
| 174 if (online) { | 184 ASSERT_TRUE(shell_); |
| 175 NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests( | 185 BrowserThread::PostTask( |
| 176 NetworkChangeNotifier::CONNECTION_WIFI); | 186 BrowserThread::IO, FROM_HERE, |
| 177 } else { | 187 base::Bind(&BackgroundSyncBrowserTest::SetOnlineOnIOThread, |
| 178 NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests( | 188 base::Unretained(this), |
| 179 NetworkChangeNotifier::CONNECTION_NONE); | 189 base::Unretained(GetSyncContextFromShell(shell_)), online)); |
| 180 } | |
| 181 base::RunLoop().RunUntilIdle(); | 190 base::RunLoop().RunUntilIdle(); |
| 182 } | 191 } |
| 183 | 192 |
| 193 void BackgroundSyncBrowserTest::SetOnlineOnIOThread( |
| 194 const scoped_refptr<BackgroundSyncContext>& sync_context, |
| 195 bool online) { |
| 196 BackgroundSyncManager* sync_manager = sync_context->background_sync_manager(); |
| 197 BackgroundSyncNetworkObserver* network_observer = |
| 198 sync_manager->GetNetworkObserverForTesting(); |
| 199 if (online) { |
| 200 network_observer->OnNetworkChanged(NetworkChangeNotifier::CONNECTION_WIFI); |
| 201 } else { |
| 202 network_observer->OnNetworkChanged(NetworkChangeNotifier::CONNECTION_NONE); |
| 203 } |
| 204 } |
| 205 |
| 184 bool BackgroundSyncBrowserTest::OneShotPending(const std::string& tag) { | 206 bool BackgroundSyncBrowserTest::OneShotPending(const std::string& tag) { |
| 185 bool is_pending; | 207 bool is_pending; |
| 186 base::RunLoop run_loop; | 208 base::RunLoop run_loop; |
| 187 | 209 |
| 188 StoragePartition* storage = BrowserContext::GetDefaultStoragePartition( | 210 StoragePartition* storage = BrowserContext::GetDefaultStoragePartition( |
| 189 shell_->web_contents()->GetBrowserContext()); | 211 shell_->web_contents()->GetBrowserContext()); |
| 190 BackgroundSyncContext* sync_context = storage->GetBackgroundSyncContext(); | 212 BackgroundSyncContext* sync_context = storage->GetBackgroundSyncContext(); |
| 191 ServiceWorkerContextWrapper* service_worker_context = | 213 ServiceWorkerContextWrapper* service_worker_context = |
| 192 static_cast<ServiceWorkerContextWrapper*>( | 214 static_cast<ServiceWorkerContextWrapper*>( |
| 193 storage->GetServiceWorkerContext()); | 215 storage->GetServiceWorkerContext()); |
| (...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 416 EXPECT_TRUE(RegisterServiceWorker()); | 438 EXPECT_TRUE(RegisterServiceWorker()); |
| 417 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. | 439 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. |
| 418 | 440 |
| 419 EXPECT_TRUE(RegisterOneShot("unregister")); | 441 EXPECT_TRUE(RegisterOneShot("unregister")); |
| 420 EXPECT_TRUE(PopConsole("ok - unregister completed")); | 442 EXPECT_TRUE(PopConsole("ok - unregister completed")); |
| 421 } | 443 } |
| 422 | 444 |
| 423 } // namespace | 445 } // namespace |
| 424 | 446 |
| 425 } // namespace content | 447 } // namespace content |
| OLD | NEW |