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 <aidl/Gtest.h> 18 #include <aidl/android/frameworks/automotive/powerpolicy/BnCarPowerPolicyChangeCallback.h> 19 #include <aidl/android/frameworks/automotive/powerpolicy/CarPowerPolicy.h> 20 #include <aidl/android/frameworks/automotive/powerpolicy/CarPowerPolicyFilter.h> 21 #include <aidl/android/frameworks/automotive/powerpolicy/PowerComponent.h> 22 #include <android-base/stringprintf.h> 23 #include <android/binder_auto_utils.h> 24 #include <android/binder_manager.h> 25 #include <android/binder_status.h> 26 27 namespace aafap = aidl::android::frameworks::automotive::powerpolicy; 28 29 class MockPowerPolicyChangeCallback : public aafap::BnCarPowerPolicyChangeCallback { 30 public: MockPowerPolicyChangeCallback()31 MockPowerPolicyChangeCallback() {} 32 onPolicyChanged(const aafap::CarPowerPolicy & policy)33 ndk::ScopedAStatus onPolicyChanged( 34 [[maybe_unused]] const aafap::CarPowerPolicy& policy) override { 35 return ndk::ScopedAStatus::ok(); 36 } 37 }; 38 39 template <typename T> 40 class PowerPolicyInterfaceTest { 41 public: SetUp(const std::string & serviceName)42 void SetUp(const std::string& serviceName) { 43 ndk::SpAIBinder binder(AServiceManager_getService(serviceName.c_str())); 44 if (binder.get() == nullptr) { 45 GTEST_SKIP() << "Service " << serviceName << " not found"; 46 } 47 powerPolicyServer = T::fromBinder(binder); 48 } 49 TestGetCurrentPowerPolicy()50 void TestGetCurrentPowerPolicy() { 51 aafap::CarPowerPolicy policy; 52 53 ndk::ScopedAStatus status = powerPolicyServer->getCurrentPowerPolicy(&policy); 54 55 ASSERT_TRUE(status.isOk() || status.getServiceSpecificError() == EX_ILLEGAL_STATE); 56 } 57 TestGetPowerComponentState()58 void TestGetPowerComponentState() { 59 bool state; 60 for (const auto componentId : ndk::enum_range<aafap::PowerComponent>()) { 61 if (componentId >= aafap::PowerComponent::MINIMUM_CUSTOM_COMPONENT_VALUE) { 62 continue; 63 } 64 ndk::ScopedAStatus status = 65 powerPolicyServer->getPowerComponentState(componentId, &state); 66 std::string errMsg = 67 android::base::StringPrintf("Getting state of component(%d) fails", componentId); 68 ASSERT_TRUE(status.isOk()) << errMsg; 69 } 70 } 71 TestGetPowerComponentState_invalidComponent()72 void TestGetPowerComponentState_invalidComponent() { 73 bool state; 74 aafap::PowerComponent invalidComponent = static_cast<aafap::PowerComponent>(-1); 75 76 ndk::ScopedAStatus status = 77 powerPolicyServer->getPowerComponentState(invalidComponent, &state); 78 79 ASSERT_FALSE(status.isOk()); 80 } 81 TestRegisterPowerPolicyCallback()82 void TestRegisterPowerPolicyCallback() { 83 std::shared_ptr<MockPowerPolicyChangeCallback> callback = 84 ndk::SharedRefBase::make<MockPowerPolicyChangeCallback>(); 85 aafap::CarPowerPolicyFilter filter; 86 filter.components.push_back(aafap::PowerComponent::AUDIO); 87 88 ndk::ScopedAStatus status = 89 powerPolicyServer->registerPowerPolicyChangeCallback(callback, filter); 90 91 ASSERT_TRUE(status.isOk()); 92 93 status = powerPolicyServer->unregisterPowerPolicyChangeCallback(callback); 94 95 ASSERT_TRUE(status.isOk()); 96 } 97 TestRegisterPowerPolicyCallback_doubleRegistering()98 void TestRegisterPowerPolicyCallback_doubleRegistering() { 99 std::shared_ptr<MockPowerPolicyChangeCallback> callback = 100 ndk::SharedRefBase::make<MockPowerPolicyChangeCallback>(); 101 aafap::CarPowerPolicyFilter filter; 102 filter.components.push_back(aafap::PowerComponent::AUDIO); 103 104 ndk::ScopedAStatus status = 105 powerPolicyServer->registerPowerPolicyChangeCallback(callback, filter); 106 107 ASSERT_TRUE(status.isOk()); 108 109 status = powerPolicyServer->registerPowerPolicyChangeCallback(callback, filter); 110 111 ASSERT_FALSE(status.isOk()); 112 ASSERT_EQ(status.getServiceSpecificError(), EX_ILLEGAL_ARGUMENT); 113 } 114 TestUnegisterNotRegisteredPowerPolicyCallback()115 void TestUnegisterNotRegisteredPowerPolicyCallback() { 116 std::shared_ptr<MockPowerPolicyChangeCallback> callback = 117 ndk::SharedRefBase::make<MockPowerPolicyChangeCallback>(); 118 119 ndk::ScopedAStatus status = 120 powerPolicyServer->unregisterPowerPolicyChangeCallback(callback); 121 122 ASSERT_FALSE(status.isOk()); 123 } 124 125 std::shared_ptr<T> powerPolicyServer; 126 }; 127