• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
IThermalServiceTest()53 IThermalServiceTest::IThermalServiceTest()
54  : mListenerStatus(0),
55    mServiceStatus(0) {
56 }
57 
setThermalOverride(int level)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 
onStatusChange(int status)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 
getStatusFromService()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 
SetUp()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     // Lock mutex for operation, so listener will only be processed after wait_for is called
90     std::unique_lock<std::mutex> lock(mMutex);
91     bool success = false;
92     binder::Status ret = mThermalSvc->registerThermalStatusListener(this, &success);
93     // Check the result
94     ASSERT_TRUE(success);
95     ASSERT_TRUE(ret.isOk());
96     // Wait for listener called after registration, shouldn't timeout
97     EXPECT_NE(mCondition.wait_for(lock, 1s), std::cv_status::timeout);
98 }
99 
TearDown()100 void IThermalServiceTest::TearDown() {
101     bool success = false;
102     binder::Status ret = mThermalSvc->unregisterThermalStatusListener(this, &success);
103     ASSERT_TRUE(success);
104     ASSERT_TRUE(ret.isOk());
105 }
106 
107 class IThermalListenerTest : public IThermalServiceTest, public testing::WithParamInterface<int32_t> {
108   public:
PrintParam(const testing::TestParamInfo<ParamType> & info)109     static auto PrintParam(const testing::TestParamInfo<ParamType> &info) {
110         return std::to_string(info.param);
111     }
112 };
113 
TEST_P(IThermalListenerTest,TestListener)114 TEST_P(IThermalListenerTest, TestListener) {
115     int level = GetParam();
116     // Lock mutex for operation, so listener will only be processed after wait_for is called
117     std::unique_lock<std::mutex> lock(mMutex);
118     // Set the override thermal status
119     setThermalOverride(level);
120     // Wait for listener called, shouldn't timeout
121     EXPECT_NE(mCondition.wait_for(lock, 1s), std::cv_status::timeout);
122     // Check the result
123     EXPECT_EQ(level, mListenerStatus);
124     ALOGI("Thermal listener status %d, expecting %d", mListenerStatus, level);
125 }
126 
127 INSTANTIATE_TEST_SUITE_P(TestListenerLevels, IThermalListenerTest, testing::Range(
128         static_cast<int>(ThermalStatus::THERMAL_STATUS_LIGHT),
129         static_cast<int>(ThermalStatus::THERMAL_STATUS_SHUTDOWN)),
130         IThermalListenerTest::PrintParam);
131 
132 class IThermalLevelTest : public IThermalServiceTest, public testing::WithParamInterface<int32_t> {
133   public:
PrintParam(const testing::TestParamInfo<ParamType> & info)134     static auto PrintParam(const testing::TestParamInfo<ParamType> &info) {
135         return std::to_string(info.param);
136     }
137 };
138 
TEST_P(IThermalLevelTest,TestGetStatusLevel)139 TEST_P(IThermalLevelTest, TestGetStatusLevel) {
140     int level = GetParam();
141     setThermalOverride(level);
142     mServiceStatus = getStatusFromService();
143     EXPECT_EQ(level, mServiceStatus);
144 }
145 
146 INSTANTIATE_TEST_SUITE_P(TestStatusLevels, IThermalLevelTest, testing::Range(
147         static_cast<int>(ThermalStatus::THERMAL_STATUS_NONE),
148         static_cast<int>(ThermalStatus::THERMAL_STATUS_SHUTDOWN)),
149         IThermalLevelTest::PrintParam);
150 
main(int argc,char ** argv)151 int main(int argc, char **argv) {
152     std::unique_ptr<std::thread> binderLoop;
153     binderLoop = std::make_unique<std::thread>(
154             [&] { IPCThreadState::self()->joinThreadPool(true); });
155 
156     ::testing::InitGoogleTest(&argc, argv);
157     int status = RUN_ALL_TESTS();
158     ALOGV("Test result = %d\n", status);
159 
160     return status;
161 }