Chromium Code Reviews| OLD | NEW |
|---|---|
| (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::StrictMock; | |
| 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 // 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 { | |
| 61 public: | |
| 62 DoodleServiceTest() : fetcher_(nullptr) { | |
| 63 auto fetcher = base::MakeUnique<FakeDoodleFetcher>(); | |
| 64 fetcher_ = fetcher.get(); | |
| 65 service_ = base::MakeUnique<DoodleService>(std::move(fetcher)); | |
| 66 } | |
| 67 | |
| 68 DoodleService* service() { return service_.get(); } | |
| 69 FakeDoodleFetcher* fetcher() { return fetcher_; } | |
| 70 | |
| 71 private: | |
| 72 std::unique_ptr<DoodleService> service_; | |
| 73 FakeDoodleFetcher* fetcher_; | |
| 74 }; | |
| 75 | |
| 76 TEST_F(DoodleServiceTest, CallsObserverOnConfigReceived) { | |
| 77 StrictMock<MockDoodleObserver> observer; | |
| 78 service()->AddObserver(&observer); | |
| 79 | |
| 80 ASSERT_THAT(service()->config(), Eq(base::nullopt)); | |
|
fhorschig
2017/02/27 17:43:53
Why not EXPECT_THAT? the code wouldn't break and w
Marc Treib
2017/02/28 10:25:48
The pattern is: ASSERT for preconditions, EXPECT f
| |
| 81 | |
| 82 // Request a refresh of the doodle config. | |
| 83 service()->Refresh(); | |
| 84 // The request should have arrived at the fetcher. | |
| 85 EXPECT_THAT(fetcher()->num_pending_callbacks(), Eq(1u)); | |
| 86 | |
| 87 // Serve it (with an arbitrary config). The observer should get notified. | |
| 88 DoodleConfig config; | |
| 89 config.doodle_type = DoodleType::SIMPLE; | |
| 90 EXPECT_CALL(observer, OnDoodleConfigUpdated(Eq(config))); | |
| 91 fetcher()->ServeAllCallbacks(DoodleState::AVAILABLE, config); | |
| 92 | |
| 93 // Remove the observer before the service gets destroyed. | |
| 94 service()->RemoveObserver(&observer); | |
| 95 } | |
| 96 | |
| 97 TEST_F(DoodleServiceTest, CallsObserverOnConfigRemoved) { | |
| 98 // Load some doodle config. | |
| 99 service()->Refresh(); | |
| 100 DoodleConfig config; | |
| 101 config.doodle_type = DoodleType::SIMPLE; | |
| 102 fetcher()->ServeAllCallbacks(DoodleState::AVAILABLE, config); | |
| 103 ASSERT_THAT(service()->config(), Eq(config)); | |
|
fhorschig
2017/02/27 17:43:53
Same as above, I think an EXPECT is enough to brea
Marc Treib
2017/02/28 10:25:49
Precondition
| |
| 104 | |
| 105 // Register an observer and request a refresh. | |
| 106 StrictMock<MockDoodleObserver> observer; | |
| 107 service()->AddObserver(&observer); | |
| 108 service()->Refresh(); | |
| 109 ASSERT_THAT(fetcher()->num_pending_callbacks(), Eq(1u)); | |
|
fhorschig
2017/02/27 17:43:59
How important is the state of the fetcher?
I see t
Marc Treib
2017/02/28 10:25:49
Precondition
| |
| 110 | |
| 111 // Serve the request with an empty doodle config. The observer should get | |
| 112 // notified. | |
| 113 EXPECT_CALL(observer, OnDoodleConfigUpdated(Eq(base::nullopt))); | |
| 114 fetcher()->ServeAllCallbacks(DoodleState::NO_DOODLE, base::nullopt); | |
| 115 | |
| 116 // Remove the observer before the service gets destroyed. | |
| 117 service()->RemoveObserver(&observer); | |
| 118 } | |
| 119 | |
| 120 TEST_F(DoodleServiceTest, CallsObserverOnConfigUpdated) { | |
| 121 // Load some doodle config. | |
| 122 service()->Refresh(); | |
| 123 DoodleConfig config; | |
| 124 config.doodle_type = DoodleType::SIMPLE; | |
| 125 fetcher()->ServeAllCallbacks(DoodleState::AVAILABLE, config); | |
| 126 ASSERT_THAT(service()->config(), Eq(config)); | |
|
fhorschig
2017/02/27 17:43:59
Same as above, EXPECT_THAT?
Marc Treib
2017/02/28 10:25:49
Precondition
| |
| 127 | |
| 128 // Register an observer and request a refresh. | |
| 129 StrictMock<MockDoodleObserver> observer; | |
| 130 service()->AddObserver(&observer); | |
| 131 service()->Refresh(); | |
| 132 ASSERT_THAT(fetcher()->num_pending_callbacks(), Eq(1u)); | |
| 133 | |
| 134 // Serve the request with a different doodle config. The observer should get | |
| 135 // notified. | |
| 136 DoodleConfig config2; | |
| 137 config2.doodle_type = DoodleType::SLIDESHOW; | |
| 138 ASSERT_FALSE(config.IsEquivalent(config2)); | |
|
fhorschig
2017/02/27 17:43:59
This looks like an invariant of the test code as n
Marc Treib
2017/02/28 10:25:49
Well, it's arguable if a change in the IsEquivalen
| |
| 139 EXPECT_CALL(observer, OnDoodleConfigUpdated(Eq(config2))); | |
| 140 fetcher()->ServeAllCallbacks(DoodleState::AVAILABLE, config2); | |
| 141 | |
| 142 // Remove the observer before the service gets destroyed. | |
| 143 service()->RemoveObserver(&observer); | |
| 144 } | |
| 145 | |
| 146 TEST_F(DoodleServiceTest, DoesNotCallObserverWhenConfigEquivalent) { | |
| 147 // Load some doodle config. | |
| 148 service()->Refresh(); | |
| 149 DoodleConfig config; | |
| 150 config.doodle_type = DoodleType::SIMPLE; | |
| 151 fetcher()->ServeAllCallbacks(DoodleState::AVAILABLE, config); | |
| 152 ASSERT_THAT(service()->config(), Eq(config)); | |
|
fhorschig
2017/02/27 17:43:59
Same as above, EXPECT_THAT?
Marc Treib
2017/02/28 10:25:49
Precondition
| |
| 153 | |
| 154 // Register an observer and request a refresh. | |
| 155 StrictMock<MockDoodleObserver> observer; | |
| 156 service()->AddObserver(&observer); | |
| 157 service()->Refresh(); | |
| 158 ASSERT_THAT(fetcher()->num_pending_callbacks(), Eq(1u)); | |
| 159 | |
| 160 // Serve the request with an equivalent doodle config. The observer should get | |
| 161 // notified. | |
| 162 DoodleConfig config2; | |
| 163 config2.doodle_type = DoodleType::SIMPLE; | |
| 164 ASSERT_TRUE(config.IsEquivalent(config2)); | |
|
fhorschig
2017/02/27 17:43:59
Same as above, DCHECK?
Also, is there a special r
Marc Treib
2017/02/28 10:25:48
Done.
| |
| 165 fetcher()->ServeAllCallbacks(DoodleState::AVAILABLE, config2); | |
| 166 | |
| 167 // Remove the observer before the service gets destroyed. | |
| 168 service()->RemoveObserver(&observer); | |
| 169 } | |
| 170 | |
| 171 } // namespace doodle | |
| OLD | NEW |