Lais Andrade | fd0e14c | 2024-07-30 15:57:23 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 21 | using namespace android; |
| 22 | using namespace testing; |
| 23 | |
| 24 | using HapticLevel = os::HapticLevel; |
| 25 | using ScaleLevel = os::ExternalVibrationScale::ScaleLevel; |
| 26 | |
| 27 | class TestVibrationController : public os::IExternalVibrationController { |
| 28 | public: |
| 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 | |
| 41 | class ExternalVibrationTest : public Test { |
| 42 | protected: |
| 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 | |
| 52 | TEST_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 Andrade | 045376e | 2024-07-24 15:35:07 +0100 | [diff] [blame] | 60 | |
Lais Andrade | fd0e14c | 2024-07-30 15:57:23 +0100 | [diff] [blame] | 61 | sp<TestVibrationController> vibrationController = new TestVibrationController(); |
| 62 | ASSERT_NE(vibrationController, nullptr); |
Lais Andrade | 045376e | 2024-07-24 15:35:07 +0100 | [diff] [blame] | 63 | |
Lais Andrade | fd0e14c | 2024-07-30 15:57:23 +0100 | [diff] [blame] | 64 | sp<os::ExternalVibration> original = |
| 65 | new os::ExternalVibration(uid, pkg, originalAttrs, vibrationController); |
| 66 | ASSERT_NE(original, nullptr); |
Lais Andrade | 045376e | 2024-07-24 15:35:07 +0100 | [diff] [blame] | 67 | |
Lais Andrade | fd0e14c | 2024-07-30 15:57:23 +0100 | [diff] [blame] | 68 | 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 Andrade | 045376e | 2024-07-24 15:35:07 +0100 | [diff] [blame] | 75 | |
Lais Andrade | fd0e14c | 2024-07-30 15:57:23 +0100 | [diff] [blame] | 76 | 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 Andrade | 045376e | 2024-07-24 15:35:07 +0100 | [diff] [blame] | 81 | |
Lais Andrade | fd0e14c | 2024-07-30 15:57:23 +0100 | [diff] [blame] | 82 | sp<os::ExternalVibration> parceled = |
| 83 | new os::ExternalVibration(0, std::string(""), defaultAttrs, nullptr); |
| 84 | ASSERT_NE(parceled, nullptr); |
Lais Andrade | 045376e | 2024-07-24 15:35:07 +0100 | [diff] [blame] | 85 | |
Lais Andrade | fd0e14c | 2024-07-30 15:57:23 +0100 | [diff] [blame] | 86 | Parcel parcel; |
| 87 | original->writeToParcel(&parcel); |
| 88 | parcel.setDataPosition(0); |
| 89 | parceled->readFromParcel(&parcel); |
Lais Andrade | 045376e | 2024-07-24 15:35:07 +0100 | [diff] [blame] | 90 | |
Lais Andrade | fd0e14c | 2024-07-30 15:57:23 +0100 | [diff] [blame] | 91 | 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 | |
| 100 | TEST_F(ExternalVibrationTest, TestExternalVibrationScaleToHapticScale) { |
| 101 | os::ExternalVibrationScale externalVibrationScale; |
| 102 | externalVibrationScale.scaleLevel = ScaleLevel::SCALE_HIGH; |
Lais Andrade | 045376e | 2024-07-24 15:35:07 +0100 | [diff] [blame] | 103 | externalVibrationScale.scaleFactor = 0.5f; |
Lais Andrade | fd0e14c | 2024-07-30 15:57:23 +0100 | [diff] [blame] | 104 | externalVibrationScale.adaptiveHapticsScale = 0.8f; |
Lais Andrade | 045376e | 2024-07-24 15:35:07 +0100 | [diff] [blame] | 105 | |
Lais Andrade | fd0e14c | 2024-07-30 15:57:23 +0100 | [diff] [blame] | 106 | os::HapticScale hapticScale = |
| 107 | os::ExternalVibration::externalVibrationScaleToHapticScale(externalVibrationScale); |
Lais Andrade | 045376e | 2024-07-24 15:35:07 +0100 | [diff] [blame] | 108 | |
| 109 | // Check scale factors are forwarded. |
Lais Andrade | fd0e14c | 2024-07-30 15:57:23 +0100 | [diff] [blame] | 110 | EXPECT_EQ(hapticScale.getLevel(), HapticLevel::HIGH); |
Lais Andrade | 045376e | 2024-07-24 15:35:07 +0100 | [diff] [blame] | 111 | EXPECT_EQ(hapticScale.getScaleFactor(), 0.5f); |
Lais Andrade | fd0e14c | 2024-07-30 15:57:23 +0100 | [diff] [blame] | 112 | EXPECT_EQ(hapticScale.getAdaptiveScaleFactor(), 0.8f); |
Lais Andrade | 045376e | 2024-07-24 15:35:07 +0100 | [diff] [blame] | 113 | |
Lais Andrade | fd0e14c | 2024-07-30 15:57:23 +0100 | [diff] [blame] | 114 | // 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 | } |