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 #include "module_unittest_generated.h"
19 #include "os/handler.h"
20 #include "os/thread.h"
21
22 #include "gtest/gtest.h"
23
24 #include <functional>
25 #include <future>
26 #include <string>
27
28 using ::bluetooth::os::Thread;
29
30 namespace bluetooth {
31 namespace {
32
33 class ModuleTest : public ::testing::Test {
34 protected:
SetUp()35 void SetUp() override {
36 thread_ = new Thread("test_thread", Thread::Priority::NORMAL);
37 registry_ = new ModuleRegistry();
38 }
39
TearDown()40 void TearDown() override {
41 delete registry_;
42 delete thread_;
43 }
44
45 ModuleRegistry* registry_;
46 Thread* thread_;
47 };
48
49 os::Handler* test_module_no_dependency_handler = nullptr;
50
51 class TestModuleNoDependency : public Module {
52 public:
53 static const ModuleFactory Factory;
54
55 protected:
ListDependencies(ModuleList * list)56 void ListDependencies(ModuleList* list) override {
57 }
58
Start()59 void Start() override {
60 // A module is not considered started until Start() finishes
61 EXPECT_FALSE(GetModuleRegistry()->IsStarted<TestModuleNoDependency>());
62 test_module_no_dependency_handler = GetHandler();
63 }
64
Stop()65 void Stop() override {
66 // A module is not considered stopped until after Stop() finishes
67 EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleNoDependency>());
68 }
69
ToString() const70 std::string ToString() const override {
71 return std::string("TestModuleNoDependency");
72 }
73 };
74
__anon37d08c970202() 75 const ModuleFactory TestModuleNoDependency::Factory = ModuleFactory([]() {
76 return new TestModuleNoDependency();
77 });
78
79 os::Handler* test_module_one_dependency_handler = nullptr;
80
81 class TestModuleOneDependency : public Module {
82 public:
83 static const ModuleFactory Factory;
84
85 protected:
ListDependencies(ModuleList * list)86 void ListDependencies(ModuleList* list) override {
87 list->add<TestModuleNoDependency>();
88 }
89
Start()90 void Start() override {
91 EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleNoDependency>());
92
93 // A module is not considered started until Start() finishes
94 EXPECT_FALSE(GetModuleRegistry()->IsStarted<TestModuleOneDependency>());
95 test_module_one_dependency_handler = GetHandler();
96 }
97
Stop()98 void Stop() override {
99 EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleNoDependency>());
100
101 // A module is not considered stopped until after Stop() finishes
102 EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleOneDependency>());
103 }
104
ToString() const105 std::string ToString() const override {
106 return std::string("TestModuleOneDependency");
107 }
108 };
109
__anon37d08c970302() 110 const ModuleFactory TestModuleOneDependency::Factory = ModuleFactory([]() {
111 return new TestModuleOneDependency();
112 });
113
114
115 class TestModuleNoDependencyTwo : public Module {
116 public:
117 static const ModuleFactory Factory;
118
119 protected:
ListDependencies(ModuleList * list)120 void ListDependencies(ModuleList* list) override {
121 }
122
Start()123 void Start() override {
124 // A module is not considered started until Start() finishes
125 EXPECT_FALSE(GetModuleRegistry()->IsStarted<TestModuleNoDependencyTwo>());
126 }
127
Stop()128 void Stop() override {
129 // A module is not considered stopped until after Stop() finishes
130 EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleNoDependencyTwo>());
131 }
132
ToString() const133 std::string ToString() const override {
134 return std::string("TestModuleNoDependencyTwo");
135 }
136 };
137
__anon37d08c970402() 138 const ModuleFactory TestModuleNoDependencyTwo::Factory = ModuleFactory([]() {
139 return new TestModuleNoDependencyTwo();
140 });
141
142 class TestModuleTwoDependencies : public Module {
143 public:
144 static const ModuleFactory Factory;
145
146 protected:
ListDependencies(ModuleList * list)147 void ListDependencies(ModuleList* list) override {
148 list->add<TestModuleOneDependency>();
149 list->add<TestModuleNoDependencyTwo>();
150 }
151
Start()152 void Start() override {
153 EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleOneDependency>());
154 EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleNoDependencyTwo>());
155
156 // A module is not considered started until Start() finishes
157 EXPECT_FALSE(GetModuleRegistry()->IsStarted<TestModuleTwoDependencies>());
158 }
159
Stop()160 void Stop() override {
161 EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleOneDependency>());
162 EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleNoDependencyTwo>());
163
164 // A module is not considered stopped until after Stop() finishes
165 EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleTwoDependencies>());
166 }
167
ToString() const168 std::string ToString() const override {
169 return std::string("TestModuleTwoDependencies");
170 }
171 };
172
__anon37d08c970502() 173 const ModuleFactory TestModuleTwoDependencies::Factory = ModuleFactory([]() {
174 return new TestModuleTwoDependencies();
175 });
176
177 // To generate module unittest flatbuffer headers:
178 // $ flatc --cpp module_unittest.fbs
179 class TestModuleDumpState : public Module {
180 public:
181 static const ModuleFactory Factory;
182
183 std::string test_string_{"Initial Test String"};
184
185 protected:
ListDependencies(ModuleList * list)186 void ListDependencies(ModuleList* list) override {
187 list->add<TestModuleNoDependency>();
188 }
189
Start()190 void Start() override {
191 EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleNoDependency>());
192
193 // A module is not considered started until Start() finishes
194 EXPECT_FALSE(GetModuleRegistry()->IsStarted<TestModuleDumpState>());
195 test_module_one_dependency_handler = GetHandler();
196 }
197
Stop()198 void Stop() override {
199 EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleNoDependency>());
200
201 // A module is not considered stopped until after Stop() finishes
202 EXPECT_TRUE(GetModuleRegistry()->IsStarted<TestModuleDumpState>());
203 }
204
ToString() const205 std::string ToString() const override {
206 return std::string("TestModuleDumpState");
207 }
208
GetDumpsysData(flatbuffers::FlatBufferBuilder * fb_builder) const209 DumpsysDataFinisher GetDumpsysData(flatbuffers::FlatBufferBuilder* fb_builder) const override {
210 auto string = fb_builder->CreateString(test_string_.c_str());
211
212 auto builder = ModuleUnitTestDataBuilder(*fb_builder);
213 builder.add_title(string);
214 auto table = builder.Finish();
215
216 return [table](DumpsysDataBuilder* builder) { builder->add_module_unittest_data(table); };
217 }
218 };
219
__anon37d08c970702() 220 const ModuleFactory TestModuleDumpState::Factory = ModuleFactory([]() { return new TestModuleDumpState(); });
221
TEST_F(ModuleTest,no_dependency)222 TEST_F(ModuleTest, no_dependency) {
223 ModuleList list;
224 list.add<TestModuleNoDependency>();
225 registry_->Start(&list, thread_);
226
227 EXPECT_TRUE(registry_->IsStarted<TestModuleNoDependency>());
228 EXPECT_FALSE(registry_->IsStarted<TestModuleOneDependency>());
229 EXPECT_FALSE(registry_->IsStarted<TestModuleNoDependencyTwo>());
230 EXPECT_FALSE(registry_->IsStarted<TestModuleTwoDependencies>());
231
232 registry_->StopAll();
233
234 EXPECT_FALSE(registry_->IsStarted<TestModuleNoDependency>());
235 EXPECT_FALSE(registry_->IsStarted<TestModuleOneDependency>());
236 EXPECT_FALSE(registry_->IsStarted<TestModuleNoDependencyTwo>());
237 EXPECT_FALSE(registry_->IsStarted<TestModuleTwoDependencies>());
238 }
239
TEST_F(ModuleTest,one_dependency)240 TEST_F(ModuleTest, one_dependency) {
241 ModuleList list;
242 list.add<TestModuleOneDependency>();
243 registry_->Start(&list, thread_);
244
245 EXPECT_TRUE(registry_->IsStarted<TestModuleNoDependency>());
246 EXPECT_TRUE(registry_->IsStarted<TestModuleOneDependency>());
247 EXPECT_FALSE(registry_->IsStarted<TestModuleNoDependencyTwo>());
248 EXPECT_FALSE(registry_->IsStarted<TestModuleTwoDependencies>());
249
250 registry_->StopAll();
251
252 EXPECT_FALSE(registry_->IsStarted<TestModuleNoDependency>());
253 EXPECT_FALSE(registry_->IsStarted<TestModuleOneDependency>());
254 EXPECT_FALSE(registry_->IsStarted<TestModuleNoDependencyTwo>());
255 EXPECT_FALSE(registry_->IsStarted<TestModuleTwoDependencies>());
256 }
257
TEST_F(ModuleTest,two_dependencies)258 TEST_F(ModuleTest, two_dependencies) {
259 ModuleList list;
260 list.add<TestModuleTwoDependencies>();
261 registry_->Start(&list, thread_);
262
263 EXPECT_TRUE(registry_->IsStarted<TestModuleNoDependency>());
264 EXPECT_TRUE(registry_->IsStarted<TestModuleOneDependency>());
265 EXPECT_TRUE(registry_->IsStarted<TestModuleNoDependencyTwo>());
266 EXPECT_TRUE(registry_->IsStarted<TestModuleTwoDependencies>());
267
268 registry_->StopAll();
269
270 EXPECT_FALSE(registry_->IsStarted<TestModuleNoDependency>());
271 EXPECT_FALSE(registry_->IsStarted<TestModuleOneDependency>());
272 EXPECT_FALSE(registry_->IsStarted<TestModuleNoDependencyTwo>());
273 EXPECT_FALSE(registry_->IsStarted<TestModuleTwoDependencies>());
274 }
275
post_to_module_one_handler()276 void post_to_module_one_handler() {
277 std::this_thread::sleep_for(std::chrono::milliseconds(100));
278 test_module_one_dependency_handler->Post(common::BindOnce([] { FAIL(); }));
279 }
280
TEST_F(ModuleTest,shutdown_with_unhandled_callback)281 TEST_F(ModuleTest, shutdown_with_unhandled_callback) {
282 ModuleList list;
283 list.add<TestModuleOneDependency>();
284 registry_->Start(&list, thread_);
285 test_module_no_dependency_handler->Post(common::BindOnce(&post_to_module_one_handler));
286 registry_->StopAll();
287 }
288
TEST_F(ModuleTest,dump_state)289 TEST_F(ModuleTest, dump_state) {
290 static const char* title = "Test Dump Title";
291 ModuleList list;
292 list.add<TestModuleDumpState>();
293 registry_->Start(&list, thread_);
294
295 ModuleDumper dumper(*registry_, title);
296
297 std::string output;
298 dumper.DumpState(&output);
299
300 auto data = flatbuffers::GetRoot<DumpsysData>(output.data());
301 EXPECT_STREQ(title, data->title()->c_str());
302
303 auto test_data = data->module_unittest_data();
304 EXPECT_STREQ("Initial Test String", test_data->title()->c_str());
305
306 TestModuleDumpState* test_module =
307 static_cast<TestModuleDumpState*>(registry_->Start(&TestModuleDumpState::Factory, nullptr));
308 test_module->test_string_ = "A Second Test String";
309
310 dumper.DumpState(&output);
311
312 data = flatbuffers::GetRoot<DumpsysData>(output.data());
313 test_data = data->module_unittest_data();
314 EXPECT_STREQ("A Second Test String", test_data->title()->c_str());
315
316 registry_->StopAll();
317 }
318
319 } // namespace
320 } // namespace bluetooth
321