Merge "Add CallInitializeTwice Test"

This commit is contained in:
TreeHugger Robot
2018-10-26 23:55:24 +00:00
committed by Android (Google) Code Review
4 changed files with 68 additions and 5 deletions

View File

@@ -47,7 +47,7 @@ class SensorsHidlEnvironmentV2_0 : public SensorsHidlEnvironmentBase {
virtual void HidlTearDown() override;
private:
protected:
friend SensorsHidlTest;
SensorsHidlEnvironmentV2_0() : mEventQueueFlag(nullptr) {}

View File

@@ -79,6 +79,9 @@ class SensorsHidlTest : public SensorsHidlTestBase {
SensorsHidlEnvironmentBase* getEnvironment() override {
return SensorsHidlEnvironmentV2_0::Instance();
}
// Helper functions
void activateAllSensors(bool enable);
};
Return<Result> SensorsHidlTest::activate(int32_t sensorHandle, bool enabled) {
@@ -437,6 +440,54 @@ TEST_F(SensorsHidlTest, MagnetometerGrallocDirectReportOperationVeryFast) {
RateLevel::VERY_FAST, NullChecker());
}
void SensorsHidlTest::activateAllSensors(bool enable) {
for (const SensorInfo& sensorInfo : getSensorsList()) {
if (isValidType(sensorInfo.type)) {
batch(sensorInfo.sensorHandle, sensorInfo.minDelay, 0 /* maxReportLatencyNs */);
activate(sensorInfo.sensorHandle, enable);
}
}
}
// Test that if initialize is called twice, then the HAL writes events to the FMQs from the second
// call to the function.
TEST_F(SensorsHidlTest, CallInitializeTwice) {
// Create a helper class so that a second environment is able to be instantiated
class SensorsHidlEnvironmentTest : public SensorsHidlEnvironmentV2_0 {};
if (getSensorsList().size() == 0) {
// No sensors
return;
}
constexpr useconds_t kCollectionTimeoutUs = 1000 * 1000; // 1s
constexpr int32_t kNumEvents = 1;
// Create a new environment that calls initialize()
std::unique_ptr<SensorsHidlEnvironmentTest> newEnv =
std::make_unique<SensorsHidlEnvironmentTest>();
newEnv->HidlSetUp();
activateAllSensors(true);
// Verify that the old environment does not receive any events
ASSERT_EQ(collectEvents(kCollectionTimeoutUs, kNumEvents, getEnvironment()).size(), 0);
// Verify that the new event queue receives sensor events
ASSERT_GE(collectEvents(kCollectionTimeoutUs, kNumEvents, newEnv.get()).size(), kNumEvents);
activateAllSensors(false);
// Cleanup the test environment
newEnv->HidlTearDown();
// Restore the test environment for future tests
SensorsHidlEnvironmentV2_0::Instance()->HidlTearDown();
SensorsHidlEnvironmentV2_0::Instance()->HidlSetUp();
// Ensure that the original environment is receiving events
activateAllSensors(true);
ASSERT_GE(collectEvents(kCollectionTimeoutUs, kNumEvents).size(), kNumEvents);
activateAllSensors(false);
}
int main(int argc, char** argv) {
::testing::AddGlobalTestEnvironment(SensorsHidlEnvironmentV2_0::Instance());
::testing::InitGoogleTest(&argc, argv);

View File

@@ -40,6 +40,14 @@ const Vec3NormChecker SensorsHidlTestBase::sGyroNormChecker(
std::vector<Event> SensorsHidlTestBase::collectEvents(useconds_t timeLimitUs, size_t nEventLimit,
bool clearBeforeStart,
bool changeCollection) {
return collectEvents(timeLimitUs, nEventLimit, getEnvironment(), clearBeforeStart,
changeCollection);
}
std::vector<Event> SensorsHidlTestBase::collectEvents(useconds_t timeLimitUs, size_t nEventLimit,
SensorsHidlEnvironmentBase* environment,
bool clearBeforeStart,
bool changeCollection) {
std::vector<Event> events;
constexpr useconds_t SLEEP_GRANULARITY = 100 * 1000; // granularity 100 ms
@@ -47,10 +55,10 @@ std::vector<Event> SensorsHidlTestBase::collectEvents(useconds_t timeLimitUs, si
clearBeforeStart);
if (changeCollection) {
getEnvironment()->setCollection(true);
environment->setCollection(true);
}
if (clearBeforeStart) {
getEnvironment()->catEvents(nullptr);
environment->catEvents(nullptr);
}
while (timeLimitUs > 0) {
@@ -58,7 +66,7 @@ std::vector<Event> SensorsHidlTestBase::collectEvents(useconds_t timeLimitUs, si
usleep(duration);
timeLimitUs -= duration;
getEnvironment()->catEvents(&events);
environment->catEvents(&events);
if (events.size() >= nEventLimit) {
break;
}
@@ -67,7 +75,7 @@ std::vector<Event> SensorsHidlTestBase::collectEvents(useconds_t timeLimitUs, si
}
if (changeCollection) {
getEnvironment()->setCollection(false);
environment->setCollection(false);
}
return events;
}

View File

@@ -85,6 +85,10 @@ class SensorsHidlTestBase : public ::testing::VtsHalHidlTargetTestBase {
std::vector<Event> collectEvents(useconds_t timeLimitUs, size_t nEventLimit,
bool clearBeforeStart = true, bool changeCollection = true);
static std::vector<Event> collectEvents(useconds_t timeLimitUs, size_t nEventLimit,
SensorsHidlEnvironmentBase* environment,
bool clearBeforeStart = true,
bool changeCollection = true);
inline static SensorFlagBits extractReportMode(uint64_t flag) {
return (SensorFlagBits)(flag & ((uint64_t)SensorFlagBits::CONTINUOUS_MODE |