| 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_network_observer.h" |
| 15 #include "content/browser/background_sync/background_sync_registration_handle.h" | 15 #include "content/browser/background_sync/background_sync_registration_handle.h" |
| 16 #include "content/browser/background_sync/background_sync_status.h" | 16 #include "content/browser/background_sync/background_sync_status.h" |
| 17 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 17 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 18 #include "content/browser/service_worker/service_worker_registration.h" | 18 #include "content/browser/service_worker/service_worker_registration.h" |
| 19 #include "content/public/browser/background_sync_context.h" | 19 #include "content/public/browser/background_sync_context.h" |
| 20 #include "content/public/browser/browser_context.h" | 20 #include "content/public/browser/browser_context.h" |
| 21 #include "content/public/browser/storage_partition.h" | 21 #include "content/public/browser/storage_partition.h" |
| 22 #include "content/public/browser/web_contents.h" | 22 #include "content/public/browser/web_contents.h" |
| 23 #include "content/public/common/content_switches.h" | 23 #include "content/public/common/content_switches.h" |
| 24 #include "content/public/test/background_sync_test_util.h" |
| 24 #include "content/public/test/browser_test_utils.h" | 25 #include "content/public/test/browser_test_utils.h" |
| 25 #include "content/public/test/content_browser_test.h" | 26 #include "content/public/test/content_browser_test.h" |
| 26 #include "content/public/test/content_browser_test_utils.h" | 27 #include "content/public/test/content_browser_test_utils.h" |
| 27 #include "content/public/test/test_utils.h" | 28 #include "content/public/test/test_utils.h" |
| 28 #include "content/shell/browser/shell.h" | 29 #include "content/shell/browser/shell.h" |
| 29 #include "net/base/network_change_notifier.h" | 30 #include "net/base/network_change_notifier.h" |
| 30 #include "net/test/embedded_test_server/embedded_test_server.h" | 31 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 31 #include "testing/gtest/include/gtest/gtest.h" | 32 #include "testing/gtest/include/gtest/gtest.h" |
| 32 | 33 |
| 33 using net::NetworkChangeNotifier; | 34 using net::NetworkChangeNotifier; |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 } | 104 } |
| 104 | 105 |
| 105 } // namespace | 106 } // namespace |
| 106 | 107 |
| 107 class BackgroundSyncBrowserTest : public ContentBrowserTest { | 108 class BackgroundSyncBrowserTest : public ContentBrowserTest { |
| 108 public: | 109 public: |
| 109 BackgroundSyncBrowserTest() {} | 110 BackgroundSyncBrowserTest() {} |
| 110 ~BackgroundSyncBrowserTest() override {} | 111 ~BackgroundSyncBrowserTest() override {} |
| 111 | 112 |
| 112 void SetUp() override { | 113 void SetUp() override { |
| 113 BackgroundSyncNetworkObserver::SetIgnoreNetworkChangeNotifierForTests(true); | 114 background_sync_test_util::SetIgnoreNetworkChangeNotifier(true); |
| 115 |
| 114 ContentBrowserTest::SetUp(); | 116 ContentBrowserTest::SetUp(); |
| 115 } | 117 } |
| 116 | 118 |
| 117 void SetIncognitoMode(bool incognito) { | 119 void SetIncognitoMode(bool incognito) { |
| 118 shell_ = incognito ? CreateOffTheRecordBrowser() : shell(); | 120 shell_ = incognito ? CreateOffTheRecordBrowser() : shell(); |
| 119 } | 121 } |
| 120 | 122 |
| 121 StoragePartition* GetStorage() { | 123 StoragePartition* GetStorage() { |
| 122 WebContents* web_contents = shell_->web_contents(); | 124 WebContents* web_contents = shell_->web_contents(); |
| 123 return BrowserContext::GetStoragePartition( | 125 return BrowserContext::GetStoragePartition( |
| 124 web_contents->GetBrowserContext(), web_contents->GetSiteInstance()); | 126 web_contents->GetBrowserContext(), web_contents->GetSiteInstance()); |
| 125 } | 127 } |
| 126 | 128 |
| 127 BackgroundSyncContext* GetSyncContext() { | 129 BackgroundSyncContext* GetSyncContext() { |
| 128 return GetStorage()->GetBackgroundSyncContext(); | 130 return GetStorage()->GetBackgroundSyncContext(); |
| 129 } | 131 } |
| 130 | 132 |
| 133 WebContents* web_contents() { return shell_->web_contents(); } |
| 134 |
| 131 void SetUpCommandLine(base::CommandLine* command_line) override { | 135 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 132 // TODO(jkarlin): Remove this once background sync is no longer | 136 // TODO(jkarlin): Remove this once background sync is no longer |
| 133 // experimental. | 137 // experimental. |
| 134 command_line->AppendSwitch( | 138 command_line->AppendSwitch( |
| 135 switches::kEnableExperimentalWebPlatformFeatures); | 139 switches::kEnableExperimentalWebPlatformFeatures); |
| 136 } | 140 } |
| 137 | 141 |
| 138 void SetUpOnMainThread() override { | 142 void SetUpOnMainThread() override { |
| 139 https_server_.reset( | 143 https_server_.reset( |
| 140 new net::EmbeddedTestServer(net::EmbeddedTestServer::TYPE_HTTPS)); | 144 new net::EmbeddedTestServer(net::EmbeddedTestServer::TYPE_HTTPS)); |
| 141 https_server_->ServeFilesFromSourceDirectory("content/test/data"); | 145 https_server_->ServeFilesFromSourceDirectory("content/test/data"); |
| 142 ASSERT_TRUE(https_server_->Start()); | 146 ASSERT_TRUE(https_server_->Start()); |
| 143 | 147 |
| 144 SetIncognitoMode(false); | 148 SetIncognitoMode(false); |
| 145 SetMaxSyncAttempts(1); | 149 SetMaxSyncAttempts(1); |
| 146 SetOnline(true); | 150 background_sync_test_util::SetOnline(web_contents(), true); |
| 147 ASSERT_TRUE(LoadTestPage(kDefaultTestURL)); | 151 ASSERT_TRUE(LoadTestPage(kDefaultTestURL)); |
| 148 | 152 |
| 149 ContentBrowserTest::SetUpOnMainThread(); | 153 ContentBrowserTest::SetUpOnMainThread(); |
| 150 } | 154 } |
| 151 | 155 |
| 152 void TearDownOnMainThread() override { https_server_.reset(); } | 156 void TearDownOnMainThread() override { https_server_.reset(); } |
| 153 | 157 |
| 154 bool LoadTestPage(const std::string& path) { | 158 bool LoadTestPage(const std::string& path) { |
| 155 return NavigateToURL(shell_, https_server_->GetURL(path)); | 159 return NavigateToURL(shell_, https_server_->GetURL(path)); |
| 156 } | 160 } |
| 157 | 161 |
| 158 bool RunScript(const std::string& script, std::string* result) { | 162 bool RunScript(const std::string& script, std::string* result) { |
| 159 return content::ExecuteScriptAndExtractString(shell_->web_contents(), | 163 return content::ExecuteScriptAndExtractString(web_contents(), script, |
| 160 script, result); | 164 result); |
| 161 } | 165 } |
| 162 | 166 |
| 163 // This runs asynchronously on the IO thread, but we don't need to wait for it | |
| 164 // to complete before running a background sync operation, since those also | |
| 165 // run on the IO thread. | |
| 166 void SetOnline(bool online); | |
| 167 void SetOnlineOnIOThread( | |
| 168 const scoped_refptr<BackgroundSyncContext>& sync_context, | |
| 169 bool online); | |
| 170 | |
| 171 // Returns true if the one-shot sync with tag is currently pending. Fails | 167 // Returns true if the one-shot sync with tag is currently pending. Fails |
| 172 // (assertion failure) if the tag isn't registered. | 168 // (assertion failure) if the tag isn't registered. |
| 173 bool OneShotPending(const std::string& tag); | 169 bool OneShotPending(const std::string& tag); |
| 174 | 170 |
| 175 // Sets the BackgroundSyncManager's max sync attempts per registration. | 171 // Sets the BackgroundSyncManager's max sync attempts per registration. |
| 176 void SetMaxSyncAttempts(int max_sync_attempts); | 172 void SetMaxSyncAttempts(int max_sync_attempts); |
| 177 | 173 |
| 178 void ClearStoragePartitionData(); | 174 void ClearStoragePartitionData(); |
| 179 | 175 |
| 180 std::string PopConsoleString(); | 176 std::string PopConsoleString(); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 192 bool CompleteDelayedOneShot(); | 188 bool CompleteDelayedOneShot(); |
| 193 bool RejectDelayedOneShot(); | 189 bool RejectDelayedOneShot(); |
| 194 | 190 |
| 195 private: | 191 private: |
| 196 scoped_ptr<net::EmbeddedTestServer> https_server_; | 192 scoped_ptr<net::EmbeddedTestServer> https_server_; |
| 197 Shell* shell_ = nullptr; | 193 Shell* shell_ = nullptr; |
| 198 | 194 |
| 199 DISALLOW_COPY_AND_ASSIGN(BackgroundSyncBrowserTest); | 195 DISALLOW_COPY_AND_ASSIGN(BackgroundSyncBrowserTest); |
| 200 }; | 196 }; |
| 201 | 197 |
| 202 void BackgroundSyncBrowserTest::SetOnline(bool online) { | |
| 203 ASSERT_TRUE(shell_); | |
| 204 BrowserThread::PostTask( | |
| 205 BrowserThread::IO, FROM_HERE, | |
| 206 base::Bind(&BackgroundSyncBrowserTest::SetOnlineOnIOThread, | |
| 207 base::Unretained(this), base::Unretained(GetSyncContext()), | |
| 208 online)); | |
| 209 base::RunLoop().RunUntilIdle(); | |
| 210 } | |
| 211 | |
| 212 void BackgroundSyncBrowserTest::SetOnlineOnIOThread( | |
| 213 const scoped_refptr<BackgroundSyncContext>& sync_context, | |
| 214 bool online) { | |
| 215 BackgroundSyncManager* sync_manager = sync_context->background_sync_manager(); | |
| 216 BackgroundSyncNetworkObserver* network_observer = | |
| 217 sync_manager->GetNetworkObserverForTesting(); | |
| 218 if (online) { | |
| 219 network_observer->NotifyManagerIfNetworkChangedForTesting( | |
| 220 NetworkChangeNotifier::CONNECTION_WIFI); | |
| 221 } else { | |
| 222 network_observer->NotifyManagerIfNetworkChangedForTesting( | |
| 223 NetworkChangeNotifier::CONNECTION_NONE); | |
| 224 } | |
| 225 } | |
| 226 | |
| 227 bool BackgroundSyncBrowserTest::OneShotPending(const std::string& tag) { | 198 bool BackgroundSyncBrowserTest::OneShotPending(const std::string& tag) { |
| 228 bool is_pending; | 199 bool is_pending; |
| 229 base::RunLoop run_loop; | 200 base::RunLoop run_loop; |
| 230 | 201 |
| 231 StoragePartition* storage = GetStorage(); | 202 StoragePartition* storage = GetStorage(); |
| 232 BackgroundSyncContext* sync_context = storage->GetBackgroundSyncContext(); | 203 BackgroundSyncContext* sync_context = storage->GetBackgroundSyncContext(); |
| 233 ServiceWorkerContextWrapper* service_worker_context = | 204 ServiceWorkerContextWrapper* service_worker_context = |
| 234 static_cast<ServiceWorkerContextWrapper*>( | 205 static_cast<ServiceWorkerContextWrapper*>( |
| 235 storage->GetServiceWorkerContext()); | 206 storage->GetServiceWorkerContext()); |
| 236 | 207 |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 399 EXPECT_TRUE(PopConsole("ok - foo_sw registered in SW")); | 370 EXPECT_TRUE(PopConsole("ok - foo_sw registered in SW")); |
| 400 EXPECT_TRUE(PopConsole("foo_sw fired")); | 371 EXPECT_TRUE(PopConsole("foo_sw fired")); |
| 401 EXPECT_FALSE(GetRegistrationOneShot("foo_sw")); | 372 EXPECT_FALSE(GetRegistrationOneShot("foo_sw")); |
| 402 } | 373 } |
| 403 | 374 |
| 404 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, OneShotDelaysForNetwork) { | 375 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, OneShotDelaysForNetwork) { |
| 405 EXPECT_TRUE(RegisterServiceWorker()); | 376 EXPECT_TRUE(RegisterServiceWorker()); |
| 406 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. | 377 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. |
| 407 | 378 |
| 408 // Prevent firing by going offline. | 379 // Prevent firing by going offline. |
| 409 SetOnline(false); | 380 background_sync_test_util::SetOnline(web_contents(), false); |
| 410 EXPECT_TRUE(RegisterOneShot("foo")); | 381 EXPECT_TRUE(RegisterOneShot("foo")); |
| 411 EXPECT_TRUE(GetRegistrationOneShot("foo")); | 382 EXPECT_TRUE(GetRegistrationOneShot("foo")); |
| 412 EXPECT_TRUE(OneShotPending("foo")); | 383 EXPECT_TRUE(OneShotPending("foo")); |
| 413 | 384 |
| 414 // Resume firing by going online. | 385 // Resume firing by going online. |
| 415 SetOnline(true); | 386 background_sync_test_util::SetOnline(web_contents(), true); |
| 416 EXPECT_TRUE(PopConsole("foo fired")); | 387 EXPECT_TRUE(PopConsole("foo fired")); |
| 417 EXPECT_FALSE(GetRegistrationOneShot("foo")); | 388 EXPECT_FALSE(GetRegistrationOneShot("foo")); |
| 418 } | 389 } |
| 419 | 390 |
| 420 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, WaitUntil) { | 391 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, WaitUntil) { |
| 421 EXPECT_TRUE(RegisterServiceWorker()); | 392 EXPECT_TRUE(RegisterServiceWorker()); |
| 422 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. | 393 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. |
| 423 | 394 |
| 424 SetOnline(true); | 395 background_sync_test_util::SetOnline(web_contents(), true); |
| 425 EXPECT_TRUE(RegisterOneShot("delay")); | 396 EXPECT_TRUE(RegisterOneShot("delay")); |
| 426 | 397 |
| 427 // Verify that it is firing. | 398 // Verify that it is firing. |
| 428 EXPECT_TRUE(GetRegistrationOneShot("delay")); | 399 EXPECT_TRUE(GetRegistrationOneShot("delay")); |
| 429 EXPECT_FALSE(OneShotPending("delay")); | 400 EXPECT_FALSE(OneShotPending("delay")); |
| 430 | 401 |
| 431 // Complete the task. | 402 // Complete the task. |
| 432 EXPECT_TRUE(CompleteDelayedOneShot()); | 403 EXPECT_TRUE(CompleteDelayedOneShot()); |
| 433 EXPECT_TRUE(PopConsole("ok - delay completed")); | 404 EXPECT_TRUE(PopConsole("ok - delay completed")); |
| 434 | 405 |
| 435 // Verify that it finished firing. | 406 // Verify that it finished firing. |
| 436 EXPECT_FALSE(GetRegistrationOneShot("delay")); | 407 EXPECT_FALSE(GetRegistrationOneShot("delay")); |
| 437 } | 408 } |
| 438 | 409 |
| 439 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, WaitUntilReject) { | 410 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, WaitUntilReject) { |
| 440 EXPECT_TRUE(RegisterServiceWorker()); | 411 EXPECT_TRUE(RegisterServiceWorker()); |
| 441 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. | 412 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. |
| 442 | 413 |
| 443 SetOnline(true); | 414 background_sync_test_util::SetOnline(web_contents(), true); |
| 444 EXPECT_TRUE(RegisterOneShot("delay")); | 415 EXPECT_TRUE(RegisterOneShot("delay")); |
| 445 | 416 |
| 446 // Verify that it is firing. | 417 // Verify that it is firing. |
| 447 EXPECT_TRUE(GetRegistrationOneShot("delay")); | 418 EXPECT_TRUE(GetRegistrationOneShot("delay")); |
| 448 EXPECT_FALSE(OneShotPending("delay")); | 419 EXPECT_FALSE(OneShotPending("delay")); |
| 449 | 420 |
| 450 // Complete the task. | 421 // Complete the task. |
| 451 EXPECT_TRUE(RejectDelayedOneShot()); | 422 EXPECT_TRUE(RejectDelayedOneShot()); |
| 452 EXPECT_TRUE(PopConsole("ok - delay rejected")); | 423 EXPECT_TRUE(PopConsole("ok - delay rejected")); |
| 453 EXPECT_FALSE(GetRegistrationOneShot("delay")); | 424 EXPECT_FALSE(GetRegistrationOneShot("delay")); |
| 454 } | 425 } |
| 455 | 426 |
| 456 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, Incognito) { | 427 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, Incognito) { |
| 457 EXPECT_TRUE(RegisterServiceWorker()); | 428 EXPECT_TRUE(RegisterServiceWorker()); |
| 458 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. | 429 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. |
| 459 | 430 |
| 460 SetOnline(false); | 431 background_sync_test_util::SetOnline(web_contents(), false); |
| 461 EXPECT_TRUE(RegisterOneShot("normal")); | 432 EXPECT_TRUE(RegisterOneShot("normal")); |
| 462 EXPECT_TRUE(OneShotPending("normal")); | 433 EXPECT_TRUE(OneShotPending("normal")); |
| 463 | 434 |
| 464 // Go incognito and verify that incognito doesn't see the registration. | 435 // Go incognito and verify that incognito doesn't see the registration. |
| 465 SetIncognitoMode(true); | 436 SetIncognitoMode(true); |
| 466 | 437 |
| 467 // Tell the new network observer that we're offline (it initializes from | 438 // Tell the new network observer that we're offline (it initializes from |
| 468 // NetworkChangeNotifier::GetCurrentConnectionType() which is not mocked out | 439 // NetworkChangeNotifier::GetCurrentConnectionType() which is not mocked out |
| 469 // in this test). | 440 // in this test). |
| 470 SetOnline(false); | 441 background_sync_test_util::SetOnline(web_contents(), false); |
| 471 | 442 |
| 472 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); | 443 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); |
| 473 EXPECT_TRUE(RegisterServiceWorker()); | 444 EXPECT_TRUE(RegisterServiceWorker()); |
| 474 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. | 445 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. |
| 475 | 446 |
| 476 EXPECT_FALSE(GetRegistrationOneShot("normal")); | 447 EXPECT_FALSE(GetRegistrationOneShot("normal")); |
| 477 | 448 |
| 478 EXPECT_TRUE(RegisterOneShot("incognito")); | 449 EXPECT_TRUE(RegisterOneShot("incognito")); |
| 479 EXPECT_TRUE(OneShotPending("incognito")); | 450 EXPECT_TRUE(OneShotPending("incognito")); |
| 480 | 451 |
| 481 // Switch back and make sure the registration is still there. | 452 // Switch back and make sure the registration is still there. |
| 482 SetIncognitoMode(false); | 453 SetIncognitoMode(false); |
| 483 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Should be controlled. | 454 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Should be controlled. |
| 484 | 455 |
| 485 EXPECT_TRUE(GetRegistrationOneShot("normal")); | 456 EXPECT_TRUE(GetRegistrationOneShot("normal")); |
| 486 EXPECT_FALSE(GetRegistrationOneShot("incognito")); | 457 EXPECT_FALSE(GetRegistrationOneShot("incognito")); |
| 487 } | 458 } |
| 488 | 459 |
| 489 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, GetRegistrations) { | 460 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, GetRegistrations) { |
| 490 EXPECT_TRUE(RegisterServiceWorker()); | 461 EXPECT_TRUE(RegisterServiceWorker()); |
| 491 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. | 462 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. |
| 492 | 463 |
| 493 std::vector<std::string> registered_tags; | 464 std::vector<std::string> registered_tags; |
| 494 EXPECT_TRUE(GetRegistrationsOneShot(registered_tags)); | 465 EXPECT_TRUE(GetRegistrationsOneShot(registered_tags)); |
| 495 | 466 |
| 496 SetOnline(false); | 467 background_sync_test_util::SetOnline(web_contents(), false); |
| 497 registered_tags.push_back("foo"); | 468 registered_tags.push_back("foo"); |
| 498 registered_tags.push_back("bar"); | 469 registered_tags.push_back("bar"); |
| 499 | 470 |
| 500 for (const std::string& tag : registered_tags) | 471 for (const std::string& tag : registered_tags) |
| 501 EXPECT_TRUE(RegisterOneShot(tag)); | 472 EXPECT_TRUE(RegisterOneShot(tag)); |
| 502 | 473 |
| 503 EXPECT_TRUE(GetRegistrationsOneShot(registered_tags)); | 474 EXPECT_TRUE(GetRegistrationsOneShot(registered_tags)); |
| 504 } | 475 } |
| 505 | 476 |
| 506 // Verify that GetRegistrations works in a service worker | 477 // Verify that GetRegistrations works in a service worker |
| 507 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, | 478 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, |
| 508 GetRegistrationsFromServiceWorker) { | 479 GetRegistrationsFromServiceWorker) { |
| 509 EXPECT_TRUE(RegisterServiceWorker()); | 480 EXPECT_TRUE(RegisterServiceWorker()); |
| 510 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. | 481 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. |
| 511 | 482 |
| 512 std::vector<std::string> registered_tags; | 483 std::vector<std::string> registered_tags; |
| 513 EXPECT_TRUE(GetRegistrationsOneShot(registered_tags)); | 484 EXPECT_TRUE(GetRegistrationsOneShot(registered_tags)); |
| 514 | 485 |
| 515 SetOnline(false); | 486 background_sync_test_util::SetOnline(web_contents(), false); |
| 516 registered_tags.push_back("foo_sw"); | 487 registered_tags.push_back("foo_sw"); |
| 517 registered_tags.push_back("bar_sw"); | 488 registered_tags.push_back("bar_sw"); |
| 518 | 489 |
| 519 for (const std::string& tag : registered_tags) { | 490 for (const std::string& tag : registered_tags) { |
| 520 EXPECT_TRUE(RegisterOneShotFromServiceWorker(tag)); | 491 EXPECT_TRUE(RegisterOneShotFromServiceWorker(tag)); |
| 521 EXPECT_TRUE(PopConsole(BuildExpectedResult(tag, "registered in SW"))); | 492 EXPECT_TRUE(PopConsole(BuildExpectedResult(tag, "registered in SW"))); |
| 522 } | 493 } |
| 523 | 494 |
| 524 EXPECT_TRUE(GetRegistrationsOneShotFromServiceWorker(registered_tags)); | 495 EXPECT_TRUE(GetRegistrationsOneShotFromServiceWorker(registered_tags)); |
| 525 } | 496 } |
| 526 | 497 |
| 527 // Verify that GetRegistration works in a service worker | 498 // Verify that GetRegistration works in a service worker |
| 528 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, | 499 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, |
| 529 GetRegistrationFromServiceWorker) { | 500 GetRegistrationFromServiceWorker) { |
| 530 EXPECT_TRUE(RegisterServiceWorker()); | 501 EXPECT_TRUE(RegisterServiceWorker()); |
| 531 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. | 502 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. |
| 532 | 503 |
| 533 std::vector<std::string> registered_tags; | 504 std::vector<std::string> registered_tags; |
| 534 EXPECT_TRUE(GetRegistrationsOneShot(registered_tags)); | 505 EXPECT_TRUE(GetRegistrationsOneShot(registered_tags)); |
| 535 | 506 |
| 536 SetOnline(false); | 507 background_sync_test_util::SetOnline(web_contents(), false); |
| 537 | 508 |
| 538 EXPECT_TRUE(RegisterOneShotFromServiceWorker("foo_sw")); | 509 EXPECT_TRUE(RegisterOneShotFromServiceWorker("foo_sw")); |
| 539 EXPECT_TRUE(PopConsole("ok - foo_sw registered in SW")); | 510 EXPECT_TRUE(PopConsole("ok - foo_sw registered in SW")); |
| 540 EXPECT_TRUE(GetRegistrationOneShotFromServiceWorker("foo_sw")); | 511 EXPECT_TRUE(GetRegistrationOneShotFromServiceWorker("foo_sw")); |
| 541 } | 512 } |
| 542 | 513 |
| 543 // Verify that a background sync registration is deleted when site data is | 514 // Verify that a background sync registration is deleted when site data is |
| 544 // cleared. | 515 // cleared. |
| 545 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, | 516 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, |
| 546 SyncRegistrationDeletedWhenClearingSiteData) { | 517 SyncRegistrationDeletedWhenClearingSiteData) { |
| 547 EXPECT_TRUE(RegisterServiceWorker()); | 518 EXPECT_TRUE(RegisterServiceWorker()); |
| 548 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. | 519 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. |
| 549 | 520 |
| 550 // Prevent firing by going offline. | 521 // Prevent firing by going offline. |
| 551 SetOnline(false); | 522 background_sync_test_util::SetOnline(web_contents(), false); |
| 552 EXPECT_TRUE(RegisterOneShot("foo")); | 523 EXPECT_TRUE(RegisterOneShot("foo")); |
| 553 EXPECT_TRUE(GetRegistrationOneShot("foo")); | 524 EXPECT_TRUE(GetRegistrationOneShot("foo")); |
| 554 EXPECT_TRUE(OneShotPending("foo")); | 525 EXPECT_TRUE(OneShotPending("foo")); |
| 555 | 526 |
| 556 // Simulate a user clearing site data (including Service Workers, crucially), | 527 // Simulate a user clearing site data (including Service Workers, crucially), |
| 557 // by clearing data from the storage partition. | 528 // by clearing data from the storage partition. |
| 558 ClearStoragePartitionData(); | 529 ClearStoragePartitionData(); |
| 559 | 530 |
| 560 EXPECT_FALSE(GetRegistrationOneShot("foo")); | 531 EXPECT_FALSE(GetRegistrationOneShot("foo")); |
| 561 } | 532 } |
| 562 | 533 |
| 563 // Verify that a background sync registration, from a service worker, is deleted | 534 // Verify that a background sync registration, from a service worker, is deleted |
| 564 // when site data is cleared. | 535 // when site data is cleared. |
| 565 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, | 536 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, |
| 566 SyncRegistrationFromSWDeletedWhenClearingSiteData) { | 537 SyncRegistrationFromSWDeletedWhenClearingSiteData) { |
| 567 EXPECT_TRUE(RegisterServiceWorker()); | 538 EXPECT_TRUE(RegisterServiceWorker()); |
| 568 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. | 539 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. |
| 569 | 540 |
| 570 std::vector<std::string> registered_tags; | 541 std::vector<std::string> registered_tags; |
| 571 EXPECT_TRUE(GetRegistrationsOneShot(registered_tags)); | 542 EXPECT_TRUE(GetRegistrationsOneShot(registered_tags)); |
| 572 | 543 |
| 573 SetOnline(false); | 544 background_sync_test_util::SetOnline(web_contents(), false); |
| 574 | 545 |
| 575 EXPECT_TRUE(RegisterOneShotFromServiceWorker("foo_sw")); | 546 EXPECT_TRUE(RegisterOneShotFromServiceWorker("foo_sw")); |
| 576 EXPECT_TRUE(PopConsole("ok - foo_sw registered in SW")); | 547 EXPECT_TRUE(PopConsole("ok - foo_sw registered in SW")); |
| 577 EXPECT_TRUE(GetRegistrationOneShotFromServiceWorker("foo_sw")); | 548 EXPECT_TRUE(GetRegistrationOneShotFromServiceWorker("foo_sw")); |
| 578 | 549 |
| 579 // Simulate a user clearing site data (including Service Workers, crucially), | 550 // Simulate a user clearing site data (including Service Workers, crucially), |
| 580 // by clearing data from the storage partition. | 551 // by clearing data from the storage partition. |
| 581 ClearStoragePartitionData(); | 552 ClearStoragePartitionData(); |
| 582 | 553 |
| 583 EXPECT_FALSE(GetRegistrationOneShotFromServiceWorker("foo")); | 554 EXPECT_FALSE(GetRegistrationOneShotFromServiceWorker("foo")); |
| 584 } | 555 } |
| 585 | 556 |
| 586 // Verify that multiple background sync registrations are deleted when site | 557 // Verify that multiple background sync registrations are deleted when site |
| 587 // data is cleared. | 558 // data is cleared. |
| 588 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, | 559 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, |
| 589 SyncRegistrationsDeletedWhenClearingSiteData) { | 560 SyncRegistrationsDeletedWhenClearingSiteData) { |
| 590 EXPECT_TRUE(RegisterServiceWorker()); | 561 EXPECT_TRUE(RegisterServiceWorker()); |
| 591 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. | 562 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. |
| 592 | 563 |
| 593 std::vector<std::string> registered_tags; | 564 std::vector<std::string> registered_tags; |
| 594 EXPECT_TRUE(GetRegistrationsOneShot(registered_tags)); | 565 EXPECT_TRUE(GetRegistrationsOneShot(registered_tags)); |
| 595 | 566 |
| 596 SetOnline(false); | 567 background_sync_test_util::SetOnline(web_contents(), false); |
| 597 registered_tags.push_back("foo"); | 568 registered_tags.push_back("foo"); |
| 598 registered_tags.push_back("bar"); | 569 registered_tags.push_back("bar"); |
| 599 | 570 |
| 600 for (const std::string& tag : registered_tags) | 571 for (const std::string& tag : registered_tags) |
| 601 EXPECT_TRUE(RegisterOneShot(tag)); | 572 EXPECT_TRUE(RegisterOneShot(tag)); |
| 602 | 573 |
| 603 EXPECT_TRUE(GetRegistrationsOneShot(registered_tags)); | 574 EXPECT_TRUE(GetRegistrationsOneShot(registered_tags)); |
| 604 | 575 |
| 605 for (const std::string& tag : registered_tags) | 576 for (const std::string& tag : registered_tags) |
| 606 EXPECT_TRUE(OneShotPending(tag)); | 577 EXPECT_TRUE(OneShotPending(tag)); |
| 607 | 578 |
| 608 // Simulate a user clearing site data (including Service Workers, crucially), | 579 // Simulate a user clearing site data (including Service Workers, crucially), |
| 609 // by clearing data from the storage partition. | 580 // by clearing data from the storage partition. |
| 610 ClearStoragePartitionData(); | 581 ClearStoragePartitionData(); |
| 611 | 582 |
| 612 for (const std::string& tag : registered_tags) | 583 for (const std::string& tag : registered_tags) |
| 613 EXPECT_FALSE(GetRegistrationOneShot(tag)); | 584 EXPECT_FALSE(GetRegistrationOneShot(tag)); |
| 614 } | 585 } |
| 615 | 586 |
| 616 // Verify that a sync event that is currently firing is deleted when site | 587 // Verify that a sync event that is currently firing is deleted when site |
| 617 // data is cleared. | 588 // data is cleared. |
| 618 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, | 589 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, |
| 619 FiringSyncEventDeletedWhenClearingSiteData) { | 590 FiringSyncEventDeletedWhenClearingSiteData) { |
| 620 EXPECT_TRUE(RegisterServiceWorker()); | 591 EXPECT_TRUE(RegisterServiceWorker()); |
| 621 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. | 592 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. |
| 622 | 593 |
| 623 SetOnline(true); | 594 background_sync_test_util::SetOnline(web_contents(), true); |
| 624 EXPECT_TRUE(RegisterOneShot("delay")); | 595 EXPECT_TRUE(RegisterOneShot("delay")); |
| 625 | 596 |
| 626 // Verify that it is firing. | 597 // Verify that it is firing. |
| 627 EXPECT_TRUE(GetRegistrationOneShot("delay")); | 598 EXPECT_TRUE(GetRegistrationOneShot("delay")); |
| 628 EXPECT_FALSE(OneShotPending("delay")); | 599 EXPECT_FALSE(OneShotPending("delay")); |
| 629 | 600 |
| 630 // Simulate a user clearing site data (including Service Workers, crucially), | 601 // Simulate a user clearing site data (including Service Workers, crucially), |
| 631 // by clearing data from the storage partition. | 602 // by clearing data from the storage partition. |
| 632 ClearStoragePartitionData(); | 603 ClearStoragePartitionData(); |
| 633 | 604 |
| 634 // Verify that it was deleted. | 605 // Verify that it was deleted. |
| 635 EXPECT_FALSE(GetRegistrationOneShot("delay")); | 606 EXPECT_FALSE(GetRegistrationOneShot("delay")); |
| 636 } | 607 } |
| 637 | 608 |
| 638 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, VerifyRetry) { | 609 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, VerifyRetry) { |
| 639 EXPECT_TRUE(RegisterServiceWorker()); | 610 EXPECT_TRUE(RegisterServiceWorker()); |
| 640 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. | 611 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. |
| 641 | 612 |
| 642 SetMaxSyncAttempts(2); | 613 SetMaxSyncAttempts(2); |
| 643 | 614 |
| 644 EXPECT_TRUE(RegisterOneShot("delay")); | 615 EXPECT_TRUE(RegisterOneShot("delay")); |
| 645 EXPECT_TRUE(RejectDelayedOneShot()); | 616 EXPECT_TRUE(RejectDelayedOneShot()); |
| 646 EXPECT_TRUE(PopConsole("ok - delay rejected")); | 617 EXPECT_TRUE(PopConsole("ok - delay rejected")); |
| 647 | 618 |
| 648 // Verify that the oneshot is still around and waiting to try again. | 619 // Verify that the oneshot is still around and waiting to try again. |
| 649 EXPECT_TRUE(OneShotPending("delay")); | 620 EXPECT_TRUE(OneShotPending("delay")); |
| 650 } | 621 } |
| 651 | 622 |
| 652 } // namespace content | 623 } // namespace content |
| OLD | NEW |