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

Side by Side Diff: components/doodle/doodle_service_unittest.cc

Issue 2725143003: [Doodle] Pull time_to_live out of DoodleConfig (Closed)
Patch Set: review Created 3 years, 9 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 #include "components/doodle/doodle_service.h" 5 #include "components/doodle/doodle_service.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <utility> 8 #include <utility>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/memory/ptr_util.h" 12 #include "base/memory/ptr_util.h"
13 #include "base/time/time.h"
13 #include "testing/gmock/include/gmock/gmock.h" 14 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
15 16
16 using testing::Eq; 17 using testing::Eq;
17 using testing::StrictMock; 18 using testing::StrictMock;
18 19
19 namespace doodle { 20 namespace doodle {
20 21
21 namespace { 22 namespace {
22 23
23 class FakeDoodleFetcher : public DoodleFetcher { 24 class FakeDoodleFetcher : public DoodleFetcher {
24 public: 25 public:
25 FakeDoodleFetcher() = default; 26 FakeDoodleFetcher() = default;
26 ~FakeDoodleFetcher() override = default; 27 ~FakeDoodleFetcher() override = default;
27 28
28 void FetchDoodle(FinishedCallback callback) override { 29 void FetchDoodle(FinishedCallback callback) override {
29 callbacks_.push_back(std::move(callback)); 30 callbacks_.push_back(std::move(callback));
30 } 31 }
31 32
32 size_t num_pending_callbacks() const { return callbacks_.size(); } 33 size_t num_pending_callbacks() const { return callbacks_.size(); }
33 34
34 void ServeAllCallbacks(DoodleState state, 35 void ServeAllCallbacks(DoodleState state,
36 base::TimeDelta time_to_live,
35 const base::Optional<DoodleConfig>& config) { 37 const base::Optional<DoodleConfig>& config) {
36 for (auto& callback : callbacks_) { 38 for (auto& callback : callbacks_) {
37 std::move(callback).Run(state, config); 39 std::move(callback).Run(state, time_to_live, config);
38 } 40 }
39 callbacks_.clear(); 41 callbacks_.clear();
40 } 42 }
41 43
42 private: 44 private:
43 std::vector<FinishedCallback> callbacks_; 45 std::vector<FinishedCallback> callbacks_;
44 }; 46 };
45 47
46 class MockDoodleObserver : public DoodleService::Observer { 48 class MockDoodleObserver : public DoodleService::Observer {
47 public: 49 public:
48 MOCK_METHOD1(OnDoodleConfigUpdated, 50 MOCK_METHOD1(OnDoodleConfigUpdated,
49 void(const base::Optional<DoodleConfig>&)); 51 void(const base::Optional<DoodleConfig>&));
50 }; 52 };
51 53
52 } // namespace 54 } // namespace
53 55
54 // Equality operator for DoodleConfigs, for use by testing::Eq.
55 // Note: This must be outside of the anonymous namespace.
56 bool operator==(const DoodleConfig& lhs, const DoodleConfig& rhs) {
57 return lhs.IsEquivalent(rhs);
58 }
59
60 class DoodleServiceTest : public testing::Test { 56 class DoodleServiceTest : public testing::Test {
61 public: 57 public:
62 DoodleServiceTest() : fetcher_(nullptr) { 58 DoodleServiceTest() : fetcher_(nullptr) {
63 auto fetcher = base::MakeUnique<FakeDoodleFetcher>(); 59 auto fetcher = base::MakeUnique<FakeDoodleFetcher>();
64 fetcher_ = fetcher.get(); 60 fetcher_ = fetcher.get();
65 service_ = base::MakeUnique<DoodleService>(std::move(fetcher)); 61 service_ = base::MakeUnique<DoodleService>(std::move(fetcher));
66 } 62 }
67 63
68 DoodleService* service() { return service_.get(); } 64 DoodleService* service() { return service_.get(); }
69 FakeDoodleFetcher* fetcher() { return fetcher_; } 65 FakeDoodleFetcher* fetcher() { return fetcher_; }
70 66
67 base::TimeDelta some_time() const { return base::TimeDelta::FromHours(1); }
mastiz 2017/03/02 19:55:42 Use a constant instead, kSomeTime?
fhorschig 2017/03/02 20:18:56 Has const base::TimeDelta and exit-time destructor
Marc Treib 2017/03/03 09:35:26 Constants of non-trivial types are mostly disallow
Marc Treib 2017/03/03 09:35:26 ...I don't follow?
mastiz 2017/03/03 11:17:44 Right, the solution is to not make it static. Simp
68
71 private: 69 private:
72 std::unique_ptr<DoodleService> service_; 70 std::unique_ptr<DoodleService> service_;
73 FakeDoodleFetcher* fetcher_; 71 FakeDoodleFetcher* fetcher_;
74 }; 72 };
75 73
76 TEST_F(DoodleServiceTest, FetchesConfigOnRefresh) { 74 TEST_F(DoodleServiceTest, FetchesConfigOnRefresh) {
77 ASSERT_THAT(service()->config(), Eq(base::nullopt)); 75 ASSERT_THAT(service()->config(), Eq(base::nullopt));
78 76
79 // Request a refresh of the doodle config. 77 // Request a refresh of the doodle config.
80 service()->Refresh(); 78 service()->Refresh();
81 // The request should have arrived at the fetcher. 79 // The request should have arrived at the fetcher.
82 EXPECT_THAT(fetcher()->num_pending_callbacks(), Eq(1u)); 80 EXPECT_THAT(fetcher()->num_pending_callbacks(), Eq(1u));
83 81
84 // Serve it (with an arbitrary config). 82 // Serve it (with an arbitrary config).
85 DoodleConfig config; 83 DoodleConfig config;
86 config.doodle_type = DoodleType::SIMPLE; 84 config.doodle_type = DoodleType::SIMPLE;
87 fetcher()->ServeAllCallbacks(DoodleState::AVAILABLE, config); 85 fetcher()->ServeAllCallbacks(DoodleState::AVAILABLE, some_time(), config);
88 86
89 // The config should be available. 87 // The config should be available.
90 EXPECT_THAT(service()->config(), Eq(config)); 88 EXPECT_THAT(service()->config(), Eq(config));
91 89
92 // Request a refresh again. 90 // Request a refresh again.
93 service()->Refresh(); 91 service()->Refresh();
94 // The request should have arrived at the fetcher again. 92 // The request should have arrived at the fetcher again.
95 EXPECT_THAT(fetcher()->num_pending_callbacks(), Eq(1u)); 93 EXPECT_THAT(fetcher()->num_pending_callbacks(), Eq(1u));
96 94
97 // Serve it with a different config. 95 // Serve it with a different config.
98 DoodleConfig other_config; 96 DoodleConfig other_config;
99 other_config.doodle_type = DoodleType::SLIDESHOW; 97 other_config.doodle_type = DoodleType::SLIDESHOW;
100 DCHECK(!config.IsEquivalent(other_config)); 98 DCHECK(config != other_config);
101 fetcher()->ServeAllCallbacks(DoodleState::AVAILABLE, other_config); 99 fetcher()->ServeAllCallbacks(DoodleState::AVAILABLE, some_time(),
100 other_config);
102 101
103 // The config should have been updated. 102 // The config should have been updated.
104 EXPECT_THAT(service()->config(), Eq(other_config)); 103 EXPECT_THAT(service()->config(), Eq(other_config));
105 } 104 }
106 105
107 TEST_F(DoodleServiceTest, CallsObserverOnConfigReceived) { 106 TEST_F(DoodleServiceTest, CallsObserverOnConfigReceived) {
108 StrictMock<MockDoodleObserver> observer; 107 StrictMock<MockDoodleObserver> observer;
109 service()->AddObserver(&observer); 108 service()->AddObserver(&observer);
110 109
111 ASSERT_THAT(service()->config(), Eq(base::nullopt)); 110 ASSERT_THAT(service()->config(), Eq(base::nullopt));
112 111
113 // Request a refresh of the doodle config. 112 // Request a refresh of the doodle config.
114 service()->Refresh(); 113 service()->Refresh();
115 // The request should have arrived at the fetcher. 114 // The request should have arrived at the fetcher.
116 ASSERT_THAT(fetcher()->num_pending_callbacks(), Eq(1u)); 115 ASSERT_THAT(fetcher()->num_pending_callbacks(), Eq(1u));
117 116
118 // Serve it (with an arbitrary config). The observer should get notified. 117 // Serve it (with an arbitrary config). The observer should get notified.
119 DoodleConfig config; 118 DoodleConfig config;
120 config.doodle_type = DoodleType::SIMPLE; 119 config.doodle_type = DoodleType::SIMPLE;
121 EXPECT_CALL(observer, OnDoodleConfigUpdated(Eq(config))); 120 EXPECT_CALL(observer, OnDoodleConfigUpdated(Eq(config)));
122 fetcher()->ServeAllCallbacks(DoodleState::AVAILABLE, config); 121 fetcher()->ServeAllCallbacks(DoodleState::AVAILABLE, some_time(), config);
123 122
124 // Remove the observer before the service gets destroyed. 123 // Remove the observer before the service gets destroyed.
125 service()->RemoveObserver(&observer); 124 service()->RemoveObserver(&observer);
126 } 125 }
127 126
128 TEST_F(DoodleServiceTest, CallsObserverOnConfigRemoved) { 127 TEST_F(DoodleServiceTest, CallsObserverOnConfigRemoved) {
129 // Load some doodle config. 128 // Load some doodle config.
130 service()->Refresh(); 129 service()->Refresh();
131 DoodleConfig config; 130 DoodleConfig config;
132 config.doodle_type = DoodleType::SIMPLE; 131 config.doodle_type = DoodleType::SIMPLE;
133 fetcher()->ServeAllCallbacks(DoodleState::AVAILABLE, config); 132 fetcher()->ServeAllCallbacks(DoodleState::AVAILABLE, some_time(), config);
134 ASSERT_THAT(service()->config(), Eq(config)); 133 ASSERT_THAT(service()->config(), Eq(config));
135 134
136 // Register an observer and request a refresh. 135 // Register an observer and request a refresh.
137 StrictMock<MockDoodleObserver> observer; 136 StrictMock<MockDoodleObserver> observer;
138 service()->AddObserver(&observer); 137 service()->AddObserver(&observer);
139 service()->Refresh(); 138 service()->Refresh();
140 ASSERT_THAT(fetcher()->num_pending_callbacks(), Eq(1u)); 139 ASSERT_THAT(fetcher()->num_pending_callbacks(), Eq(1u));
141 140
142 // Serve the request with an empty doodle config. The observer should get 141 // Serve the request with an empty doodle config. The observer should get
143 // notified. 142 // notified.
144 EXPECT_CALL(observer, OnDoodleConfigUpdated(Eq(base::nullopt))); 143 EXPECT_CALL(observer, OnDoodleConfigUpdated(Eq(base::nullopt)));
145 fetcher()->ServeAllCallbacks(DoodleState::NO_DOODLE, base::nullopt); 144 fetcher()->ServeAllCallbacks(DoodleState::NO_DOODLE, base::TimeDelta(),
145 base::nullopt);
146 146
147 // Remove the observer before the service gets destroyed. 147 // Remove the observer before the service gets destroyed.
148 service()->RemoveObserver(&observer); 148 service()->RemoveObserver(&observer);
149 } 149 }
150 150
151 TEST_F(DoodleServiceTest, CallsObserverOnConfigUpdated) { 151 TEST_F(DoodleServiceTest, CallsObserverOnConfigUpdated) {
152 // Load some doodle config. 152 // Load some doodle config.
153 service()->Refresh(); 153 service()->Refresh();
154 DoodleConfig config; 154 DoodleConfig config;
155 config.doodle_type = DoodleType::SIMPLE; 155 config.doodle_type = DoodleType::SIMPLE;
156 fetcher()->ServeAllCallbacks(DoodleState::AVAILABLE, config); 156 fetcher()->ServeAllCallbacks(DoodleState::AVAILABLE, some_time(), config);
157 ASSERT_THAT(service()->config(), Eq(config)); 157 ASSERT_THAT(service()->config(), Eq(config));
158 158
159 // Register an observer and request a refresh. 159 // Register an observer and request a refresh.
160 StrictMock<MockDoodleObserver> observer; 160 StrictMock<MockDoodleObserver> observer;
161 service()->AddObserver(&observer); 161 service()->AddObserver(&observer);
162 service()->Refresh(); 162 service()->Refresh();
163 ASSERT_THAT(fetcher()->num_pending_callbacks(), Eq(1u)); 163 ASSERT_THAT(fetcher()->num_pending_callbacks(), Eq(1u));
164 164
165 // Serve the request with a different doodle config. The observer should get 165 // Serve the request with a different doodle config. The observer should get
166 // notified. 166 // notified.
167 DoodleConfig other_config; 167 DoodleConfig other_config;
168 other_config.doodle_type = DoodleType::SLIDESHOW; 168 other_config.doodle_type = DoodleType::SLIDESHOW;
169 DCHECK(!config.IsEquivalent(other_config)); 169 DCHECK(config != other_config);
170 EXPECT_CALL(observer, OnDoodleConfigUpdated(Eq(other_config))); 170 EXPECT_CALL(observer, OnDoodleConfigUpdated(Eq(other_config)));
171 fetcher()->ServeAllCallbacks(DoodleState::AVAILABLE, other_config); 171 fetcher()->ServeAllCallbacks(DoodleState::AVAILABLE, some_time(),
172 other_config);
172 173
173 // Remove the observer before the service gets destroyed. 174 // Remove the observer before the service gets destroyed.
174 service()->RemoveObserver(&observer); 175 service()->RemoveObserver(&observer);
175 } 176 }
176 177
177 TEST_F(DoodleServiceTest, DoesNotCallObserverWhenConfigEquivalent) { 178 TEST_F(DoodleServiceTest, DoesNotCallObserverWhenConfigEquivalent) {
178 // Load some doodle config. 179 // Load some doodle config.
179 service()->Refresh(); 180 service()->Refresh();
180 DoodleConfig config; 181 DoodleConfig config;
181 config.doodle_type = DoodleType::SIMPLE; 182 config.doodle_type = DoodleType::SIMPLE;
182 fetcher()->ServeAllCallbacks(DoodleState::AVAILABLE, config); 183 fetcher()->ServeAllCallbacks(DoodleState::AVAILABLE, some_time(), config);
183 ASSERT_THAT(service()->config(), Eq(config)); 184 ASSERT_THAT(service()->config(), Eq(config));
184 185
185 // Register an observer and request a refresh. 186 // Register an observer and request a refresh.
186 StrictMock<MockDoodleObserver> observer; 187 StrictMock<MockDoodleObserver> observer;
187 service()->AddObserver(&observer); 188 service()->AddObserver(&observer);
188 service()->Refresh(); 189 service()->Refresh();
189 ASSERT_THAT(fetcher()->num_pending_callbacks(), Eq(1u)); 190 ASSERT_THAT(fetcher()->num_pending_callbacks(), Eq(1u));
190 191
191 // Serve the request with an equivalent doodle config. The observer should 192 // Serve the request with an equivalent doodle config. The observer should
192 // *not* get notified. 193 // *not* get notified.
193 DoodleConfig equivalent_config; 194 DoodleConfig equivalent_config;
194 equivalent_config.doodle_type = DoodleType::SIMPLE; 195 equivalent_config.doodle_type = DoodleType::SIMPLE;
195 DCHECK(config.IsEquivalent(equivalent_config)); 196 DCHECK(config == equivalent_config);
196 fetcher()->ServeAllCallbacks(DoodleState::AVAILABLE, equivalent_config); 197 fetcher()->ServeAllCallbacks(DoodleState::AVAILABLE, some_time(),
198 equivalent_config);
197 199
198 // Remove the observer before the service gets destroyed. 200 // Remove the observer before the service gets destroyed.
199 service()->RemoveObserver(&observer); 201 service()->RemoveObserver(&observer);
200 } 202 }
201 203
202 } // namespace doodle 204 } // namespace doodle
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698