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

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

Issue 2751103002: Reporting: Wrap existing classes in context. (Closed)
Patch Set: Move before BrowsingDataRemover, GarbageCollector, and Serializer CLs. 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
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() { return test_delegate_; }
103 base::SimpleTestClock* test_clock() { return test_clock_; }
104 base::SimpleTestTickClock* test_tick_clock() { return test_tick_clock_; }
105 TestReportingUploader* test_uploader() { return test_uploader_; }
106
107 private:
108 // These pointers are owned by the superclass, not this class; they're here
109 // just to keep the more specific type.
110 TestReportingDelegate* test_delegate_;
111 base::SimpleTestClock* test_clock_;
112 base::SimpleTestTickClock* test_tick_clock_;
113 TestReportingUploader* test_uploader_;
114
115 DISALLOW_COPY_AND_ASSIGN(TestReportingContext);
116 };
117
118 // A unit test base class that provides a TestReportingContext and shorthand
119 // getters.
120 class ReportingTestBase : public ::testing::Test {
121 protected:
122 ReportingTestBase();
123 ~ReportingTestBase() override;
124
125 void UsePolicy(const ReportingPolicy& policy);
126
127 TestReportingContext* context() { return context_.get(); }
128
129 const ReportingPolicy& policy() { return context_->policy(); }
130
131 TestReportingDelegate* delegate() { return context_->test_delegate(); }
132 base::SimpleTestClock* clock() { return context_->test_clock(); }
133 base::SimpleTestTickClock* tick_clock() {
134 return context_->test_tick_clock();
135 }
136 TestReportingUploader* uploader() { return context_->test_uploader(); }
137
138 ReportingCache* cache() { return context_->cache(); }
139 ReportingEndpointManager* endpoint_manager() {
140 return context_->endpoint_manager();
141 }
142 ReportingDeliveryAgent* delivery_agent() {
143 return context_->delivery_agent();
144 }
145
146 base::TimeTicks yesterday();
147
148 base::TimeTicks tomorrow();
149
150 private:
151 std::unique_ptr<TestReportingContext> context_;
152
153 DISALLOW_COPY_AND_ASSIGN(ReportingTestBase);
154 };
155
25 } // namespace net 156 } // namespace net
26 157
27 #endif // NET_REPORING_REPORTING_TEST_UTIL_H_ 158 #endif // NET_REPORING_REPORTING_TEST_UTIL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698