From b075a6a599fd6f4a9d12df2edebe0f626632fbdc Mon Sep 17 00:00:00 2001 From: "Harpreet \\\"Eli\\\" Sangha" Date: Fri, 10 Apr 2020 15:11:58 +0900 Subject: [PATCH] vibrator: vts: Validate Primitive Duration Bug: 148496381 Test: Ran on Pixel 4 Signed-off-by: Harpreet \"Eli\" Sangha Change-Id: Ie74f9458b881a51746539f61526fbe4898485b00 --- vibrator/aidl/default/Vibrator.cpp | 4 ++ .../aidl/vts/VtsHalVibratorTargetTest.cpp | 61 +++++++++++++------ 2 files changed, 48 insertions(+), 17 deletions(-) diff --git a/vibrator/aidl/default/Vibrator.cpp b/vibrator/aidl/default/Vibrator.cpp index 9236b95ddc..1a8fd3bf2c 100644 --- a/vibrator/aidl/default/Vibrator.cpp +++ b/vibrator/aidl/default/Vibrator.cpp @@ -163,6 +163,10 @@ ndk::ScopedAStatus Vibrator::compose(const std::vector& composi } LOG(INFO) << "triggering primitive " << static_cast(e.primitive) << " @ scale " << e.scale; + + int32_t durationMs; + getPrimitiveDuration(e.primitive, &durationMs); + usleep(durationMs * 1000); } if (callback != nullptr) { diff --git a/vibrator/aidl/vts/VtsHalVibratorTargetTest.cpp b/vibrator/aidl/vts/VtsHalVibratorTargetTest.cpp index 411fe7a2fe..9a1b660153 100644 --- a/vibrator/aidl/vts/VtsHalVibratorTargetTest.cpp +++ b/vibrator/aidl/vts/VtsHalVibratorTargetTest.cpp @@ -33,6 +33,7 @@ using android::hardware::vibrator::CompositePrimitive; using android::hardware::vibrator::Effect; using android::hardware::vibrator::EffectStrength; using android::hardware::vibrator::IVibrator; +using std::chrono::high_resolution_clock; const std::vector kEffects{android::enum_range().begin(), android::enum_range().end()}; @@ -381,26 +382,52 @@ TEST_P(VibratorAidl, CompseSizeBoundary) { } TEST_P(VibratorAidl, ComposeCallback) { + constexpr std::chrono::milliseconds allowedLatency{10}; + if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) { - std::promise completionPromise; - std::future completionFuture{completionPromise.get_future()}; - sp callback = - new CompletionCallback([&completionPromise] { completionPromise.set_value(); }); - CompositePrimitive primitive = CompositePrimitive::CLICK; - CompositeEffect effect; - std::vector composite; - int32_t duration; + std::vector supported; - effect.delayMs = 0; - effect.primitive = primitive; - effect.scale = 1.0f; - composite.emplace_back(effect); + ASSERT_TRUE(vibrator->getSupportedPrimitives(&supported).isOk()); - EXPECT_EQ(Status::EX_NONE, - vibrator->getPrimitiveDuration(primitive, &duration).exceptionCode()); - EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, callback).exceptionCode()); - EXPECT_EQ(completionFuture.wait_for(std::chrono::milliseconds(duration * 2)), - std::future_status::ready); + for (auto primitive : supported) { + if (primitive == CompositePrimitive::NOOP) { + continue; + } + + std::promise completionPromise; + std::future completionFuture{completionPromise.get_future()}; + sp callback = + new CompletionCallback([&completionPromise] { completionPromise.set_value(); }); + CompositeEffect effect; + std::vector composite; + int32_t durationMs; + std::chrono::milliseconds duration; + std::chrono::time_point start, end; + std::chrono::milliseconds elapsed; + + effect.delayMs = 0; + effect.primitive = primitive; + effect.scale = 1.0f; + composite.emplace_back(effect); + + EXPECT_EQ(Status::EX_NONE, + vibrator->getPrimitiveDuration(primitive, &durationMs).exceptionCode()) + << toString(primitive); + duration = std::chrono::milliseconds(durationMs); + + EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, callback).exceptionCode()) + << toString(primitive); + start = high_resolution_clock::now(); + + EXPECT_EQ(completionFuture.wait_for(duration + allowedLatency), + std::future_status::ready) + << toString(primitive); + end = high_resolution_clock::now(); + + elapsed = std::chrono::duration_cast(end - start); + EXPECT_LE(elapsed.count(), (duration + allowedLatency).count()) << toString(primitive); + EXPECT_GE(elapsed.count(), (duration - allowedLatency).count()) << toString(primitive); + } } }