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 #ifndef VIBRATORSERVICE_UNITTEST_UTIL_H_
18 #define VIBRATORSERVICE_UNITTEST_UTIL_H_
19
20 #include <android/hardware/vibrator/IVibrator.h>
21
22 #include <vibratorservice/VibratorHalWrapper.h>
23
24 namespace android {
25
26 namespace vibrator {
27
28 using ::android::hardware::vibrator::ActivePwle;
29 using ::android::hardware::vibrator::Braking;
30 using ::android::hardware::vibrator::BrakingPwle;
31 using ::android::hardware::vibrator::CompositeEffect;
32 using ::android::hardware::vibrator::CompositePrimitive;
33 using ::android::hardware::vibrator::PrimitivePwle;
34
35 // -------------------------------------------------------------------------------------------------
36
37 class MockCallbackScheduler : public vibrator::CallbackScheduler {
38 public:
39 MOCK_METHOD(void, schedule, (std::function<void()> callback, std::chrono::milliseconds delay),
40 (override));
41 };
42
ACTION(TriggerSchedulerCallback)43 ACTION(TriggerSchedulerCallback) {
44 arg0();
45 }
46
47 // -------------------------------------------------------------------------------------------------
48
49 class TestFactory {
50 public:
createCompositeEffect(CompositePrimitive primitive,std::chrono::milliseconds delay,float scale)51 static CompositeEffect createCompositeEffect(CompositePrimitive primitive,
52 std::chrono::milliseconds delay, float scale) {
53 CompositeEffect effect;
54 effect.primitive = primitive;
55 effect.delayMs = delay.count();
56 effect.scale = scale;
57 return effect;
58 }
59
createActivePwle(float startAmplitude,float startFrequency,float endAmplitude,float endFrequency,std::chrono::milliseconds duration)60 static PrimitivePwle createActivePwle(float startAmplitude, float startFrequency,
61 float endAmplitude, float endFrequency,
62 std::chrono::milliseconds duration) {
63 ActivePwle pwle;
64 pwle.startAmplitude = startAmplitude;
65 pwle.endAmplitude = endAmplitude;
66 pwle.startFrequency = startFrequency;
67 pwle.endFrequency = endFrequency;
68 pwle.duration = duration.count();
69 return pwle;
70 }
71
createBrakingPwle(Braking braking,std::chrono::milliseconds duration)72 static PrimitivePwle createBrakingPwle(Braking braking, std::chrono::milliseconds duration) {
73 BrakingPwle pwle;
74 pwle.braking = braking;
75 pwle.duration = duration.count();
76 return pwle;
77 }
78
createCountingCallback(int32_t * counter)79 static std::function<void()> createCountingCallback(int32_t* counter) {
80 return [counter]() { *counter += 1; };
81 }
82
83 private:
84 TestFactory() = delete;
85 ~TestFactory() = delete;
86 };
87
88 class TestCounter {
89 public:
mMutex()90 TestCounter(int32_t init = 0) : mMutex(), mCondVar(), mCount(init) {}
91
get()92 int32_t get() {
93 std::lock_guard<std::mutex> lock(mMutex);
94 return mCount;
95 }
96
increment()97 void increment() {
98 {
99 std::lock_guard<std::mutex> lock(mMutex);
100 mCount += 1;
101 }
102 mCondVar.notify_all();
103 }
104
tryWaitUntilCountIsAtLeast(int32_t count,std::chrono::milliseconds timeout)105 void tryWaitUntilCountIsAtLeast(int32_t count, std::chrono::milliseconds timeout) {
106 std::unique_lock<std::mutex> lock(mMutex);
107 mCondVar.wait_for(lock, timeout, [&] { return mCount >= count; });
108 }
109
110 private:
111 std::mutex mMutex;
112 std::condition_variable mCondVar;
113 int32_t mCount GUARDED_BY(mMutex);
114 };
115
116 // -------------------------------------------------------------------------------------------------
117
118 } // namespace vibrator
119
120 } // namespace android
121
122 #endif // VIBRATORSERVICE_UNITTEST_UTIL_H_
123