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 <memory> | |
| 6 #include <utility> | |
| 7 | |
| 8 #include "base/auto_reset.h" | |
| 9 #include "base/memory/ptr_util.h" | |
| 10 #include "components/arc/arc_bridge_service.h" | |
| 11 #include "components/arc/arc_service_manager.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | |
| 13 | |
| 14 namespace arc { | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 class AnonymousService : public ArcService { | |
| 19 public: | |
| 20 AnonymousService(ArcBridgeService* arc_bridge_service, bool* alive) | |
| 21 : ArcService(arc_bridge_service), alive_(alive, true) {} | |
| 22 ~AnonymousService() override = default; | |
| 23 | |
| 24 private: | |
| 25 base::AutoReset<bool> alive_; | |
| 26 }; | |
| 27 | |
| 28 class NamedService : public ArcService { | |
| 29 public: | |
| 30 static const char kArcServiceName[]; | |
| 31 NamedService(ArcBridgeService* arc_bridge_service, bool* alive) | |
| 32 : ArcService(arc_bridge_service), alive_(alive, true) {} | |
| 33 ~NamedService() override = default; | |
| 34 | |
| 35 private: | |
| 36 base::AutoReset<bool> alive_; | |
| 37 }; | |
| 38 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.
| |
| 39 "arc::(anonymous namespace)::NamedService"; | |
| 40 | |
| 41 } // namespace | |
| 42 | |
| 43 class ArcServiceManagerTest : public testing::Test { | |
| 44 public: | |
| 45 ArcServiceManagerTest() = default; | |
| 46 | |
| 47 void SetUp() override { | |
| 48 arc_bridge_service_ = base::MakeUnique<ArcBridgeService>(); | |
| 49 } | |
| 50 | |
| 51 void TearDown() override { arc_bridge_service_.reset(); } | |
| 52 | |
| 53 ArcBridgeService* arc_bridge_service() { return arc_bridge_service_.get(); } | |
| 54 | |
| 55 private: | |
| 56 std::unique_ptr<ArcBridgeService> arc_bridge_service_; | |
| 57 | |
| 58 DISALLOW_COPY_AND_ASSIGN(ArcServiceManagerTest); | |
| 59 }; | |
| 60 | |
| 61 // Exercises the basic getter functionality of ArcServiceManager. | |
| 62 TEST_F(ArcServiceManagerTest, BasicGetter) { | |
| 63 bool named_service_alive = false; | |
| 64 | |
| 65 // ArcServiceManager is empty, GetService() should return nullptr. | |
| 66 auto manager = base::MakeUnique<ArcServiceManager>(nullptr); | |
| 67 EXPECT_EQ(nullptr, manager->GetService<NamedService>()); | |
| 68 | |
| 69 EXPECT_TRUE(manager->AddService(base::MakeUnique<NamedService>( | |
| 70 arc_bridge_service(), &named_service_alive))); | |
| 71 EXPECT_TRUE(named_service_alive); | |
| 72 EXPECT_NE(nullptr, manager->GetService<NamedService>()); | |
| 73 | |
| 74 // Finally, the service should not be alive anymore. | |
| 75 manager.reset(); | |
| 76 EXPECT_FALSE(named_service_alive); | |
| 77 } | |
| 78 | |
| 79 // There is no way to distinguish between anonymous services, so it should be | |
| 80 // possible to add them twice (not that it's recommended). | |
| 81 TEST_F(ArcServiceManagerTest, MultipleAnonymousServices) { | |
| 82 bool anonymous_service_alive = false; | |
| 83 bool second_anonymous_service_alive = false; | |
| 84 | |
| 85 auto manager = base::MakeUnique<ArcServiceManager>(nullptr); | |
| 86 | |
| 87 EXPECT_TRUE(manager->AddService(base::MakeUnique<AnonymousService>( | |
| 88 arc_bridge_service(), &anonymous_service_alive))); | |
| 89 EXPECT_TRUE(anonymous_service_alive); | |
| 90 EXPECT_TRUE(manager->AddService(base::MakeUnique<AnonymousService>( | |
| 91 arc_bridge_service(), &second_anonymous_service_alive))); | |
| 92 EXPECT_TRUE(second_anonymous_service_alive); | |
| 93 | |
| 94 // Finally, the individual services should not be alive anymore. | |
| 95 manager.reset(); | |
| 96 EXPECT_FALSE(anonymous_service_alive); | |
| 97 EXPECT_FALSE(second_anonymous_service_alive); | |
| 98 } | |
| 99 | |
| 100 // 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.
| |
| 101 TEST_F(ArcServiceManagerTest, MultipleNamedServices) { | |
| 102 bool named_service_alive = false; | |
| 103 bool second_named_service_alive = false; | |
| 104 | |
| 105 auto manager = base::MakeUnique<ArcServiceManager>(nullptr); | |
| 106 | |
| 107 auto named_service = base::MakeUnique<NamedService>(arc_bridge_service(), | |
| 108 &named_service_alive); | |
| 109 NamedService* raw_named_service = named_service.get(); | |
| 110 EXPECT_TRUE(named_service_alive); | |
| 111 EXPECT_TRUE(manager->AddService(std::move(named_service))); | |
| 112 auto second_named_service = base::MakeUnique<NamedService>( | |
| 113 arc_bridge_service(), &second_named_service_alive); | |
| 114 EXPECT_TRUE(second_named_service_alive); | |
| 115 // This will fail and immediately destroy the service. | |
| 116 EXPECT_FALSE(manager->AddService(std::move(second_named_service))); | |
| 117 EXPECT_FALSE(second_named_service_alive); | |
| 118 | |
| 119 EXPECT_EQ(raw_named_service, manager->GetService<NamedService>()); | |
| 120 | |
| 121 manager.reset(); | |
| 122 EXPECT_FALSE(named_service_alive); | |
| 123 } | |
| 124 | |
| 125 } // namespace arc | |
| OLD | NEW |