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" | |
15 #include "content/browser/background_sync/background_sync_registration_handle.h" | 14 #include "content/browser/background_sync/background_sync_registration_handle.h" |
16 #include "content/browser/background_sync/background_sync_status.h" | 15 #include "content/browser/background_sync/background_sync_status.h" |
17 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 16 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
18 #include "content/browser/service_worker/service_worker_registration.h" | 17 #include "content/browser/service_worker/service_worker_registration.h" |
19 #include "content/public/browser/background_sync_context.h" | 18 #include "content/public/browser/background_sync_context.h" |
20 #include "content/public/browser/browser_context.h" | 19 #include "content/public/browser/browser_context.h" |
21 #include "content/public/browser/storage_partition.h" | 20 #include "content/public/browser/storage_partition.h" |
22 #include "content/public/browser/web_contents.h" | 21 #include "content/public/browser/web_contents.h" |
23 #include "content/public/common/content_switches.h" | 22 #include "content/public/common/content_switches.h" |
24 #include "content/public/test/browser_test_utils.h" | 23 #include "content/public/test/browser_test_utils.h" |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 const scoped_refptr<BackgroundSyncContext> sync_context, | 85 const scoped_refptr<BackgroundSyncContext> sync_context, |
87 const scoped_refptr<ServiceWorkerContextWrapper> sw_context, | 86 const scoped_refptr<ServiceWorkerContextWrapper> sw_context, |
88 const std::string& tag, | 87 const std::string& tag, |
89 const GURL& url, | 88 const GURL& url, |
90 const base::Callback<void(bool)>& callback) { | 89 const base::Callback<void(bool)>& callback) { |
91 sw_context->FindRegistrationForDocument( | 90 sw_context->FindRegistrationForDocument( |
92 url, base::Bind(&OneShotPendingDidGetSWRegistration, sync_context, tag, | 91 url, base::Bind(&OneShotPendingDidGetSWRegistration, sync_context, tag, |
93 callback)); | 92 callback)); |
94 } | 93 } |
95 | 94 |
96 } // namespace | |
97 | |
98 class BackgroundSyncBrowserTest : public ContentBrowserTest { | 95 class BackgroundSyncBrowserTest : public ContentBrowserTest { |
99 public: | 96 public: |
100 BackgroundSyncBrowserTest() {} | 97 BackgroundSyncBrowserTest() {} |
101 ~BackgroundSyncBrowserTest() override {} | 98 ~BackgroundSyncBrowserTest() override {} |
102 | 99 |
103 void SetUp() override { | 100 void SetUp() override { |
104 BackgroundSyncNetworkObserver::SetIgnoreNetworkChangeNotifierForTests(true); | 101 NetworkChangeNotifier::SetTestNotificationsOnly(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 |
105 ContentBrowserTest::SetUp(); | 109 ContentBrowserTest::SetUp(); |
106 } | 110 } |
107 | 111 |
108 void SetIncognitoMode(bool incognito) { | 112 void SetIncognitoMode(bool incognito) { |
109 shell_ = incognito ? CreateOffTheRecordBrowser() : shell(); | 113 shell_ = incognito ? CreateOffTheRecordBrowser() : shell(); |
110 } | 114 } |
111 | 115 |
112 BackgroundSyncContext* GetSyncContextFromShell(Shell* shell) { | |
113 StoragePartition* storage = BrowserContext::GetDefaultStoragePartition( | |
114 shell_->web_contents()->GetBrowserContext()); | |
115 return storage->GetBackgroundSyncContext(); | |
116 } | |
117 | |
118 void SetUpCommandLine(base::CommandLine* command_line) override { | 116 void SetUpCommandLine(base::CommandLine* command_line) override { |
119 // TODO(jkarlin): Remove this once background sync is no longer | 117 // TODO(jkarlin): Remove this once background sync is no longer |
120 // experimental. | 118 // experimental. |
121 command_line->AppendSwitch( | 119 command_line->AppendSwitch( |
122 switches::kEnableExperimentalWebPlatformFeatures); | 120 switches::kEnableExperimentalWebPlatformFeatures); |
123 } | 121 } |
124 | 122 |
125 void SetUpOnMainThread() override { | 123 void SetUpOnMainThread() override { |
126 https_server_.reset(new net::SpawnedTestServer( | 124 https_server_.reset(new net::SpawnedTestServer( |
127 net::SpawnedTestServer::TYPE_HTTPS, | 125 net::SpawnedTestServer::TYPE_HTTPS, |
128 net::BaseTestServer::SSLOptions( | 126 net::BaseTestServer::SSLOptions( |
129 net::BaseTestServer::SSLOptions::CERT_OK), | 127 net::BaseTestServer::SSLOptions::CERT_OK), |
130 base::FilePath(FILE_PATH_LITERAL("content/test/data/")))); | 128 base::FilePath(FILE_PATH_LITERAL("content/test/data/")))); |
131 ASSERT_TRUE(https_server_->Start()); | 129 ASSERT_TRUE(https_server_->Start()); |
132 | 130 |
| 131 SetOnline(true); |
| 132 |
133 SetIncognitoMode(false); | 133 SetIncognitoMode(false); |
134 | 134 |
135 SetOnline(true); | |
136 | |
137 ASSERT_TRUE(LoadTestPage(kDefaultTestURL)); | 135 ASSERT_TRUE(LoadTestPage(kDefaultTestURL)); |
138 | 136 |
139 ContentBrowserTest::SetUpOnMainThread(); | 137 ContentBrowserTest::SetUpOnMainThread(); |
140 } | 138 } |
141 | 139 |
142 void TearDownOnMainThread() override { https_server_.reset(); } | 140 void TearDownOnMainThread() override { https_server_.reset(); } |
143 | 141 |
144 bool LoadTestPage(const std::string& path) { | 142 bool LoadTestPage(const std::string& path) { |
145 return NavigateToURL(shell_, https_server_->GetURL(path)); | 143 return NavigateToURL(shell_, https_server_->GetURL(path)); |
146 } | 144 } |
147 | 145 |
148 bool RunScript(const std::string& script, std::string* result) { | 146 bool RunScript(const std::string& script, std::string* result) { |
149 return content::ExecuteScriptAndExtractString(shell_->web_contents(), | 147 return content::ExecuteScriptAndExtractString(shell_->web_contents(), |
150 script, result); | 148 script, result); |
151 } | 149 } |
152 | 150 |
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. | |
156 void SetOnline(bool online); | 151 void SetOnline(bool online); |
157 void SetOnlineOnIOThread( | |
158 const scoped_refptr<BackgroundSyncContext>& sync_context, | |
159 bool online); | |
160 | 152 |
161 // Returns true if the one-shot sync with tag is currently pending. Fails | 153 // Returns true if the one-shot sync with tag is currently pending. Fails |
162 // (assertion failure) if the tag isn't registered. | 154 // (assertion failure) if the tag isn't registered. |
163 bool OneShotPending(const std::string& tag); | 155 bool OneShotPending(const std::string& tag); |
164 | 156 |
165 bool PopConsole(const std::string& expected_msg); | 157 bool PopConsole(const std::string& expected_msg); |
166 bool RegisterServiceWorker(); | 158 bool RegisterServiceWorker(); |
167 bool RegisterOneShot(const std::string& tag); | 159 bool RegisterOneShot(const std::string& tag); |
168 bool UnregisterOneShot(const std::string& tag); | 160 bool UnregisterOneShot(const std::string& tag); |
169 bool UnregisterOneShotTwice(const std::string& tag); | 161 bool UnregisterOneShotTwice(const std::string& tag); |
170 bool GetRegistrationOneShot(const std::string& tag); | 162 bool GetRegistrationOneShot(const std::string& tag); |
171 bool GetRegistrationsOneShot(const std::vector<std::string>& expected_tags); | 163 bool GetRegistrationsOneShot(const std::vector<std::string>& expected_tags); |
172 bool CompleteDelayedOneShot(); | 164 bool CompleteDelayedOneShot(); |
173 bool RejectDelayedOneShot(); | 165 bool RejectDelayedOneShot(); |
174 bool NotifyWhenDoneOneShot(const std::string& tag); | 166 bool NotifyWhenDoneOneShot(const std::string& tag); |
175 bool NotifyWhenDoneImmediateOneShot(const std::string& expected_msg); | 167 bool NotifyWhenDoneImmediateOneShot(const std::string& expected_msg); |
176 bool StoreRegistrationOneShot(const std::string& tag); | 168 bool StoreRegistrationOneShot(const std::string& tag); |
177 | 169 |
178 private: | 170 private: |
179 scoped_ptr<net::SpawnedTestServer> https_server_; | 171 scoped_ptr<net::SpawnedTestServer> https_server_; |
180 Shell* shell_ = nullptr; | 172 Shell* shell_ = nullptr; |
181 | 173 |
182 DISALLOW_COPY_AND_ASSIGN(BackgroundSyncBrowserTest); | 174 DISALLOW_COPY_AND_ASSIGN(BackgroundSyncBrowserTest); |
183 }; | 175 }; |
184 | 176 |
185 void BackgroundSyncBrowserTest::SetOnline(bool online) { | 177 void BackgroundSyncBrowserTest::SetOnline(bool online) { |
186 ASSERT_TRUE(shell_); | 178 if (online) { |
187 BrowserThread::PostTask( | 179 NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests( |
188 BrowserThread::IO, FROM_HERE, | 180 NetworkChangeNotifier::CONNECTION_WIFI); |
189 base::Bind(&BackgroundSyncBrowserTest::SetOnlineOnIOThread, | 181 } else { |
190 base::Unretained(this), | 182 NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests( |
191 base::Unretained(GetSyncContextFromShell(shell_)), online)); | 183 NetworkChangeNotifier::CONNECTION_NONE); |
| 184 } |
192 base::RunLoop().RunUntilIdle(); | 185 base::RunLoop().RunUntilIdle(); |
193 } | 186 } |
194 | 187 |
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 | |
210 bool BackgroundSyncBrowserTest::OneShotPending(const std::string& tag) { | 188 bool BackgroundSyncBrowserTest::OneShotPending(const std::string& tag) { |
211 bool is_pending; | 189 bool is_pending; |
212 base::RunLoop run_loop; | 190 base::RunLoop run_loop; |
213 | 191 |
214 StoragePartition* storage = BrowserContext::GetDefaultStoragePartition( | 192 StoragePartition* storage = BrowserContext::GetDefaultStoragePartition( |
215 shell_->web_contents()->GetBrowserContext()); | 193 shell_->web_contents()->GetBrowserContext()); |
216 BackgroundSyncContext* sync_context = storage->GetBackgroundSyncContext(); | 194 BackgroundSyncContext* sync_context = storage->GetBackgroundSyncContext(); |
217 ServiceWorkerContextWrapper* service_worker_context = | 195 ServiceWorkerContextWrapper* service_worker_context = |
218 static_cast<ServiceWorkerContextWrapper*>( | 196 static_cast<ServiceWorkerContextWrapper*>( |
219 storage->GetServiceWorkerContext()); | 197 storage->GetServiceWorkerContext()); |
(...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
528 SetOnline(true); | 506 SetOnline(true); |
529 EXPECT_TRUE(RegisterOneShot("delay")); | 507 EXPECT_TRUE(RegisterOneShot("delay")); |
530 EXPECT_FALSE(OneShotPending("delay")); | 508 EXPECT_FALSE(OneShotPending("delay")); |
531 EXPECT_TRUE(StoreRegistrationOneShot("delay")); | 509 EXPECT_TRUE(StoreRegistrationOneShot("delay")); |
532 | 510 |
533 EXPECT_TRUE(RejectDelayedOneShot()); | 511 EXPECT_TRUE(RejectDelayedOneShot()); |
534 EXPECT_TRUE(PopConsole("ok - delay rejected")); | 512 EXPECT_TRUE(PopConsole("ok - delay rejected")); |
535 EXPECT_TRUE(NotifyWhenDoneImmediateOneShot("ok - delay result: false")); | 513 EXPECT_TRUE(NotifyWhenDoneImmediateOneShot("ok - delay result: false")); |
536 } | 514 } |
537 | 515 |
| 516 } // namespace |
| 517 |
538 } // namespace content | 518 } // namespace content |
OLD | NEW |