From 30fa258cf8feb752f10257db02de477bc21341ff Mon Sep 17 00:00:00 2001 From: Hansong Zhang Date: Tue, 7 May 2019 16:40:45 -0700 Subject: [PATCH] ModuleRegistry: Start() injected modules Call Start() on injected modules, and set registry_ and handler_ properly Test: atest --host bluetooth_test_gd Change-Id: Id655d537982eab285549ff797a907dabb15ca64d --- gd/module.cc | 9 +++++++++ gd/module.h | 25 +++++++++++-------------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/gd/module.cc b/gd/module.cc index 10ab72d0e..7f2dd32a3 100644 --- a/gd/module.cc +++ b/gd/module.cc @@ -94,4 +94,13 @@ 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 diff --git a/gd/module.h b/gd/module.h index 7df63341a..33e032561 100644 --- a/gd/module.h +++ b/gd/module.h @@ -115,23 +115,20 @@ class ModuleRegistry { // Stop all running modules in reverse order of start void StopAll(); - protected: - Module* Get(const ModuleFactory* module) const; + // 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); - std::map started_modules_; - std::vector start_order_; -}; - -class TestModuleRegistry : public ModuleRegistry { - public: - void InjectTestModule(const ModuleFactory* module, Module* instance) { - start_order_.push_back(module); - started_modules_[module] = instance; + // Helper for dependency injection in test code. DO NOT USE in prod code! + template + T* get_module_under_test() const { + return static_cast(Get(&T::Factory)); } - Module* GetModuleUnderTest(const ModuleFactory* module) const { - return Get(module); - } + private: + Module* Get(const ModuleFactory* module) const; + std::map started_modules_; + std::vector start_order_; }; } // namespace bluetooth -- 2.11.0