blob: 4133836785bbead35ba71ada93184bb869bef885 [file] [log] [blame]
Lais Andradefd0e14c2024-07-30 15:57:23 +01001/*
2 * Copyright (C) 2024 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#include <binder/Parcel.h>
18#include <gtest/gtest.h>
19#include <vibrator/ExternalVibration.h>
20
21using namespace android;
22using namespace testing;
23
24using HapticLevel = os::HapticLevel;
25using ScaleLevel = os::ExternalVibrationScale::ScaleLevel;
26
27class TestVibrationController : public os::IExternalVibrationController {
28public:
29 explicit TestVibrationController() {}
30 IBinder *onAsBinder() override { return nullptr; }
31 binder::Status mute(/*out*/ bool *ret) override {
32 *ret = false;
33 return binder::Status::ok();
34 };
35 binder::Status unmute(/*out*/ bool *ret) override {
36 *ret = false;
37 return binder::Status::ok();
38 };
39};
40
41class ExternalVibrationTest : public Test {
42protected:
43 HapticLevel toHapticLevel(ScaleLevel level) {
44 os::ExternalVibrationScale externalVibrationScale;
45 externalVibrationScale.scaleLevel = level;
46 os::HapticScale hapticScale =
47 os::ExternalVibration::externalVibrationScaleToHapticScale(externalVibrationScale);
48 return hapticScale.getLevel();
49 }
50};
51
52TEST_F(ExternalVibrationTest, TestReadAndWriteToParcel) {
53 int32_t uid = 1;
54 std::string pkg("package.name");
55 audio_attributes_t originalAttrs;
56 originalAttrs.content_type = AUDIO_CONTENT_TYPE_SONIFICATION;
57 originalAttrs.usage = AUDIO_USAGE_ASSISTANCE_SONIFICATION;
58 originalAttrs.source = AUDIO_SOURCE_VOICE_COMMUNICATION;
59 originalAttrs.flags = AUDIO_FLAG_BYPASS_MUTE;
Lais Andrade045376e2024-07-24 15:35:07 +010060
Lais Andradefd0e14c2024-07-30 15:57:23 +010061 sp<TestVibrationController> vibrationController = new TestVibrationController();
62 ASSERT_NE(vibrationController, nullptr);
Lais Andrade045376e2024-07-24 15:35:07 +010063
Lais Andradefd0e14c2024-07-30 15:57:23 +010064 sp<os::ExternalVibration> original =
65 new os::ExternalVibration(uid, pkg, originalAttrs, vibrationController);
66 ASSERT_NE(original, nullptr);
Lais Andrade045376e2024-07-24 15:35:07 +010067
Lais Andradefd0e14c2024-07-30 15:57:23 +010068 EXPECT_EQ(original->getUid(), uid);
69 EXPECT_EQ(original->getPackage(), pkg);
70 EXPECT_EQ(original->getAudioAttributes().content_type, originalAttrs.content_type);
71 EXPECT_EQ(original->getAudioAttributes().usage, originalAttrs.usage);
72 EXPECT_EQ(original->getAudioAttributes().source, originalAttrs.source);
73 EXPECT_EQ(original->getAudioAttributes().flags, originalAttrs.flags);
74 EXPECT_EQ(original->getController(), vibrationController);
Lais Andrade045376e2024-07-24 15:35:07 +010075
Lais Andradefd0e14c2024-07-30 15:57:23 +010076 audio_attributes_t defaultAttrs;
77 defaultAttrs.content_type = AUDIO_CONTENT_TYPE_UNKNOWN;
78 defaultAttrs.usage = AUDIO_USAGE_UNKNOWN;
79 defaultAttrs.source = AUDIO_SOURCE_DEFAULT;
80 defaultAttrs.flags = AUDIO_FLAG_NONE;
Lais Andrade045376e2024-07-24 15:35:07 +010081
Lais Andradefd0e14c2024-07-30 15:57:23 +010082 sp<os::ExternalVibration> parceled =
83 new os::ExternalVibration(0, std::string(""), defaultAttrs, nullptr);
84 ASSERT_NE(parceled, nullptr);
Lais Andrade045376e2024-07-24 15:35:07 +010085
Lais Andradefd0e14c2024-07-30 15:57:23 +010086 Parcel parcel;
87 original->writeToParcel(&parcel);
88 parcel.setDataPosition(0);
89 parceled->readFromParcel(&parcel);
Lais Andrade045376e2024-07-24 15:35:07 +010090
Lais Andradefd0e14c2024-07-30 15:57:23 +010091 EXPECT_EQ(parceled->getUid(), uid);
92 EXPECT_EQ(parceled->getPackage(), pkg);
93 EXPECT_EQ(parceled->getAudioAttributes().content_type, originalAttrs.content_type);
94 EXPECT_EQ(parceled->getAudioAttributes().usage, originalAttrs.usage);
95 EXPECT_EQ(parceled->getAudioAttributes().source, originalAttrs.source);
96 EXPECT_EQ(parceled->getAudioAttributes().flags, originalAttrs.flags);
97 // TestVibrationController does not implement onAsBinder, skip controller parcel in this test.
98}
99
100TEST_F(ExternalVibrationTest, TestExternalVibrationScaleToHapticScale) {
101 os::ExternalVibrationScale externalVibrationScale;
102 externalVibrationScale.scaleLevel = ScaleLevel::SCALE_HIGH;
Lais Andrade045376e2024-07-24 15:35:07 +0100103 externalVibrationScale.scaleFactor = 0.5f;
Lais Andradefd0e14c2024-07-30 15:57:23 +0100104 externalVibrationScale.adaptiveHapticsScale = 0.8f;
Lais Andrade045376e2024-07-24 15:35:07 +0100105
Lais Andradefd0e14c2024-07-30 15:57:23 +0100106 os::HapticScale hapticScale =
107 os::ExternalVibration::externalVibrationScaleToHapticScale(externalVibrationScale);
Lais Andrade045376e2024-07-24 15:35:07 +0100108
109 // Check scale factors are forwarded.
Lais Andradefd0e14c2024-07-30 15:57:23 +0100110 EXPECT_EQ(hapticScale.getLevel(), HapticLevel::HIGH);
Lais Andrade045376e2024-07-24 15:35:07 +0100111 EXPECT_EQ(hapticScale.getScaleFactor(), 0.5f);
Lais Andradefd0e14c2024-07-30 15:57:23 +0100112 EXPECT_EQ(hapticScale.getAdaptiveScaleFactor(), 0.8f);
Lais Andrade045376e2024-07-24 15:35:07 +0100113
Lais Andradefd0e14c2024-07-30 15:57:23 +0100114 // Check conversion for all levels.
115 EXPECT_EQ(toHapticLevel(ScaleLevel::SCALE_MUTE), HapticLevel::MUTE);
116 EXPECT_EQ(toHapticLevel(ScaleLevel::SCALE_VERY_LOW), HapticLevel::VERY_LOW);
117 EXPECT_EQ(toHapticLevel(ScaleLevel::SCALE_LOW), HapticLevel::LOW);
118 EXPECT_EQ(toHapticLevel(ScaleLevel::SCALE_NONE), HapticLevel::NONE);
119 EXPECT_EQ(toHapticLevel(ScaleLevel::SCALE_HIGH), HapticLevel::HIGH);
120 EXPECT_EQ(toHapticLevel(ScaleLevel::SCALE_VERY_HIGH), HapticLevel::VERY_HIGH);
121}