From 0705059e94b68366a915176d431fb9a03c7c4f62 Mon Sep 17 00:00:00 2001 From: hai007 Date: Wed, 5 Apr 2023 11:41:29 -0700 Subject: [PATCH] Create Fake Presenter and Factory of presenter for further testing in UI broker PiperOrigin-RevId: 522111553 --- .../ui/fast_pair/fake_fast_pair_presenter.h | 57 +++++++++++++++++++ .../ui/fast_pair/fast_pair_presenter_impl.cc | 20 +++++++ .../ui/fast_pair/fast_pair_presenter_impl.h | 15 ++++- 3 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 fastpair/ui/fast_pair/fake_fast_pair_presenter.h diff --git a/fastpair/ui/fast_pair/fake_fast_pair_presenter.h b/fastpair/ui/fast_pair/fake_fast_pair_presenter.h new file mode 100644 index 00000000..f2fe938c --- /dev/null +++ b/fastpair/ui/fast_pair/fake_fast_pair_presenter.h @@ -0,0 +1,57 @@ +// Copyright 2023 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef THIRD_PARTY_NEARBY_FASTPAIR_UI_FAST_PAIR_FAKE_FAST_PAIR_PRESENTER_H_ +#define THIRD_PARTY_NEARBY_FASTPAIR_UI_FAST_PAIR_FAKE_FAST_PAIR_PRESENTER_H_ + +#include + +#include "fastpair/ui/fast_pair/fast_pair_presenter_impl.h" + +namespace nearby { +namespace fastpair { + +class FakeFastPairPresenter : public FastPairPresenter { + public: + void ShowDiscovery( + const FastPairDevice& device, + FastPairNotificationController& notification_controller) override { + show_discovery_ = true; + } + bool show_deiscovery() { return show_discovery_; } + + private: + bool show_discovery_ = false; +}; + +class FakeFastPairPresenterFactory : public FastPairPresenterImpl::Factory { + public: + std::unique_ptr CreateInstance() override { + auto fake_fast_pair_presenter = std::make_unique(); + fake_fast_pair_presenter_ = fake_fast_pair_presenter.get(); + return fake_fast_pair_presenter; + } + + FakeFastPairPresenter* fake_fast_pair_presenter() { + return fake_fast_pair_presenter_; + } + + protected: + FakeFastPairPresenter* fake_fast_pair_presenter_ = nullptr; +}; + +} // namespace fastpair +} // namespace nearby + +#endif // THIRD_PARTY_NEARBY_FASTPAIR_UI_FAST_PAIR_FAKE_FAST_PAIR_PRESENTER_H_ diff --git a/fastpair/ui/fast_pair/fast_pair_presenter_impl.cc b/fastpair/ui/fast_pair/fast_pair_presenter_impl.cc index d9e12572..a5916cdf 100644 --- a/fastpair/ui/fast_pair/fast_pair_presenter_impl.cc +++ b/fastpair/ui/fast_pair/fast_pair_presenter_impl.cc @@ -25,6 +25,26 @@ namespace nearby { namespace fastpair { +// static +FastPairPresenterImpl::Factory* + FastPairPresenterImpl::Factory::g_test_factory_ = nullptr; + +// static +std::unique_ptr FastPairPresenterImpl::Factory::Create() { + if (g_test_factory_) { + return g_test_factory_->CreateInstance(); + } + return std::make_unique(); +} + +// static +void FastPairPresenterImpl::Factory::SetFactoryForTesting( + Factory* g_test_factory) { + g_test_factory_ = g_test_factory; +} + +FastPairPresenterImpl::Factory::~Factory() = default; + void FastPairPresenterImpl::ShowDiscovery( const FastPairDevice& device, FastPairNotificationController& notification_controller) { diff --git a/fastpair/ui/fast_pair/fast_pair_presenter_impl.h b/fastpair/ui/fast_pair/fast_pair_presenter_impl.h index 1ee63c88..10bd71b5 100644 --- a/fastpair/ui/fast_pair/fast_pair_presenter_impl.h +++ b/fastpair/ui/fast_pair/fast_pair_presenter_impl.h @@ -27,10 +27,23 @@ namespace fastpair { class FastPairPresenterImpl : public FastPairPresenter { public: + class Factory { + public: + static std::unique_ptr Create(); + + static void SetFactoryForTesting(Factory* g_test_factory); + + protected: + virtual ~Factory(); + virtual std::unique_ptr CreateInstance() = 0; + + private: + static Factory* g_test_factory_; + }; + FastPairPresenterImpl() = default; FastPairPresenterImpl(const FastPairPresenterImpl&) = delete; FastPairPresenterImpl& operator=(const FastPairPresenterImpl&) = delete; - ~FastPairPresenterImpl() override = default; void ShowDiscovery( const FastPairDevice& device,