|
| 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; |
| 60 | + sp<TestVibrationController> vibrationController = new TestVibrationController(); |
| 61 | + ASSERT_NE(vibrationController, nullptr); |
| 62 | + sp<os::ExternalVibration> original = |
| 63 | + new os::ExternalVibration(uid, pkg, originalAttrs, vibrationController); |
| 64 | + ASSERT_NE(original, nullptr); |
| 65 | + EXPECT_EQ(original->getUid(), uid); |
| 66 | + EXPECT_EQ(original->getPackage(), pkg); |
| 67 | + EXPECT_EQ(original->getAudioAttributes().content_type, originalAttrs.content_type); |
| 68 | + EXPECT_EQ(original->getAudioAttributes().usage, originalAttrs.usage); |
| 69 | + EXPECT_EQ(original->getAudioAttributes().source, originalAttrs.source); |
| 70 | + EXPECT_EQ(original->getAudioAttributes().flags, originalAttrs.flags); |
| 71 | + EXPECT_EQ(original->getController(), vibrationController); |
| 72 | + audio_attributes_t defaultAttrs; |
| 73 | + defaultAttrs.content_type = AUDIO_CONTENT_TYPE_UNKNOWN; |
| 74 | + defaultAttrs.usage = AUDIO_USAGE_UNKNOWN; |
| 75 | + defaultAttrs.source = AUDIO_SOURCE_DEFAULT; |
| 76 | + defaultAttrs.flags = AUDIO_FLAG_NONE; |
| 77 | + sp<os::ExternalVibration> parceled = |
| 78 | + new os::ExternalVibration(0, std::string(""), defaultAttrs, nullptr); |
| 79 | + ASSERT_NE(parceled, nullptr); |
| 80 | + Parcel parcel; |
| 81 | + original->writeToParcel(&parcel); |
| 82 | + parcel.setDataPosition(0); |
| 83 | + parceled->readFromParcel(&parcel); |
| 84 | + EXPECT_EQ(parceled->getUid(), uid); |
| 85 | + EXPECT_EQ(parceled->getPackage(), pkg); |
| 86 | + EXPECT_EQ(parceled->getAudioAttributes().content_type, originalAttrs.content_type); |
| 87 | + EXPECT_EQ(parceled->getAudioAttributes().usage, originalAttrs.usage); |
| 88 | + EXPECT_EQ(parceled->getAudioAttributes().source, originalAttrs.source); |
| 89 | + EXPECT_EQ(parceled->getAudioAttributes().flags, originalAttrs.flags); |
| 90 | + // TestVibrationController does not implement onAsBinder, skip controller parcel in this test. |
| 91 | +} |
| 92 | + |
| 93 | +TEST_F(ExternalVibrationTest, TestExternalVibrationScaleToHapticScale) { |
| 94 | + os::ExternalVibrationScale externalVibrationScale; |
| 95 | + externalVibrationScale.scaleLevel = ScaleLevel::SCALE_HIGH; |
| 96 | + externalVibrationScale.adaptiveHapticsScale = 0.8f; |
| 97 | + os::HapticScale hapticScale = |
| 98 | + os::ExternalVibration::externalVibrationScaleToHapticScale(externalVibrationScale); |
| 99 | + // Check scale factor is forwarded. |
| 100 | + EXPECT_EQ(hapticScale.getLevel(), HapticLevel::HIGH); |
| 101 | + EXPECT_EQ(hapticScale.getAdaptiveScaleFactor(), 0.8f); |
| 102 | + // Check conversion for all levels. |
| 103 | + EXPECT_EQ(toHapticLevel(ScaleLevel::SCALE_MUTE), HapticLevel::MUTE); |
| 104 | + EXPECT_EQ(toHapticLevel(ScaleLevel::SCALE_VERY_LOW), HapticLevel::VERY_LOW); |
| 105 | + EXPECT_EQ(toHapticLevel(ScaleLevel::SCALE_LOW), HapticLevel::LOW); |
| 106 | + EXPECT_EQ(toHapticLevel(ScaleLevel::SCALE_NONE), HapticLevel::NONE); |
| 107 | + EXPECT_EQ(toHapticLevel(ScaleLevel::SCALE_HIGH), HapticLevel::HIGH); |
| 108 | + EXPECT_EQ(toHapticLevel(ScaleLevel::SCALE_VERY_HIGH), HapticLevel::VERY_HIGH); |
| 109 | +} |
0 commit comments