Set up shell to use for unit tests
Sets up a unit test file that can be used to unit test the HalProxy. The
unit tests will eventually end up containing code that injects the fake
SensorsSubHal implementation into the HalProxy and verifies various
parts of the implementation. This will likely require some modifications
to the SensorsSubHal class to modify it during test execution, but it's
better that the same implementation is shared between unit / manual
integration testing.
Test: Run atest android.hardware.sensors@2.0-halproxy-unit-tests
Bug: 136511617
Change-Id: I7b6865564ea41ab3c58f77d3168366c95e3289e5
diff --git a/sensors/2.0/multihal/Android.bp b/sensors/2.0/multihal/Android.bp
index dff28ab..44dddfd 100644
--- a/sensors/2.0/multihal/Android.bp
+++ b/sensors/2.0/multihal/Android.bp
@@ -13,18 +13,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.
-cc_binary {
- name: "android.hardware.sensors@2.0-service.multihal",
- defaults: ["hidl_defaults"],
- vendor: true,
- relative_install_path: "hw",
- srcs: [
- "service.cpp",
- "HalProxy.cpp",
- ],
- init_rc: ["android.hardware.sensors@2.0-service-multihal.rc"],
+cc_defaults {
+ name: "android.hardware.sensors@2.0-multihal-defaults",
header_libs: [
- "android.hardware.sensors@2.0-subhal.header",
+ "android.hardware.sensors@2.0-multihal.header",
],
shared_libs: [
"android.hardware.sensors@1.0",
@@ -37,11 +29,39 @@
"libpower",
"libutils",
],
+}
+
+cc_binary {
+ name: "android.hardware.sensors@2.0-service.multihal",
+ defaults: [
+ "hidl_defaults",
+ "android.hardware.sensors@2.0-multihal-defaults",
+ ],
+ vendor: true,
+ relative_install_path: "hw",
+ srcs: [
+ "service.cpp",
+ "HalProxy.cpp",
+ ],
+ init_rc: ["android.hardware.sensors@2.0-service-multihal.rc"],
vintf_fragments: ["android.hardware.sensors@2.0-multihal.xml"],
}
cc_library_headers {
- name: "android.hardware.sensors@2.0-subhal.header",
- vendor: true,
+ name: "android.hardware.sensors@2.0-multihal.header",
+ vendor_available: true,
export_include_dirs: ["include"],
}
+
+// The below targets should only be used for testing.
+cc_test_library {
+ name: "android.hardware.sensors@2.0-HalProxy",
+ defaults: ["android.hardware.sensors@2.0-multihal-defaults"],
+ vendor_available: true,
+ srcs: [
+ "HalProxy.cpp",
+ ],
+ export_header_lib_headers: [
+ "android.hardware.sensors@2.0-multihal.header",
+ ],
+}
diff --git a/sensors/2.0/multihal/HalProxy.cpp b/sensors/2.0/multihal/HalProxy.cpp
index 41c3548..80d7296 100644
--- a/sensors/2.0/multihal/HalProxy.cpp
+++ b/sensors/2.0/multihal/HalProxy.cpp
@@ -104,6 +104,10 @@
// TODO: Discover sensors
}
+HalProxy::HalProxy(std::vector<ISensorsSubHal*>& subHalList) : mSubHalList(subHalList) {
+ // TODO: Perform the same steps as the empty constructor.
+}
+
HalProxy::~HalProxy() {
// TODO: Join any running threads and clean up FMQs and any other allocated
// state.
diff --git a/sensors/2.0/multihal/HalProxy.h b/sensors/2.0/multihal/include/HalProxy.h
similarity index 96%
rename from sensors/2.0/multihal/HalProxy.h
rename to sensors/2.0/multihal/include/HalProxy.h
index e5799fd..9d5787c 100644
--- a/sensors/2.0/multihal/HalProxy.h
+++ b/sensors/2.0/multihal/include/HalProxy.h
@@ -46,7 +46,9 @@
using Result = ::android::hardware::sensors::V1_0::Result;
using SharedMemInfo = ::android::hardware::sensors::V1_0::SharedMemInfo;
- HalProxy();
+ explicit HalProxy();
+ // Test only constructor.
+ explicit HalProxy(std::vector<ISensorsSubHal*>& subHalList);
~HalProxy();
// Methods from ::android::hardware::sensors::V2_0::ISensors follow.
@@ -78,7 +80,7 @@
Return<void> debug(const hidl_handle& fd, const hidl_vec<hidl_string>& args) override;
- // Below methods from ::android::hardware::sensors::V2_0::ISensorsCaback with a minor change
+ // Below methods from ::android::hardware::sensors::V2_0::ISensorsCallback with a minor change
// to pass in the sub-HAL index. While the above methods are invoked from the sensors framework
// via the binder, these methods are invoked from a callback provided to sub-HALs inside the
// same process as the HalProxy, but potentially running on different threads.
diff --git a/sensors/2.0/multihal/testing/Android.bp b/sensors/2.0/multihal/tests/Android.bp
similarity index 60%
rename from sensors/2.0/multihal/testing/Android.bp
rename to sensors/2.0/multihal/tests/Android.bp
index 3dedbd6..13d80f7 100644
--- a/sensors/2.0/multihal/testing/Android.bp
+++ b/sensors/2.0/multihal/tests/Android.bp
@@ -15,14 +15,13 @@
cc_defaults {
name: "android.hardware.sensors@2.0-fakesubhal-defaults",
- vendor: true,
srcs: [
- "Sensor.cpp",
- "SensorsSubHal.cpp",
+ "fake_subhal/*.cpp",
],
header_libs: [
- "android.hardware.sensors@2.0-subhal.header",
+ "android.hardware.sensors@2.0-multihal.header",
],
+ export_include_dirs: ["fake_subhal"],
shared_libs: [
"android.hardware.sensors@1.0",
"android.hardware.sensors@2.0",
@@ -38,6 +37,7 @@
cc_library {
name: "android.hardware.sensors@2.0-fakesubhal-config1",
+ vendor: true,
defaults: ["android.hardware.sensors@2.0-fakesubhal-defaults"],
cflags: [
"-DSUPPORT_CONTINUOUS_SENSORS",
@@ -47,9 +47,43 @@
cc_library {
name: "android.hardware.sensors@2.0-fakesubhal-config2",
+ vendor: true,
defaults: ["android.hardware.sensors@2.0-fakesubhal-defaults"],
cflags: [
"-DSUPPORT_ON_CHANGE_SENSORS",
"-DSUB_HAL_NAME=\"FakeSubHal-OnChange\"",
],
+}
+
+cc_test_library {
+ name: "android.hardware.sensors@2.0-fakesubhal-unittest",
+ vendor_available: true,
+ defaults: ["android.hardware.sensors@2.0-fakesubhal-defaults"],
+ cflags: [
+ "-DSUPPORT_ON_CHANGE_SENSORS",
+ "-DSUPPORT_CONTINUOUS_SENSORS",
+ "-DSUB_HAL_NAME=\"FakeSubHal-Test\"",
+ ],
+}
+
+cc_test {
+ name: "android.hardware.sensors@2.0-halproxy-unit-tests",
+ srcs: ["HalProxy_test.cpp"],
+ vendor: true,
+ static_libs: [
+ "android.hardware.sensors@2.0-HalProxy",
+ "android.hardware.sensors@2.0-fakesubhal-unittest",
+ ],
+ shared_libs: [
+ "android.hardware.sensors@1.0",
+ "android.hardware.sensors@2.0",
+ "libcutils",
+ "libfmq",
+ "libhidlbase",
+ "libhidltransport",
+ "liblog",
+ "libpower",
+ "libutils",
+ ],
+ test_suites: ["device-tests"],
}
\ No newline at end of file
diff --git a/sensors/2.0/multihal/tests/HalProxy_test.cpp b/sensors/2.0/multihal/tests/HalProxy_test.cpp
new file mode 100644
index 0000000..9edc88a
--- /dev/null
+++ b/sensors/2.0/multihal/tests/HalProxy_test.cpp
@@ -0,0 +1,39 @@
+//
+// Copyright (C) 2019 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include <gtest/gtest.h>
+
+#include "HalProxy.h"
+#include "SensorsSubHal.h"
+
+using ::android::hardware::sensors::V2_0::implementation::HalProxy;
+using ::android::hardware::sensors::V2_0::subhal::implementation::SensorsSubHal;
+
+// TODO: Add more interesting tests such as
+// - verify setOperationMode invokes all subhals
+// - verify if a subhal fails to change operation mode, that state is reset properly
+// - Available sensors are obtained during initialization
+//
+// You can run this suite using "atest android.hardware.sensors@2.0-halproxy-unit-tests".
+//
+// See https://source.android.com/compatibility/tests/development/native-func-e2e.md for more info
+// on how tests are set up and for information on the gtest framework itself.
+TEST(HalProxyTest, ExampleTest) {
+ SensorsSubHal subHal;
+ std::vector<ISensorsSubHal*> fakeSubHals;
+ fakeSubHals.push_back(&subHal);
+
+ HalProxy proxy(fakeSubHals);
+}
\ No newline at end of file
diff --git a/sensors/2.0/multihal/testing/README b/sensors/2.0/multihal/tests/fake_subhal/README
similarity index 100%
rename from sensors/2.0/multihal/testing/README
rename to sensors/2.0/multihal/tests/fake_subhal/README
diff --git a/sensors/2.0/multihal/testing/Sensor.cpp b/sensors/2.0/multihal/tests/fake_subhal/Sensor.cpp
similarity index 97%
rename from sensors/2.0/multihal/testing/Sensor.cpp
rename to sensors/2.0/multihal/tests/fake_subhal/Sensor.cpp
index e095efe..4d53665 100644
--- a/sensors/2.0/multihal/testing/Sensor.cpp
+++ b/sensors/2.0/multihal/tests/fake_subhal/Sensor.cpp
@@ -43,11 +43,14 @@
}
Sensor::~Sensor() {
- std::unique_lock<std::mutex> lock(mRunMutex);
- mStopThread = true;
- mIsEnabled = false;
- mWaitCV.notify_all();
- lock.release();
+ // Ensure that lock is unlocked before calling mRunThread.join() or a
+ // deadlock will occur.
+ {
+ std::unique_lock<std::mutex> lock(mRunMutex);
+ mStopThread = true;
+ mIsEnabled = false;
+ mWaitCV.notify_all();
+ }
mRunThread.join();
}
diff --git a/sensors/2.0/multihal/testing/Sensor.h b/sensors/2.0/multihal/tests/fake_subhal/Sensor.h
similarity index 100%
rename from sensors/2.0/multihal/testing/Sensor.h
rename to sensors/2.0/multihal/tests/fake_subhal/Sensor.h
diff --git a/sensors/2.0/multihal/testing/SensorsSubHal.cpp b/sensors/2.0/multihal/tests/fake_subhal/SensorsSubHal.cpp
similarity index 100%
rename from sensors/2.0/multihal/testing/SensorsSubHal.cpp
rename to sensors/2.0/multihal/tests/fake_subhal/SensorsSubHal.cpp
diff --git a/sensors/2.0/multihal/testing/SensorsSubHal.h b/sensors/2.0/multihal/tests/fake_subhal/SensorsSubHal.h
similarity index 100%
rename from sensors/2.0/multihal/testing/SensorsSubHal.h
rename to sensors/2.0/multihal/tests/fake_subhal/SensorsSubHal.h