Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(76)

Side by Side Diff: net/reporting/reporting_test_util.h

Issue 2751103002: Reporting: Wrap existing classes in context. (Closed)
Patch Set: rebase Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/reporting/reporting_policy.cc ('k') | net/reporting/reporting_test_util.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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>
9 #include <string>
10 #include <vector>
11
12 #include "base/macros.h"
13 #include "net/reporting/reporting_context.h"
14 #include "net/reporting/reporting_delegate.h"
15 #include "net/reporting/reporting_uploader.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17
8 class GURL; 18 class GURL;
9 19
20 namespace base {
21 class SimpleTestClock;
22 class SimpleTestTickClock;
23 class Value;
24 } // namespace base
25
10 namespace url { 26 namespace url {
11 class Origin; 27 class Origin;
12 } // namespace url 28 } // namespace url
13 29
14 namespace net { 30 namespace net {
15 31
16 class ReportingCache; 32 class ReportingCache;
17 struct ReportingClient; 33 struct ReportingClient;
18 34
19 // Finds a particular client (by origin and endpoint) in the cache and returns 35 // Finds a particular client (by origin and endpoint) in the cache and returns
20 // it (or nullptr if not found). 36 // it (or nullptr if not found).
21 const ReportingClient* FindClientInCache(const ReportingCache* cache, 37 const ReportingClient* FindClientInCache(const ReportingCache* cache,
22 const url::Origin& origin, 38 const url::Origin& origin,
23 const GURL& endpoint); 39 const GURL& endpoint);
24 40
41 // A simple implementation of ReportingDelegate that only persists data in RAM.
42 class TestReportingDelegate : public ReportingDelegate {
43 public:
44 TestReportingDelegate();
45
46 ~TestReportingDelegate() override;
47
48 // ReportingDelegate implementation:
49 std::unique_ptr<const base::Value> GetPersistedData() override;
50
51 void PersistData(std::unique_ptr<const base::Value> persisted_data) override;
52
53 private:
54 std::unique_ptr<const base::Value> persisted_data_;
55
56 DISALLOW_COPY_AND_ASSIGN(TestReportingDelegate);
57 };
58
59 // A test implementation of ReportingUploader that holds uploads for tests to
60 // examine and complete with a specified outcome.
61 class TestReportingUploader : public ReportingUploader {
62 public:
63 class PendingUpload {
64 public:
65 virtual ~PendingUpload();
66
67 virtual const GURL& url() const = 0;
68 virtual const std::string& json() const = 0;
69 virtual std::unique_ptr<base::Value> GetValue() const = 0;
70
71 virtual void Complete(Outcome outcome) = 0;
72
73 protected:
74 PendingUpload();
75 };
76
77 TestReportingUploader();
78 ~TestReportingUploader() override;
79
80 const std::vector<std::unique_ptr<PendingUpload>>& pending_uploads() const {
81 return pending_uploads_;
82 }
83
84 // ReportingUploader implementation:
85 void StartUpload(const GURL& url,
86 const std::string& json,
87 const Callback& callback) override;
88
89 private:
90 std::vector<std::unique_ptr<PendingUpload>> pending_uploads_;
91
92 DISALLOW_COPY_AND_ASSIGN(TestReportingUploader);
93 };
94
95 // A test implementation of ReportingContext that uses test versions of
96 // ReportingDelegate, Clock, TickClock, and ReportingUploader.
97 class TestReportingContext : public ReportingContext {
98 public:
99 TestReportingContext(const ReportingPolicy& policy);
100 ~TestReportingContext();
101
102 TestReportingDelegate* test_delegate() {
103 return reinterpret_cast<TestReportingDelegate*>(delegate());
104 }
105 base::SimpleTestClock* test_clock() {
106 return reinterpret_cast<base::SimpleTestClock*>(clock());
107 }
108 base::SimpleTestTickClock* test_tick_clock() {
109 return reinterpret_cast<base::SimpleTestTickClock*>(tick_clock());
110 }
111 TestReportingUploader* test_uploader() {
112 return reinterpret_cast<TestReportingUploader*>(uploader());
113 }
114
115 private:
116 DISALLOW_COPY_AND_ASSIGN(TestReportingContext);
117 };
118
119 // A unit test base class that provides a TestReportingContext and shorthand
120 // getters.
121 class ReportingTestBase : public ::testing::Test {
122 protected:
123 ReportingTestBase();
124 ~ReportingTestBase() override;
125
126 void UsePolicy(const ReportingPolicy& policy);
127
128 TestReportingContext* context() { return context_.get(); }
129
130 const ReportingPolicy& policy() { return context_->policy(); }
131
132 TestReportingDelegate* delegate() { return context_->test_delegate(); }
133 base::SimpleTestClock* clock() { return context_->test_clock(); }
134 base::SimpleTestTickClock* tick_clock() {
135 return context_->test_tick_clock();
136 }
137 TestReportingUploader* uploader() { return context_->test_uploader(); }
138
139 ReportingCache* cache() { return context_->cache(); }
140 ReportingEndpointManager* endpoint_manager() {
141 return context_->endpoint_manager();
142 }
143 ReportingDeliveryAgent* delivery_agent() {
144 return context_->delivery_agent();
145 }
146
147 base::TimeTicks yesterday();
148
149 base::TimeTicks tomorrow();
150
151 private:
152 std::unique_ptr<TestReportingContext> context_;
153
154 DISALLOW_COPY_AND_ASSIGN(ReportingTestBase);
155 };
156
25 } // namespace net 157 } // namespace net
26 158
27 #endif // NET_REPORING_REPORTING_TEST_UTIL_H_ 159 #endif // NET_REPORING_REPORTING_TEST_UTIL_H_
OLDNEW
« no previous file with comments | « net/reporting/reporting_policy.cc ('k') | net/reporting/reporting_test_util.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698