1 /*
2 * Copyright (c) 2024-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 #include <gtest/gtest.h>
16 #include <iostream>
17
18 #include "thermal_collector.h"
19
20 using namespace testing::ext;
21 using namespace OHOS::HiviewDFX;
22 using namespace OHOS::HiviewDFX::UCollectUtil;
23
24 class ThermalCollectorTest : public testing::Test {
25 public:
SetUp()26 void SetUp() {};
TearDown()27 void TearDown() {};
SetUpTestCase()28 static void SetUpTestCase() {};
TearDownTestCase()29 static void TearDownTestCase() {};
30 };
31
32 #ifdef UNIFIED_COLLECTOR_THERMAL_ENABLE
TestResult(const CollectResult<int32_t> & result)33 void TestResult(const CollectResult<int32_t>& result)
34 {
35 if (result.retCode == UCollect::UcError::SUCCESS) {
36 ASSERT_NE(result.data, 0);
37 } else {
38 ASSERT_EQ(result.retCode, UCollect::UcError::UNSUPPORT);
39 }
40 }
41
42 /**
43 * @tc.name: ThermalCollectorTest001
44 * @tc.desc: used to test ThermalCollector.CollectDevThermal
45 * @tc.type: FUNC
46 */
47 HWTEST_F(ThermalCollectorTest, ThermalCollectorTest001, TestSize.Level1)
48 {
49 std::shared_ptr<ThermalCollector> collector = ThermalCollector::Create();
50 CollectResult<int32_t> result = collector->CollectDevThermal(ThermalZone::SHELL_FRONT);
51 TestResult(result);
52 }
53
54 /**
55 * @tc.name: ThermalCollectorTest002
56 * @tc.desc: used to test ThermalCollector.CollectDevThermal
57 * @tc.type: FUNC
58 */
59 HWTEST_F(ThermalCollectorTest, ThermalCollectorTest002, TestSize.Level1)
60 {
61 std::shared_ptr<ThermalCollector> collector = ThermalCollector::Create();
62 CollectResult<int32_t> result = collector->CollectDevThermal(ThermalZone::SHELL_FRAME);
63 TestResult(result);
64 }
65
66 /**
67 * @tc.name: ThermalCollectorTest003
68 * @tc.desc: used to test ThermalCollector.CollectDevThermal
69 * @tc.type: FUNC
70 */
71 HWTEST_F(ThermalCollectorTest, ThermalCollectorTest003, TestSize.Level1)
72 {
73 std::shared_ptr<ThermalCollector> collector = ThermalCollector::Create();
74 CollectResult<int32_t> result = collector->CollectDevThermal(ThermalZone::SHELL_BACK);
75 TestResult(result);
76 }
77
78 /**
79 * @tc.name: ThermalCollectorTest004
80 * @tc.desc: used to test ThermalCollector.CollectDevThermal
81 * @tc.type: FUNC
82 */
83 HWTEST_F(ThermalCollectorTest, ThermalCollectorTest004, TestSize.Level1)
84 {
85 std::shared_ptr<ThermalCollector> collector = ThermalCollector::Create();
86 CollectResult<int32_t> result = collector->CollectDevThermal(ThermalZone::SOC_THERMAL);
87 TestResult(result);
88 }
89
90 /**
91 * @tc.name: ThermalCollectorTest005
92 * @tc.desc: used to test ThermalCollector.CollectDevThermal
93 * @tc.type: FUNC
94 */
95 HWTEST_F(ThermalCollectorTest, ThermalCollectorTest005, TestSize.Level1)
96 {
97 std::shared_ptr<ThermalCollector> collector = ThermalCollector::Create();
98 CollectResult<int32_t> result = collector->CollectDevThermal(ThermalZone::SYSTEM);
99 TestResult(result);
100 }
101
102 /**
103 * @tc.name: ThermalCollectorTest006
104 * @tc.desc: used to test ThermalCollector.CollectThermaLevel
105 * @tc.type: FUNC
106 */
107 HWTEST_F(ThermalCollectorTest, ThermalCollectorTest006, TestSize.Level1)
108 {
109 std::shared_ptr<ThermalCollector> collector = ThermalCollector::Create();
110 CollectResult<uint32_t> result = collector->CollectThermaLevel();
111 #ifdef THERMAL_MANAGER_ENABLE
112 ASSERT_EQ(result.retCode, UCollect::UcError::SUCCESS);
113 #else
114 ASSERT_EQ(result.retCode, UCollect::UcError::UNSUPPORT);
115 #endif
116 }
117 #else
118
119 /**
120 * @tc.name: ThermalCollectorTest001
121 * @tc.desc: used to test empty ThermalCollector
122 * @tc.type: FUNC
123 */
124 HWTEST_F(ThermalCollectorTest, ThermalCollectorTest001, TestSize.Level1)
125 {
126 std::shared_ptr<ThermalCollector> collector = ThermalCollector::Create();
127 auto result1 = collector->CollectDevThermal(ThermalZone::SHELL_FRONT);
128 ASSERT_EQ(result1.retCode, UCollect::UcError::FEATURE_CLOSED);
129
130 auto result2 = collector->CollectThermaLevel();
131 ASSERT_EQ(result2.retCode, UCollect::UcError::FEATURE_CLOSED);
132 }
133 #endif
134