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