| 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 #ifndef CONTENT_TEST_TEST_BACKGROUND_SYNC_MANAGER_H_ | 5 #ifndef CONTENT_TEST_TEST_BACKGROUND_SYNC_MANAGER_H_ |
| 6 #define CONTENT_TEST_TEST_BACKGROUND_SYNC_MANAGER_H_ | 6 #define CONTENT_TEST_TEST_BACKGROUND_SYNC_MANAGER_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <memory> | 10 #include <memory> |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 class ServiceWorkerVersion; | 28 class ServiceWorkerVersion; |
| 29 | 29 |
| 30 // A BackgroundSyncManager for use in unit tests. This class provides control | 30 // A BackgroundSyncManager for use in unit tests. This class provides control |
| 31 // over internal behavior and state, to allow tests to simulate various | 31 // over internal behavior and state, to allow tests to simulate various |
| 32 // scenarios. Examples include (but are not limited to): | 32 // scenarios. Examples include (but are not limited to): |
| 33 // - Delaying and corrupting the backend storage. | 33 // - Delaying and corrupting the backend storage. |
| 34 // - Controlling the firing of service worker onsync events. | 34 // - Controlling the firing of service worker onsync events. |
| 35 // - Setting the network state | 35 // - Setting the network state |
| 36 class TestBackgroundSyncManager : public BackgroundSyncManager { | 36 class TestBackgroundSyncManager : public BackgroundSyncManager { |
| 37 public: | 37 public: |
| 38 using DispatchSyncCallback = | 38 using DispatchSyncCallback = base::RepeatingCallback<void( |
| 39 base::Callback<void(const scoped_refptr<ServiceWorkerVersion>&, | 39 const scoped_refptr<ServiceWorkerVersion>&, |
| 40 const ServiceWorkerVersion::StatusCallback&)>; | 40 const ServiceWorkerVersion::StatusCallback&)>; |
| 41 | 41 |
| 42 explicit TestBackgroundSyncManager( | 42 explicit TestBackgroundSyncManager( |
| 43 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context); | 43 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context); |
| 44 ~TestBackgroundSyncManager() override; | 44 ~TestBackgroundSyncManager() override; |
| 45 | 45 |
| 46 // Force a call to the internal Init() method. | 46 // Force a call to the internal Init() method. |
| 47 void DoInit(); | 47 void DoInit(); |
| 48 | 48 |
| 49 // Resume any delayed backend operation. | 49 // Resume any delayed backend operation. |
| 50 void ResumeBackendOperation(); | 50 void ResumeBackendOperation(); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 68 // when the event happens. | 68 // when the event happens. |
| 69 void set_dispatch_sync_callback(const DispatchSyncCallback& callback) { | 69 void set_dispatch_sync_callback(const DispatchSyncCallback& callback) { |
| 70 dispatch_sync_callback_ = callback; | 70 dispatch_sync_callback_ = callback; |
| 71 } | 71 } |
| 72 | 72 |
| 73 // Sets the response to checks for a main frame for register attempts. | 73 // Sets the response to checks for a main frame for register attempts. |
| 74 void set_has_main_frame_provider_host(bool value) { | 74 void set_has_main_frame_provider_host(bool value) { |
| 75 has_main_frame_provider_host_ = value; | 75 has_main_frame_provider_host_ = value; |
| 76 } | 76 } |
| 77 | 77 |
| 78 bool IsDelayedTaskScheduled() const { return !delayed_task_.is_null(); } |
| 79 void RunDelayedTask() { std::move(delayed_task_).Run(); } |
| 80 |
| 78 // Accessors to internal state | 81 // Accessors to internal state |
| 79 base::Closure delayed_task() const { return delayed_task_; } | |
| 80 base::TimeDelta delayed_task_delta() const { return delayed_task_delta_; } | 82 base::TimeDelta delayed_task_delta() const { return delayed_task_delta_; } |
| 81 blink::mojom::BackgroundSyncEventLastChance last_chance() const { | 83 blink::mojom::BackgroundSyncEventLastChance last_chance() const { |
| 82 return last_chance_; | 84 return last_chance_; |
| 83 } | 85 } |
| 84 const BackgroundSyncParameters* background_sync_parameters() const { | 86 const BackgroundSyncParameters* background_sync_parameters() const { |
| 85 return parameters_.get(); | 87 return parameters_.get(); |
| 86 } | 88 } |
| 87 | 89 |
| 88 protected: | 90 protected: |
| 89 // Override to allow delays to be injected by tests. | 91 // Override to allow delays to be injected by tests. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 103 // Override to avoid actual dispatching of the event, just call the provided | 105 // Override to avoid actual dispatching of the event, just call the provided |
| 104 // callback instead. | 106 // callback instead. |
| 105 void DispatchSyncEvent( | 107 void DispatchSyncEvent( |
| 106 const std::string& tag, | 108 const std::string& tag, |
| 107 scoped_refptr<ServiceWorkerVersion> active_version, | 109 scoped_refptr<ServiceWorkerVersion> active_version, |
| 108 blink::mojom::BackgroundSyncEventLastChance last_chance, | 110 blink::mojom::BackgroundSyncEventLastChance last_chance, |
| 109 const ServiceWorkerVersion::StatusCallback& callback) override; | 111 const ServiceWorkerVersion::StatusCallback& callback) override; |
| 110 | 112 |
| 111 // Override to just store delayed task, and allow tests to control the clock | 113 // Override to just store delayed task, and allow tests to control the clock |
| 112 // and when delayed tasks are executed. | 114 // and when delayed tasks are executed. |
| 113 void ScheduleDelayedTask(const base::Closure& callback, | 115 void ScheduleDelayedTask(base::OnceClosure callback, |
| 114 base::TimeDelta delay) override; | 116 base::TimeDelta delay) override; |
| 115 | 117 |
| 116 // Override to avoid actual check for main frame, instead return the value set | 118 // Override to avoid actual check for main frame, instead return the value set |
| 117 // by tests. | 119 // by tests. |
| 118 void HasMainFrameProviderHost(const GURL& origin, | 120 void HasMainFrameProviderHost(const GURL& origin, |
| 119 const BoolCallback& callback) override; | 121 BoolCallback callback) override; |
| 120 | 122 |
| 121 private: | 123 private: |
| 122 // Callback to resume the StoreDataInBackend operation, after explicit | 124 // Callback to resume the StoreDataInBackend operation, after explicit |
| 123 // delays injected by tests. | 125 // delays injected by tests. |
| 124 void StoreDataInBackendContinue( | 126 void StoreDataInBackendContinue( |
| 125 int64_t sw_registration_id, | 127 int64_t sw_registration_id, |
| 126 const GURL& origin, | 128 const GURL& origin, |
| 127 const std::string& key, | 129 const std::string& key, |
| 128 const std::string& data, | 130 const std::string& data, |
| 129 const ServiceWorkerStorage::StatusCallback& callback); | 131 const ServiceWorkerStorage::StatusCallback& callback); |
| 130 | 132 |
| 131 // Callback to resume the GetDataFromBackend operation, after explicit delays | 133 // Callback to resume the GetDataFromBackend operation, after explicit delays |
| 132 // injected by tests. | 134 // injected by tests. |
| 133 void GetDataFromBackendContinue( | 135 void GetDataFromBackendContinue( |
| 134 const std::string& key, | 136 const std::string& key, |
| 135 const ServiceWorkerStorage::GetUserDataForAllRegistrationsCallback& | 137 const ServiceWorkerStorage::GetUserDataForAllRegistrationsCallback& |
| 136 callback); | 138 callback); |
| 137 | 139 |
| 138 bool corrupt_backend_ = false; | 140 bool corrupt_backend_ = false; |
| 139 bool delay_backend_ = false; | 141 bool delay_backend_ = false; |
| 140 bool has_main_frame_provider_host_ = true; | 142 bool has_main_frame_provider_host_ = true; |
| 141 blink::mojom::BackgroundSyncEventLastChance last_chance_ = | 143 blink::mojom::BackgroundSyncEventLastChance last_chance_ = |
| 142 blink::mojom::BackgroundSyncEventLastChance::IS_NOT_LAST_CHANCE; | 144 blink::mojom::BackgroundSyncEventLastChance::IS_NOT_LAST_CHANCE; |
| 143 base::Closure continuation_; | 145 base::OnceClosure continuation_; |
| 144 DispatchSyncCallback dispatch_sync_callback_; | 146 DispatchSyncCallback dispatch_sync_callback_; |
| 145 base::Closure delayed_task_; | 147 base::OnceClosure delayed_task_; |
| 146 base::TimeDelta delayed_task_delta_; | 148 base::TimeDelta delayed_task_delta_; |
| 147 | 149 |
| 148 DISALLOW_COPY_AND_ASSIGN(TestBackgroundSyncManager); | 150 DISALLOW_COPY_AND_ASSIGN(TestBackgroundSyncManager); |
| 149 }; | 151 }; |
| 150 | 152 |
| 151 } // namespace content | 153 } // namespace content |
| 152 | 154 |
| 153 #endif // CONTENT_TEST_TEST_BACKGROUND_SYNC_MANAGER_H_ | 155 #endif // CONTENT_TEST_TEST_BACKGROUND_SYNC_MANAGER_H_ |
| OLD | NEW |