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" |
24 #include "content/public/test/content_browser_test.h" | 25 #include "content/public/test/content_browser_test.h" |
25 #include "content/public/test/content_browser_test_utils.h" | 26 #include "content/public/test/content_browser_test_utils.h" |
26 #include "content/public/test/test_utils.h" | 27 #include "content/public/test/test_utils.h" |
27 #include "content/shell/browser/shell.h" | 28 #include "content/shell/browser/shell.h" |
28 #include "net/base/network_change_notifier.h" | 29 #include "net/base/network_change_notifier.h" |
29 #include "testing/gtest/include/gtest/gtest.h" | 30 #include "testing/gtest/include/gtest/gtest.h" |
30 | 31 |
| 32 #if defined(OS_ANDROID) |
| 33 #include "content/browser/android/background_sync_network_observer_android.h" |
| 34 #endif |
| 35 |
31 using net::NetworkChangeNotifier; | 36 using net::NetworkChangeNotifier; |
32 | 37 |
33 namespace content { | 38 namespace content { |
34 | 39 |
35 namespace { | 40 namespace { |
36 | 41 |
37 const char kDefaultTestURL[] = "files/background_sync/test.html"; | 42 const char kDefaultTestURL[] = "files/background_sync/test.html"; |
38 | 43 |
39 const char kSuccessfulOperationPrefix[] = "ok - "; | 44 const char kSuccessfulOperationPrefix[] = "ok - "; |
40 | 45 |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 url, base::Bind(&OneShotPendingDidGetSWRegistration, sync_context, tag, | 96 url, base::Bind(&OneShotPendingDidGetSWRegistration, sync_context, tag, |
92 callback)); | 97 callback)); |
93 } | 98 } |
94 | 99 |
95 class BackgroundSyncBrowserTest : public ContentBrowserTest { | 100 class BackgroundSyncBrowserTest : public ContentBrowserTest { |
96 public: | 101 public: |
97 BackgroundSyncBrowserTest() {} | 102 BackgroundSyncBrowserTest() {} |
98 ~BackgroundSyncBrowserTest() override {} | 103 ~BackgroundSyncBrowserTest() override {} |
99 | 104 |
100 void SetUp() override { | 105 void SetUp() override { |
101 NetworkChangeNotifier::SetTestNotificationsOnly(true); | 106 BackgroundSyncNetworkObserver::SetIgnoreNetworkChangeNotifierForTests(true); |
102 | 107 |
103 #if defined(OS_CHROMEOS) | 108 #if defined(OS_CHROMEOS) |
104 // ChromeOS's NetworkChangeNotifier doesn't get created in | 109 // ChromeOS's NetworkChangeNotifier doesn't get created in |
105 // content_browsertests, so make one now. | 110 // content_browsertests, so make one now. |
106 net::NetworkChangeNotifier::CreateMock(); | 111 net::NetworkChangeNotifier::CreateMock(); |
107 #endif | 112 #endif |
108 | 113 |
109 ContentBrowserTest::SetUp(); | 114 ContentBrowserTest::SetUp(); |
110 } | 115 } |
111 | 116 |
112 void SetIncognitoMode(bool incognito) { | 117 void SetIncognitoMode(bool incognito) { |
113 shell_ = incognito ? CreateOffTheRecordBrowser() : shell(); | 118 shell_ = incognito ? CreateOffTheRecordBrowser() : shell(); |
114 } | 119 } |
115 | 120 |
| 121 BackgroundSyncContext* GetSyncContextFromShell(Shell* shell) { |
| 122 StoragePartition* storage = BrowserContext::GetDefaultStoragePartition( |
| 123 shell_->web_contents()->GetBrowserContext()); |
| 124 return storage->GetBackgroundSyncContext(); |
| 125 } |
| 126 |
116 void SetUpCommandLine(base::CommandLine* command_line) override { | 127 void SetUpCommandLine(base::CommandLine* command_line) override { |
117 // TODO(jkarlin): Remove this once background sync is no longer | 128 // TODO(jkarlin): Remove this once background sync is no longer |
118 // experimental. | 129 // experimental. |
119 command_line->AppendSwitch( | 130 command_line->AppendSwitch( |
120 switches::kEnableExperimentalWebPlatformFeatures); | 131 switches::kEnableExperimentalWebPlatformFeatures); |
121 } | 132 } |
122 | 133 |
123 void SetUpOnMainThread() override { | 134 void SetUpOnMainThread() override { |
124 https_server_.reset(new net::SpawnedTestServer( | 135 https_server_.reset(new net::SpawnedTestServer( |
125 net::SpawnedTestServer::TYPE_HTTPS, | 136 net::SpawnedTestServer::TYPE_HTTPS, |
126 net::BaseTestServer::SSLOptions( | 137 net::BaseTestServer::SSLOptions( |
127 net::BaseTestServer::SSLOptions::CERT_OK), | 138 net::BaseTestServer::SSLOptions::CERT_OK), |
128 base::FilePath(FILE_PATH_LITERAL("content/test/data/")))); | 139 base::FilePath(FILE_PATH_LITERAL("content/test/data/")))); |
129 ASSERT_TRUE(https_server_->Start()); | 140 ASSERT_TRUE(https_server_->Start()); |
130 | 141 |
| 142 SetIncognitoMode(false); |
| 143 |
131 SetOnline(true); | 144 SetOnline(true); |
132 | 145 |
133 SetIncognitoMode(false); | |
134 | |
135 ASSERT_TRUE(LoadTestPage(kDefaultTestURL)); | 146 ASSERT_TRUE(LoadTestPage(kDefaultTestURL)); |
136 | 147 |
137 ContentBrowserTest::SetUpOnMainThread(); | 148 ContentBrowserTest::SetUpOnMainThread(); |
138 } | 149 } |
139 | 150 |
140 void TearDownOnMainThread() override { https_server_.reset(); } | 151 void TearDownOnMainThread() override { https_server_.reset(); } |
141 | 152 |
142 bool LoadTestPage(const std::string& path) { | 153 bool LoadTestPage(const std::string& path) { |
143 return NavigateToURL(shell_, https_server_->GetURL(path)); | 154 return NavigateToURL(shell_, https_server_->GetURL(path)); |
144 } | 155 } |
145 | 156 |
146 bool RunScript(const std::string& script, std::string* result) { | 157 bool RunScript(const std::string& script, std::string* result) { |
147 return content::ExecuteScriptAndExtractString(shell_->web_contents(), | 158 return content::ExecuteScriptAndExtractString(shell_->web_contents(), |
148 script, result); | 159 script, result); |
149 } | 160 } |
150 | 161 |
| 162 // This runs asynchronously on the IO thread, but we don't need to wait for it |
| 163 // to complete before running a background sync operation, since those also |
| 164 // run on the IO thread. |
151 void SetOnline(bool online); | 165 void SetOnline(bool online); |
| 166 void SetOnlineOnIOThread( |
| 167 const scoped_refptr<BackgroundSyncContext>& sync_context, |
| 168 bool online); |
152 | 169 |
153 // Returns true if the one-shot sync with tag is currently pending. Fails | 170 // Returns true if the one-shot sync with tag is currently pending. Fails |
154 // (assertion failure) if the tag isn't registered. | 171 // (assertion failure) if the tag isn't registered. |
155 bool OneShotPending(const std::string& tag); | 172 bool OneShotPending(const std::string& tag); |
156 | 173 |
157 bool PopConsole(const std::string& expected_msg); | 174 bool PopConsole(const std::string& expected_msg); |
158 bool RegisterServiceWorker(); | 175 bool RegisterServiceWorker(); |
159 bool RegisterOneShot(const std::string& tag); | 176 bool RegisterOneShot(const std::string& tag); |
160 bool UnregisterOneShot(const std::string& tag); | 177 bool UnregisterOneShot(const std::string& tag); |
161 bool UnregisterOneShotTwice(const std::string& tag); | 178 bool UnregisterOneShotTwice(const std::string& tag); |
162 bool GetRegistrationOneShot(const std::string& tag); | 179 bool GetRegistrationOneShot(const std::string& tag); |
163 bool GetRegistrationsOneShot(const std::vector<std::string>& expected_tags); | 180 bool GetRegistrationsOneShot(const std::vector<std::string>& expected_tags); |
164 bool CompleteDelayedOneShot(); | 181 bool CompleteDelayedOneShot(); |
165 bool RejectDelayedOneShot(); | 182 bool RejectDelayedOneShot(); |
166 | 183 |
167 private: | 184 private: |
168 scoped_ptr<net::SpawnedTestServer> https_server_; | 185 scoped_ptr<net::SpawnedTestServer> https_server_; |
169 Shell* shell_ = nullptr; | 186 Shell* shell_ = nullptr; |
170 | 187 |
171 DISALLOW_COPY_AND_ASSIGN(BackgroundSyncBrowserTest); | 188 DISALLOW_COPY_AND_ASSIGN(BackgroundSyncBrowserTest); |
172 }; | 189 }; |
173 | 190 |
174 void BackgroundSyncBrowserTest::SetOnline(bool online) { | 191 void BackgroundSyncBrowserTest::SetOnline(bool online) { |
175 if (online) { | 192 ASSERT_TRUE(shell_); |
176 NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests( | 193 BrowserThread::PostTask( |
177 NetworkChangeNotifier::CONNECTION_WIFI); | 194 BrowserThread::IO, FROM_HERE, |
178 } else { | 195 base::Bind(&BackgroundSyncBrowserTest::SetOnlineOnIOThread, |
179 NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests( | 196 base::Unretained(this), |
180 NetworkChangeNotifier::CONNECTION_NONE); | 197 base::Unretained(GetSyncContextFromShell(shell_)), online)); |
181 } | |
182 base::RunLoop().RunUntilIdle(); | 198 base::RunLoop().RunUntilIdle(); |
183 } | 199 } |
184 | 200 |
| 201 void BackgroundSyncBrowserTest::SetOnlineOnIOThread( |
| 202 const scoped_refptr<BackgroundSyncContext>& sync_context, |
| 203 bool online) { |
| 204 BackgroundSyncManager* sync_manager = sync_context->background_sync_manager(); |
| 205 BackgroundSyncNetworkObserver* network_observer = |
| 206 sync_manager->GetNetworkObserverForTesting(); |
| 207 if (online) { |
| 208 network_observer->NotifyManagerIfNetworkChanged( |
| 209 NetworkChangeNotifier::CONNECTION_WIFI); |
| 210 } else { |
| 211 network_observer->NotifyManagerIfNetworkChanged( |
| 212 NetworkChangeNotifier::CONNECTION_NONE); |
| 213 } |
| 214 } |
| 215 |
185 bool BackgroundSyncBrowserTest::OneShotPending(const std::string& tag) { | 216 bool BackgroundSyncBrowserTest::OneShotPending(const std::string& tag) { |
186 bool is_pending; | 217 bool is_pending; |
187 base::RunLoop run_loop; | 218 base::RunLoop run_loop; |
188 | 219 |
189 StoragePartition* storage = BrowserContext::GetDefaultStoragePartition( | 220 StoragePartition* storage = BrowserContext::GetDefaultStoragePartition( |
190 shell_->web_contents()->GetBrowserContext()); | 221 shell_->web_contents()->GetBrowserContext()); |
191 BackgroundSyncContext* sync_context = storage->GetBackgroundSyncContext(); | 222 BackgroundSyncContext* sync_context = storage->GetBackgroundSyncContext(); |
192 ServiceWorkerContextWrapper* service_worker_context = | 223 ServiceWorkerContextWrapper* service_worker_context = |
193 static_cast<ServiceWorkerContextWrapper*>( | 224 static_cast<ServiceWorkerContextWrapper*>( |
194 storage->GetServiceWorkerContext()); | 225 storage->GetServiceWorkerContext()); |
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
417 EXPECT_TRUE(RegisterServiceWorker()); | 448 EXPECT_TRUE(RegisterServiceWorker()); |
418 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. | 449 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. |
419 | 450 |
420 EXPECT_TRUE(RegisterOneShot("unregister")); | 451 EXPECT_TRUE(RegisterOneShot("unregister")); |
421 EXPECT_TRUE(PopConsole("ok - unregister completed")); | 452 EXPECT_TRUE(PopConsole("ok - unregister completed")); |
422 } | 453 } |
423 | 454 |
424 } // namespace | 455 } // namespace |
425 | 456 |
426 } // namespace content | 457 } // namespace content |
OLD | NEW |