Chromium Code Reviews| Index: components/arc/arc_service_manager_unittest.cc |
| diff --git a/components/arc/arc_service_manager_unittest.cc b/components/arc/arc_service_manager_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..418eec74dd76ff3c5809d9337671a7ea5351b1f4 |
| --- /dev/null |
| +++ b/components/arc/arc_service_manager_unittest.cc |
| @@ -0,0 +1,125 @@ |
| +// 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 <memory> |
| +#include <utility> |
| + |
| +#include "base/auto_reset.h" |
| +#include "base/memory/ptr_util.h" |
| +#include "components/arc/arc_bridge_service.h" |
| +#include "components/arc/arc_service_manager.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace arc { |
| + |
| +namespace { |
| + |
| +class AnonymousService : public ArcService { |
| + public: |
| + AnonymousService(ArcBridgeService* arc_bridge_service, bool* alive) |
| + : ArcService(arc_bridge_service), alive_(alive, true) {} |
| + ~AnonymousService() override = default; |
| + |
| + private: |
| + base::AutoReset<bool> alive_; |
| +}; |
| + |
| +class NamedService : public ArcService { |
| + public: |
| + static const char kArcServiceName[]; |
| + NamedService(ArcBridgeService* arc_bridge_service, bool* alive) |
| + : ArcService(arc_bridge_service), alive_(alive, true) {} |
| + ~NamedService() override = default; |
| + |
| + private: |
| + base::AutoReset<bool> alive_; |
| +}; |
| +const char NamedService::kArcServiceName[] = |
|
Yusuke Sato
2017/01/10 19:30:57
nit: space between L37 and 38
Luis Héctor Chávez
2017/01/10 23:48:09
Done.
|
| + "arc::(anonymous namespace)::NamedService"; |
| + |
| +} // namespace |
| + |
| +class ArcServiceManagerTest : public testing::Test { |
| + public: |
| + ArcServiceManagerTest() = default; |
| + |
| + void SetUp() override { |
| + arc_bridge_service_ = base::MakeUnique<ArcBridgeService>(); |
| + } |
| + |
| + void TearDown() override { arc_bridge_service_.reset(); } |
| + |
| + ArcBridgeService* arc_bridge_service() { return arc_bridge_service_.get(); } |
| + |
| + private: |
| + std::unique_ptr<ArcBridgeService> arc_bridge_service_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ArcServiceManagerTest); |
| +}; |
| + |
| +// Exercises the basic getter functionality of ArcServiceManager. |
| +TEST_F(ArcServiceManagerTest, BasicGetter) { |
| + bool named_service_alive = false; |
| + |
| + // ArcServiceManager is empty, GetService() should return nullptr. |
| + auto manager = base::MakeUnique<ArcServiceManager>(nullptr); |
| + EXPECT_EQ(nullptr, manager->GetService<NamedService>()); |
| + |
| + EXPECT_TRUE(manager->AddService(base::MakeUnique<NamedService>( |
| + arc_bridge_service(), &named_service_alive))); |
| + EXPECT_TRUE(named_service_alive); |
| + EXPECT_NE(nullptr, manager->GetService<NamedService>()); |
| + |
| + // Finally, the service should not be alive anymore. |
| + manager.reset(); |
| + EXPECT_FALSE(named_service_alive); |
| +} |
| + |
| +// There is no way to distinguish between anonymous services, so it should be |
| +// possible to add them twice (not that it's recommended). |
| +TEST_F(ArcServiceManagerTest, MultipleAnonymousServices) { |
| + bool anonymous_service_alive = false; |
| + bool second_anonymous_service_alive = false; |
| + |
| + auto manager = base::MakeUnique<ArcServiceManager>(nullptr); |
| + |
| + EXPECT_TRUE(manager->AddService(base::MakeUnique<AnonymousService>( |
| + arc_bridge_service(), &anonymous_service_alive))); |
| + EXPECT_TRUE(anonymous_service_alive); |
| + EXPECT_TRUE(manager->AddService(base::MakeUnique<AnonymousService>( |
| + arc_bridge_service(), &second_anonymous_service_alive))); |
| + EXPECT_TRUE(second_anonymous_service_alive); |
| + |
| + // Finally, the individual services should not be alive anymore. |
| + manager.reset(); |
| + EXPECT_FALSE(anonymous_service_alive); |
| + EXPECT_FALSE(second_anonymous_service_alive); |
| +} |
| + |
| +// Named services can only be added once, but can still be retrieved. |
|
Yusuke Sato
2017/01/10 19:30:57
Could you also check that SecondNamedService can b
Luis Héctor Chávez
2017/01/10 23:48:09
Done.
|
| +TEST_F(ArcServiceManagerTest, MultipleNamedServices) { |
| + bool named_service_alive = false; |
| + bool second_named_service_alive = false; |
| + |
| + auto manager = base::MakeUnique<ArcServiceManager>(nullptr); |
| + |
| + auto named_service = base::MakeUnique<NamedService>(arc_bridge_service(), |
| + &named_service_alive); |
| + NamedService* raw_named_service = named_service.get(); |
| + EXPECT_TRUE(named_service_alive); |
| + EXPECT_TRUE(manager->AddService(std::move(named_service))); |
| + auto second_named_service = base::MakeUnique<NamedService>( |
| + arc_bridge_service(), &second_named_service_alive); |
| + EXPECT_TRUE(second_named_service_alive); |
| + // This will fail and immediately destroy the service. |
| + EXPECT_FALSE(manager->AddService(std::move(second_named_service))); |
| + EXPECT_FALSE(second_named_service_alive); |
| + |
| + EXPECT_EQ(raw_named_service, manager->GetService<NamedService>()); |
| + |
| + manager.reset(); |
| + EXPECT_FALSE(named_service_alive); |
| +} |
| + |
| +} // namespace arc |