| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 NET_REPORTING_REPORTING_TEST_UTIL_H_ | 5 #ifndef NET_REPORTING_REPORTING_TEST_UTIL_H_ |
| 6 #define NET_REPORTING_REPORTING_TEST_UTIL_H_ | 6 #define NET_REPORTING_REPORTING_TEST_UTIL_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "net/reporting/reporting_context.h" | 13 #include "net/reporting/reporting_context.h" |
| 14 #include "net/reporting/reporting_delegate.h" | |
| 15 #include "net/reporting/reporting_uploader.h" | 14 #include "net/reporting/reporting_uploader.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 16 |
| 18 class GURL; | 17 class GURL; |
| 19 | 18 |
| 20 namespace base { | 19 namespace base { |
| 21 class MockTimer; | 20 class MockTimer; |
| 22 class SimpleTestClock; | 21 class SimpleTestClock; |
| 23 class SimpleTestTickClock; | 22 class SimpleTestTickClock; |
| 24 class Value; | 23 class Value; |
| 25 } // namespace base | 24 } // namespace base |
| 26 | 25 |
| 27 namespace url { | 26 namespace url { |
| 28 class Origin; | 27 class Origin; |
| 29 } // namespace url | 28 } // namespace url |
| 30 | 29 |
| 31 namespace net { | 30 namespace net { |
| 32 | 31 |
| 33 class ReportingCache; | 32 class ReportingCache; |
| 34 struct ReportingClient; | 33 struct ReportingClient; |
| 35 class ReportingGarbageCollector; | 34 class ReportingGarbageCollector; |
| 36 | 35 |
| 37 // Finds a particular client (by origin and endpoint) in the cache and returns | 36 // Finds a particular client (by origin and endpoint) in the cache and returns |
| 38 // it (or nullptr if not found). | 37 // it (or nullptr if not found). |
| 39 const ReportingClient* FindClientInCache(const ReportingCache* cache, | 38 const ReportingClient* FindClientInCache(const ReportingCache* cache, |
| 40 const url::Origin& origin, | 39 const url::Origin& origin, |
| 41 const GURL& endpoint); | 40 const GURL& endpoint); |
| 42 | 41 |
| 43 // A simple implementation of ReportingDelegate that only persists data in RAM. | |
| 44 class TestReportingDelegate : public ReportingDelegate { | |
| 45 public: | |
| 46 TestReportingDelegate(); | |
| 47 | |
| 48 ~TestReportingDelegate() override; | |
| 49 | |
| 50 // ReportingDelegate implementation: | |
| 51 std::unique_ptr<const base::Value> GetPersistedData() override; | |
| 52 | |
| 53 void PersistData(std::unique_ptr<const base::Value> persisted_data) override; | |
| 54 | |
| 55 private: | |
| 56 std::unique_ptr<const base::Value> persisted_data_; | |
| 57 | |
| 58 DISALLOW_COPY_AND_ASSIGN(TestReportingDelegate); | |
| 59 }; | |
| 60 | |
| 61 // A test implementation of ReportingUploader that holds uploads for tests to | 42 // A test implementation of ReportingUploader that holds uploads for tests to |
| 62 // examine and complete with a specified outcome. | 43 // examine and complete with a specified outcome. |
| 63 class TestReportingUploader : public ReportingUploader { | 44 class TestReportingUploader : public ReportingUploader { |
| 64 public: | 45 public: |
| 65 class PendingUpload { | 46 class PendingUpload { |
| 66 public: | 47 public: |
| 67 virtual ~PendingUpload(); | 48 virtual ~PendingUpload(); |
| 68 | 49 |
| 69 virtual const GURL& url() const = 0; | 50 virtual const GURL& url() const = 0; |
| 70 virtual const std::string& json() const = 0; | 51 virtual const std::string& json() const = 0; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 88 const std::string& json, | 69 const std::string& json, |
| 89 const Callback& callback) override; | 70 const Callback& callback) override; |
| 90 | 71 |
| 91 private: | 72 private: |
| 92 std::vector<std::unique_ptr<PendingUpload>> pending_uploads_; | 73 std::vector<std::unique_ptr<PendingUpload>> pending_uploads_; |
| 93 | 74 |
| 94 DISALLOW_COPY_AND_ASSIGN(TestReportingUploader); | 75 DISALLOW_COPY_AND_ASSIGN(TestReportingUploader); |
| 95 }; | 76 }; |
| 96 | 77 |
| 97 // A test implementation of ReportingContext that uses test versions of | 78 // A test implementation of ReportingContext that uses test versions of |
| 98 // ReportingDelegate, Clock, TickClock, and ReportingUploader. | 79 // Clock, TickClock, Timer, and ReportingUploader. |
| 99 class TestReportingContext : public ReportingContext { | 80 class TestReportingContext : public ReportingContext { |
| 100 public: | 81 public: |
| 101 TestReportingContext(const ReportingPolicy& policy); | 82 TestReportingContext(const ReportingPolicy& policy); |
| 102 ~TestReportingContext(); | 83 ~TestReportingContext(); |
| 103 | 84 |
| 104 TestReportingDelegate* test_delegate() { | |
| 105 return reinterpret_cast<TestReportingDelegate*>(delegate()); | |
| 106 } | |
| 107 base::SimpleTestClock* test_clock() { | 85 base::SimpleTestClock* test_clock() { |
| 108 return reinterpret_cast<base::SimpleTestClock*>(clock()); | 86 return reinterpret_cast<base::SimpleTestClock*>(clock()); |
| 109 } | 87 } |
| 110 base::SimpleTestTickClock* test_tick_clock() { | 88 base::SimpleTestTickClock* test_tick_clock() { |
| 111 return reinterpret_cast<base::SimpleTestTickClock*>(tick_clock()); | 89 return reinterpret_cast<base::SimpleTestTickClock*>(tick_clock()); |
| 112 } | 90 } |
| 113 base::MockTimer* test_delivery_timer() { return delivery_timer_; } | 91 base::MockTimer* test_delivery_timer() { return delivery_timer_; } |
| 114 base::MockTimer* test_persistence_timer() { return persistence_timer_; } | |
| 115 base::MockTimer* test_garbage_collection_timer() { | 92 base::MockTimer* test_garbage_collection_timer() { |
| 116 return garbage_collection_timer_; | 93 return garbage_collection_timer_; |
| 117 } | 94 } |
| 118 TestReportingUploader* test_uploader() { | 95 TestReportingUploader* test_uploader() { |
| 119 return reinterpret_cast<TestReportingUploader*>(uploader()); | 96 return reinterpret_cast<TestReportingUploader*>(uploader()); |
| 120 } | 97 } |
| 121 | 98 |
| 122 private: | 99 private: |
| 123 // Owned by the Persister and GarbageCollector, respectively, but referenced | 100 // Owned by the Persister and GarbageCollector, respectively, but referenced |
| 124 // here to preserve type: | 101 // here to preserve type: |
| 125 | 102 |
| 126 base::MockTimer* delivery_timer_; | 103 base::MockTimer* delivery_timer_; |
| 127 base::MockTimer* persistence_timer_; | |
| 128 base::MockTimer* garbage_collection_timer_; | 104 base::MockTimer* garbage_collection_timer_; |
| 129 | 105 |
| 130 DISALLOW_COPY_AND_ASSIGN(TestReportingContext); | 106 DISALLOW_COPY_AND_ASSIGN(TestReportingContext); |
| 131 }; | 107 }; |
| 132 | 108 |
| 133 // A unit test base class that provides a TestReportingContext and shorthand | 109 // A unit test base class that provides a TestReportingContext and shorthand |
| 134 // getters. | 110 // getters. |
| 135 class ReportingTestBase : public ::testing::Test { | 111 class ReportingTestBase : public ::testing::Test { |
| 136 protected: | 112 protected: |
| 137 ReportingTestBase(); | 113 ReportingTestBase(); |
| 138 ~ReportingTestBase() override; | 114 ~ReportingTestBase() override; |
| 139 | 115 |
| 140 void UsePolicy(const ReportingPolicy& policy); | 116 void UsePolicy(const ReportingPolicy& policy); |
| 141 | 117 |
| 142 // Simulates an embedder restart, preserving the ReportingPolicy and any data | 118 // Simulates an embedder restart, preserving the ReportingPolicy. |
| 143 // persisted via the TestReportingDelegate, but nothing else. | |
| 144 // | 119 // |
| 145 // Advances the Clock by |delta|, and the TickClock by |delta_ticks|. Both can | 120 // Advances the Clock by |delta|, and the TickClock by |delta_ticks|. Both can |
| 146 // be zero or negative. | 121 // be zero or negative. |
| 147 void SimulateRestart(base::TimeDelta delta, base::TimeDelta delta_ticks); | 122 void SimulateRestart(base::TimeDelta delta, base::TimeDelta delta_ticks); |
| 148 | 123 |
| 149 TestReportingContext* context() { return context_.get(); } | 124 TestReportingContext* context() { return context_.get(); } |
| 150 | 125 |
| 151 const ReportingPolicy& policy() { return context_->policy(); } | 126 const ReportingPolicy& policy() { return context_->policy(); } |
| 152 | 127 |
| 153 TestReportingDelegate* delegate() { return context_->test_delegate(); } | |
| 154 base::SimpleTestClock* clock() { return context_->test_clock(); } | 128 base::SimpleTestClock* clock() { return context_->test_clock(); } |
| 155 base::SimpleTestTickClock* tick_clock() { | 129 base::SimpleTestTickClock* tick_clock() { |
| 156 return context_->test_tick_clock(); | 130 return context_->test_tick_clock(); |
| 157 } | 131 } |
| 158 base::MockTimer* delivery_timer() { return context_->test_delivery_timer(); } | 132 base::MockTimer* delivery_timer() { return context_->test_delivery_timer(); } |
| 159 base::MockTimer* persistence_timer() { | |
| 160 return context_->test_persistence_timer(); | |
| 161 } | |
| 162 base::MockTimer* garbage_collection_timer() { | 133 base::MockTimer* garbage_collection_timer() { |
| 163 return context_->test_garbage_collection_timer(); | 134 return context_->test_garbage_collection_timer(); |
| 164 } | 135 } |
| 165 TestReportingUploader* uploader() { return context_->test_uploader(); } | 136 TestReportingUploader* uploader() { return context_->test_uploader(); } |
| 166 | 137 |
| 167 ReportingCache* cache() { return context_->cache(); } | 138 ReportingCache* cache() { return context_->cache(); } |
| 168 ReportingEndpointManager* endpoint_manager() { | 139 ReportingEndpointManager* endpoint_manager() { |
| 169 return context_->endpoint_manager(); | 140 return context_->endpoint_manager(); |
| 170 } | 141 } |
| 171 ReportingDeliveryAgent* delivery_agent() { | 142 ReportingDeliveryAgent* delivery_agent() { |
| 172 return context_->delivery_agent(); | 143 return context_->delivery_agent(); |
| 173 } | 144 } |
| 174 ReportingGarbageCollector* garbage_collector() { | 145 ReportingGarbageCollector* garbage_collector() { |
| 175 return context_->garbage_collector(); | 146 return context_->garbage_collector(); |
| 176 } | 147 } |
| 177 | 148 |
| 178 ReportingPersister* persister() { return context_->persister(); } | 149 ReportingPersister* persister() { return context_->persister(); } |
| 179 | 150 |
| 180 base::TimeTicks yesterday(); | 151 base::TimeTicks yesterday(); |
| 181 base::TimeTicks now(); | 152 base::TimeTicks now(); |
| 182 base::TimeTicks tomorrow(); | 153 base::TimeTicks tomorrow(); |
| 183 | 154 |
| 184 const std::vector<std::unique_ptr<TestReportingUploader::PendingUpload>>& | 155 const std::vector<std::unique_ptr<TestReportingUploader::PendingUpload>>& |
| 185 pending_uploads() { | 156 pending_uploads() { |
| 186 return uploader()->pending_uploads(); | 157 return uploader()->pending_uploads(); |
| 187 } | 158 } |
| 188 | 159 |
| 189 private: | 160 private: |
| 190 void CreateAndInitializeContext( | 161 void CreateContext(const ReportingPolicy& policy, |
| 191 const ReportingPolicy& policy, | 162 base::Time now, |
| 192 std::unique_ptr<const base::Value> persisted_data, | 163 base::TimeTicks now_ticks); |
| 193 base::Time now, | |
| 194 base::TimeTicks now_ticks); | |
| 195 | 164 |
| 196 std::unique_ptr<TestReportingContext> context_; | 165 std::unique_ptr<TestReportingContext> context_; |
| 197 | 166 |
| 198 DISALLOW_COPY_AND_ASSIGN(ReportingTestBase); | 167 DISALLOW_COPY_AND_ASSIGN(ReportingTestBase); |
| 199 }; | 168 }; |
| 200 | 169 |
| 201 } // namespace net | 170 } // namespace net |
| 202 | 171 |
| 203 #endif // NET_REPORING_REPORTING_TEST_UTIL_H_ | 172 #endif // NET_REPORING_REPORTING_TEST_UTIL_H_ |
| OLD | NEW |