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" |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 | 102 |
103 void SetUp() override { | 103 void SetUp() override { |
104 BackgroundSyncNetworkObserver::SetIgnoreNetworkChangeNotifierForTests(true); | 104 BackgroundSyncNetworkObserver::SetIgnoreNetworkChangeNotifierForTests(true); |
105 ContentBrowserTest::SetUp(); | 105 ContentBrowserTest::SetUp(); |
106 } | 106 } |
107 | 107 |
108 void SetIncognitoMode(bool incognito) { | 108 void SetIncognitoMode(bool incognito) { |
109 shell_ = incognito ? CreateOffTheRecordBrowser() : shell(); | 109 shell_ = incognito ? CreateOffTheRecordBrowser() : shell(); |
110 } | 110 } |
111 | 111 |
112 BackgroundSyncContext* GetSyncContextFromShell(Shell* shell) { | 112 StoragePartition* GetStorage() { |
113 StoragePartition* storage = BrowserContext::GetDefaultStoragePartition( | 113 WebContents* web_contents = shell_->web_contents(); |
114 shell_->web_contents()->GetBrowserContext()); | 114 return BrowserContext::GetStoragePartition( |
115 return storage->GetBackgroundSyncContext(); | 115 web_contents->GetBrowserContext(), web_contents->GetSiteInstance()); |
| 116 } |
| 117 |
| 118 BackgroundSyncContext* GetSyncContext() { |
| 119 return GetStorage()->GetBackgroundSyncContext(); |
116 } | 120 } |
117 | 121 |
118 void SetUpCommandLine(base::CommandLine* command_line) override { | 122 void SetUpCommandLine(base::CommandLine* command_line) override { |
119 // TODO(jkarlin): Remove this once background sync is no longer | 123 // TODO(jkarlin): Remove this once background sync is no longer |
120 // experimental. | 124 // experimental. |
121 command_line->AppendSwitch( | 125 command_line->AppendSwitch( |
122 switches::kEnableExperimentalWebPlatformFeatures); | 126 switches::kEnableExperimentalWebPlatformFeatures); |
123 } | 127 } |
124 | 128 |
125 void SetUpOnMainThread() override { | 129 void SetUpOnMainThread() override { |
(...skipping 29 matching lines...) Expand all Loading... |
155 // run on the IO thread. | 159 // run on the IO thread. |
156 void SetOnline(bool online); | 160 void SetOnline(bool online); |
157 void SetOnlineOnIOThread( | 161 void SetOnlineOnIOThread( |
158 const scoped_refptr<BackgroundSyncContext>& sync_context, | 162 const scoped_refptr<BackgroundSyncContext>& sync_context, |
159 bool online); | 163 bool online); |
160 | 164 |
161 // Returns true if the one-shot sync with tag is currently pending. Fails | 165 // Returns true if the one-shot sync with tag is currently pending. Fails |
162 // (assertion failure) if the tag isn't registered. | 166 // (assertion failure) if the tag isn't registered. |
163 bool OneShotPending(const std::string& tag); | 167 bool OneShotPending(const std::string& tag); |
164 | 168 |
| 169 void ClearStoragePartitionData(); |
| 170 |
165 std::string PopConsoleString(); | 171 std::string PopConsoleString(); |
166 bool PopConsole(const std::string& expected_msg); | 172 bool PopConsole(const std::string& expected_msg); |
167 bool RegisterServiceWorker(); | 173 bool RegisterServiceWorker(); |
168 bool RegisterOneShot(const std::string& tag); | 174 bool RegisterOneShot(const std::string& tag); |
169 bool RegisterOneShotFromServiceWorker(const std::string& tag); | 175 bool RegisterOneShotFromServiceWorker(const std::string& tag); |
170 bool UnregisterOneShot(const std::string& tag); | 176 bool UnregisterOneShot(const std::string& tag); |
171 bool UnregisterOneShotTwice(const std::string& tag); | 177 bool UnregisterOneShotTwice(const std::string& tag); |
172 bool GetRegistrationOneShot(const std::string& tag); | 178 bool GetRegistrationOneShot(const std::string& tag); |
173 bool GetRegistrationOneShotFromServiceWorker(const std::string& tag); | 179 bool GetRegistrationOneShotFromServiceWorker(const std::string& tag); |
174 bool MatchRegistrations(const std::string& script_result, | 180 bool MatchRegistrations(const std::string& script_result, |
(...skipping 12 matching lines...) Expand all Loading... |
187 Shell* shell_ = nullptr; | 193 Shell* shell_ = nullptr; |
188 | 194 |
189 DISALLOW_COPY_AND_ASSIGN(BackgroundSyncBrowserTest); | 195 DISALLOW_COPY_AND_ASSIGN(BackgroundSyncBrowserTest); |
190 }; | 196 }; |
191 | 197 |
192 void BackgroundSyncBrowserTest::SetOnline(bool online) { | 198 void BackgroundSyncBrowserTest::SetOnline(bool online) { |
193 ASSERT_TRUE(shell_); | 199 ASSERT_TRUE(shell_); |
194 BrowserThread::PostTask( | 200 BrowserThread::PostTask( |
195 BrowserThread::IO, FROM_HERE, | 201 BrowserThread::IO, FROM_HERE, |
196 base::Bind(&BackgroundSyncBrowserTest::SetOnlineOnIOThread, | 202 base::Bind(&BackgroundSyncBrowserTest::SetOnlineOnIOThread, |
197 base::Unretained(this), | 203 base::Unretained(this), base::Unretained(GetSyncContext()), |
198 base::Unretained(GetSyncContextFromShell(shell_)), online)); | 204 online)); |
199 base::RunLoop().RunUntilIdle(); | 205 base::RunLoop().RunUntilIdle(); |
200 } | 206 } |
201 | 207 |
202 void BackgroundSyncBrowserTest::SetOnlineOnIOThread( | 208 void BackgroundSyncBrowserTest::SetOnlineOnIOThread( |
203 const scoped_refptr<BackgroundSyncContext>& sync_context, | 209 const scoped_refptr<BackgroundSyncContext>& sync_context, |
204 bool online) { | 210 bool online) { |
205 BackgroundSyncManager* sync_manager = sync_context->background_sync_manager(); | 211 BackgroundSyncManager* sync_manager = sync_context->background_sync_manager(); |
206 BackgroundSyncNetworkObserver* network_observer = | 212 BackgroundSyncNetworkObserver* network_observer = |
207 sync_manager->GetNetworkObserverForTesting(); | 213 sync_manager->GetNetworkObserverForTesting(); |
208 if (online) { | 214 if (online) { |
209 network_observer->NotifyManagerIfNetworkChanged( | 215 network_observer->NotifyManagerIfNetworkChangedForTesting( |
210 NetworkChangeNotifier::CONNECTION_WIFI); | 216 NetworkChangeNotifier::CONNECTION_WIFI); |
211 } else { | 217 } else { |
212 network_observer->NotifyManagerIfNetworkChanged( | 218 network_observer->NotifyManagerIfNetworkChangedForTesting( |
213 NetworkChangeNotifier::CONNECTION_NONE); | 219 NetworkChangeNotifier::CONNECTION_NONE); |
214 } | 220 } |
215 } | 221 } |
216 | 222 |
217 bool BackgroundSyncBrowserTest::OneShotPending(const std::string& tag) { | 223 bool BackgroundSyncBrowserTest::OneShotPending(const std::string& tag) { |
218 bool is_pending; | 224 bool is_pending; |
219 base::RunLoop run_loop; | 225 base::RunLoop run_loop; |
220 | 226 |
221 StoragePartition* storage = BrowserContext::GetDefaultStoragePartition( | 227 StoragePartition* storage = GetStorage(); |
222 shell_->web_contents()->GetBrowserContext()); | |
223 BackgroundSyncContext* sync_context = storage->GetBackgroundSyncContext(); | 228 BackgroundSyncContext* sync_context = storage->GetBackgroundSyncContext(); |
224 ServiceWorkerContextWrapper* service_worker_context = | 229 ServiceWorkerContextWrapper* service_worker_context = |
225 static_cast<ServiceWorkerContextWrapper*>( | 230 static_cast<ServiceWorkerContextWrapper*>( |
226 storage->GetServiceWorkerContext()); | 231 storage->GetServiceWorkerContext()); |
227 | 232 |
228 base::Callback<void(bool)> callback = | 233 base::Callback<void(bool)> callback = |
229 base::Bind(&OneShotPendingCallback, run_loop.QuitClosure(), | 234 base::Bind(&OneShotPendingCallback, run_loop.QuitClosure(), |
230 base::ThreadTaskRunnerHandle::Get(), &is_pending); | 235 base::ThreadTaskRunnerHandle::Get(), &is_pending); |
231 | 236 |
232 BrowserThread::PostTask( | 237 BrowserThread::PostTask( |
233 BrowserThread::IO, FROM_HERE, | 238 BrowserThread::IO, FROM_HERE, |
234 base::Bind(&OneShotPendingOnIOThread, make_scoped_refptr(sync_context), | 239 base::Bind(&OneShotPendingOnIOThread, make_scoped_refptr(sync_context), |
235 make_scoped_refptr(service_worker_context), tag, | 240 make_scoped_refptr(service_worker_context), tag, |
236 https_server_->GetURL(kDefaultTestURL), callback)); | 241 https_server_->GetURL(kDefaultTestURL), callback)); |
237 | 242 |
238 run_loop.Run(); | 243 run_loop.Run(); |
239 | 244 |
240 return is_pending; | 245 return is_pending; |
241 } | 246 } |
242 | 247 |
| 248 void BackgroundSyncBrowserTest::ClearStoragePartitionData() { |
| 249 // Clear data from the storage partition. Parameters are set to clear data |
| 250 // for service workers, for all origins, for an unbounded time range. |
| 251 StoragePartition* storage = GetStorage(); |
| 252 |
| 253 uint32 storage_partition_mask = |
| 254 StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS; |
| 255 uint32 quota_storage_mask = StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL; |
| 256 const GURL& delete_origin = GURL(); |
| 257 const base::Time delete_begin = base::Time(); |
| 258 base::Time delete_end = base::Time::Max(); |
| 259 |
| 260 base::RunLoop run_loop; |
| 261 |
| 262 storage->ClearData(storage_partition_mask, quota_storage_mask, delete_origin, |
| 263 StoragePartition::OriginMatcherFunction(), delete_begin, |
| 264 delete_end, run_loop.QuitClosure()); |
| 265 |
| 266 run_loop.Run(); |
| 267 } |
| 268 |
243 std::string BackgroundSyncBrowserTest::PopConsoleString() { | 269 std::string BackgroundSyncBrowserTest::PopConsoleString() { |
244 std::string script_result; | 270 std::string script_result; |
245 EXPECT_TRUE(RunScript("resultQueue.pop()", &script_result)); | 271 EXPECT_TRUE(RunScript("resultQueue.pop()", &script_result)); |
246 return script_result; | 272 return script_result; |
247 } | 273 } |
248 | 274 |
249 bool BackgroundSyncBrowserTest::PopConsole(const std::string& expected_msg) { | 275 bool BackgroundSyncBrowserTest::PopConsole(const std::string& expected_msg) { |
250 std::string script_result = PopConsoleString(); | 276 std::string script_result = PopConsoleString(); |
251 return script_result == expected_msg; | 277 return script_result == expected_msg; |
252 } | 278 } |
(...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
590 EXPECT_TRUE(NotifyWhenFinishedOneShot("delay")); | 616 EXPECT_TRUE(NotifyWhenFinishedOneShot("delay")); |
591 | 617 |
592 EXPECT_TRUE(RejectDelayedOneShot()); | 618 EXPECT_TRUE(RejectDelayedOneShot()); |
593 // The ordering of PopConsole messages tells us that the event fired | 619 // The ordering of PopConsole messages tells us that the event fired |
594 // before finished resolved. | 620 // before finished resolved. |
595 EXPECT_TRUE(PopConsole("ok - delay rejected")); | 621 EXPECT_TRUE(PopConsole("ok - delay rejected")); |
596 EXPECT_TRUE(PopConsole("delay finished result: false")); | 622 EXPECT_TRUE(PopConsole("delay finished result: false")); |
597 } | 623 } |
598 | 624 |
599 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, | 625 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, |
600 CallFinishedAfterSyncSuceeds) { | 626 CallFinishedAfterSyncSucceeds) { |
601 EXPECT_TRUE(RegisterServiceWorker()); | 627 EXPECT_TRUE(RegisterServiceWorker()); |
602 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. | 628 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. |
603 | 629 |
604 SetOnline(false); | 630 SetOnline(false); |
605 EXPECT_TRUE(RegisterOneShot("foo")); | 631 EXPECT_TRUE(RegisterOneShot("foo")); |
606 EXPECT_TRUE(StoreRegistrationOneShot("foo")); | 632 EXPECT_TRUE(StoreRegistrationOneShot("foo")); |
607 | 633 |
608 SetOnline(true); | 634 SetOnline(true); |
609 EXPECT_TRUE(PopConsole("foo fired")); | 635 EXPECT_TRUE(PopConsole("foo fired")); |
610 EXPECT_FALSE(GetRegistrationOneShot("foo")); | 636 EXPECT_FALSE(GetRegistrationOneShot("foo")); |
(...skipping 20 matching lines...) Expand all Loading... |
631 SetOnline(true); | 657 SetOnline(true); |
632 EXPECT_TRUE(RegisterOneShot("delay")); | 658 EXPECT_TRUE(RegisterOneShot("delay")); |
633 EXPECT_FALSE(OneShotPending("delay")); | 659 EXPECT_FALSE(OneShotPending("delay")); |
634 EXPECT_TRUE(StoreRegistrationOneShot("delay")); | 660 EXPECT_TRUE(StoreRegistrationOneShot("delay")); |
635 | 661 |
636 EXPECT_TRUE(RejectDelayedOneShot()); | 662 EXPECT_TRUE(RejectDelayedOneShot()); |
637 EXPECT_TRUE(PopConsole("ok - delay rejected")); | 663 EXPECT_TRUE(PopConsole("ok - delay rejected")); |
638 EXPECT_TRUE(NotifyWhenFinishedImmediateOneShot("ok - delay result: false")); | 664 EXPECT_TRUE(NotifyWhenFinishedImmediateOneShot("ok - delay result: false")); |
639 } | 665 } |
640 | 666 |
| 667 // Verify that a background sync registration is deleted when site data is |
| 668 // cleared. |
| 669 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, |
| 670 SyncRegistrationDeletedWhenClearingSiteData) { |
| 671 EXPECT_TRUE(RegisterServiceWorker()); |
| 672 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. |
| 673 |
| 674 // Prevent firing by going offline. |
| 675 SetOnline(false); |
| 676 EXPECT_TRUE(RegisterOneShot("foo")); |
| 677 EXPECT_TRUE(GetRegistrationOneShot("foo")); |
| 678 EXPECT_TRUE(OneShotPending("foo")); |
| 679 |
| 680 // Simulate a user clearing site data (including Service Workers, crucially), |
| 681 // by clearing data from the storage partition. |
| 682 ClearStoragePartitionData(); |
| 683 |
| 684 EXPECT_FALSE(GetRegistrationOneShot("foo")); |
| 685 } |
| 686 |
| 687 // Verify that a background sync registration, from a service worker, is deleted |
| 688 // when site data is cleared. |
| 689 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, |
| 690 SyncRegistrationFromSWDeletedWhenClearingSiteData) { |
| 691 EXPECT_TRUE(RegisterServiceWorker()); |
| 692 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. |
| 693 |
| 694 std::vector<std::string> registered_tags; |
| 695 EXPECT_TRUE(GetRegistrationsOneShot(registered_tags)); |
| 696 |
| 697 SetOnline(false); |
| 698 |
| 699 EXPECT_TRUE(RegisterOneShotFromServiceWorker("foo_sw")); |
| 700 EXPECT_TRUE(PopConsole("ok - foo_sw registered in SW")); |
| 701 EXPECT_TRUE(GetRegistrationOneShotFromServiceWorker("foo_sw")); |
| 702 |
| 703 // Simulate a user clearing site data (including Service Workers, crucially), |
| 704 // by clearing data from the storage partition. |
| 705 ClearStoragePartitionData(); |
| 706 |
| 707 EXPECT_FALSE(GetRegistrationOneShotFromServiceWorker("foo")); |
| 708 } |
| 709 |
| 710 // Verify that multiple background sync registrations are deleted when site |
| 711 // data is cleared. |
| 712 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, |
| 713 SyncRegistrationsDeletedWhenClearingSiteData) { |
| 714 EXPECT_TRUE(RegisterServiceWorker()); |
| 715 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. |
| 716 |
| 717 std::vector<std::string> registered_tags; |
| 718 EXPECT_TRUE(GetRegistrationsOneShot(registered_tags)); |
| 719 |
| 720 SetOnline(false); |
| 721 registered_tags.push_back("foo"); |
| 722 registered_tags.push_back("bar"); |
| 723 |
| 724 for (const std::string& tag : registered_tags) |
| 725 EXPECT_TRUE(RegisterOneShot(tag)); |
| 726 |
| 727 EXPECT_TRUE(GetRegistrationsOneShot(registered_tags)); |
| 728 |
| 729 for (const std::string& tag : registered_tags) |
| 730 EXPECT_TRUE(OneShotPending(tag)); |
| 731 |
| 732 // Simulate a user clearing site data (including Service Workers, crucially), |
| 733 // by clearing data from the storage partition. |
| 734 ClearStoragePartitionData(); |
| 735 |
| 736 for (const std::string& tag : registered_tags) |
| 737 EXPECT_FALSE(GetRegistrationOneShot(tag)); |
| 738 } |
| 739 |
| 740 // Verify that a sync event that is currently firing is deleted when site |
| 741 // data is cleared. |
| 742 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, |
| 743 FiringSyncEventDeletedWhenClearingSiteData) { |
| 744 EXPECT_TRUE(RegisterServiceWorker()); |
| 745 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. |
| 746 |
| 747 SetOnline(true); |
| 748 EXPECT_TRUE(RegisterOneShot("delay")); |
| 749 |
| 750 // Verify that it is firing. |
| 751 EXPECT_TRUE(GetRegistrationOneShot("delay")); |
| 752 EXPECT_FALSE(OneShotPending("delay")); |
| 753 |
| 754 // Simulate a user clearing site data (including Service Workers, crucially), |
| 755 // by clearing data from the storage partition. |
| 756 ClearStoragePartitionData(); |
| 757 |
| 758 // Verify that it was deleted. |
| 759 EXPECT_FALSE(GetRegistrationOneShot("delay")); |
| 760 } |
| 761 |
641 } // namespace content | 762 } // namespace content |
OLD | NEW |