blob: 741495878ad8b2914ce3e395f0cae192502b1902 [file] [log] [blame]
Chris Ye1a5a8882020-01-15 10:51:47 -08001/*
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
32using namespace android;
33using namespace android::os;
34using namespace std::chrono_literals;
35
Steven Moreland9ae49342022-07-27 16:40:13 +000036class IThermalServiceTestListener : public BnThermalStatusListener {
37 public:
38 virtual binder::Status onStatusChange(int status) override;
39 std::condition_variable mCondition;
40 int mListenerStatus = 0;
41 std::mutex mMutex;
42};
43
44binder::Status IThermalServiceTestListener::onStatusChange(int status) {
45 std::unique_lock<std::mutex> lock(mMutex);
46 mListenerStatus = status;
47 ALOGI("IThermalServiceTestListener::notifyListener %d", mListenerStatus);
48 mCondition.notify_all();
49 return binder::Status::ok();
50}
51
52class IThermalServiceTest : public testing::Test {
Chris Ye1a5a8882020-01-15 10:51:47 -080053 public:
54 IThermalServiceTest();
55 void setThermalOverride(int level);
Chris Ye1a5a8882020-01-15 10:51:47 -080056 int getStatusFromService();
57 void SetUp() override;
58 void TearDown() override;
59 protected:
60 sp<IThermalService> mThermalSvc;
Chris Ye1a5a8882020-01-15 10:51:47 -080061 int mServiceStatus;
Steven Moreland9ae49342022-07-27 16:40:13 +000062 sp<IThermalServiceTestListener> mCallback;
Chris Ye1a5a8882020-01-15 10:51:47 -080063};
64
65IThermalServiceTest::IThermalServiceTest()
Steven Moreland9ae49342022-07-27 16:40:13 +000066 : mServiceStatus(0),
67 mCallback(sp<IThermalServiceTestListener>::make()) {
Chris Ye1a5a8882020-01-15 10:51:47 -080068}
69
70void IThermalServiceTest::setThermalOverride(int level) {
71 std::string cmdStr = "cmd thermalservice override-status " + std::to_string(level);
72 system(cmdStr.c_str());
73}
74
Chris Ye1a5a8882020-01-15 10:51:47 -080075int IThermalServiceTest::getStatusFromService() {
76 int status;
77 binder::Status ret = mThermalSvc->getCurrentThermalStatus(&status);
78 if (ret.isOk()) {
79 return status;
80 } else {
81 return BAD_VALUE;
82 }
83}
84
85void IThermalServiceTest::SetUp() {
86 setThermalOverride(0);
87 // use checkService() to avoid blocking if thermal service is not up yet
88 sp<IBinder> binder =
89 defaultServiceManager()->checkService(String16("thermalservice"));
90 EXPECT_NE(binder, nullptr);
91 mThermalSvc = interface_cast<IThermalService>(binder);
92 EXPECT_NE(mThermalSvc, nullptr);
Lais Andradea3c8a602020-09-07 10:13:34 +000093 // Lock mutex for operation, so listener will only be processed after wait_for is called
Steven Moreland9ae49342022-07-27 16:40:13 +000094 std::unique_lock<std::mutex> lock(mCallback->mMutex);
Chris Ye1a5a8882020-01-15 10:51:47 -080095 bool success = false;
Steven Moreland9ae49342022-07-27 16:40:13 +000096 binder::Status ret = mThermalSvc->registerThermalStatusListener(mCallback, &success);
Lais Andradea3c8a602020-09-07 10:13:34 +000097 // Check the result
Chris Ye1a5a8882020-01-15 10:51:47 -080098 ASSERT_TRUE(success);
99 ASSERT_TRUE(ret.isOk());
100 // Wait for listener called after registration, shouldn't timeout
Steven Moreland9ae49342022-07-27 16:40:13 +0000101 EXPECT_NE(mCallback->mCondition.wait_for(lock, 1s), std::cv_status::timeout);
Chris Ye1a5a8882020-01-15 10:51:47 -0800102}
103
104void IThermalServiceTest::TearDown() {
105 bool success = false;
Steven Moreland9ae49342022-07-27 16:40:13 +0000106 binder::Status ret = mThermalSvc->unregisterThermalStatusListener(mCallback, &success);
Chris Ye1a5a8882020-01-15 10:51:47 -0800107 ASSERT_TRUE(success);
108 ASSERT_TRUE(ret.isOk());
109}
110
111class IThermalListenerTest : public IThermalServiceTest, public testing::WithParamInterface<int32_t> {
112 public:
113 static auto PrintParam(const testing::TestParamInfo<ParamType> &info) {
114 return std::to_string(info.param);
115 }
116};
117
118TEST_P(IThermalListenerTest, TestListener) {
119 int level = GetParam();
Lais Andradea3c8a602020-09-07 10:13:34 +0000120 // Lock mutex for operation, so listener will only be processed after wait_for is called
Steven Moreland9ae49342022-07-27 16:40:13 +0000121 std::unique_lock<std::mutex> lock(mCallback->mMutex);
Chris Ye1a5a8882020-01-15 10:51:47 -0800122 // Set the override thermal status
123 setThermalOverride(level);
124 // Wait for listener called, shouldn't timeout
Steven Moreland9ae49342022-07-27 16:40:13 +0000125 EXPECT_NE(mCallback->mCondition.wait_for(lock, 1s), std::cv_status::timeout);
Chris Ye1a5a8882020-01-15 10:51:47 -0800126 // Check the result
Steven Moreland9ae49342022-07-27 16:40:13 +0000127 EXPECT_EQ(level, mCallback->mListenerStatus);
128 ALOGI("Thermal listener status %d, expecting %d", mCallback->mListenerStatus, level);
Chris Ye1a5a8882020-01-15 10:51:47 -0800129}
130
131INSTANTIATE_TEST_SUITE_P(TestListenerLevels, IThermalListenerTest, testing::Range(
132 static_cast<int>(ThermalStatus::THERMAL_STATUS_LIGHT),
133 static_cast<int>(ThermalStatus::THERMAL_STATUS_SHUTDOWN)),
134 IThermalListenerTest::PrintParam);
135
136class IThermalLevelTest : public IThermalServiceTest, public testing::WithParamInterface<int32_t> {
137 public:
138 static auto PrintParam(const testing::TestParamInfo<ParamType> &info) {
139 return std::to_string(info.param);
140 }
141};
142
143TEST_P(IThermalLevelTest, TestGetStatusLevel) {
144 int level = GetParam();
145 setThermalOverride(level);
146 mServiceStatus = getStatusFromService();
147 EXPECT_EQ(level, mServiceStatus);
148}
149
150INSTANTIATE_TEST_SUITE_P(TestStatusLevels, IThermalLevelTest, testing::Range(
151 static_cast<int>(ThermalStatus::THERMAL_STATUS_NONE),
152 static_cast<int>(ThermalStatus::THERMAL_STATUS_SHUTDOWN)),
153 IThermalLevelTest::PrintParam);
154
155int main(int argc, char **argv) {
156 std::unique_ptr<std::thread> binderLoop;
157 binderLoop = std::make_unique<std::thread>(
158 [&] { IPCThreadState::self()->joinThreadPool(true); });
159
160 ::testing::InitGoogleTest(&argc, argv);
161 int status = RUN_ALL_TESTS();
162 ALOGV("Test result = %d\n", status);
163
164 return status;
Steven Moreland9ae49342022-07-27 16:40:13 +0000165}