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 
21 using ::bluetooth::os::Thread;
22 
23 namespace bluetooth {
24 namespace {
25 
26 class ModuleTest : public ::testing::Test {
27  protected:
28   void SetUp() override {
29     thread_ = new Thread("test_thread", Thread::Priority::NORMAL);
30     registry_ = new ModuleRegistry();
31   }
32 
33   void TearDown() override {
34     delete registry_;
35     delete thread_;
36   }
37 
38   ModuleRegistry* registry_;
39   Thread* thread_;
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 
50   void Start() override {
51     // A module is not considered started until Start() finishes
52     EXPECT_FALSE(GetModuleRegistry()->IsStarted<TestModuleNoDependency>());
53   }
54 
55   void Stop() override {
56     // A module is not considered stopped until after Stop() finishes
57     EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleNoDependency>());
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 
74   void Start() override {
75     EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleNoDependency>());
76 
77     // A module is not considered started until Start() finishes
78     EXPECT_FALSE(GetModuleRegistry()->IsStarted<TestModuleOneDependency>());
79   }
80 
81   void Stop() override {
82     EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleNoDependency>());
83 
84     // A module is not considered stopped until after Stop() finishes
85     EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleOneDependency>());
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 
102   void Start() override {
103     // A module is not considered started until Start() finishes
104     EXPECT_FALSE(GetModuleRegistry()->IsStarted<TestModuleNoDependencyTwo>());
105   }
106 
107   void Stop() override {
108     // A module is not considered stopped until after Stop() finishes
109     EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleNoDependencyTwo>());
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 
127   void Start() override {
128     EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleOneDependency>());
129     EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleNoDependencyTwo>());
130 
131     // A module is not considered started until Start() finishes
132     EXPECT_FALSE(GetModuleRegistry()->IsStarted<TestModuleTwoDependencies>());
133   }
134 
135   void Stop() override {
136     EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleOneDependency>());
137     EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleNoDependencyTwo>());
138 
139     // A module is not considered stopped until after Stop() finishes
140     EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleTwoDependencies>());
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>();
151   registry_->Start(&list, thread_);
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>();
169   registry_->Start(&list, thread_);
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>();
187   registry_->Start(&list, thread_);
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
204