Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(572)

Side by Side Diff: content/browser/background_sync/background_sync_browsertest.cc

Issue 1423313006: Add browser tests for clearing sync registrations with site data (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | content/browser/background_sync/background_sync_manager_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 return BrowserContext::GetDefaultStoragePartition(
jkarlin 2015/10/30 13:41:13 GetDefaultStoragePartition is deprecated, while yo
chasej 2015/10/30 15:59:23 Done. I changed to pass the actual site instance f
114 shell_->web_contents()->GetBrowserContext()); 114 shell_->web_contents()->GetBrowserContext());
115 return storage->GetBackgroundSyncContext(); 115 }
116
117 BackgroundSyncContext* GetSyncContext() {
118 return GetStorage()->GetBackgroundSyncContext();
116 } 119 }
117 120
118 void SetUpCommandLine(base::CommandLine* command_line) override { 121 void SetUpCommandLine(base::CommandLine* command_line) override {
119 // TODO(jkarlin): Remove this once background sync is no longer 122 // TODO(jkarlin): Remove this once background sync is no longer
120 // experimental. 123 // experimental.
121 command_line->AppendSwitch( 124 command_line->AppendSwitch(
122 switches::kEnableExperimentalWebPlatformFeatures); 125 switches::kEnableExperimentalWebPlatformFeatures);
123 } 126 }
124 127
125 void SetUpOnMainThread() override { 128 void SetUpOnMainThread() override {
(...skipping 29 matching lines...) Expand all
155 // run on the IO thread. 158 // run on the IO thread.
156 void SetOnline(bool online); 159 void SetOnline(bool online);
157 void SetOnlineOnIOThread( 160 void SetOnlineOnIOThread(
158 const scoped_refptr<BackgroundSyncContext>& sync_context, 161 const scoped_refptr<BackgroundSyncContext>& sync_context,
159 bool online); 162 bool online);
160 163
161 // Returns true if the one-shot sync with tag is currently pending. Fails 164 // Returns true if the one-shot sync with tag is currently pending. Fails
162 // (assertion failure) if the tag isn't registered. 165 // (assertion failure) if the tag isn't registered.
163 bool OneShotPending(const std::string& tag); 166 bool OneShotPending(const std::string& tag);
164 167
168 void ClearStoragePartitionData();
169
165 std::string PopConsoleString(); 170 std::string PopConsoleString();
166 bool PopConsole(const std::string& expected_msg); 171 bool PopConsole(const std::string& expected_msg);
167 bool RegisterServiceWorker(); 172 bool RegisterServiceWorker();
168 bool RegisterOneShot(const std::string& tag); 173 bool RegisterOneShot(const std::string& tag);
169 bool RegisterOneShotFromServiceWorker(const std::string& tag); 174 bool RegisterOneShotFromServiceWorker(const std::string& tag);
170 bool UnregisterOneShot(const std::string& tag); 175 bool UnregisterOneShot(const std::string& tag);
171 bool UnregisterOneShotTwice(const std::string& tag); 176 bool UnregisterOneShotTwice(const std::string& tag);
172 bool GetRegistrationOneShot(const std::string& tag); 177 bool GetRegistrationOneShot(const std::string& tag);
173 bool GetRegistrationOneShotFromServiceWorker(const std::string& tag); 178 bool GetRegistrationOneShotFromServiceWorker(const std::string& tag);
174 bool MatchRegistrations(const std::string& script_result, 179 bool MatchRegistrations(const std::string& script_result,
(...skipping 12 matching lines...) Expand all
187 Shell* shell_ = nullptr; 192 Shell* shell_ = nullptr;
188 193
189 DISALLOW_COPY_AND_ASSIGN(BackgroundSyncBrowserTest); 194 DISALLOW_COPY_AND_ASSIGN(BackgroundSyncBrowserTest);
190 }; 195 };
191 196
192 void BackgroundSyncBrowserTest::SetOnline(bool online) { 197 void BackgroundSyncBrowserTest::SetOnline(bool online) {
193 ASSERT_TRUE(shell_); 198 ASSERT_TRUE(shell_);
194 BrowserThread::PostTask( 199 BrowserThread::PostTask(
195 BrowserThread::IO, FROM_HERE, 200 BrowserThread::IO, FROM_HERE,
196 base::Bind(&BackgroundSyncBrowserTest::SetOnlineOnIOThread, 201 base::Bind(&BackgroundSyncBrowserTest::SetOnlineOnIOThread,
197 base::Unretained(this), 202 base::Unretained(this), base::Unretained(GetSyncContext()),
198 base::Unretained(GetSyncContextFromShell(shell_)), online)); 203 online));
199 base::RunLoop().RunUntilIdle(); 204 base::RunLoop().RunUntilIdle();
200 } 205 }
201 206
202 void BackgroundSyncBrowserTest::SetOnlineOnIOThread( 207 void BackgroundSyncBrowserTest::SetOnlineOnIOThread(
203 const scoped_refptr<BackgroundSyncContext>& sync_context, 208 const scoped_refptr<BackgroundSyncContext>& sync_context,
204 bool online) { 209 bool online) {
205 BackgroundSyncManager* sync_manager = sync_context->background_sync_manager(); 210 BackgroundSyncManager* sync_manager = sync_context->background_sync_manager();
206 BackgroundSyncNetworkObserver* network_observer = 211 BackgroundSyncNetworkObserver* network_observer =
207 sync_manager->GetNetworkObserverForTesting(); 212 sync_manager->GetNetworkObserverForTesting();
208 if (online) { 213 if (online) {
209 network_observer->NotifyManagerIfNetworkChanged( 214 network_observer->NotifyManagerIfNetworkChangedForTesting(
210 NetworkChangeNotifier::CONNECTION_WIFI); 215 NetworkChangeNotifier::CONNECTION_WIFI);
211 } else { 216 } else {
212 network_observer->NotifyManagerIfNetworkChanged( 217 network_observer->NotifyManagerIfNetworkChangedForTesting(
213 NetworkChangeNotifier::CONNECTION_NONE); 218 NetworkChangeNotifier::CONNECTION_NONE);
214 } 219 }
215 } 220 }
216 221
217 bool BackgroundSyncBrowserTest::OneShotPending(const std::string& tag) { 222 bool BackgroundSyncBrowserTest::OneShotPending(const std::string& tag) {
218 bool is_pending; 223 bool is_pending;
219 base::RunLoop run_loop; 224 base::RunLoop run_loop;
220 225
221 StoragePartition* storage = BrowserContext::GetDefaultStoragePartition( 226 StoragePartition* storage = GetStorage();
222 shell_->web_contents()->GetBrowserContext());
223 BackgroundSyncContext* sync_context = storage->GetBackgroundSyncContext(); 227 BackgroundSyncContext* sync_context = storage->GetBackgroundSyncContext();
224 ServiceWorkerContextWrapper* service_worker_context = 228 ServiceWorkerContextWrapper* service_worker_context =
225 static_cast<ServiceWorkerContextWrapper*>( 229 static_cast<ServiceWorkerContextWrapper*>(
226 storage->GetServiceWorkerContext()); 230 storage->GetServiceWorkerContext());
227 231
228 base::Callback<void(bool)> callback = 232 base::Callback<void(bool)> callback =
229 base::Bind(&OneShotPendingCallback, run_loop.QuitClosure(), 233 base::Bind(&OneShotPendingCallback, run_loop.QuitClosure(),
230 base::ThreadTaskRunnerHandle::Get(), &is_pending); 234 base::ThreadTaskRunnerHandle::Get(), &is_pending);
231 235
232 BrowserThread::PostTask( 236 BrowserThread::PostTask(
233 BrowserThread::IO, FROM_HERE, 237 BrowserThread::IO, FROM_HERE,
234 base::Bind(&OneShotPendingOnIOThread, make_scoped_refptr(sync_context), 238 base::Bind(&OneShotPendingOnIOThread, make_scoped_refptr(sync_context),
235 make_scoped_refptr(service_worker_context), tag, 239 make_scoped_refptr(service_worker_context), tag,
236 https_server_->GetURL(kDefaultTestURL), callback)); 240 https_server_->GetURL(kDefaultTestURL), callback));
237 241
238 run_loop.Run(); 242 run_loop.Run();
239 243
240 return is_pending; 244 return is_pending;
241 } 245 }
242 246
247 void BackgroundSyncBrowserTest::ClearStoragePartitionData() {
248 // Clear data from the storage partition. Parameters are set to clear data
249 // for service workers, for all origins, for an unbounded time range.
250 StoragePartition* storage = GetStorage();
251
252 uint32 storage_partition_mask =
253 StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS;
254 uint32 quota_storage_mask = StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL;
255 const GURL& delete_origin = GURL();
256 const base::Time delete_begin = base::Time();
257 base::Time delete_end = base::Time::Max();
258
259 base::RunLoop run_loop;
260
261 storage->ClearData(storage_partition_mask, quota_storage_mask, delete_origin,
262 StoragePartition::OriginMatcherFunction(), delete_begin,
263 delete_end, run_loop.QuitClosure());
264
265 run_loop.Run();
266 }
267
243 std::string BackgroundSyncBrowserTest::PopConsoleString() { 268 std::string BackgroundSyncBrowserTest::PopConsoleString() {
244 std::string script_result; 269 std::string script_result;
245 EXPECT_TRUE(RunScript("resultQueue.pop()", &script_result)); 270 EXPECT_TRUE(RunScript("resultQueue.pop()", &script_result));
246 return script_result; 271 return script_result;
247 } 272 }
248 273
249 bool BackgroundSyncBrowserTest::PopConsole(const std::string& expected_msg) { 274 bool BackgroundSyncBrowserTest::PopConsole(const std::string& expected_msg) {
250 std::string script_result = PopConsoleString(); 275 std::string script_result = PopConsoleString();
251 return script_result == expected_msg; 276 return script_result == expected_msg;
252 } 277 }
(...skipping 378 matching lines...) Expand 10 before | Expand all | Expand 10 after
631 SetOnline(true); 656 SetOnline(true);
632 EXPECT_TRUE(RegisterOneShot("delay")); 657 EXPECT_TRUE(RegisterOneShot("delay"));
633 EXPECT_FALSE(OneShotPending("delay")); 658 EXPECT_FALSE(OneShotPending("delay"));
634 EXPECT_TRUE(StoreRegistrationOneShot("delay")); 659 EXPECT_TRUE(StoreRegistrationOneShot("delay"));
635 660
636 EXPECT_TRUE(RejectDelayedOneShot()); 661 EXPECT_TRUE(RejectDelayedOneShot());
637 EXPECT_TRUE(PopConsole("ok - delay rejected")); 662 EXPECT_TRUE(PopConsole("ok - delay rejected"));
638 EXPECT_TRUE(NotifyWhenFinishedImmediateOneShot("ok - delay result: false")); 663 EXPECT_TRUE(NotifyWhenFinishedImmediateOneShot("ok - delay result: false"));
639 } 664 }
640 665
666 // Verify that a background sync registration is deleted when site data is
667 // cleared.
668 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest,
669 SyncRegistrationDeletedWhenClearingSiteData) {
670 EXPECT_TRUE(RegisterServiceWorker());
671 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page.
672
673 // Prevent firing by going offline.
674 SetOnline(false);
675 EXPECT_TRUE(RegisterOneShot("foo"));
676 EXPECT_TRUE(GetRegistrationOneShot("foo"));
677 EXPECT_TRUE(OneShotPending("foo"));
678
679 // Simulate a user clearing site data (including Service Workers, crucially),
680 // by clearing data from the storage partition.
681 ClearStoragePartitionData();
682
683 EXPECT_FALSE(GetRegistrationOneShot("foo"));
684 }
685
686 // Verify that a background sync registration, from a service worker, is deleted
687 // when site data is cleared.
688 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest,
689 SyncRegistrationFromSWDeletedWhenClearingSiteData) {
690 EXPECT_TRUE(RegisterServiceWorker());
691 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page.
692
693 std::vector<std::string> registered_tags;
694 EXPECT_TRUE(GetRegistrationsOneShot(registered_tags));
695
696 SetOnline(false);
697
698 EXPECT_TRUE(RegisterOneShotFromServiceWorker("foo_sw"));
699 EXPECT_TRUE(PopConsole("ok - foo_sw registered in SW"));
700 EXPECT_TRUE(GetRegistrationOneShotFromServiceWorker("foo_sw"));
701
702 // Simulate a user clearing site data (including Service Workers, crucially),
703 // by clearing data from the storage partition.
704 ClearStoragePartitionData();
705
706 EXPECT_FALSE(GetRegistrationOneShotFromServiceWorker("foo"));
707 }
708
709 // Verify that multiple background sync registrations are deleted when site
710 // data is cleared.
711 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest,
712 SyncRegistrationsDeletedWhenClearingSiteData) {
713 EXPECT_TRUE(RegisterServiceWorker());
714 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page.
715
716 std::vector<std::string> registered_tags;
717 EXPECT_TRUE(GetRegistrationsOneShot(registered_tags));
718
719 SetOnline(false);
720 registered_tags.push_back("foo");
721 registered_tags.push_back("bar");
722
723 for (const std::string& tag : registered_tags)
724 EXPECT_TRUE(RegisterOneShot(tag));
725
726 EXPECT_TRUE(GetRegistrationsOneShot(registered_tags));
727
728 for (const std::string& tag : registered_tags)
729 EXPECT_TRUE(OneShotPending(tag));
730
731 // Simulate a user clearing site data (including Service Workers, crucially),
732 // by clearing data from the storage partition.
733 ClearStoragePartitionData();
734
735 for (const std::string& tag : registered_tags)
736 EXPECT_FALSE(GetRegistrationOneShot(tag));
737 }
jkarlin 2015/10/30 13:41:13 Can you also test what happens if a sync is in the
chasej 2015/10/30 15:59:23 Done.
738
641 } // namespace content 739 } // namespace content
OLDNEW
« no previous file with comments | « no previous file | content/browser/background_sync/background_sync_manager_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698