1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <gtest/gtest.h>
17
18 #include "ohos.thermal.proj.hpp"
19 #include "ohos.thermal.impl.hpp"
20 #include "ohos.thermal.user.hpp"
21 #include "taihe/runtime.hpp"
22 #include "thermal_mgr_client.h"
23 #include "thermal_log.h"
24 #include "thermal_level_callback.h"
25
26 using namespace taihe;
27 using namespace ohos::thermal;
28 using namespace OHOS::PowerMgr;
29
30 using namespace testing;
31 using namespace testing::ext;
32
33 namespace {
34 bool g_pass = false;
35 bool g_flag = false;
36 }
37
38 namespace taihe {
set_business_error(int32_t err_code,taihe::string_view msg)39 void set_business_error(int32_t err_code, taihe::string_view msg)
40 {
41 (void)err_code;
42 (void)msg;
43 }
44 }
45
46 namespace OHOS::PowerMgr {
SubscribeThermalLevelCallback(const sptr<IThermalLevelCallback> & callback)47 bool ThermalMgrClient::SubscribeThermalLevelCallback(const sptr<IThermalLevelCallback>& callback)
48 {
49 g_pass = true;
50 return true;
51 }
52
UnSubscribeThermalLevelCallback(const sptr<IThermalLevelCallback> & callback)53 bool ThermalMgrClient::UnSubscribeThermalLevelCallback(const sptr<IThermalLevelCallback>& callback)
54 {
55 g_pass = true;
56 return true;
57 }
58
GetThermalLevel()59 ThermalLevel ThermalMgrClient::GetThermalLevel()
60 {
61 g_pass = true;
62 ThermalLevel level = ThermalLevel::COOL;
63 return level;
64 }
65 } // namespace OHOS::PowerMgr
66
67 namespace {
68 class ThermalTaiheNativeTest : public ::testing::Test {
69 public:
SetUpTestCase()70 static void SetUpTestCase() {}
TearDownTestCase()71 static void TearDownTestCase() {}
SetUp()72 void SetUp() {}
TearDown()73 void TearDown()
74 {
75 g_pass = false;
76 g_flag = false;
77 }
78 };
79
80 /**
81 * @tc.name: ThermalTaiheNativeTest_001
82 * @tc.desc: test thermal taihe native
83 * @tc.type: FUNC
84 * @tc.require: issue#ICAK9Z
85 */
86 HWTEST_F(ThermalTaiheNativeTest, ThermalTaiheNativeTest_001, TestSize.Level1)
87 {
88 THERMAL_HILOGI(LABEL_TEST, "ThermalTaiheNativeTest_001 start");
89 struct MyCallbackImpl {
operator ()__anon599ca1790211::MyCallbackImpl90 void operator()(ohos::thermal::ThermalLevel level)
91 {
92 g_flag = true;
93 EXPECT_TRUE(level.get_key() == ohos::thermal::ThermalLevel::key_t::COOL);
94 }
95 };
96 callback<void(ohos::thermal::ThermalLevel)> thermalCb =
97 ::taihe::make_holder<MyCallbackImpl, ::taihe::callback<void(::ohos::thermal::ThermalLevel)>>();
98 RegisterThermalLevelCallback(thermalCb);
99
100 std::shared_ptr<ThermalLevelCallback> cb = std::make_shared<ThermalLevelCallback>();
101 cb->UpdateCallback(thermalCb);
102 OHOS::PowerMgr::ThermalLevel level = OHOS::PowerMgr::ThermalLevel::COOL;
103 bool ret = cb->OnThermalLevelChanged(level);
104 EXPECT_TRUE(g_flag);
105 EXPECT_TRUE(ret);
106 EXPECT_TRUE(g_pass);
107 THERMAL_HILOGI(LABEL_TEST, "ThermalTaiheNativeTest_001 end");
108 }
109
110 /**
111 * @tc.name: ThermalTaiheNativeTest_002
112 * @tc.desc: test thermal taihe native
113 * @tc.type: FUNC
114 * @tc.require: issue#ICAK9Z
115 */
116 HWTEST_F(ThermalTaiheNativeTest, ThermalTaiheNativeTest_002, TestSize.Level1)
117 {
118 THERMAL_HILOGI(LABEL_TEST, "ThermalTaiheNativeTest_002 start");
119 optional<callback<void(MyUndefined const&)>> thermalCb;
120 UnregisterThermalLevelCallback(thermalCb);
121
122 struct MyCallbackImpl {
operator ()__anon599ca1790211::MyCallbackImpl123 void operator()(MyUndefined const& level)
124 {
125 g_flag = true;
126 EXPECT_TRUE(level.holds_Undefined());
127 }
128 };
129 ::taihe::callback<void(::ohos::thermal::MyUndefined const&)>* thermalCbPtr = nullptr;
130 callback<void(MyUndefined const&)> undefCb =
131 ::taihe::make_holder<MyCallbackImpl, ::taihe::callback<void(MyUndefined const&)>>();
132 thermalCbPtr = new ::taihe::callback<void(::ohos::thermal::MyUndefined const&)>(std::move(undefCb));
133 optional<callback<void(MyUndefined const&)>> notNullThermalCb(thermalCbPtr);
134 UnregisterThermalLevelCallback(notNullThermalCb);
135 EXPECT_TRUE(g_flag);
136 EXPECT_TRUE(g_pass);
137 THERMAL_HILOGI(LABEL_TEST, "ThermalTaiheNativeTest_002 end");
138 }
139
140 /**
141 * @tc.name: ThermalTaiheNativeTest_003
142 * @tc.desc: test thermal taihe native
143 * @tc.type: FUNC
144 * @tc.require: issue#ICAK9Z
145 */
146 HWTEST_F(ThermalTaiheNativeTest, ThermalTaiheNativeTest_003, TestSize.Level1)
147 {
148 THERMAL_HILOGI(LABEL_TEST, "ThermalTaiheNativeTest_003 start");
149 ohos::thermal::ThermalLevel level = GetLevel();
150 ohos::thermal::ThermalLevel expectLevel(ohos::thermal::ThermalLevel::key_t::COOL);
151 EXPECT_TRUE(level == expectLevel);
152 EXPECT_TRUE(g_pass);
153 THERMAL_HILOGI(LABEL_TEST, "ThermalTaiheNativeTest_003 end");
154 }
155
156 /**
157 * @tc.name: ThermalTaiheNativeTest_004
158 * @tc.desc: test thermal taihe native
159 * @tc.type: FUNC
160 * @tc.require: issue#ICAK9Z
161 */
162 HWTEST_F(ThermalTaiheNativeTest, ThermalTaiheNativeTest_004, TestSize.Level1)
163 {
164 THERMAL_HILOGI(LABEL_TEST, "ThermalTaiheNativeTest_004 start");
165 std::shared_ptr<ThermalLevelCallback> cb = std::make_shared<ThermalLevelCallback>();
166 OHOS::PowerMgr::ThermalLevel level = OHOS::PowerMgr::ThermalLevel::COOL;
167 bool ret = cb->OnThermalLevelChanged(level);
168 EXPECT_TRUE(ret);
169 EXPECT_FALSE(g_flag);
170 THERMAL_HILOGI(LABEL_TEST, "ThermalTaiheNativeTest_004 end");
171 }
172 } // namespace