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 |
| 39 class DifferentNamedService : public ArcService { |
| 40 public: |
| 41 static const char kArcServiceName[]; |
| 42 DifferentNamedService(ArcBridgeService* arc_bridge_service, bool* alive) |
| 43 : ArcService(arc_bridge_service), alive_(alive, true) {} |
| 44 ~DifferentNamedService() override = default; |
| 45 |
| 46 private: |
| 47 base::AutoReset<bool> alive_; |
| 48 }; |
| 49 |
| 50 class EmptyNamedService : public ArcService { |
| 51 public: |
| 52 static const char kArcServiceName[]; |
| 53 EmptyNamedService(ArcBridgeService* arc_bridge_service, bool* alive) |
| 54 : ArcService(arc_bridge_service), alive_(alive, true) {} |
| 55 ~EmptyNamedService() override = default; |
| 56 |
| 57 private: |
| 58 base::AutoReset<bool> alive_; |
| 59 }; |
| 60 |
| 61 const char NamedService::kArcServiceName[] = |
| 62 "arc::(anonymous namespace)::NamedService"; |
| 63 |
| 64 const char DifferentNamedService::kArcServiceName[] = |
| 65 "arc::(anonymous namespace)::DifferentNamedService"; |
| 66 |
| 67 const char EmptyNamedService::kArcServiceName[] = ""; |
| 68 |
| 69 } // namespace |
| 70 |
| 71 class ArcServiceManagerTest : public testing::Test { |
| 72 public: |
| 73 ArcServiceManagerTest() = default; |
| 74 |
| 75 void SetUp() override { |
| 76 arc_bridge_service_ = base::MakeUnique<ArcBridgeService>(); |
| 77 } |
| 78 |
| 79 void TearDown() override { arc_bridge_service_.reset(); } |
| 80 |
| 81 ArcBridgeService* arc_bridge_service() { return arc_bridge_service_.get(); } |
| 82 |
| 83 private: |
| 84 std::unique_ptr<ArcBridgeService> arc_bridge_service_; |
| 85 |
| 86 DISALLOW_COPY_AND_ASSIGN(ArcServiceManagerTest); |
| 87 }; |
| 88 |
| 89 // Exercises the basic getter functionality of ArcServiceManager. |
| 90 TEST_F(ArcServiceManagerTest, BasicGetter) { |
| 91 bool named_service_alive = false; |
| 92 |
| 93 // ArcServiceManager is empty, GetService() should return nullptr. |
| 94 auto manager = base::MakeUnique<ArcServiceManager>(nullptr); |
| 95 EXPECT_EQ(nullptr, manager->GetService<NamedService>()); |
| 96 |
| 97 EXPECT_TRUE(manager->AddService(base::MakeUnique<NamedService>( |
| 98 arc_bridge_service(), &named_service_alive))); |
| 99 EXPECT_TRUE(named_service_alive); |
| 100 EXPECT_NE(nullptr, manager->GetService<NamedService>()); |
| 101 |
| 102 // Finally, the service should not be alive anymore. |
| 103 manager.reset(); |
| 104 EXPECT_FALSE(named_service_alive); |
| 105 } |
| 106 |
| 107 // There is no way to distinguish between anonymous services, so it should be |
| 108 // possible to add them twice (not that it's recommended). |
| 109 TEST_F(ArcServiceManagerTest, MultipleAnonymousServices) { |
| 110 bool anonymous_service_alive = false; |
| 111 bool second_anonymous_service_alive = false; |
| 112 |
| 113 auto manager = base::MakeUnique<ArcServiceManager>(nullptr); |
| 114 |
| 115 EXPECT_TRUE(manager->AddService(base::MakeUnique<AnonymousService>( |
| 116 arc_bridge_service(), &anonymous_service_alive))); |
| 117 EXPECT_TRUE(anonymous_service_alive); |
| 118 EXPECT_TRUE(manager->AddService(base::MakeUnique<AnonymousService>( |
| 119 arc_bridge_service(), &second_anonymous_service_alive))); |
| 120 EXPECT_TRUE(second_anonymous_service_alive); |
| 121 |
| 122 // Finally, the individual services should not be alive anymore. |
| 123 manager.reset(); |
| 124 EXPECT_FALSE(anonymous_service_alive); |
| 125 EXPECT_FALSE(second_anonymous_service_alive); |
| 126 } |
| 127 |
| 128 // Named services can only be added once, but can still be retrieved. |
| 129 TEST_F(ArcServiceManagerTest, MultipleNamedServices) { |
| 130 bool named_service_alive = false; |
| 131 bool second_named_service_alive = false; |
| 132 bool different_named_service_alive = false; |
| 133 |
| 134 auto manager = base::MakeUnique<ArcServiceManager>(nullptr); |
| 135 |
| 136 auto named_service = base::MakeUnique<NamedService>(arc_bridge_service(), |
| 137 &named_service_alive); |
| 138 NamedService* raw_named_service = named_service.get(); |
| 139 EXPECT_TRUE(named_service_alive); |
| 140 EXPECT_TRUE(manager->AddService(std::move(named_service))); |
| 141 auto second_named_service = base::MakeUnique<NamedService>( |
| 142 arc_bridge_service(), &second_named_service_alive); |
| 143 EXPECT_TRUE(second_named_service_alive); |
| 144 // This will fail and immediately destroy the service. |
| 145 EXPECT_FALSE(manager->AddService(std::move(second_named_service))); |
| 146 EXPECT_FALSE(second_named_service_alive); |
| 147 |
| 148 // We should still be able to add a different-named service. |
| 149 auto different_named_service = base::MakeUnique<DifferentNamedService>( |
| 150 arc_bridge_service(), &different_named_service_alive); |
| 151 DifferentNamedService* raw_different_named_service = |
| 152 different_named_service.get(); |
| 153 EXPECT_TRUE(different_named_service_alive); |
| 154 EXPECT_TRUE(manager->AddService(std::move(different_named_service))); |
| 155 |
| 156 // And find both. |
| 157 EXPECT_EQ(raw_named_service, manager->GetService<NamedService>()); |
| 158 EXPECT_EQ(raw_different_named_service, |
| 159 manager->GetService<DifferentNamedService>()); |
| 160 |
| 161 manager.reset(); |
| 162 EXPECT_FALSE(named_service_alive); |
| 163 EXPECT_FALSE(different_named_service_alive); |
| 164 } |
| 165 |
| 166 // Named services with an empty name are treated as anonymous services. |
| 167 // Developers shouldn't do that, though, and will trigger an error log. |
| 168 TEST_F(ArcServiceManagerTest, EmptyNamedServices) { |
| 169 bool empty_named_service_alive = false; |
| 170 |
| 171 auto manager = base::MakeUnique<ArcServiceManager>(nullptr); |
| 172 |
| 173 EXPECT_TRUE(manager->AddService(base::MakeUnique<EmptyNamedService>( |
| 174 arc_bridge_service(), &empty_named_service_alive))); |
| 175 EXPECT_TRUE(empty_named_service_alive); |
| 176 EXPECT_EQ(nullptr, manager->GetService<EmptyNamedService>()); |
| 177 |
| 178 manager.reset(); |
| 179 EXPECT_FALSE(empty_named_service_alive); |
| 180 } |
| 181 |
| 182 } // namespace arc |
OLD | NEW |