Loading system/gd/hci/hci_layer_test.cc +5 −8 Original line number Diff line number Diff line Loading @@ -173,18 +173,16 @@ class HciTest : public ::testing::Test { counting_bytes.push_back(i); counting_down_bytes.push_back(~i); } thread_ = new Thread("test_thread", Thread::Priority::NORMAL); hal = new TestHciHal(); fake_registry_.inject_test_module(&hal::HciHal::Factory, hal, thread_); fake_registry_.Start<DependsOnHci>(thread_); hci = fake_registry_.get_module_under_test<HciLayer>(); upper = fake_registry_.get_module_under_test<DependsOnHci>(); fake_registry_.InjectTestModule(&hal::HciHal::Factory, hal); fake_registry_.StartTestModule<DependsOnHci>(); hci = static_cast<HciLayer*>(fake_registry_.GetModuleUnderTest(&HciLayer::Factory)); upper = static_cast<DependsOnHci*>(fake_registry_.GetModuleUnderTest(&DependsOnHci::Factory)); ASSERT(fake_registry_.IsStarted<HciLayer>()); } void TearDown() override { fake_registry_.StopAll(); delete thread_; } std::vector<uint8_t> GetPacketBytes(std::unique_ptr<packet::BasePacketBuilder> packet) { Loading @@ -198,8 +196,7 @@ class HciTest : public ::testing::Test { DependsOnHci* upper = nullptr; TestHciHal* hal = nullptr; HciLayer* hci = nullptr; ModuleRegistry fake_registry_; Thread* thread_ = nullptr; TestModuleRegistry fake_registry_; }; TEST_F(HciTest, initAndClose) {} Loading system/gd/module.cc +0 −9 Original line number Diff line number Diff line Loading @@ -94,13 +94,4 @@ void ModuleRegistry::StopAll() { ASSERT(started_modules_.empty()); start_order_.clear(); } void ModuleRegistry::inject_test_module(const ModuleFactory* module, Module* instance, os::Thread* thread) { instance->registry_ = this; instance->handler_ = new Handler(thread); start_order_.push_back(module); started_modules_[module] = instance; instance->Start(); } } // namespace bluetooth system/gd/module.h +28 −10 Original line number Diff line number Diff line Loading @@ -115,20 +115,38 @@ class ModuleRegistry { // Stop all running modules in reverse order of start void StopAll(); // Helper for dependency injection in test code. DO NOT USE in prod code! // Ownership of |instance| is transferred to the registry. void inject_test_module(const ModuleFactory* module, Module* instance, os::Thread* thread); protected: Module* Get(const ModuleFactory* module) const; std::map<const ModuleFactory*, Module*> started_modules_; std::vector<const ModuleFactory*> start_order_; }; class TestModuleRegistry : public ModuleRegistry { public: void InjectTestModule(const ModuleFactory* module, Module* instance) { start_order_.push_back(module); started_modules_[module] = instance; } Module* GetModuleUnderTest(const ModuleFactory* module) const { return Get(module); } os::Handler* GetTestModuleHandler() { return new os::Handler(&test_thread); } os::Thread& GetTestThread() { return test_thread; } // Helper for dependency injection in test code. DO NOT USE in prod code! template <class T> T* get_module_under_test() const { return static_cast<T*>(Get(&T::Factory)); T* StartTestModule() { return Start<T>(&test_thread); } private: Module* Get(const ModuleFactory* module) const; std::map<const ModuleFactory*, Module*> started_modules_; std::vector<const ModuleFactory*> start_order_; os::Thread test_thread{"test_thread", os::Thread::Priority::NORMAL}; }; } // namespace bluetooth Loading
system/gd/hci/hci_layer_test.cc +5 −8 Original line number Diff line number Diff line Loading @@ -173,18 +173,16 @@ class HciTest : public ::testing::Test { counting_bytes.push_back(i); counting_down_bytes.push_back(~i); } thread_ = new Thread("test_thread", Thread::Priority::NORMAL); hal = new TestHciHal(); fake_registry_.inject_test_module(&hal::HciHal::Factory, hal, thread_); fake_registry_.Start<DependsOnHci>(thread_); hci = fake_registry_.get_module_under_test<HciLayer>(); upper = fake_registry_.get_module_under_test<DependsOnHci>(); fake_registry_.InjectTestModule(&hal::HciHal::Factory, hal); fake_registry_.StartTestModule<DependsOnHci>(); hci = static_cast<HciLayer*>(fake_registry_.GetModuleUnderTest(&HciLayer::Factory)); upper = static_cast<DependsOnHci*>(fake_registry_.GetModuleUnderTest(&DependsOnHci::Factory)); ASSERT(fake_registry_.IsStarted<HciLayer>()); } void TearDown() override { fake_registry_.StopAll(); delete thread_; } std::vector<uint8_t> GetPacketBytes(std::unique_ptr<packet::BasePacketBuilder> packet) { Loading @@ -198,8 +196,7 @@ class HciTest : public ::testing::Test { DependsOnHci* upper = nullptr; TestHciHal* hal = nullptr; HciLayer* hci = nullptr; ModuleRegistry fake_registry_; Thread* thread_ = nullptr; TestModuleRegistry fake_registry_; }; TEST_F(HciTest, initAndClose) {} Loading
system/gd/module.cc +0 −9 Original line number Diff line number Diff line Loading @@ -94,13 +94,4 @@ void ModuleRegistry::StopAll() { ASSERT(started_modules_.empty()); start_order_.clear(); } void ModuleRegistry::inject_test_module(const ModuleFactory* module, Module* instance, os::Thread* thread) { instance->registry_ = this; instance->handler_ = new Handler(thread); start_order_.push_back(module); started_modules_[module] = instance; instance->Start(); } } // namespace bluetooth
system/gd/module.h +28 −10 Original line number Diff line number Diff line Loading @@ -115,20 +115,38 @@ class ModuleRegistry { // Stop all running modules in reverse order of start void StopAll(); // Helper for dependency injection in test code. DO NOT USE in prod code! // Ownership of |instance| is transferred to the registry. void inject_test_module(const ModuleFactory* module, Module* instance, os::Thread* thread); protected: Module* Get(const ModuleFactory* module) const; std::map<const ModuleFactory*, Module*> started_modules_; std::vector<const ModuleFactory*> start_order_; }; class TestModuleRegistry : public ModuleRegistry { public: void InjectTestModule(const ModuleFactory* module, Module* instance) { start_order_.push_back(module); started_modules_[module] = instance; } Module* GetModuleUnderTest(const ModuleFactory* module) const { return Get(module); } os::Handler* GetTestModuleHandler() { return new os::Handler(&test_thread); } os::Thread& GetTestThread() { return test_thread; } // Helper for dependency injection in test code. DO NOT USE in prod code! template <class T> T* get_module_under_test() const { return static_cast<T*>(Get(&T::Factory)); T* StartTestModule() { return Start<T>(&test_thread); } private: Module* Get(const ModuleFactory* module) const; std::map<const ModuleFactory*, Module*> started_modules_; std::vector<const ModuleFactory*> start_order_; os::Thread test_thread{"test_thread", os::Thread::Priority::NORMAL}; }; } // namespace bluetooth