| Chris Ye | 1a5a888 | 2020-01-15 10:51:47 -0800 | [diff] [blame] | 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 | #define LOG_TAG "ThermalManagerTest" | 
 | 18 | //#define LOG_NDEBUG 0 | 
 | 19 |  | 
 | 20 | #include <thread> | 
 | 21 |  | 
 | 22 | #include <android/os/BnThermalStatusListener.h> | 
 | 23 | #include <android/os/IThermalService.h> | 
 | 24 | #include <binder/IPCThreadState.h> | 
 | 25 | #include <binder/IServiceManager.h> | 
 | 26 | #include <binder/Parcel.h> | 
 | 27 | #include <condition_variable> | 
 | 28 | #include <gtest/gtest.h> | 
 | 29 | #include <powermanager/PowerManager.h> | 
 | 30 | #include <utils/Log.h> | 
 | 31 |  | 
 | 32 | using namespace android; | 
 | 33 | using namespace android::os; | 
 | 34 | using namespace std::chrono_literals; | 
 | 35 |  | 
 | 36 | class IThermalServiceTest : public testing::Test, | 
 | 37 |                             public BnThermalStatusListener{ | 
 | 38 |     public: | 
 | 39 |         IThermalServiceTest(); | 
 | 40 |         void setThermalOverride(int level); | 
 | 41 |         virtual binder::Status onStatusChange(int status) override; | 
 | 42 |         int getStatusFromService(); | 
 | 43 |         void SetUp() override; | 
 | 44 |         void TearDown() override; | 
 | 45 |     protected: | 
 | 46 |         sp<IThermalService> mThermalSvc; | 
 | 47 |         std::condition_variable mCondition; | 
 | 48 |         int mListenerStatus; | 
 | 49 |         int mServiceStatus; | 
 | 50 |         std::mutex mMutex; | 
 | 51 | }; | 
 | 52 |  | 
 | 53 | IThermalServiceTest::IThermalServiceTest() | 
 | 54 |  : mListenerStatus(0), | 
 | 55 |    mServiceStatus(0) { | 
 | 56 | } | 
 | 57 |  | 
 | 58 | void IThermalServiceTest::setThermalOverride(int level) { | 
 | 59 |     std::string cmdStr = "cmd thermalservice override-status " + std::to_string(level); | 
 | 60 |     system(cmdStr.c_str()); | 
 | 61 | } | 
 | 62 |  | 
 | 63 | binder::Status IThermalServiceTest::onStatusChange(int status) { | 
 | 64 |     std::unique_lock<std::mutex> lock(mMutex); | 
 | 65 |     mListenerStatus = status; | 
 | 66 |     ALOGI("IThermalServiceTest::notifyListener %d", mListenerStatus); | 
 | 67 |     mCondition.notify_all(); | 
 | 68 |     return binder::Status::ok(); | 
 | 69 | } | 
 | 70 |  | 
 | 71 | int IThermalServiceTest::getStatusFromService() { | 
 | 72 |     int status; | 
 | 73 |     binder::Status ret = mThermalSvc->getCurrentThermalStatus(&status); | 
 | 74 |     if (ret.isOk()) { | 
 | 75 |         return status; | 
 | 76 |     } else { | 
 | 77 |         return BAD_VALUE; | 
 | 78 |     } | 
 | 79 | } | 
 | 80 |  | 
 | 81 | void IThermalServiceTest::SetUp() { | 
 | 82 |     setThermalOverride(0); | 
 | 83 |     // use checkService() to avoid blocking if thermal service is not up yet | 
 | 84 |     sp<IBinder> binder = | 
 | 85 |         defaultServiceManager()->checkService(String16("thermalservice")); | 
 | 86 |     EXPECT_NE(binder, nullptr); | 
 | 87 |     mThermalSvc = interface_cast<IThermalService>(binder); | 
 | 88 |     EXPECT_NE(mThermalSvc, nullptr); | 
 | 89 |     bool success = false; | 
 | 90 |     binder::Status ret = mThermalSvc->registerThermalStatusListener(this, &success); | 
 | 91 |     ASSERT_TRUE(success); | 
 | 92 |     ASSERT_TRUE(ret.isOk()); | 
 | 93 |     // Wait for listener called after registration, shouldn't timeout | 
 | 94 |     std::unique_lock<std::mutex> lock(mMutex); | 
 | 95 |     EXPECT_NE(mCondition.wait_for(lock, 1s), std::cv_status::timeout); | 
 | 96 | } | 
 | 97 |  | 
 | 98 | void IThermalServiceTest::TearDown() { | 
 | 99 |     bool success = false; | 
 | 100 |     binder::Status ret = mThermalSvc->unregisterThermalStatusListener(this, &success); | 
 | 101 |     ASSERT_TRUE(success); | 
 | 102 |     ASSERT_TRUE(ret.isOk()); | 
 | 103 | } | 
 | 104 |  | 
 | 105 | class IThermalListenerTest : public IThermalServiceTest, public testing::WithParamInterface<int32_t> { | 
 | 106 |   public: | 
 | 107 |     static auto PrintParam(const testing::TestParamInfo<ParamType> &info) { | 
 | 108 |         return std::to_string(info.param); | 
 | 109 |     } | 
 | 110 | }; | 
 | 111 |  | 
 | 112 | TEST_P(IThermalListenerTest, TestListener) { | 
 | 113 |     int level = GetParam(); | 
 | 114 |     std::unique_lock<std::mutex> lock(mMutex); | 
 | 115 |     // Set the override thermal status | 
 | 116 |     setThermalOverride(level); | 
 | 117 |     // Wait for listener called, shouldn't timeout | 
 | 118 |     EXPECT_NE(mCondition.wait_for(lock, 1s), std::cv_status::timeout); | 
 | 119 |     // Check the result | 
 | 120 |     EXPECT_EQ(level, mListenerStatus); | 
 | 121 |     ALOGI("Thermal listener status %d, expecting %d", mListenerStatus, level); | 
 | 122 | } | 
 | 123 |  | 
 | 124 | INSTANTIATE_TEST_SUITE_P(TestListenerLevels, IThermalListenerTest, testing::Range( | 
 | 125 |         static_cast<int>(ThermalStatus::THERMAL_STATUS_LIGHT), | 
 | 126 |         static_cast<int>(ThermalStatus::THERMAL_STATUS_SHUTDOWN)), | 
 | 127 |         IThermalListenerTest::PrintParam); | 
 | 128 |  | 
 | 129 | class IThermalLevelTest : public IThermalServiceTest, public testing::WithParamInterface<int32_t> { | 
 | 130 |   public: | 
 | 131 |     static auto PrintParam(const testing::TestParamInfo<ParamType> &info) { | 
 | 132 |         return std::to_string(info.param); | 
 | 133 |     } | 
 | 134 | }; | 
 | 135 |  | 
 | 136 | TEST_P(IThermalLevelTest, TestGetStatusLevel) { | 
 | 137 |     int level = GetParam(); | 
 | 138 |     setThermalOverride(level); | 
 | 139 |     mServiceStatus = getStatusFromService(); | 
 | 140 |     EXPECT_EQ(level, mServiceStatus); | 
 | 141 | } | 
 | 142 |  | 
 | 143 | INSTANTIATE_TEST_SUITE_P(TestStatusLevels, IThermalLevelTest, testing::Range( | 
 | 144 |         static_cast<int>(ThermalStatus::THERMAL_STATUS_NONE), | 
 | 145 |         static_cast<int>(ThermalStatus::THERMAL_STATUS_SHUTDOWN)), | 
 | 146 |         IThermalLevelTest::PrintParam); | 
 | 147 |  | 
 | 148 | int main(int argc, char **argv) { | 
 | 149 |     std::unique_ptr<std::thread> binderLoop; | 
 | 150 |     binderLoop = std::make_unique<std::thread>( | 
 | 151 |             [&] { IPCThreadState::self()->joinThreadPool(true); }); | 
 | 152 |  | 
 | 153 |     ::testing::InitGoogleTest(&argc, argv); | 
 | 154 |     int status = RUN_ALL_TESTS(); | 
 | 155 |     ALOGV("Test result = %d\n", status); | 
 | 156 |  | 
 | 157 |     return status; | 
 | 158 | } |