diff options
author | Bertrand SIMONNET <bsimonnet@google.com> | 2016-03-28 09:59:36 -0700 |
---|---|---|
committer | Bertrand Simonnet <bsimonnet@google.com> | 2016-03-30 23:09:18 +0000 |
commit | c5d5330637ed208e92b81c80ad12b53ffe4f0721 (patch) | |
tree | 568547222b63728fd4a23e076bfc3a0900cba347 | |
parent | 2abf0833508ecfb74e44c3f00e6b6cdf8081019b (diff) | |
download | peripheralmanager-c5d5330637ed208e92b81c80ad12b53ffe4f0721.tar.gz |
Fix binder mocking in unit tests.
This CL mocks binder correctly in the unit tests, avoiding calling
service manager.
Bug: 27901103
Test: Unit tests pass. Service manager is not called.
Change-Id: Ic4d8d80e47768dcabb69ae970b66bab2e3b1ee8d
-rw-r--r-- | client/Android.mk | 1 | ||||
-rw-r--r-- | client/gpio_unittest.cc | 12 | ||||
-rw-r--r-- | client/i2c_unittest.cc | 10 | ||||
-rw-r--r-- | client/led_unittest.cc | 10 | ||||
-rw-r--r-- | client/peripheral_manager_client_impl.cc | 10 | ||||
-rw-r--r-- | client/spi_unittest.cc | 12 | ||||
-rw-r--r-- | daemon/peripheral_manager.cc | 7 | ||||
-rw-r--r-- | daemon/peripheral_manager.h | 2 |
8 files changed, 36 insertions, 28 deletions
diff --git a/client/Android.mk b/client/Android.mk index 1819b53..5371a98 100644 --- a/client/Android.mk +++ b/client/Android.mk @@ -69,6 +69,7 @@ LOCAL_STATIC_LIBRARIES := libgtest libBionicGtestMain \ LOCAL_SHARED_LIBRARIES := \ $(libperipheralman_CommonSharedLibraries) \ libperipheralman \ + libbinderwrapper_test_support \ LOCAL_SRC_FILES := \ gpio_unittest.cc \ diff --git a/client/gpio_unittest.cc b/client/gpio_unittest.cc index bdcaffd..4b1ada2 100644 --- a/client/gpio_unittest.cc +++ b/client/gpio_unittest.cc @@ -16,6 +16,8 @@ #include <memory> +#include <binderwrapper/binder_test_base.h> +#include <binderwrapper/stub_binder_wrapper.h> #include <gtest/gtest.h> #include "gpio_driver_mock.h" @@ -32,10 +34,13 @@ using android::PeripheralManager; // Base class used to test the Gpio C API. // As we rely on static, global managers, we cannot run this tests in parallel. // Please use -j1 when running theses tests or you may see false negatives. -class GpioTest : public ::testing::Test { +class GpioTest : public android::BinderTestBase { public: void SetUp() { - pman_.InitForTest(); + android::sp<PeripheralManager> pman(new PeripheralManager); + android::String8 interface_desc(pman->getInterfaceDescriptor()); + binder_wrapper()->SetBinderForService(interface_desc.string(), pman); + GpioManager* man = GpioManager::GetGpioManager(); man->RegisterDriver(std::unique_ptr<GpioDriverInfoBase>( new GpioDriverInfo<GpioDriverMock, void*>(nullptr))); @@ -48,9 +53,6 @@ class GpioTest : public ::testing::Test { } void TearDown() { GpioManager::ResetGpioManager(); } - - private: - PeripheralManager pman_; }; // Test that we list the Gpios available correctly. diff --git a/client/i2c_unittest.cc b/client/i2c_unittest.cc index fd04563..c1f6a97 100644 --- a/client/i2c_unittest.cc +++ b/client/i2c_unittest.cc @@ -16,6 +16,8 @@ #include <memory> +#include <binderwrapper/binder_test_base.h> +#include <binderwrapper/stub_binder_wrapper.h> #include <gtest/gtest.h> #include "fake_devices.h" @@ -32,10 +34,13 @@ using android::I2cDriverInfoBase; using android::I2cManager; using android::PeripheralManager; -class I2cTest : public ::testing::Test { +class I2cTest : public android::BinderTestBase { public: void SetUp() { - pman_.InitForTest(); + android::sp<PeripheralManager> pman(new PeripheralManager); + android::String8 interface_desc(pman->getInterfaceDescriptor()); + binder_wrapper()->SetBinderForService(interface_desc.string(), pman); + I2cManager* man = I2cManager::GetI2cManager(); man->RegisterDriver(std::unique_ptr<I2cDriverInfoBase>( new I2cDriverInfo<I2cDriverI2cDev, CharDeviceFactory*>( @@ -48,7 +53,6 @@ class I2cTest : public ::testing::Test { void TearDown() { I2cManager::ResetI2cManager(); } private: - PeripheralManager pman_; FakeDeviceFactory char_device_factory_; }; diff --git a/client/led_unittest.cc b/client/led_unittest.cc index fbcba59..04c10d4 100644 --- a/client/led_unittest.cc +++ b/client/led_unittest.cc @@ -18,6 +18,8 @@ #include <base/files/file_util.h> #include <base/files/scoped_temp_dir.h> +#include <binderwrapper/binder_test_base.h> +#include <binderwrapper/stub_binder_wrapper.h> #include <gtest/gtest.h> #include "led_driver_sysfs.h" @@ -32,7 +34,7 @@ using android::LedDriverSysfs; using android::LedManager; using android::PeripheralManager; -class LedTest : public ::testing::Test { +class LedTest : public android::BinderTestBase { public: void SetUp() { CHECK(dir_.CreateUniqueTempDir()); @@ -43,7 +45,10 @@ class LedTest : public ::testing::Test { led_path_prefix_ = dir_.path().value() + "/"; // Initialize the peripheral manager with the stub binder wrapper. - pman_.InitForTest(); + android::sp<PeripheralManager> pman(new PeripheralManager); + android::String8 interface_desc(pman->getInterfaceDescriptor()); + binder_wrapper()->SetBinderForService(interface_desc.string(), pman); + LedManager* man = LedManager::GetLedManager(); // Register the testing version of the LED driver and the fake LEDs. @@ -72,7 +77,6 @@ class LedTest : public ::testing::Test { void TearDown() { LedManager::ResetLedManager(); } private: - PeripheralManager pman_; base::ScopedTempDir dir_; std::string led_path_prefix_; }; diff --git a/client/peripheral_manager_client_impl.cc b/client/peripheral_manager_client_impl.cc index 8231102..407a40f 100644 --- a/client/peripheral_manager_client_impl.cc +++ b/client/peripheral_manager_client_impl.cc @@ -17,17 +17,17 @@ #include "peripheral_manager_client_impl.h" #include <android/os/IPeripheralManager.h> -#include <binder/IServiceManager.h> +#include <binderwrapper/binder_wrapper.h> using android::binder::Status; bool PeripheralManagerClientImpl::Init() { - auto manager = android::interface_cast<android::os::IPeripheralManager>( - android::defaultServiceManager()->getService( - android::String16("android.os.IPeripheralManager"))); + auto wrapper = android::interface_cast<android::os::IPeripheralManager>( + android::BinderWrapper::GetOrCreateInstance()->GetService( + "android.os.IPeripheralManager")); lifeline_ = new android::BBinder; - return manager->GetClient(lifeline_, &client_).isOk(); + return wrapper->GetClient(lifeline_, &client_).isOk(); } int PeripheralManagerClientImpl::OpenGpio(const std::string& name, diff --git a/client/spi_unittest.cc b/client/spi_unittest.cc index d0529c0..cc8a3fb 100644 --- a/client/spi_unittest.cc +++ b/client/spi_unittest.cc @@ -16,6 +16,8 @@ #include <memory> +#include <binderwrapper/binder_test_base.h> +#include <binderwrapper/stub_binder_wrapper.h> #include <gtest/gtest.h> #include "fake_devices.h" @@ -35,10 +37,13 @@ using android::PeripheralManager; // Base class used to test the Spi C API. // As we rely on static, global managers, we cannot run this tests in parallel. // Please use -j1 when running theses tests or you may see false negatives. -class SpiTest : public ::testing::Test { +class SpiTest : public android::BinderTestBase { public: void SetUp() { - pman_.InitForTest(); + android::sp<PeripheralManager> pman(new PeripheralManager); + android::String8 interface_desc(pman->getInterfaceDescriptor()); + binder_wrapper()->SetBinderForService(interface_desc.string(), pman); + SpiManager* man = SpiManager::GetSpiManager(); man->GetSpiDevBuses(); @@ -53,7 +58,6 @@ class SpiTest : public ::testing::Test { void TearDown() { SpiManager::ResetSpiManager(); } private: - PeripheralManager pman_; FakeDeviceFactory device_factory_; }; @@ -186,7 +190,7 @@ TEST_F(SpiTest, CantOpenDeviceTwice) { TEST_F(SpiTest, CantOpenUnknownDevice) { BPeripheralManagerClient* client = BPeripheralManagerClient_new(); - BSpiDevice* device; + BSpiDevice* device = nullptr; ASSERT_EQ( ENODEV, BPeripheralManagerClient_openSpiDevice(client, "SPI_UNKOWN", &device)); diff --git a/daemon/peripheral_manager.cc b/daemon/peripheral_manager.cc index d24cbe0..c946932 100644 --- a/daemon/peripheral_manager.cc +++ b/daemon/peripheral_manager.cc @@ -18,6 +18,7 @@ #include <base/logging.h> #include <binderwrapper/binder_wrapper.h> +#include <binderwrapper/stub_binder_wrapper.h> #include <hardware/hardware.h> #include <hardware/peripheral_io.h> @@ -113,12 +114,6 @@ bool PeripheralManager::Init() { return BinderWrapper::Get()->RegisterService(interface_desc.string(), this); } -bool PeripheralManager::InitForTest() { - BinderWrapper::Create(); - String8 interface_desc(getInterfaceDescriptor()); - return BinderWrapper::Get()->RegisterService(interface_desc.string(), this); -} - Status PeripheralManager::GetClient(const sp<IBinder>& lifeline, sp<os::IPeripheralManagerClient>* client) { sp<PeripheralManagerClient> c = new PeripheralManagerClient; diff --git a/daemon/peripheral_manager.h b/daemon/peripheral_manager.h index a1e354f..2c97a56 100644 --- a/daemon/peripheral_manager.h +++ b/daemon/peripheral_manager.h @@ -38,8 +38,6 @@ class PeripheralManager : public BnPeripheralManager, bool Init(); - bool InitForTest(); - // IPeripheralManager Interface Status GetClient(const sp<IBinder>& lifeline, sp<os::IPeripheralManagerClient>* _aidl_return); |