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

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

Issue 2725143003: [Doodle] Pull time_to_live out of DoodleConfig (Closed)
Patch Set: mastiz 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
« no previous file with comments | « components/doodle/doodle_service.cc ('k') | components/doodle/doodle_types.h » ('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 #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
71 private: 67 private:
72 std::unique_ptr<DoodleService> service_; 68 std::unique_ptr<DoodleService> service_;
73 FakeDoodleFetcher* fetcher_; 69 FakeDoodleFetcher* fetcher_;
74 }; 70 };
75 71
76 TEST_F(DoodleServiceTest, FetchesConfigOnRefresh) { 72 TEST_F(DoodleServiceTest, FetchesConfigOnRefresh) {
77 ASSERT_THAT(service()->config(), Eq(base::nullopt)); 73 ASSERT_THAT(service()->config(), Eq(base::nullopt));
78 74
79 // Request a refresh of the doodle config. 75 // Request a refresh of the doodle config.
80 service()->Refresh(); 76 service()->Refresh();
81 // The request should have arrived at the fetcher. 77 // The request should have arrived at the fetcher.
82 EXPECT_THAT(fetcher()->num_pending_callbacks(), Eq(1u)); 78 EXPECT_THAT(fetcher()->num_pending_callbacks(), Eq(1u));
83 79
84 // Serve it (with an arbitrary config). 80 // Serve it (with an arbitrary config).
85 DoodleConfig config; 81 DoodleConfig config;
86 config.doodle_type = DoodleType::SIMPLE; 82 config.doodle_type = DoodleType::SIMPLE;
87 fetcher()->ServeAllCallbacks(DoodleState::AVAILABLE, config); 83 fetcher()->ServeAllCallbacks(DoodleState::AVAILABLE,
84 base::TimeDelta::FromHours(1), config);
88 85
89 // The config should be available. 86 // The config should be available.
90 EXPECT_THAT(service()->config(), Eq(config)); 87 EXPECT_THAT(service()->config(), Eq(config));
91 88
92 // Request a refresh again. 89 // Request a refresh again.
93 service()->Refresh(); 90 service()->Refresh();
94 // The request should have arrived at the fetcher again. 91 // The request should have arrived at the fetcher again.
95 EXPECT_THAT(fetcher()->num_pending_callbacks(), Eq(1u)); 92 EXPECT_THAT(fetcher()->num_pending_callbacks(), Eq(1u));
96 93
97 // Serve it with a different config. 94 // Serve it with a different config.
98 DoodleConfig other_config; 95 DoodleConfig other_config;
99 other_config.doodle_type = DoodleType::SLIDESHOW; 96 other_config.doodle_type = DoodleType::SLIDESHOW;
100 DCHECK(!config.IsEquivalent(other_config)); 97 DCHECK(config != other_config);
101 fetcher()->ServeAllCallbacks(DoodleState::AVAILABLE, other_config); 98 fetcher()->ServeAllCallbacks(DoodleState::AVAILABLE,
99 base::TimeDelta::FromHours(1), other_config);
102 100
103 // The config should have been updated. 101 // The config should have been updated.
104 EXPECT_THAT(service()->config(), Eq(other_config)); 102 EXPECT_THAT(service()->config(), Eq(other_config));
105 } 103 }
106 104
107 TEST_F(DoodleServiceTest, CallsObserverOnConfigReceived) { 105 TEST_F(DoodleServiceTest, CallsObserverOnConfigReceived) {
108 StrictMock<MockDoodleObserver> observer; 106 StrictMock<MockDoodleObserver> observer;
109 service()->AddObserver(&observer); 107 service()->AddObserver(&observer);
110 108
111 ASSERT_THAT(service()->config(), Eq(base::nullopt)); 109 ASSERT_THAT(service()->config(), Eq(base::nullopt));
112 110
113 // Request a refresh of the doodle config. 111 // Request a refresh of the doodle config.
114 service()->Refresh(); 112 service()->Refresh();
115 // The request should have arrived at the fetcher. 113 // The request should have arrived at the fetcher.
116 ASSERT_THAT(fetcher()->num_pending_callbacks(), Eq(1u)); 114 ASSERT_THAT(fetcher()->num_pending_callbacks(), Eq(1u));
117 115
118 // Serve it (with an arbitrary config). The observer should get notified. 116 // Serve it (with an arbitrary config). The observer should get notified.
119 DoodleConfig config; 117 DoodleConfig config;
120 config.doodle_type = DoodleType::SIMPLE; 118 config.doodle_type = DoodleType::SIMPLE;
121 EXPECT_CALL(observer, OnDoodleConfigUpdated(Eq(config))); 119 EXPECT_CALL(observer, OnDoodleConfigUpdated(Eq(config)));
122 fetcher()->ServeAllCallbacks(DoodleState::AVAILABLE, config); 120 fetcher()->ServeAllCallbacks(DoodleState::AVAILABLE,
121 base::TimeDelta::FromHours(1), 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,
133 base::TimeDelta::FromHours(1), config);
134 ASSERT_THAT(service()->config(), Eq(config)); 134 ASSERT_THAT(service()->config(), Eq(config));
135 135
136 // Register an observer and request a refresh. 136 // Register an observer and request a refresh.
137 StrictMock<MockDoodleObserver> observer; 137 StrictMock<MockDoodleObserver> observer;
138 service()->AddObserver(&observer); 138 service()->AddObserver(&observer);
139 service()->Refresh(); 139 service()->Refresh();
140 ASSERT_THAT(fetcher()->num_pending_callbacks(), Eq(1u)); 140 ASSERT_THAT(fetcher()->num_pending_callbacks(), Eq(1u));
141 141
142 // Serve the request with an empty doodle config. The observer should get 142 // Serve the request with an empty doodle config. The observer should get
143 // notified. 143 // notified.
144 EXPECT_CALL(observer, OnDoodleConfigUpdated(Eq(base::nullopt))); 144 EXPECT_CALL(observer, OnDoodleConfigUpdated(Eq(base::nullopt)));
145 fetcher()->ServeAllCallbacks(DoodleState::NO_DOODLE, base::nullopt); 145 fetcher()->ServeAllCallbacks(DoodleState::NO_DOODLE, base::TimeDelta(),
146 base::nullopt);
146 147
147 // Remove the observer before the service gets destroyed. 148 // Remove the observer before the service gets destroyed.
148 service()->RemoveObserver(&observer); 149 service()->RemoveObserver(&observer);
149 } 150 }
150 151
151 TEST_F(DoodleServiceTest, CallsObserverOnConfigUpdated) { 152 TEST_F(DoodleServiceTest, CallsObserverOnConfigUpdated) {
152 // Load some doodle config. 153 // Load some doodle config.
153 service()->Refresh(); 154 service()->Refresh();
154 DoodleConfig config; 155 DoodleConfig config;
155 config.doodle_type = DoodleType::SIMPLE; 156 config.doodle_type = DoodleType::SIMPLE;
156 fetcher()->ServeAllCallbacks(DoodleState::AVAILABLE, config); 157 fetcher()->ServeAllCallbacks(DoodleState::AVAILABLE,
158 base::TimeDelta::FromHours(1), config);
157 ASSERT_THAT(service()->config(), Eq(config)); 159 ASSERT_THAT(service()->config(), Eq(config));
158 160
159 // Register an observer and request a refresh. 161 // Register an observer and request a refresh.
160 StrictMock<MockDoodleObserver> observer; 162 StrictMock<MockDoodleObserver> observer;
161 service()->AddObserver(&observer); 163 service()->AddObserver(&observer);
162 service()->Refresh(); 164 service()->Refresh();
163 ASSERT_THAT(fetcher()->num_pending_callbacks(), Eq(1u)); 165 ASSERT_THAT(fetcher()->num_pending_callbacks(), Eq(1u));
164 166
165 // Serve the request with a different doodle config. The observer should get 167 // Serve the request with a different doodle config. The observer should get
166 // notified. 168 // notified.
167 DoodleConfig other_config; 169 DoodleConfig other_config;
168 other_config.doodle_type = DoodleType::SLIDESHOW; 170 other_config.doodle_type = DoodleType::SLIDESHOW;
169 DCHECK(!config.IsEquivalent(other_config)); 171 DCHECK(config != other_config);
170 EXPECT_CALL(observer, OnDoodleConfigUpdated(Eq(other_config))); 172 EXPECT_CALL(observer, OnDoodleConfigUpdated(Eq(other_config)));
171 fetcher()->ServeAllCallbacks(DoodleState::AVAILABLE, other_config); 173 fetcher()->ServeAllCallbacks(DoodleState::AVAILABLE,
174 base::TimeDelta::FromHours(1), other_config);
172 175
173 // Remove the observer before the service gets destroyed. 176 // Remove the observer before the service gets destroyed.
174 service()->RemoveObserver(&observer); 177 service()->RemoveObserver(&observer);
175 } 178 }
176 179
177 TEST_F(DoodleServiceTest, DoesNotCallObserverWhenConfigEquivalent) { 180 TEST_F(DoodleServiceTest, DoesNotCallObserverWhenConfigEquivalent) {
178 // Load some doodle config. 181 // Load some doodle config.
179 service()->Refresh(); 182 service()->Refresh();
180 DoodleConfig config; 183 DoodleConfig config;
181 config.doodle_type = DoodleType::SIMPLE; 184 config.doodle_type = DoodleType::SIMPLE;
182 fetcher()->ServeAllCallbacks(DoodleState::AVAILABLE, config); 185 fetcher()->ServeAllCallbacks(DoodleState::AVAILABLE,
186 base::TimeDelta::FromHours(1), config);
183 ASSERT_THAT(service()->config(), Eq(config)); 187 ASSERT_THAT(service()->config(), Eq(config));
184 188
185 // Register an observer and request a refresh. 189 // Register an observer and request a refresh.
186 StrictMock<MockDoodleObserver> observer; 190 StrictMock<MockDoodleObserver> observer;
187 service()->AddObserver(&observer); 191 service()->AddObserver(&observer);
188 service()->Refresh(); 192 service()->Refresh();
189 ASSERT_THAT(fetcher()->num_pending_callbacks(), Eq(1u)); 193 ASSERT_THAT(fetcher()->num_pending_callbacks(), Eq(1u));
190 194
191 // Serve the request with an equivalent doodle config. The observer should 195 // Serve the request with an equivalent doodle config. The observer should
192 // *not* get notified. 196 // *not* get notified.
193 DoodleConfig equivalent_config; 197 DoodleConfig equivalent_config;
194 equivalent_config.doodle_type = DoodleType::SIMPLE; 198 equivalent_config.doodle_type = DoodleType::SIMPLE;
195 DCHECK(config.IsEquivalent(equivalent_config)); 199 DCHECK(config == equivalent_config);
196 fetcher()->ServeAllCallbacks(DoodleState::AVAILABLE, equivalent_config); 200 fetcher()->ServeAllCallbacks(
201 DoodleState::AVAILABLE, base::TimeDelta::FromHours(1), equivalent_config);
197 202
198 // Remove the observer before the service gets destroyed. 203 // Remove the observer before the service gets destroyed.
199 service()->RemoveObserver(&observer); 204 service()->RemoveObserver(&observer);
200 } 205 }
201 206
202 } // namespace doodle 207 } // namespace doodle
OLDNEW
« no previous file with comments | « components/doodle/doodle_service.cc ('k') | components/doodle/doodle_types.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698