| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "content/test/test_background_sync_manager.h" | 5 #include "content/test/test_background_sync_manager.h" |
| 6 | 6 |
| 7 #include "base/threading/thread_task_runner_handle.h" | 7 #include "base/threading/thread_task_runner_handle.h" |
| 8 #include "base/time/time.h" | 8 #include "base/time/time.h" |
| 9 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 9 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 #include "url/gurl.h" | 11 #include "url/gurl.h" |
| 12 | 12 |
| 13 namespace content { | 13 namespace content { |
| 14 | 14 |
| 15 TestBackgroundSyncManager::TestBackgroundSyncManager( | 15 TestBackgroundSyncManager::TestBackgroundSyncManager( |
| 16 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context) | 16 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context) |
| 17 : BackgroundSyncManager(service_worker_context) {} | 17 : BackgroundSyncManager(service_worker_context) {} |
| 18 | 18 |
| 19 TestBackgroundSyncManager::~TestBackgroundSyncManager() {} | 19 TestBackgroundSyncManager::~TestBackgroundSyncManager() {} |
| 20 | 20 |
| 21 void TestBackgroundSyncManager::DoInit() { | 21 void TestBackgroundSyncManager::DoInit() { |
| 22 Init(); | 22 Init(); |
| 23 } | 23 } |
| 24 | 24 |
| 25 void TestBackgroundSyncManager::ResumeBackendOperation() { | 25 void TestBackgroundSyncManager::ResumeBackendOperation() { |
| 26 ASSERT_FALSE(continuation_.is_null()); | 26 ASSERT_FALSE(continuation_.is_null()); |
| 27 continuation_.Run(); | 27 std::move(continuation_).Run(); |
| 28 continuation_.Reset(); | |
| 29 } | 28 } |
| 30 | 29 |
| 31 void TestBackgroundSyncManager::ClearDelayedTask() { | 30 void TestBackgroundSyncManager::ClearDelayedTask() { |
| 32 delayed_task_.Reset(); | 31 delayed_task_.Reset(); |
| 33 } | 32 } |
| 34 | 33 |
| 35 void TestBackgroundSyncManager::StoreDataInBackend( | 34 void TestBackgroundSyncManager::StoreDataInBackend( |
| 36 int64_t sw_registration_id, | 35 int64_t sw_registration_id, |
| 37 const GURL& origin, | 36 const GURL& origin, |
| 38 const std::string& key, | 37 const std::string& key, |
| 39 const std::string& data, | 38 const std::string& data, |
| 40 const ServiceWorkerStorage::StatusCallback& callback) { | 39 const ServiceWorkerStorage::StatusCallback& callback) { |
| 41 EXPECT_TRUE(continuation_.is_null()); | 40 EXPECT_TRUE(continuation_.is_null()); |
| 42 if (corrupt_backend_) { | 41 if (corrupt_backend_) { |
| 43 base::ThreadTaskRunnerHandle::Get()->PostTask( | 42 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 44 FROM_HERE, base::Bind(callback, SERVICE_WORKER_ERROR_FAILED)); | 43 FROM_HERE, base::BindOnce(callback, SERVICE_WORKER_ERROR_FAILED)); |
| 45 return; | 44 return; |
| 46 } | 45 } |
| 47 continuation_ = base::Bind( | 46 continuation_ = base::BindOnce( |
| 48 &TestBackgroundSyncManager::StoreDataInBackendContinue, | 47 &TestBackgroundSyncManager::StoreDataInBackendContinue, |
| 49 base::Unretained(this), sw_registration_id, origin, key, data, callback); | 48 base::Unretained(this), sw_registration_id, origin, key, data, callback); |
| 50 if (delay_backend_) | 49 if (delay_backend_) |
| 51 return; | 50 return; |
| 52 | 51 |
| 53 ResumeBackendOperation(); | 52 ResumeBackendOperation(); |
| 54 } | 53 } |
| 55 | 54 |
| 56 void TestBackgroundSyncManager::GetDataFromBackend( | 55 void TestBackgroundSyncManager::GetDataFromBackend( |
| 57 const std::string& key, | 56 const std::string& key, |
| 58 const ServiceWorkerStorage::GetUserDataForAllRegistrationsCallback& | 57 const ServiceWorkerStorage::GetUserDataForAllRegistrationsCallback& |
| 59 callback) { | 58 callback) { |
| 60 EXPECT_TRUE(continuation_.is_null()); | 59 EXPECT_TRUE(continuation_.is_null()); |
| 61 if (corrupt_backend_) { | 60 if (corrupt_backend_) { |
| 62 base::ThreadTaskRunnerHandle::Get()->PostTask( | 61 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 63 FROM_HERE, | 62 FROM_HERE, |
| 64 base::Bind(callback, std::vector<std::pair<int64_t, std::string>>(), | 63 base::Bind(callback, std::vector<std::pair<int64_t, std::string>>(), |
| 65 SERVICE_WORKER_ERROR_FAILED)); | 64 SERVICE_WORKER_ERROR_FAILED)); |
| 66 return; | 65 return; |
| 67 } | 66 } |
| 68 continuation_ = | 67 continuation_ = |
| 69 base::Bind(&TestBackgroundSyncManager::GetDataFromBackendContinue, | 68 base::BindOnce(&TestBackgroundSyncManager::GetDataFromBackendContinue, |
| 70 base::Unretained(this), key, callback); | 69 base::Unretained(this), key, callback); |
| 71 if (delay_backend_) | 70 if (delay_backend_) |
| 72 return; | 71 return; |
| 73 | 72 |
| 74 ResumeBackendOperation(); | 73 ResumeBackendOperation(); |
| 75 } | 74 } |
| 76 | 75 |
| 77 void TestBackgroundSyncManager::DispatchSyncEvent( | 76 void TestBackgroundSyncManager::DispatchSyncEvent( |
| 78 const std::string& tag, | 77 const std::string& tag, |
| 79 scoped_refptr<ServiceWorkerVersion> active_version, | 78 scoped_refptr<ServiceWorkerVersion> active_version, |
| 80 blink::mojom::BackgroundSyncEventLastChance last_chance, | 79 blink::mojom::BackgroundSyncEventLastChance last_chance, |
| 81 const ServiceWorkerVersion::StatusCallback& callback) { | 80 const ServiceWorkerVersion::StatusCallback& callback) { |
| 82 ASSERT_FALSE(dispatch_sync_callback_.is_null()); | 81 ASSERT_FALSE(dispatch_sync_callback_.is_null()); |
| 83 last_chance_ = last_chance; | 82 last_chance_ = last_chance; |
| 84 dispatch_sync_callback_.Run(active_version, callback); | 83 dispatch_sync_callback_.Run(active_version, callback); |
| 85 } | 84 } |
| 86 | 85 |
| 87 void TestBackgroundSyncManager::ScheduleDelayedTask( | 86 void TestBackgroundSyncManager::ScheduleDelayedTask(base::OnceClosure callback, |
| 88 const base::Closure& callback, | 87 base::TimeDelta delay) { |
| 89 base::TimeDelta delay) { | 88 delayed_task_ = std::move(callback); |
| 90 delayed_task_ = callback; | |
| 91 delayed_task_delta_ = delay; | 89 delayed_task_delta_ = delay; |
| 92 } | 90 } |
| 93 | 91 |
| 94 void TestBackgroundSyncManager::HasMainFrameProviderHost( | 92 void TestBackgroundSyncManager::HasMainFrameProviderHost( |
| 95 const GURL& origin, | 93 const GURL& origin, |
| 96 const BoolCallback& callback) { | 94 BoolCallback callback) { |
| 97 callback.Run(has_main_frame_provider_host_); | 95 std::move(callback).Run(has_main_frame_provider_host_); |
| 98 } | 96 } |
| 99 | 97 |
| 100 void TestBackgroundSyncManager::StoreDataInBackendContinue( | 98 void TestBackgroundSyncManager::StoreDataInBackendContinue( |
| 101 int64_t sw_registration_id, | 99 int64_t sw_registration_id, |
| 102 const GURL& origin, | 100 const GURL& origin, |
| 103 const std::string& key, | 101 const std::string& key, |
| 104 const std::string& data, | 102 const std::string& data, |
| 105 const ServiceWorkerStorage::StatusCallback& callback) { | 103 const ServiceWorkerStorage::StatusCallback& callback) { |
| 106 BackgroundSyncManager::StoreDataInBackend(sw_registration_id, origin, key, | 104 BackgroundSyncManager::StoreDataInBackend(sw_registration_id, origin, key, |
| 107 data, callback); | 105 data, callback); |
| 108 } | 106 } |
| 109 | 107 |
| 110 void TestBackgroundSyncManager::GetDataFromBackendContinue( | 108 void TestBackgroundSyncManager::GetDataFromBackendContinue( |
| 111 const std::string& key, | 109 const std::string& key, |
| 112 const ServiceWorkerStorage::GetUserDataForAllRegistrationsCallback& | 110 const ServiceWorkerStorage::GetUserDataForAllRegistrationsCallback& |
| 113 callback) { | 111 callback) { |
| 114 BackgroundSyncManager::GetDataFromBackend(key, callback); | 112 BackgroundSyncManager::GetDataFromBackend(key, callback); |
| 115 } | 113 } |
| 116 | 114 |
| 117 } // namespace content | 115 } // namespace content |
| OLD | NEW |