Zach Johnson | 429c94b | 2019-04-25 22:24:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "module.h" |
| 18 | |
| 19 | #include "gtest/gtest.h" |
| 20 | |
Zach Johnson | 7ea2068 | 2019-04-29 14:48:42 -0700 | [diff] [blame] | 21 | using ::bluetooth::os::Thread; |
| 22 | |
Zach Johnson | 429c94b | 2019-04-25 22:24:54 -0700 | [diff] [blame] | 23 | namespace bluetooth { |
| 24 | namespace { |
| 25 | |
| 26 | class ModuleTest : public ::testing::Test { |
| 27 | protected: |
| 28 | void SetUp() override { |
Zach Johnson | 7ea2068 | 2019-04-29 14:48:42 -0700 | [diff] [blame] | 29 | thread_ = new Thread("test_thread", Thread::Priority::NORMAL); |
Zach Johnson | 429c94b | 2019-04-25 22:24:54 -0700 | [diff] [blame] | 30 | registry_ = new ModuleRegistry(); |
| 31 | } |
| 32 | |
| 33 | void TearDown() override { |
| 34 | delete registry_; |
Zach Johnson | 7ea2068 | 2019-04-29 14:48:42 -0700 | [diff] [blame] | 35 | delete thread_; |
Zach Johnson | 429c94b | 2019-04-25 22:24:54 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | ModuleRegistry* registry_; |
Zach Johnson | 7ea2068 | 2019-04-29 14:48:42 -0700 | [diff] [blame] | 39 | Thread* thread_; |
Zach Johnson | 429c94b | 2019-04-25 22:24:54 -0700 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | class TestModuleNoDependency : public Module { |
| 43 | public: |
| 44 | static const ModuleFactory Factory; |
| 45 | |
| 46 | protected: |
| 47 | void ListDependencies(ModuleList* list) override { |
| 48 | } |
| 49 | |
Zach Johnson | 84b448b | 2019-04-29 15:34:55 -0700 | [diff] [blame] | 50 | void Start() override { |
Zach Johnson | 429c94b | 2019-04-25 22:24:54 -0700 | [diff] [blame] | 51 | // A module is not considered started until Start() finishes |
Zach Johnson | 84b448b | 2019-04-29 15:34:55 -0700 | [diff] [blame] | 52 | EXPECT_FALSE(GetModuleRegistry()->IsStarted<TestModuleNoDependency>()); |
Zach Johnson | 429c94b | 2019-04-25 22:24:54 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Zach Johnson | 84b448b | 2019-04-29 15:34:55 -0700 | [diff] [blame] | 55 | void Stop() override { |
Zach Johnson | 429c94b | 2019-04-25 22:24:54 -0700 | [diff] [blame] | 56 | // A module is not considered stopped until after Stop() finishes |
Zach Johnson | 84b448b | 2019-04-29 15:34:55 -0700 | [diff] [blame] | 57 | EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleNoDependency>()); |
Zach Johnson | 429c94b | 2019-04-25 22:24:54 -0700 | [diff] [blame] | 58 | } |
| 59 | }; |
| 60 | |
| 61 | const ModuleFactory TestModuleNoDependency::Factory = ModuleFactory([]() { |
| 62 | return new TestModuleNoDependency(); |
| 63 | }); |
| 64 | |
| 65 | class TestModuleOneDependency : public Module { |
| 66 | public: |
| 67 | static const ModuleFactory Factory; |
| 68 | |
| 69 | protected: |
| 70 | void ListDependencies(ModuleList* list) override { |
| 71 | list->add<TestModuleNoDependency>(); |
| 72 | } |
| 73 | |
Zach Johnson | 84b448b | 2019-04-29 15:34:55 -0700 | [diff] [blame] | 74 | void Start() override { |
| 75 | EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleNoDependency>()); |
Zach Johnson | 429c94b | 2019-04-25 22:24:54 -0700 | [diff] [blame] | 76 | |
| 77 | // A module is not considered started until Start() finishes |
Zach Johnson | 84b448b | 2019-04-29 15:34:55 -0700 | [diff] [blame] | 78 | EXPECT_FALSE(GetModuleRegistry()->IsStarted<TestModuleOneDependency>()); |
Zach Johnson | 429c94b | 2019-04-25 22:24:54 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Zach Johnson | 84b448b | 2019-04-29 15:34:55 -0700 | [diff] [blame] | 81 | void Stop() override { |
| 82 | EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleNoDependency>()); |
Zach Johnson | 429c94b | 2019-04-25 22:24:54 -0700 | [diff] [blame] | 83 | |
| 84 | // A module is not considered stopped until after Stop() finishes |
Zach Johnson | 84b448b | 2019-04-29 15:34:55 -0700 | [diff] [blame] | 85 | EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleOneDependency>()); |
Zach Johnson | 429c94b | 2019-04-25 22:24:54 -0700 | [diff] [blame] | 86 | } |
| 87 | }; |
| 88 | |
| 89 | const ModuleFactory TestModuleOneDependency::Factory = ModuleFactory([]() { |
| 90 | return new TestModuleOneDependency(); |
| 91 | }); |
| 92 | |
| 93 | |
| 94 | class TestModuleNoDependencyTwo : public Module { |
| 95 | public: |
| 96 | static const ModuleFactory Factory; |
| 97 | |
| 98 | protected: |
| 99 | void ListDependencies(ModuleList* list) override { |
| 100 | } |
| 101 | |
Zach Johnson | 84b448b | 2019-04-29 15:34:55 -0700 | [diff] [blame] | 102 | void Start() override { |
Zach Johnson | 429c94b | 2019-04-25 22:24:54 -0700 | [diff] [blame] | 103 | // A module is not considered started until Start() finishes |
Zach Johnson | 84b448b | 2019-04-29 15:34:55 -0700 | [diff] [blame] | 104 | EXPECT_FALSE(GetModuleRegistry()->IsStarted<TestModuleNoDependencyTwo>()); |
Zach Johnson | 429c94b | 2019-04-25 22:24:54 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Zach Johnson | 84b448b | 2019-04-29 15:34:55 -0700 | [diff] [blame] | 107 | void Stop() override { |
Zach Johnson | 429c94b | 2019-04-25 22:24:54 -0700 | [diff] [blame] | 108 | // A module is not considered stopped until after Stop() finishes |
Zach Johnson | 84b448b | 2019-04-29 15:34:55 -0700 | [diff] [blame] | 109 | EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleNoDependencyTwo>()); |
Zach Johnson | 429c94b | 2019-04-25 22:24:54 -0700 | [diff] [blame] | 110 | } |
| 111 | }; |
| 112 | |
| 113 | const ModuleFactory TestModuleNoDependencyTwo::Factory = ModuleFactory([]() { |
| 114 | return new TestModuleNoDependencyTwo(); |
| 115 | }); |
| 116 | |
| 117 | class TestModuleTwoDependencies : public Module { |
| 118 | public: |
| 119 | static const ModuleFactory Factory; |
| 120 | |
| 121 | protected: |
| 122 | void ListDependencies(ModuleList* list) override { |
| 123 | list->add<TestModuleOneDependency>(); |
| 124 | list->add<TestModuleNoDependencyTwo>(); |
| 125 | } |
| 126 | |
Zach Johnson | 84b448b | 2019-04-29 15:34:55 -0700 | [diff] [blame] | 127 | void Start() override { |
| 128 | EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleOneDependency>()); |
| 129 | EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleNoDependencyTwo>()); |
Zach Johnson | 429c94b | 2019-04-25 22:24:54 -0700 | [diff] [blame] | 130 | |
| 131 | // A module is not considered started until Start() finishes |
Zach Johnson | 84b448b | 2019-04-29 15:34:55 -0700 | [diff] [blame] | 132 | EXPECT_FALSE(GetModuleRegistry()->IsStarted<TestModuleTwoDependencies>()); |
Zach Johnson | 429c94b | 2019-04-25 22:24:54 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Zach Johnson | 84b448b | 2019-04-29 15:34:55 -0700 | [diff] [blame] | 135 | void Stop() override { |
| 136 | EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleOneDependency>()); |
| 137 | EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleNoDependencyTwo>()); |
Zach Johnson | 429c94b | 2019-04-25 22:24:54 -0700 | [diff] [blame] | 138 | |
| 139 | // A module is not considered stopped until after Stop() finishes |
Zach Johnson | 84b448b | 2019-04-29 15:34:55 -0700 | [diff] [blame] | 140 | EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleTwoDependencies>()); |
Zach Johnson | 429c94b | 2019-04-25 22:24:54 -0700 | [diff] [blame] | 141 | } |
| 142 | }; |
| 143 | |
| 144 | const ModuleFactory TestModuleTwoDependencies::Factory = ModuleFactory([]() { |
| 145 | return new TestModuleTwoDependencies(); |
| 146 | }); |
| 147 | |
| 148 | TEST_F(ModuleTest, no_dependency) { |
| 149 | ModuleList list; |
| 150 | list.add<TestModuleNoDependency>(); |
Zach Johnson | 7ea2068 | 2019-04-29 14:48:42 -0700 | [diff] [blame] | 151 | registry_->Start(&list, thread_); |
Zach Johnson | 429c94b | 2019-04-25 22:24:54 -0700 | [diff] [blame] | 152 | |
| 153 | EXPECT_TRUE(registry_->IsStarted<TestModuleNoDependency>()); |
| 154 | EXPECT_FALSE(registry_->IsStarted<TestModuleOneDependency>()); |
| 155 | EXPECT_FALSE(registry_->IsStarted<TestModuleNoDependencyTwo>()); |
| 156 | EXPECT_FALSE(registry_->IsStarted<TestModuleTwoDependencies>()); |
| 157 | |
| 158 | registry_->StopAll(); |
| 159 | |
| 160 | EXPECT_FALSE(registry_->IsStarted<TestModuleNoDependency>()); |
| 161 | EXPECT_FALSE(registry_->IsStarted<TestModuleOneDependency>()); |
| 162 | EXPECT_FALSE(registry_->IsStarted<TestModuleNoDependencyTwo>()); |
| 163 | EXPECT_FALSE(registry_->IsStarted<TestModuleTwoDependencies>()); |
| 164 | } |
| 165 | |
| 166 | TEST_F(ModuleTest, one_dependency) { |
| 167 | ModuleList list; |
| 168 | list.add<TestModuleOneDependency>(); |
Zach Johnson | 7ea2068 | 2019-04-29 14:48:42 -0700 | [diff] [blame] | 169 | registry_->Start(&list, thread_); |
Zach Johnson | 429c94b | 2019-04-25 22:24:54 -0700 | [diff] [blame] | 170 | |
| 171 | EXPECT_TRUE(registry_->IsStarted<TestModuleNoDependency>()); |
| 172 | EXPECT_TRUE(registry_->IsStarted<TestModuleOneDependency>()); |
| 173 | EXPECT_FALSE(registry_->IsStarted<TestModuleNoDependencyTwo>()); |
| 174 | EXPECT_FALSE(registry_->IsStarted<TestModuleTwoDependencies>()); |
| 175 | |
| 176 | registry_->StopAll(); |
| 177 | |
| 178 | EXPECT_FALSE(registry_->IsStarted<TestModuleNoDependency>()); |
| 179 | EXPECT_FALSE(registry_->IsStarted<TestModuleOneDependency>()); |
| 180 | EXPECT_FALSE(registry_->IsStarted<TestModuleNoDependencyTwo>()); |
| 181 | EXPECT_FALSE(registry_->IsStarted<TestModuleTwoDependencies>()); |
| 182 | } |
| 183 | |
| 184 | TEST_F(ModuleTest, two_dependencies) { |
| 185 | ModuleList list; |
| 186 | list.add<TestModuleTwoDependencies>(); |
Zach Johnson | 7ea2068 | 2019-04-29 14:48:42 -0700 | [diff] [blame] | 187 | registry_->Start(&list, thread_); |
Zach Johnson | 429c94b | 2019-04-25 22:24:54 -0700 | [diff] [blame] | 188 | |
| 189 | EXPECT_TRUE(registry_->IsStarted<TestModuleNoDependency>()); |
| 190 | EXPECT_TRUE(registry_->IsStarted<TestModuleOneDependency>()); |
| 191 | EXPECT_TRUE(registry_->IsStarted<TestModuleNoDependencyTwo>()); |
| 192 | EXPECT_TRUE(registry_->IsStarted<TestModuleTwoDependencies>()); |
| 193 | |
| 194 | registry_->StopAll(); |
| 195 | |
| 196 | EXPECT_FALSE(registry_->IsStarted<TestModuleNoDependency>()); |
| 197 | EXPECT_FALSE(registry_->IsStarted<TestModuleOneDependency>()); |
| 198 | EXPECT_FALSE(registry_->IsStarted<TestModuleNoDependencyTwo>()); |
| 199 | EXPECT_FALSE(registry_->IsStarted<TestModuleTwoDependencies>()); |
| 200 | } |
| 201 | |
| 202 | } // namespace |
| 203 | } // namespace bluetooth |