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

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

Issue 2710003006: [Doodle] Introduce a DoodleService (Closed)
Patch Set: 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "components/doodle/doodle_service.h"
6
7 #include <memory>
8 #include <utility>
9 #include <vector>
10
11 #include "base/bind.h"
12 #include "base/memory/ptr_util.h"
13 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 using testing::Eq;
17 using testing::Ne;
18
19 namespace doodle {
20
21 namespace {
22
23 class FakeDoodleFetcher : public DoodleFetcher {
24 public:
25 FakeDoodleFetcher() = default;
26 ~FakeDoodleFetcher() override = default;
27
28 void FetchDoodle(FinishedCallback callback) override {
29 callbacks_.push_back(std::move(callback));
30 }
31
32 size_t num_pending_callbacks() const { return callbacks_.size(); }
33
34 void ServeAllCallbacks(DoodleState state,
35 const base::Optional<DoodleConfig>& config) {
36 for (auto& callback : callbacks_) {
37 std::move(callback).Run(state, config);
38 }
39 callbacks_.clear();
40 }
41
42 private:
43 std::vector<FinishedCallback> callbacks_;
44 };
45
46 class MockDoodleObserver : public DoodleService::Observer {
47 public:
48 MOCK_METHOD1(OnDoodleConfigUpdated,
49 void(const base::Optional<DoodleConfig>&));
50 };
51
52 } // namespace
53
54 class DoodleServiceTest : public testing::Test {
55 public:
56 DoodleServiceTest() : fetcher_(nullptr) {
57 auto fetcher = base::MakeUnique<FakeDoodleFetcher>();
58 fetcher_ = fetcher.get();
59 service_ = base::MakeUnique<DoodleService>(std::move(fetcher));
60 }
61
62 DoodleService* service() { return service_.get(); }
63 FakeDoodleFetcher* fetcher() { return fetcher_; }
64
65 private:
66 std::unique_ptr<DoodleService> service_;
67 FakeDoodleFetcher* fetcher_;
68 };
69
70 TEST_F(DoodleServiceTest, CallsObserverImmediately) {
71 MockDoodleObserver observer;
72
73 // When an observer is added, it should immediately receive the current doodle
74 // config (which in this case is empty).
75 EXPECT_CALL(observer, OnDoodleConfigUpdated(Eq(base::nullopt)));
76 service()->AddObserver(&observer);
fhorschig 2017/02/24 15:33:14 If you remove the "immediate call side effect"; de
Marc Treib 2017/02/24 16:18:51 Well, so far there are only two tests, so not all
77
78 // Remove the observer before the service gets destroyed.
79 service()->RemoveObserver(&observer);
80 }
81
82 TEST_F(DoodleServiceTest, CallsObserverOnReceivingConfig) {
83 MockDoodleObserver observer;
84
85 // When an observer is added, it should immediately receive the current doodle
86 // config (which at this point is empty).
87 EXPECT_CALL(observer, OnDoodleConfigUpdated(Eq(base::nullopt)));
88 service()->AddObserver(&observer);
89
90 // Request a refresh of the doodle config.
91 service()->Refresh();
92 // The request should have arrived at the fetcher.
93 EXPECT_THAT(fetcher()->num_pending_callbacks(), Eq(1u));
94
95 // Serve it (with an arbitrary config). The observer should get notified.
96 DoodleConfig config;
97 config.doodle_type = DoodleType::SLIDESHOW;
98 EXPECT_CALL(observer, OnDoodleConfigUpdated(Ne(base::nullopt)));
99 // TODO: also check that the config matches?
100 fetcher()->ServeAllCallbacks(DoodleState::AVAILABLE, config);
101
102 // Remove the observer before the service gets destroyed.
103 service()->RemoveObserver(&observer);
104 }
105
106 } // namespace doodle
OLDNEW
« components/doodle/doodle_service.cc ('K') | « components/doodle/doodle_service.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698