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
30using namespace testing::ext;
31using namespace OHOS::PowerMgr;
32using namespace OHOS;
33using namespace std;
34const int32_t INVAILID_VALUE = -1;
35namespace {
36int32_t MockEventCb(const HdfThermalCallbackInfo& event)
37{
38    return 0;
39}
40
41bool 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 */
52HWTEST_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    delete thermalCb;
63    thermalCb = nullptr;
64    delete info;
65    info = nullptr;
66    THERMAL_HILOGD(LABEL_TEST, "ThermalClientTest001 end.");
67}
68
69/**
70 * @tc.name: ThermalClientTest002
71 * @tc.desc: listener on receive
72 * @tc.type: FUNC
73 * @tc.require: issueI5YZQ2
74 */
75HWTEST_F(ThermalClientTest, ThermalClientTest002, TestSize.Level0)
76{
77    THERMAL_HILOGD(LABEL_TEST, "ThermalClientTest002 start.");
78    using StatusCallback = std::function<void(const OHOS::HDI::ServiceManager::V1_0::ServiceStatus&)>;
79    StatusCallback cb = MockStatusCb;
80    HdiServiceStatusListener* listener = new HdiServiceStatusListener(cb);
81    EXPECT_FALSE(listener == nullptr);
82    OHOS::HDI::ServiceManager::V1_0::ServiceStatus status = {"a", 0, 0, "a"};
83    listener->OnReceive(status);
84    delete listener;
85    listener = nullptr;
86    THERMAL_HILOGD(LABEL_TEST, "ThermalClientTest002 end.");
87}
88
89/**
90 * @tc.name: ThermalClientTest003
91 * @tc.desc: subscribe callback and unsubscribe callback
92 * @tc.type: FUNC
93 * @tc.require: issueI5YZQ2
94 */
95HWTEST_F(ThermalClientTest, ThermalClientTest003, TestSize.Level0)
96{
97    THERMAL_HILOGD(LABEL_TEST, "ThermalClientTest003 start.");
98    auto& client = ThermalMgrClient::GetInstance();
99    std::vector<std::string> typeList;
100    sptr<IThermalTempCallback> tempCallback = nullptr;
101    EXPECT_FALSE(client.SubscribeThermalTempCallback(typeList, tempCallback));
102    EXPECT_FALSE(client.UnSubscribeThermalTempCallback(tempCallback));
103    sptr<MockThermalRemoteObject> sptrRemoteObj = new MockThermalRemoteObject();
104    EXPECT_FALSE(sptrRemoteObj == nullptr);
105    tempCallback = new ThermalTempCallbackProxy(sptrRemoteObj);
106    EXPECT_FALSE(tempCallback == nullptr);
107    EXPECT_TRUE(client.SubscribeThermalTempCallback(typeList, tempCallback));
108    EXPECT_TRUE(client.UnSubscribeThermalTempCallback(tempCallback));
109    sptr<IThermalLevelCallback> levelCallback = nullptr;
110    EXPECT_FALSE(client.SubscribeThermalLevelCallback(levelCallback));
111    EXPECT_FALSE(client.UnSubscribeThermalLevelCallback(levelCallback));
112    levelCallback = new ThermalLevelCallbackProxy(sptrRemoteObj);
113    EXPECT_FALSE(levelCallback == nullptr);
114    EXPECT_TRUE(client.SubscribeThermalLevelCallback(levelCallback));
115    EXPECT_TRUE(client.UnSubscribeThermalLevelCallback(levelCallback));
116    sptr<IThermalActionCallback> actionCallback = nullptr;
117    std::string actionList;
118    EXPECT_FALSE(client.SubscribeThermalActionCallback(typeList, actionList, actionCallback));
119    EXPECT_FALSE(client.UnSubscribeThermalActionCallback(actionCallback));
120    actionCallback = new ThermalActionCallbackProxy(sptrRemoteObj);
121    EXPECT_FALSE(actionCallback == nullptr);
122    EXPECT_TRUE(client.SubscribeThermalActionCallback(typeList, actionList, actionCallback));
123    EXPECT_TRUE(client.UnSubscribeThermalActionCallback(actionCallback));
124    THERMAL_HILOGD(LABEL_TEST, "ThermalClientTest003 end.");
125}
126
127/**
128 * @tc.name: ThermalClientTest004
129 * @tc.desc: ThermalSrvSensorInfo Marshalling and Unmarshalling test
130 * @tc.type: FUNC
131 * @tc.require: issueI5YZQ2
132 */
133HWTEST_F(ThermalClientTest, ThermalClientTest004, TestSize.Level0)
134{
135    THERMAL_HILOGD(LABEL_TEST, "ThermalClientTest004 start.");
136    sptr<ThermalSrvSensorInfo> info = new ThermalSrvSensorInfo();
137    MessageParcel parcel;
138    info->Unmarshalling(parcel);
139    EXPECT_TRUE(info->Marshalling(parcel));
140    THERMAL_HILOGD(LABEL_TEST, "ThermalClientTest004 end.");
141}
142
143/**
144 * @tc.name: ThermalClientTest005
145 * @tc.desc: thermalListener additional test
146 * @tc.type: FUNC
147 * @tc.require: issueI5YZQ2
148 */
149HWTEST_F(ThermalClientTest, ThermalClientTest005, TestSize.Level0)
150{
151    THERMAL_HILOGD(LABEL_TEST, "ThermalClientTest005 start.");
152    std::shared_ptr<ThermalMgrListener> thermalListener = std::make_shared<ThermalMgrListener>();
153    ASSERT_NE(thermalListener, nullptr);
154    int32_t ret = thermalListener->SubscribeLevelEvent(nullptr);
155    EXPECT_NE(ret, ERR_OK);
156    ret = thermalListener->UnSubscribeLevelEvent();
157    EXPECT_NE(ret, ERR_OK);
158    ThermalLevel level = thermalListener->GetThermalLevel();
159    EXPECT_NE(static_cast<int32_t>(level), INVAILID_VALUE);
160    sptr<IThermalLevelCallback> callback = new ThermalMgrListener::ThermalLevelCallback(nullptr);
161    bool result = callback->OnThermalLevelChanged(level);
162    EXPECT_EQ(result, false);
163    THERMAL_HILOGD(LABEL_TEST, "ThermalClientTest005 end.");
164}
165} // namespace
166