• 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 #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 // -------------------------------------------------------------------------------------------------
89 
90 } // namespace vibrator
91 
92 } // namespace android
93 
94 #endif // VIBRATORSERVICE_UNITTEST_UTIL_H_