1 /*
2  * Copyright (c) 2022 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 "thermal_client_test.h"
17 
18 #include "hdf_base.h"
19 #include "hdi_service_status_listener.h"
20 #include "mock_thermal_remote_object.h"
21 #include "thermal_action_callback_proxy.h"
22 #include "thermal_callback.h"
23 #include "thermal_level_callback_proxy.h"
24 #include "thermal_log.h"
25 #include "thermal_mgr_client.h"
26 #include "thermal_srv_sensor_info.h"
27 #include "thermal_temp_callback_proxy.h"
28 #include "thermal_mgr_listener.h"
29 
30 using namespace testing::ext;
31 using namespace OHOS::PowerMgr;
32 using namespace OHOS;
33 using namespace std;
34 const int32_t INVAILID_VALUE = -1;
35 namespace {
MockEventCb(const HdfThermalCallbackInfo & event)36 int32_t MockEventCb(const HdfThermalCallbackInfo& event)
37 {
38     return 0;
39 }
40 
MockStatusCb(const OHOS::HDI::ServiceManager::V1_0::ServiceStatus &)41 bool MockStatusCb(const OHOS::HDI::ServiceManager::V1_0::ServiceStatus&)
42 {
43     return true;
44 }
45 
46 /**
47  * @tc.name: ThermalClientTest001
48  * @tc.desc: register thermal event
49  * @tc.type: FUNC
50  * @tc.require: issueI5YZQ2
51  */
52 HWTEST_F(ThermalClientTest, ThermalClientTest001, TestSize.Level0)
53 {
54     THERMAL_HILOGD(LABEL_TEST, "ThermalClientTest001 start.");
55     ThermalCallback* thermalCb = new ThermalCallback();
56     HdfThermalCallbackInfo* info = new HdfThermalCallbackInfo();
57     EXPECT_TRUE(thermalCb->OnThermalDataEvent(*info) == HDF_FAILURE);
58     using ThermalEventCallback = std::function<int32_t(const HdfThermalCallbackInfo& event)>;
59     ThermalEventCallback cb = MockEventCb;
60     EXPECT_TRUE(thermalCb->RegisterThermalEvent(cb) == HDF_SUCCESS);
61     EXPECT_TRUE(thermalCb->OnThermalDataEvent(*info) != HDF_FAILURE);
62     THERMAL_HILOGD(LABEL_TEST, "ThermalClientTest001 end.");
63 }
64 
65 /**
66  * @tc.name: ThermalClientTest002
67  * @tc.desc: listener on receive
68  * @tc.type: FUNC
69  * @tc.require: issueI5YZQ2
70  */
71 HWTEST_F(ThermalClientTest, ThermalClientTest002, TestSize.Level0)
72 {
73     THERMAL_HILOGD(LABEL_TEST, "ThermalClientTest002 start.");
74     using StatusCallback = std::function<void(const OHOS::HDI::ServiceManager::V1_0::ServiceStatus&)>;
75     StatusCallback cb = MockStatusCb;
76     HdiServiceStatusListener* listener = new HdiServiceStatusListener(cb);
77     EXPECT_FALSE(listener == nullptr);
78     OHOS::HDI::ServiceManager::V1_0::ServiceStatus status = {"a", 0, 0, "a"};
79     listener->OnReceive(status);
80     THERMAL_HILOGD(LABEL_TEST, "ThermalClientTest002 end.");
81 }
82 
83 /**
84  * @tc.name: ThermalClientTest003
85  * @tc.desc: subscribe callback and unsubscribe callback
86  * @tc.type: FUNC
87  * @tc.require: issueI5YZQ2
88  */
89 HWTEST_F(ThermalClientTest, ThermalClientTest003, TestSize.Level0)
90 {
91     THERMAL_HILOGD(LABEL_TEST, "ThermalClientTest003 start.");
92     auto& client = ThermalMgrClient::GetInstance();
93     std::vector<std::string> typeList;
94     sptr<IThermalTempCallback> tempCallback = nullptr;
95     EXPECT_FALSE(client.SubscribeThermalTempCallback(typeList, tempCallback));
96     EXPECT_FALSE(client.UnSubscribeThermalTempCallback(tempCallback));
97     sptr<MockThermalRemoteObject> sptrRemoteObj = new MockThermalRemoteObject();
98     EXPECT_FALSE(sptrRemoteObj == nullptr);
99     tempCallback = new ThermalTempCallbackProxy(sptrRemoteObj);
100     EXPECT_FALSE(tempCallback == nullptr);
101     EXPECT_TRUE(client.SubscribeThermalTempCallback(typeList, tempCallback));
102     EXPECT_TRUE(client.UnSubscribeThermalTempCallback(tempCallback));
103     sptr<IThermalLevelCallback> levelCallback = nullptr;
104     EXPECT_FALSE(client.SubscribeThermalLevelCallback(levelCallback));
105     EXPECT_FALSE(client.UnSubscribeThermalLevelCallback(levelCallback));
106     levelCallback = new ThermalLevelCallbackProxy(sptrRemoteObj);
107     EXPECT_FALSE(levelCallback == nullptr);
108     EXPECT_TRUE(client.SubscribeThermalLevelCallback(levelCallback));
109     EXPECT_TRUE(client.UnSubscribeThermalLevelCallback(levelCallback));
110     sptr<IThermalActionCallback> actionCallback = nullptr;
111     std::string actionList;
112     EXPECT_FALSE(client.SubscribeThermalActionCallback(typeList, actionList, actionCallback));
113     EXPECT_FALSE(client.UnSubscribeThermalActionCallback(actionCallback));
114     actionCallback = new ThermalActionCallbackProxy(sptrRemoteObj);
115     EXPECT_FALSE(actionCallback == nullptr);
116     EXPECT_TRUE(client.SubscribeThermalActionCallback(typeList, actionList, actionCallback));
117     EXPECT_TRUE(client.UnSubscribeThermalActionCallback(actionCallback));
118     THERMAL_HILOGD(LABEL_TEST, "ThermalClientTest003 end.");
119 }
120 
121 /**
122  * @tc.name: ThermalClientTest004
123  * @tc.desc: ThermalSrvSensorInfo Marshalling and Unmarshalling test
124  * @tc.type: FUNC
125  * @tc.require: issueI5YZQ2
126  */
127 HWTEST_F(ThermalClientTest, ThermalClientTest004, TestSize.Level0)
128 {
129     THERMAL_HILOGD(LABEL_TEST, "ThermalClientTest004 start.");
130     sptr<ThermalSrvSensorInfo> info = new ThermalSrvSensorInfo();
131     MessageParcel parcel;
132     info->Unmarshalling(parcel);
133     EXPECT_TRUE(info->Marshalling(parcel));
134     THERMAL_HILOGD(LABEL_TEST, "ThermalClientTest004 end.");
135 }
136 
137 /**
138  * @tc.name: ThermalClientTest005
139  * @tc.desc: thermalListener additional test
140  * @tc.type: FUNC
141  * @tc.require: issueI5YZQ2
142  */
143 HWTEST_F(ThermalClientTest, ThermalClientTest005, TestSize.Level0)
144 {
145     THERMAL_HILOGD(LABEL_TEST, "ThermalClientTest005 start.");
146     std::shared_ptr<ThermalMgrListener> thermalListener = std::make_shared<ThermalMgrListener>();
147     ASSERT_NE(thermalListener, nullptr);
148     int32_t ret = thermalListener->SubscribeLevelEvent(nullptr);
149     EXPECT_NE(ret, ERR_OK);
150     ret = thermalListener->UnSubscribeLevelEvent();
151     EXPECT_NE(ret, ERR_OK);
152     ThermalLevel level = thermalListener->GetThermalLevel();
153     EXPECT_NE(static_cast<int32_t>(level), INVAILID_VALUE);
154     sptr<IThermalLevelCallback> callback = new ThermalMgrListener::ThermalLevelCallback(nullptr);
155     bool result = callback->OnThermalLevelChanged(level);
156     EXPECT_EQ(result, false);
157     THERMAL_HILOGD(LABEL_TEST, "ThermalClientTest005 end.");
158 }
159 } // namespace
160