Add Mediator Factory to avoid creating variables other than mediator in further adapter. The main purpose of the adapter is connecting dart (flutter app) and c++ (Fast Pair libraries).

PiperOrigin-RevId: 538604362
This commit is contained in:
hai007
2023-06-07 15:12:21 -07:00
committed by Copybara-Service
parent 2027fed32f
commit 4095b7978d
4 changed files with 122 additions and 0 deletions
+3
View File
@@ -18,9 +18,11 @@ cc_library(
name = "keyed_service",
srcs = [
"fast_pair_mediator.cc",
"fast_pair_mediator_factory.cc",
],
hdrs = [
"fast_pair_mediator.h",
"fast_pair_mediator_factory.h",
],
compatible_with = ["//buildenv/target:non_prod"],
visibility = [
@@ -43,6 +45,7 @@ cc_test(
name = "fast_pair_mediator_test",
size = "small",
srcs = [
"fast_pair_mediator_factory_test.cc",
"fast_pair_mediator_test.cc",
],
shard_count = 16,
@@ -0,0 +1,43 @@
// 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.
#include "fastpair/keyed_service/fast_pair_mediator_factory.h"
#include <memory>
#include "fastpair/internal/mediums/mediums.h"
#include "fastpair/server_access/fast_pair_repository_impl.h"
#include "fastpair/ui/fast_pair/fast_pair_notification_controller.h"
#include "fastpair/ui/ui_broker_impl.h"
#include "internal/platform/single_thread_executor.h"
namespace nearby {
namespace fastpair {
MediatorFactory* MediatorFactory::GetInstance() {
static MediatorFactory* instance = new MediatorFactory();
return instance;
}
Mediator* MediatorFactory::CreateMediator() {
mediator_ = std::make_unique<Mediator>(
std::make_unique<Mediums>(), std::make_unique<UIBrokerImpl>(),
std::make_unique<FastPairNotificationController>(),
std::make_unique<FastPairRepositoryImpl>(),
std::make_unique<SingleThreadExecutor>());
return mediator_.get();
}
} // namespace fastpair
} // namespace nearby
@@ -0,0 +1,39 @@
// 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_KEYED_SERVICE_FAST_PAIR_MEDIATOR_FACTORY_H_
#define THIRD_PARTY_NEARBY_FASTPAIR_KEYED_SERVICE_FAST_PAIR_MEDIATOR_FACTORY_H_
#include <memory>
#include "fastpair/keyed_service/fast_pair_mediator.h"
namespace nearby {
namespace fastpair {
class MediatorFactory {
public:
// Return a singleton instance of Mediator Factory
static MediatorFactory* GetInstance();
Mediator* CreateMediator();
private:
std::unique_ptr<Mediator> mediator_;
};
} // namespace fastpair
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_FASTPAIR_KEYED_SERVICE_FAST_PAIR_MEDIATOR_FACTORY_H_
@@ -0,0 +1,37 @@
// 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.
#include "fastpair/keyed_service/fast_pair_mediator_factory.h"
#include "gmock/gmock.h"
#include "protobuf-matchers/protocol-buffer-matchers.h"
#include "gtest/gtest.h"
#include "fastpair/keyed_service/fast_pair_mediator.h"
namespace nearby {
namespace fastpair {
namespace {
TEST(MediatorFactoryTest, checkMediatorCreateWithFactory) {
MediatorFactory *factory_ = nullptr;
Mediator *mediator_ = nullptr;
factory_ = MediatorFactory::GetInstance();
EXPECT_THAT(factory_, testing::NotNull());
mediator_ = factory_->CreateMediator();
EXPECT_THAT(mediator_, testing::NotNull());
}
} // namespace
} // namespace fastpair
} // namespace nearby