Chromium Code Reviews| 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_status.h" | 15 #include "content/browser/background_sync/background_sync_status.h" |
| 15 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 16 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 16 #include "content/browser/service_worker/service_worker_registration.h" | 17 #include "content/browser/service_worker/service_worker_registration.h" |
| 17 #include "content/public/browser/background_sync_context.h" | 18 #include "content/public/browser/background_sync_context.h" |
| 18 #include "content/public/browser/browser_context.h" | 19 #include "content/public/browser/browser_context.h" |
| 19 #include "content/public/browser/storage_partition.h" | 20 #include "content/public/browser/storage_partition.h" |
| 20 #include "content/public/browser/web_contents.h" | 21 #include "content/public/browser/web_contents.h" |
| 21 #include "content/public/common/content_switches.h" | 22 #include "content/public/common/content_switches.h" |
| 22 #include "content/public/test/browser_test_utils.h" | 23 #include "content/public/test/browser_test_utils.h" |
| 23 #include "content/public/test/content_browser_test.h" | 24 #include "content/public/test/content_browser_test.h" |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 104 net::NetworkChangeNotifier::CreateMock(); | 105 net::NetworkChangeNotifier::CreateMock(); |
| 105 #endif | 106 #endif |
| 106 | 107 |
| 107 ContentBrowserTest::SetUp(); | 108 ContentBrowserTest::SetUp(); |
| 108 } | 109 } |
| 109 | 110 |
| 110 void SetIncognitoMode(bool incognito) { | 111 void SetIncognitoMode(bool incognito) { |
| 111 shell_ = incognito ? CreateOffTheRecordBrowser() : shell(); | 112 shell_ = incognito ? CreateOffTheRecordBrowser() : shell(); |
| 112 } | 113 } |
| 113 | 114 |
| 115 BackgroundSyncContext* GetSyncContextFromShell(Shell* shell) { | |
| 116 StoragePartition* storage = BrowserContext::GetDefaultStoragePartition( | |
| 117 shell_->web_contents()->GetBrowserContext()); | |
| 118 return storage->GetBackgroundSyncContext(); | |
| 119 } | |
| 120 | |
| 114 void SetUpCommandLine(base::CommandLine* command_line) override { | 121 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 115 // TODO(jkarlin): Remove this once background sync is no longer | 122 // TODO(jkarlin): Remove this once background sync is no longer |
| 116 // experimental. | 123 // experimental. |
| 117 command_line->AppendSwitch( | 124 command_line->AppendSwitch( |
| 118 switches::kEnableExperimentalWebPlatformFeatures); | 125 switches::kEnableExperimentalWebPlatformFeatures); |
| 119 } | 126 } |
| 120 | 127 |
| 121 void SetUpOnMainThread() override { | 128 void SetUpOnMainThread() override { |
| 122 https_server_.reset(new net::SpawnedTestServer( | 129 https_server_.reset(new net::SpawnedTestServer( |
| 123 net::SpawnedTestServer::TYPE_HTTPS, | 130 net::SpawnedTestServer::TYPE_HTTPS, |
| 124 net::BaseTestServer::SSLOptions( | 131 net::BaseTestServer::SSLOptions( |
| 125 net::BaseTestServer::SSLOptions::CERT_OK), | 132 net::BaseTestServer::SSLOptions::CERT_OK), |
| 126 base::FilePath(FILE_PATH_LITERAL("content/test/data/")))); | 133 base::FilePath(FILE_PATH_LITERAL("content/test/data/")))); |
| 127 ASSERT_TRUE(https_server_->Start()); | 134 ASSERT_TRUE(https_server_->Start()); |
| 128 | 135 |
| 136 SetIncognitoMode(false); | |
| 137 | |
| 129 SetOnline(true); | 138 SetOnline(true); |
| 130 | 139 |
| 131 SetIncognitoMode(false); | |
| 132 | |
| 133 ASSERT_TRUE(LoadTestPage(kDefaultTestURL)); | 140 ASSERT_TRUE(LoadTestPage(kDefaultTestURL)); |
| 134 | 141 |
| 135 ContentBrowserTest::SetUpOnMainThread(); | 142 ContentBrowserTest::SetUpOnMainThread(); |
| 136 } | 143 } |
| 137 | 144 |
| 138 void TearDownOnMainThread() override { https_server_.reset(); } | 145 void TearDownOnMainThread() override { https_server_.reset(); } |
| 139 | 146 |
| 140 bool LoadTestPage(const std::string& path) { | 147 bool LoadTestPage(const std::string& path) { |
| 141 return NavigateToURL(shell_, https_server_->GetURL(path)); | 148 return NavigateToURL(shell_, https_server_->GetURL(path)); |
| 142 } | 149 } |
| 143 | 150 |
| 144 bool RunScript(const std::string& script, std::string* result) { | 151 bool RunScript(const std::string& script, std::string* result) { |
| 145 return content::ExecuteScriptAndExtractString(shell_->web_contents(), | 152 return content::ExecuteScriptAndExtractString(shell_->web_contents(), |
| 146 script, result); | 153 script, result); |
| 147 } | 154 } |
| 148 | 155 |
| 149 void SetOnline(bool online); | 156 void SetOnline(bool online); |
| 157 void SetOnlineOnIOThread(BackgroundSyncContext* sync_context, bool online); | |
|
jkarlin
2015/09/11 00:05:23
const scoped_refptr<BackgroundSyncContext>&
iclelland
2015/09/16 17:17:11
Done.
| |
| 150 | 158 |
| 151 // Returns true if the one-shot sync with tag is currently pending. Fails | 159 // Returns true if the one-shot sync with tag is currently pending. Fails |
| 152 // (assertion failure) if the tag isn't registered. | 160 // (assertion failure) if the tag isn't registered. |
| 153 bool OneShotPending(const std::string& tag); | 161 bool OneShotPending(const std::string& tag); |
| 154 | 162 |
| 155 bool PopConsole(const std::string& expected_msg); | 163 bool PopConsole(const std::string& expected_msg); |
| 156 bool RegisterServiceWorker(); | 164 bool RegisterServiceWorker(); |
| 157 bool RegisterOneShot(const std::string& tag); | 165 bool RegisterOneShot(const std::string& tag); |
| 158 bool GetRegistrationOneShot(const std::string& tag); | 166 bool GetRegistrationOneShot(const std::string& tag); |
| 159 bool GetRegistrationsOneShot(const std::vector<std::string>& expected_tags); | 167 bool GetRegistrationsOneShot(const std::vector<std::string>& expected_tags); |
| 160 bool CompleteDelayedOneShot(); | 168 bool CompleteDelayedOneShot(); |
| 161 bool RejectDelayedOneShot(); | 169 bool RejectDelayedOneShot(); |
| 162 | 170 |
| 163 private: | 171 private: |
| 164 scoped_ptr<net::SpawnedTestServer> https_server_; | 172 scoped_ptr<net::SpawnedTestServer> https_server_; |
| 165 Shell* shell_ = nullptr; | 173 Shell* shell_ = nullptr; |
| 166 | 174 |
| 167 DISALLOW_COPY_AND_ASSIGN(BackgroundSyncBrowserTest); | 175 DISALLOW_COPY_AND_ASSIGN(BackgroundSyncBrowserTest); |
| 168 }; | 176 }; |
| 169 | 177 |
| 170 void BackgroundSyncBrowserTest::SetOnline(bool online) { | 178 void BackgroundSyncBrowserTest::SetOnline(bool online) { |
| 171 if (online) { | 179 if (shell_) { |
|
jkarlin
2015/09/11 00:05:23
What happens if there isn't a shell?
Perhaps ASSE
iclelland
2015/09/11 14:42:15
It would just not try to get the sync context from
iclelland
2015/09/16 17:17:11
Done.
| |
| 172 NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests( | 180 BrowserThread::PostTask( |
| 173 NetworkChangeNotifier::CONNECTION_WIFI); | 181 BrowserThread::IO, FROM_HERE, |
| 174 } else { | 182 base::Bind(&BackgroundSyncBrowserTest::SetOnlineOnIOThread, |
| 175 NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests( | 183 base::Unretained(this), |
| 176 NetworkChangeNotifier::CONNECTION_NONE); | 184 base::Unretained(GetSyncContextFromShell(shell_)), online)); |
| 177 } | 185 } |
| 178 base::RunLoop().RunUntilIdle(); | 186 base::RunLoop().RunUntilIdle(); |
| 179 } | 187 } |
| 180 | 188 |
| 189 void BackgroundSyncBrowserTest::SetOnlineOnIOThread( | |
| 190 BackgroundSyncContext* sync_context, | |
| 191 bool online) { | |
| 192 BackgroundSyncManager* sync_manager = sync_context->background_sync_manager(); | |
| 193 BackgroundSyncNetworkObserver* network_observer = | |
| 194 sync_manager->GetNetworkObserverForTesting(); | |
| 195 if (online) { | |
| 196 network_observer->OnNetworkChanged(NetworkChangeNotifier::CONNECTION_WIFI); | |
| 197 } else { | |
| 198 network_observer->OnNetworkChanged(NetworkChangeNotifier::CONNECTION_NONE); | |
| 199 } | |
| 200 } | |
| 201 | |
| 181 bool BackgroundSyncBrowserTest::OneShotPending(const std::string& tag) { | 202 bool BackgroundSyncBrowserTest::OneShotPending(const std::string& tag) { |
| 182 bool is_pending; | 203 bool is_pending; |
| 183 base::RunLoop run_loop; | 204 base::RunLoop run_loop; |
| 184 | 205 |
| 185 StoragePartition* storage = BrowserContext::GetDefaultStoragePartition( | 206 StoragePartition* storage = BrowserContext::GetDefaultStoragePartition( |
| 186 shell_->web_contents()->GetBrowserContext()); | 207 shell_->web_contents()->GetBrowserContext()); |
| 187 BackgroundSyncContext* sync_context = storage->GetBackgroundSyncContext(); | 208 BackgroundSyncContext* sync_context = storage->GetBackgroundSyncContext(); |
| 188 ServiceWorkerContextWrapper* service_worker_context = | 209 ServiceWorkerContextWrapper* service_worker_context = |
| 189 static_cast<ServiceWorkerContextWrapper*>( | 210 static_cast<ServiceWorkerContextWrapper*>( |
| 190 storage->GetServiceWorkerContext()); | 211 storage->GetServiceWorkerContext()); |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 370 | 391 |
| 371 for (const std::string& tag : registered_tags) | 392 for (const std::string& tag : registered_tags) |
| 372 EXPECT_TRUE(RegisterOneShot(tag)); | 393 EXPECT_TRUE(RegisterOneShot(tag)); |
| 373 | 394 |
| 374 EXPECT_TRUE(GetRegistrationsOneShot(registered_tags)); | 395 EXPECT_TRUE(GetRegistrationsOneShot(registered_tags)); |
| 375 } | 396 } |
| 376 | 397 |
| 377 } // namespace | 398 } // namespace |
| 378 | 399 |
| 379 } // namespace content | 400 } // namespace content |
| OLD | NEW |