Index: content/browser/background_sync/background_sync_manager_unittest.cc |
diff --git a/content/browser/background_sync/background_sync_manager_unittest.cc b/content/browser/background_sync/background_sync_manager_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..355fb0819b447284e2764a811d03c22adcbe0ac0 |
--- /dev/null |
+++ b/content/browser/background_sync/background_sync_manager_unittest.cc |
@@ -0,0 +1,253 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "content/browser/background_sync/background_sync_manager.h" |
+ |
+#include "base/files/scoped_temp_dir.h" |
+#include "base/logging.h" |
+#include "base/run_loop.h" |
+#include "base/thread_task_runner_handle.h" |
+#include "content/browser/browser_thread_impl.h" |
+#include "content/browser/service_worker/service_worker_context_wrapper.h" |
+#include "content/browser/service_worker/service_worker_storage.h" |
+#include "content/public/test/test_browser_thread_bundle.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace { |
+const char* kOriginUrl = "https://example.com"; |
+const int64 kVersionId = 0; |
+const int64 kRegistrationId1 = 0; |
michaeln
2015/02/28 00:34:23
nit: might help to call this kServiceWorkerId1 bec
jkarlin
2015/03/02 14:41:03
Done.
|
+const int64 kRegistrationId2 = 1; |
+} |
+ |
+namespace content { |
+ |
+class BackgroundSyncManagerTest : public testing::Test { |
+ public: |
+ BackgroundSyncManagerTest() |
+ : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), |
+ service_worker_context_(new ServiceWorkerContextWrapper(NULL)), |
+ origin_(kOriginUrl), |
+ callback_error_(BackgroundSyncManager::ErrorTypeOK), |
+ callback_sw_status_code_(SERVICE_WORKER_OK) { |
+ sync_reg_1_.id.push_back('1'); |
michaeln
2015/02/28 00:34:23
nit: makes me think id i a non-string like collect
jkarlin
2015/03/02 14:41:03
Replaced with std::string
|
+ sync_reg_2_.id.push_back('2'); |
+ } |
+ |
+ void SetUp() override { |
+ scoped_ptr<ServiceWorkerDatabaseTaskManager> database_task_manager( |
+ new MockServiceWorkerDatabaseTaskManager( |
+ base::ThreadTaskRunnerHandle::Get())); |
+ |
+ service_worker_context_->InitInternal( |
+ base::FilePath(), base::ThreadTaskRunnerHandle::Get(), |
+ database_task_manager.Pass(), base::ThreadTaskRunnerHandle::Get(), NULL, |
+ NULL); |
+ context_ptr_ = service_worker_context_->context()->AsWeakPtr(); |
+ |
+ background_sync_manager_ = |
+ BackgroundSyncManager::Create(service_worker_context_); |
+ |
+ // Wait for storage to finish initializing before registering service |
+ // workers. |
+ base::RunLoop().RunUntilIdle(); |
+ |
+ RegisterServiceWorker(kRegistrationId1); |
+ RegisterServiceWorker(kRegistrationId2); |
+ } |
+ |
+ protected: |
+ bool Register(const BackgroundSyncManager::BackgroundSyncRegistration& |
+ sync_registration) { |
+ return RegisterWithId(kRegistrationId1, sync_registration); |
+ } |
+ |
+ bool RegisterWithId(int64 sw_registration_id, |
+ const BackgroundSyncManager::BackgroundSyncRegistration& |
+ sync_registration) { |
+ bool was_called = false; |
+ background_sync_manager_->Register( |
+ origin_, sw_registration_id, sync_registration, |
+ base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback, |
+ base::Unretained(this), &was_called)); |
+ base::RunLoop().RunUntilIdle(); |
+ EXPECT_TRUE(was_called); |
+ return callback_error_ == BackgroundSyncManager::ErrorTypeOK; |
+ } |
+ |
+ bool Unregister(const BackgroundSyncManager::BackgroundSyncRegistration& |
+ sync_registration) { |
+ return UnregisterWithId(kRegistrationId1, sync_registration); |
+ } |
+ |
+ bool UnregisterWithId(int64 sw_registration_id, |
+ const BackgroundSyncManager::BackgroundSyncRegistration& |
+ sync_registration) { |
+ bool was_called = false; |
+ background_sync_manager_->Unregister( |
+ origin_, sw_registration_id, sync_registration, |
+ base::Bind(&BackgroundSyncManagerTest::StatusCallback, |
+ base::Unretained(this), &was_called)); |
+ base::RunLoop().RunUntilIdle(); |
+ EXPECT_TRUE(was_called); |
+ return callback_error_ == BackgroundSyncManager::ErrorTypeOK; |
+ } |
+ |
+ bool GetRegistration(const base::string16 sync_registration_id) { |
+ return GetRegistrationWithId(kRegistrationId1, sync_registration_id); |
+ } |
+ |
+ bool GetRegistrationWithId(int64 sw_registration_id, |
+ const base::string16 sync_registration_id) { |
+ bool was_called = false; |
+ background_sync_manager_->GetRegistration( |
+ origin_, sw_registration_id, sync_registration_id, |
+ base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback, |
+ base::Unretained(this), &was_called)); |
+ base::RunLoop().RunUntilIdle(); |
+ EXPECT_TRUE(was_called); |
+ if (callback_error_ == BackgroundSyncManager::ErrorTypeOK) |
+ EXPECT_TRUE(sync_registration_id == callback_registration_.id); |
+ |
+ return callback_error_ == BackgroundSyncManager::ErrorTypeOK; |
+ } |
+ |
+ void StatusAndRegistrationCallback( |
+ bool* was_called, |
+ BackgroundSyncManager::ErrorType error, |
+ const BackgroundSyncManager::BackgroundSyncRegistration& registration) { |
+ *was_called = true; |
+ callback_error_ = error; |
+ callback_registration_ = registration; |
+ } |
+ |
+ void StatusCallback(bool* was_called, |
+ BackgroundSyncManager::ErrorType error) { |
+ *was_called = true; |
+ callback_error_ = error; |
+ } |
+ |
+ void StorageRegistrationCallback(ServiceWorkerStatusCode result) { |
+ callback_sw_status_code_ = result; |
+ } |
+ |
+ void RegisterServiceWorker(uint64 sw_registration_id) { |
+ scoped_refptr<ServiceWorkerRegistration> live_registration = |
+ new ServiceWorkerRegistration(origin_, sw_registration_id, |
+ context_ptr_); |
+ |
+ scoped_refptr<ServiceWorkerVersion> live_version = new ServiceWorkerVersion( |
+ live_registration.get(), GURL(std::string(kOriginUrl) + "/script.js"), |
+ kVersionId, context_ptr_); |
+ live_version->SetStatus(ServiceWorkerVersion::INSTALLED); |
+ live_registration->SetWaitingVersion(live_version.get()); |
+ |
+ service_worker_context_->context()->storage()->StoreRegistration( |
+ live_registration.get(), live_version.get(), |
+ base::Bind(&BackgroundSyncManagerTest::StorageRegistrationCallback, |
+ base::Unretained(this))); |
+ |
+ base::RunLoop().RunUntilIdle(); |
+ EXPECT_EQ(SERVICE_WORKER_OK, callback_sw_status_code_); |
+ } |
+ |
+ TestBrowserThreadBundle browser_thread_bundle_; |
+ scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_; |
+ scoped_ptr<BackgroundSyncManager> background_sync_manager_; |
+ base::WeakPtr<ServiceWorkerContextCore> context_ptr_; |
+ |
+ const GURL origin_; |
+ BackgroundSyncManager::BackgroundSyncRegistration sync_reg_1_; |
+ BackgroundSyncManager::BackgroundSyncRegistration sync_reg_2_; |
+ |
+ // Callback values. |
+ BackgroundSyncManager::ErrorType callback_error_; |
+ BackgroundSyncManager::BackgroundSyncRegistration callback_registration_; |
+ ServiceWorkerStatusCode callback_sw_status_code_; |
+}; |
+ |
+TEST_F(BackgroundSyncManagerTest, Register) { |
+ EXPECT_TRUE(Register(sync_reg_1_)); |
+} |
+ |
+TEST_F(BackgroundSyncManagerTest, RegistractionIntact) { |
+ EXPECT_TRUE(Register(sync_reg_1_)); |
+ EXPECT_TRUE(sync_reg_1_.id == callback_registration_.id); |
+} |
+ |
+TEST_F(BackgroundSyncManagerTest, RegisterExisting) { |
+ EXPECT_TRUE(Register(sync_reg_1_)); |
+ EXPECT_FALSE(Register(sync_reg_1_)); |
+ EXPECT_EQ(BackgroundSyncManager::ErrorTypeExists, callback_error_); |
+} |
+ |
+TEST_F(BackgroundSyncManagerTest, TwoRegistrations) { |
+ EXPECT_TRUE(Register(sync_reg_1_)); |
+ EXPECT_TRUE(Register(sync_reg_2_)); |
+} |
+ |
+TEST_F(BackgroundSyncManagerTest, GetRegistrationNonExisting) { |
+ EXPECT_FALSE(GetRegistration(sync_reg_1_.id)); |
+} |
+ |
+TEST_F(BackgroundSyncManagerTest, GetRegistrationExisting) { |
+ EXPECT_TRUE(Register(sync_reg_1_)); |
+ EXPECT_TRUE(GetRegistration(sync_reg_1_.id)); |
+ EXPECT_FALSE(GetRegistration(sync_reg_2_.id)); |
+} |
+ |
+TEST_F(BackgroundSyncManagerTest, Unregister) { |
+ EXPECT_TRUE(Register(sync_reg_1_)); |
+ EXPECT_TRUE(Unregister(sync_reg_1_)); |
+} |
+ |
+TEST_F(BackgroundSyncManagerTest, Reregister) { |
+ EXPECT_TRUE(Register(sync_reg_1_)); |
+ EXPECT_TRUE(Unregister(sync_reg_1_)); |
+ EXPECT_TRUE(Register(sync_reg_1_)); |
+} |
+ |
+TEST_F(BackgroundSyncManagerTest, UnregisterNonExisting) { |
+ EXPECT_FALSE(Unregister(sync_reg_1_)); |
+ EXPECT_EQ(BackgroundSyncManager::ErrorTypeNotFound, callback_error_); |
+} |
+ |
+TEST_F(BackgroundSyncManagerTest, UnregisterSecond) { |
+ EXPECT_TRUE(Register(sync_reg_1_)); |
+ EXPECT_TRUE(Register(sync_reg_2_)); |
+ EXPECT_TRUE(Unregister(sync_reg_2_)); |
+ EXPECT_FALSE(Register(sync_reg_1_)); |
+ EXPECT_TRUE(Register(sync_reg_2_)); |
+} |
+ |
+TEST_F(BackgroundSyncManagerTest, RebootRecovery) { |
+ EXPECT_TRUE(Register(sync_reg_1_)); |
+ |
+ background_sync_manager_ = |
+ BackgroundSyncManager::Create(service_worker_context_); |
+ |
+ EXPECT_TRUE(GetRegistration(sync_reg_1_.id)); |
+ EXPECT_FALSE(GetRegistration(sync_reg_2_.id)); |
+} |
+ |
+TEST_F(BackgroundSyncManagerTest, RebootRecoveryTwoServiceWorkers) { |
+ EXPECT_TRUE(RegisterWithId(kRegistrationId1, sync_reg_1_)); |
+ EXPECT_TRUE(RegisterWithId(kRegistrationId2, sync_reg_2_)); |
+ |
+ background_sync_manager_ = |
+ BackgroundSyncManager::Create(service_worker_context_); |
+ |
+ EXPECT_TRUE(GetRegistrationWithId(kRegistrationId1, sync_reg_1_.id)); |
+ EXPECT_FALSE(GetRegistrationWithId(kRegistrationId1, sync_reg_2_.id)); |
+ EXPECT_FALSE(GetRegistrationWithId(kRegistrationId2, sync_reg_1_.id)); |
+ EXPECT_TRUE(GetRegistrationWithId(kRegistrationId2, sync_reg_2_.id)); |
+ |
+ EXPECT_FALSE(RegisterWithId(kRegistrationId1, sync_reg_1_)); |
+ EXPECT_FALSE(RegisterWithId(kRegistrationId2, sync_reg_2_)); |
+ |
+ EXPECT_TRUE(RegisterWithId(kRegistrationId1, sync_reg_2_)); |
+ EXPECT_TRUE(RegisterWithId(kRegistrationId2, sync_reg_1_)); |
+} |
+ |
+} // namespace content |