• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
29 using namespace testing::ext;
30 using namespace OHOS::PowerMgr;
31 using namespace OHOS;
32 using namespace std;
33 
34 namespace {
MockEventCb(const HdfThermalCallbackInfo & event)35 int32_t MockEventCb(const HdfThermalCallbackInfo& event)
36 {
37     return 0;
38 }
39 
MockStatusCb(const OHOS::HDI::ServiceManager::V1_0::ServiceStatus &)40 bool MockStatusCb(const OHOS::HDI::ServiceManager::V1_0::ServiceStatus&)
41 {
42     return true;
43 }
44 
45 /**
46  * @tc.name: ThermalClientTest001
47  * @tc.desc: register thermal event
48  * @tc.type: FUNC
49  * @tc.require: issueI5YZQ2
50  */
51 HWTEST_F(ThermalClientTest, ThermalClientTest001, TestSize.Level0)
52 {
53     THERMAL_HILOGD(LABEL_TEST, "ThermalClientTest001 start.");
54     ThermalCallback* thermalCb = new ThermalCallback();
55     HdfThermalCallbackInfo* info = new HdfThermalCallbackInfo();
56     EXPECT_TRUE(thermalCb->OnThermalDataEvent(*info) == HDF_FAILURE);
57     using ThermalEventCallback = std::function<int32_t(const HdfThermalCallbackInfo& event)>;
58     ThermalEventCallback cb = MockEventCb;
59     EXPECT_TRUE(thermalCb->RegisterThermalEvent(cb) == HDF_SUCCESS);
60     EXPECT_TRUE(thermalCb->OnThermalDataEvent(*info) != HDF_FAILURE);
61     THERMAL_HILOGD(LABEL_TEST, "ThermalClientTest001 end.");
62 }
63 
64 /**
65  * @tc.name: ThermalClientTest002
66  * @tc.desc: listener on receive
67  * @tc.type: FUNC
68  * @tc.require: issueI5YZQ2
69  */
70 HWTEST_F(ThermalClientTest, ThermalClientTest002, TestSize.Level0)
71 {
72     THERMAL_HILOGD(LABEL_TEST, "ThermalClientTest002 start.");
73     using StatusCallback = std::function<void(const OHOS::HDI::ServiceManager::V1_0::ServiceStatus&)>;
74     StatusCallback cb = MockStatusCb;
75     HdiServiceStatusListener* listener = new HdiServiceStatusListener(cb);
76     EXPECT_FALSE(listener == nullptr);
77     OHOS::HDI::ServiceManager::V1_0::ServiceStatus status = {"a", 0, 0, "a"};
78     listener->OnReceive(status);
79     THERMAL_HILOGD(LABEL_TEST, "ThermalClientTest002 end.");
80 }
81 
82 /**
83  * @tc.name: ThermalClientTest003
84  * @tc.desc: subscribe callback and unsubscribe callback
85  * @tc.type: FUNC
86  * @tc.require: issueI5YZQ2
87  */
88 HWTEST_F(ThermalClientTest, ThermalClientTest003, TestSize.Level0)
89 {
90     THERMAL_HILOGD(LABEL_TEST, "ThermalClientTest003 start.");
91     auto& client = ThermalMgrClient::GetInstance();
92     std::vector<std::string> typeList;
93     sptr<IThermalTempCallback> tempCallback = nullptr;
94     EXPECT_FALSE(client.SubscribeThermalTempCallback(typeList, tempCallback));
95     EXPECT_FALSE(client.UnSubscribeThermalTempCallback(tempCallback));
96     sptr<MockThermalRemoteObject> sptrRemoteObj = new MockThermalRemoteObject();
97     EXPECT_FALSE(sptrRemoteObj == nullptr);
98     tempCallback = new ThermalTempCallbackProxy(sptrRemoteObj);
99     EXPECT_FALSE(tempCallback == nullptr);
100     EXPECT_TRUE(client.SubscribeThermalTempCallback(typeList, tempCallback));
101     EXPECT_TRUE(client.UnSubscribeThermalTempCallback(tempCallback));
102     sptr<IThermalLevelCallback> levelCallback = nullptr;
103     EXPECT_FALSE(client.SubscribeThermalLevelCallback(levelCallback));
104     EXPECT_FALSE(client.UnSubscribeThermalLevelCallback(levelCallback));
105     levelCallback = new ThermalLevelCallbackProxy(sptrRemoteObj);
106     EXPECT_FALSE(levelCallback == nullptr);
107     EXPECT_TRUE(client.SubscribeThermalLevelCallback(levelCallback));
108     EXPECT_TRUE(client.UnSubscribeThermalLevelCallback(levelCallback));
109     sptr<IThermalActionCallback> actionCallback = nullptr;
110     std::string actionList;
111     EXPECT_FALSE(client.SubscribeThermalActionCallback(typeList, actionList, actionCallback));
112     EXPECT_FALSE(client.UnSubscribeThermalActionCallback(actionCallback));
113     actionCallback = new ThermalActionCallbackProxy(sptrRemoteObj);
114     EXPECT_FALSE(actionCallback == nullptr);
115     EXPECT_TRUE(client.SubscribeThermalActionCallback(typeList, actionList, actionCallback));
116     EXPECT_TRUE(client.UnSubscribeThermalActionCallback(actionCallback));
117     THERMAL_HILOGD(LABEL_TEST, "ThermalClientTest003 end.");
118 }
119 
120 HWTEST_F(ThermalClientTest, ThermalClientTest004, TestSize.Level0)
121 {
122     THERMAL_HILOGD(LABEL_TEST, "ThermalClientTest004 start.");
123     sptr<ThermalSrvSensorInfo> info = new ThermalSrvSensorInfo();
124     MessageParcel parcel;
125     info->Unmarshalling(parcel);
126     EXPECT_TRUE(info->Marshalling(parcel));
127     THERMAL_HILOGD(LABEL_TEST, "ThermalClientTest004 end.");
128 }
129 } // namespace
130