Chromium Code Reviews| Index: components/doodle/doodle_service_unittest.cc |
| diff --git a/components/doodle/doodle_service_unittest.cc b/components/doodle/doodle_service_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f35e2e56a497a9dea1bd176dad5da3af8aa00dd7 |
| --- /dev/null |
| +++ b/components/doodle/doodle_service_unittest.cc |
| @@ -0,0 +1,106 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/doodle/doodle_service.h" |
| + |
| +#include <memory> |
| +#include <utility> |
| +#include <vector> |
| + |
| +#include "base/bind.h" |
| +#include "base/memory/ptr_util.h" |
| +#include "testing/gmock/include/gmock/gmock.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +using testing::Eq; |
| +using testing::Ne; |
| + |
| +namespace doodle { |
| + |
| +namespace { |
| + |
| +class FakeDoodleFetcher : public DoodleFetcher { |
| + public: |
| + FakeDoodleFetcher() = default; |
| + ~FakeDoodleFetcher() override = default; |
| + |
| + void FetchDoodle(FinishedCallback callback) override { |
| + callbacks_.push_back(std::move(callback)); |
| + } |
| + |
| + size_t num_pending_callbacks() const { return callbacks_.size(); } |
| + |
| + void ServeAllCallbacks(DoodleState state, |
| + const base::Optional<DoodleConfig>& config) { |
| + for (auto& callback : callbacks_) { |
| + std::move(callback).Run(state, config); |
| + } |
| + callbacks_.clear(); |
| + } |
| + |
| + private: |
| + std::vector<FinishedCallback> callbacks_; |
| +}; |
| + |
| +class MockDoodleObserver : public DoodleService::Observer { |
| + public: |
| + MOCK_METHOD1(OnDoodleConfigUpdated, |
| + void(const base::Optional<DoodleConfig>&)); |
| +}; |
| + |
| +} // namespace |
| + |
| +class DoodleServiceTest : public testing::Test { |
| + public: |
| + DoodleServiceTest() : fetcher_(nullptr) { |
| + auto fetcher = base::MakeUnique<FakeDoodleFetcher>(); |
| + fetcher_ = fetcher.get(); |
| + service_ = base::MakeUnique<DoodleService>(std::move(fetcher)); |
| + } |
| + |
| + DoodleService* service() { return service_.get(); } |
| + FakeDoodleFetcher* fetcher() { return fetcher_; } |
| + |
| + private: |
| + std::unique_ptr<DoodleService> service_; |
| + FakeDoodleFetcher* fetcher_; |
| +}; |
| + |
| +TEST_F(DoodleServiceTest, CallsObserverImmediately) { |
| + MockDoodleObserver observer; |
| + |
| + // When an observer is added, it should immediately receive the current doodle |
| + // config (which in this case is empty). |
| + EXPECT_CALL(observer, OnDoodleConfigUpdated(Eq(base::nullopt))); |
| + 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
|
| + |
| + // Remove the observer before the service gets destroyed. |
| + service()->RemoveObserver(&observer); |
| +} |
| + |
| +TEST_F(DoodleServiceTest, CallsObserverOnReceivingConfig) { |
| + MockDoodleObserver observer; |
| + |
| + // When an observer is added, it should immediately receive the current doodle |
| + // config (which at this point is empty). |
| + EXPECT_CALL(observer, OnDoodleConfigUpdated(Eq(base::nullopt))); |
| + service()->AddObserver(&observer); |
| + |
| + // Request a refresh of the doodle config. |
| + service()->Refresh(); |
| + // The request should have arrived at the fetcher. |
| + EXPECT_THAT(fetcher()->num_pending_callbacks(), Eq(1u)); |
| + |
| + // Serve it (with an arbitrary config). The observer should get notified. |
| + DoodleConfig config; |
| + config.doodle_type = DoodleType::SLIDESHOW; |
| + EXPECT_CALL(observer, OnDoodleConfigUpdated(Ne(base::nullopt))); |
| + // TODO: also check that the config matches? |
| + fetcher()->ServeAllCallbacks(DoodleState::AVAILABLE, config); |
| + |
| + // Remove the observer before the service gets destroyed. |
| + service()->RemoveObserver(&observer); |
| +} |
| + |
| +} // namespace doodle |