blob: d0efb5db5a577e91d8e7763c38482eb8dbb3c020 [file] [log] [blame]
Zach Johnson429c94b2019-04-25 22:24:54 -07001/*
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 Johnson7ea20682019-04-29 14:48:42 -070021using ::bluetooth::os::Thread;
22
Zach Johnson429c94b2019-04-25 22:24:54 -070023namespace bluetooth {
24namespace {
25
26class ModuleTest : public ::testing::Test {
27 protected:
28 void SetUp() override {
Zach Johnson7ea20682019-04-29 14:48:42 -070029 thread_ = new Thread("test_thread", Thread::Priority::NORMAL);
Zach Johnson429c94b2019-04-25 22:24:54 -070030 registry_ = new ModuleRegistry();
31 }
32
33 void TearDown() override {
34 delete registry_;
Zach Johnson7ea20682019-04-29 14:48:42 -070035 delete thread_;
Zach Johnson429c94b2019-04-25 22:24:54 -070036 }
37
38 ModuleRegistry* registry_;
Zach Johnson7ea20682019-04-29 14:48:42 -070039 Thread* thread_;
Zach Johnson429c94b2019-04-25 22:24:54 -070040};
41
42class TestModuleNoDependency : public Module {
43 public:
44 static const ModuleFactory Factory;
45
46 protected:
47 void ListDependencies(ModuleList* list) override {
48 }
49
Zach Johnson84b448b2019-04-29 15:34:55 -070050 void Start() override {
Zach Johnson429c94b2019-04-25 22:24:54 -070051 // A module is not considered started until Start() finishes
Zach Johnson84b448b2019-04-29 15:34:55 -070052 EXPECT_FALSE(GetModuleRegistry()->IsStarted<TestModuleNoDependency>());
Zach Johnson429c94b2019-04-25 22:24:54 -070053 }
54
Zach Johnson84b448b2019-04-29 15:34:55 -070055 void Stop() override {
Zach Johnson429c94b2019-04-25 22:24:54 -070056 // A module is not considered stopped until after Stop() finishes
Zach Johnson84b448b2019-04-29 15:34:55 -070057 EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleNoDependency>());
Zach Johnson429c94b2019-04-25 22:24:54 -070058 }
59};
60
61const ModuleFactory TestModuleNoDependency::Factory = ModuleFactory([]() {
62 return new TestModuleNoDependency();
63});
64
65class 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 Johnson84b448b2019-04-29 15:34:55 -070074 void Start() override {
75 EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleNoDependency>());
Zach Johnson429c94b2019-04-25 22:24:54 -070076
77 // A module is not considered started until Start() finishes
Zach Johnson84b448b2019-04-29 15:34:55 -070078 EXPECT_FALSE(GetModuleRegistry()->IsStarted<TestModuleOneDependency>());
Zach Johnson429c94b2019-04-25 22:24:54 -070079 }
80
Zach Johnson84b448b2019-04-29 15:34:55 -070081 void Stop() override {
82 EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleNoDependency>());
Zach Johnson429c94b2019-04-25 22:24:54 -070083
84 // A module is not considered stopped until after Stop() finishes
Zach Johnson84b448b2019-04-29 15:34:55 -070085 EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleOneDependency>());
Zach Johnson429c94b2019-04-25 22:24:54 -070086 }
87};
88
89const ModuleFactory TestModuleOneDependency::Factory = ModuleFactory([]() {
90 return new TestModuleOneDependency();
91});
92
93
94class TestModuleNoDependencyTwo : public Module {
95 public:
96 static const ModuleFactory Factory;
97
98 protected:
99 void ListDependencies(ModuleList* list) override {
100 }
101
Zach Johnson84b448b2019-04-29 15:34:55 -0700102 void Start() override {
Zach Johnson429c94b2019-04-25 22:24:54 -0700103 // A module is not considered started until Start() finishes
Zach Johnson84b448b2019-04-29 15:34:55 -0700104 EXPECT_FALSE(GetModuleRegistry()->IsStarted<TestModuleNoDependencyTwo>());
Zach Johnson429c94b2019-04-25 22:24:54 -0700105 }
106
Zach Johnson84b448b2019-04-29 15:34:55 -0700107 void Stop() override {
Zach Johnson429c94b2019-04-25 22:24:54 -0700108 // A module is not considered stopped until after Stop() finishes
Zach Johnson84b448b2019-04-29 15:34:55 -0700109 EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleNoDependencyTwo>());
Zach Johnson429c94b2019-04-25 22:24:54 -0700110 }
111};
112
113const ModuleFactory TestModuleNoDependencyTwo::Factory = ModuleFactory([]() {
114 return new TestModuleNoDependencyTwo();
115});
116
117class 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 Johnson84b448b2019-04-29 15:34:55 -0700127 void Start() override {
128 EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleOneDependency>());
129 EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleNoDependencyTwo>());
Zach Johnson429c94b2019-04-25 22:24:54 -0700130
131 // A module is not considered started until Start() finishes
Zach Johnson84b448b2019-04-29 15:34:55 -0700132 EXPECT_FALSE(GetModuleRegistry()->IsStarted<TestModuleTwoDependencies>());
Zach Johnson429c94b2019-04-25 22:24:54 -0700133 }
134
Zach Johnson84b448b2019-04-29 15:34:55 -0700135 void Stop() override {
136 EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleOneDependency>());
137 EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleNoDependencyTwo>());
Zach Johnson429c94b2019-04-25 22:24:54 -0700138
139 // A module is not considered stopped until after Stop() finishes
Zach Johnson84b448b2019-04-29 15:34:55 -0700140 EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleTwoDependencies>());
Zach Johnson429c94b2019-04-25 22:24:54 -0700141 }
142};
143
144const ModuleFactory TestModuleTwoDependencies::Factory = ModuleFactory([]() {
145 return new TestModuleTwoDependencies();
146});
147
148TEST_F(ModuleTest, no_dependency) {
149 ModuleList list;
150 list.add<TestModuleNoDependency>();
Zach Johnson7ea20682019-04-29 14:48:42 -0700151 registry_->Start(&list, thread_);
Zach Johnson429c94b2019-04-25 22:24:54 -0700152
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
166TEST_F(ModuleTest, one_dependency) {
167 ModuleList list;
168 list.add<TestModuleOneDependency>();
Zach Johnson7ea20682019-04-29 14:48:42 -0700169 registry_->Start(&list, thread_);
Zach Johnson429c94b2019-04-25 22:24:54 -0700170
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
184TEST_F(ModuleTest, two_dependencies) {
185 ModuleList list;
186 list.add<TestModuleTwoDependencies>();
Zach Johnson7ea20682019-04-29 14:48:42 -0700187 registry_->Start(&list, thread_);
Zach Johnson429c94b2019-04-25 22:24:54 -0700188
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