• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *            http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "VibratorHalWrapperAidlTest"
18 
19 #include <android-base/thread_annotations.h>
20 #include <android/hardware/vibrator/IVibrator.h>
21 #include <condition_variable>
22 
23 #include <gmock/gmock.h>
24 #include <gtest/gtest.h>
25 
26 #include <utils/Log.h>
27 #include <thread>
28 
29 #include <vibratorservice/VibratorCallbackScheduler.h>
30 
31 using std::chrono::milliseconds;
32 using std::chrono::steady_clock;
33 using std::chrono::time_point;
34 
35 using namespace android;
36 using namespace std::chrono_literals;
37 using namespace testing;
38 
39 // -------------------------------------------------------------------------------------------------
40 
41 class VibratorCallbackSchedulerTest : public Test {
42 public:
SetUp()43     void SetUp() override {
44         mScheduler = std::make_unique<vibrator::CallbackScheduler>();
45         std::lock_guard<std::mutex> lock(mMutex);
46         mExpiredCallbacks.clear();
47     }
48 
49 protected:
50     std::mutex mMutex;
51     std::condition_variable_any mCondition;
52     std::unique_ptr<vibrator::CallbackScheduler> mScheduler = nullptr;
53     std::vector<int32_t> mExpiredCallbacks GUARDED_BY(mMutex);
54 
createCallback(int32_t id)55     std::function<void()> createCallback(int32_t id) {
56         return [=]() {
57             {
58                 std::lock_guard<std::mutex> lock(mMutex);
59                 mExpiredCallbacks.push_back(id);
60             }
61             mCondition.notify_all();
62         };
63     }
64 
getExpiredCallbacks()65     std::vector<int32_t> getExpiredCallbacks() {
66         std::lock_guard<std::mutex> lock(mMutex);
67         return std::vector<int32_t>(mExpiredCallbacks);
68     }
69 
waitForCallbacks(uint32_t callbackCount,milliseconds timeout)70     bool waitForCallbacks(uint32_t callbackCount, milliseconds timeout) {
71         time_point<steady_clock> expiration = steady_clock::now() + timeout;
72         while (steady_clock::now() < expiration) {
73             std::lock_guard<std::mutex> lock(mMutex);
74             if (callbackCount <= mExpiredCallbacks.size()) {
75                 return true;
76             }
77             mCondition.wait_until(mMutex, expiration);
78         }
79         return false;
80     }
81 };
82 
83 // -------------------------------------------------------------------------------------------------
84 
TEST_F(VibratorCallbackSchedulerTest,TestScheduleRunsOnlyAfterDelay)85 TEST_F(VibratorCallbackSchedulerTest, TestScheduleRunsOnlyAfterDelay) {
86     mScheduler->schedule(createCallback(1), 15ms);
87 
88     // Not triggered before delay.
89     ASSERT_FALSE(waitForCallbacks(1, 10ms));
90     ASSERT_TRUE(getExpiredCallbacks().empty());
91 
92     ASSERT_TRUE(waitForCallbacks(1, 10ms));
93     ASSERT_THAT(getExpiredCallbacks(), ElementsAre(1));
94 }
95 
TEST_F(VibratorCallbackSchedulerTest,TestScheduleMultipleCallbacksRunsInDelayOrder)96 TEST_F(VibratorCallbackSchedulerTest, TestScheduleMultipleCallbacksRunsInDelayOrder) {
97     mScheduler->schedule(createCallback(1), 10ms);
98     mScheduler->schedule(createCallback(2), 5ms);
99     mScheduler->schedule(createCallback(3), 1ms);
100 
101     ASSERT_TRUE(waitForCallbacks(3, 15ms));
102     ASSERT_THAT(getExpiredCallbacks(), ElementsAre(3, 2, 1));
103 }
104 
TEST_F(VibratorCallbackSchedulerTest,TestScheduleInParallelRunsInDelayOrder)105 TEST_F(VibratorCallbackSchedulerTest, TestScheduleInParallelRunsInDelayOrder) {
106     std::vector<std::thread> threads;
107     for (int i = 0; i < 5; i++) {
108         threads.push_back(std::thread(
109                 [=]() { mScheduler->schedule(createCallback(i), milliseconds(10 + 2 * i)); }));
110     }
111     std::for_each(threads.begin(), threads.end(), [](std::thread& t) { t.join(); });
112 
113     ASSERT_TRUE(waitForCallbacks(5, 25ms));
114     ASSERT_THAT(getExpiredCallbacks(), ElementsAre(0, 1, 2, 3, 4));
115 }
116 
TEST_F(VibratorCallbackSchedulerTest,TestDestructorDropsPendingCallbacksAndKillsThread)117 TEST_F(VibratorCallbackSchedulerTest, TestDestructorDropsPendingCallbacksAndKillsThread) {
118     mScheduler->schedule(createCallback(1), 5ms);
119     mScheduler.reset(nullptr);
120 
121     // Should time out waiting for callback to run.
122     ASSERT_FALSE(waitForCallbacks(1, 10ms));
123     ASSERT_TRUE(getExpiredCallbacks().empty());
124 }
125