137a09cd7Sopenharmony_ci/*
237a09cd7Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
337a09cd7Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
437a09cd7Sopenharmony_ci * you may not use this file except in compliance with the License.
537a09cd7Sopenharmony_ci * You may obtain a copy of the License at
637a09cd7Sopenharmony_ci *
737a09cd7Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
837a09cd7Sopenharmony_ci *
937a09cd7Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1037a09cd7Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1137a09cd7Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1237a09cd7Sopenharmony_ci * See the License for the specific language governing permissions and
1337a09cd7Sopenharmony_ci * limitations under the License.
1437a09cd7Sopenharmony_ci */
1537a09cd7Sopenharmony_ci
1637a09cd7Sopenharmony_ci#include "thermal_client_test.h"
1737a09cd7Sopenharmony_ci
1837a09cd7Sopenharmony_ci#include "hdf_base.h"
1937a09cd7Sopenharmony_ci#include "hdi_service_status_listener.h"
2037a09cd7Sopenharmony_ci#include "mock_thermal_remote_object.h"
2137a09cd7Sopenharmony_ci#include "thermal_action_callback_proxy.h"
2237a09cd7Sopenharmony_ci#include "thermal_callback.h"
2337a09cd7Sopenharmony_ci#include "thermal_level_callback_proxy.h"
2437a09cd7Sopenharmony_ci#include "thermal_log.h"
2537a09cd7Sopenharmony_ci#include "thermal_mgr_client.h"
2637a09cd7Sopenharmony_ci#include "thermal_srv_sensor_info.h"
2737a09cd7Sopenharmony_ci#include "thermal_temp_callback_proxy.h"
2837a09cd7Sopenharmony_ci#include "thermal_mgr_listener.h"
2937a09cd7Sopenharmony_ci
3037a09cd7Sopenharmony_ciusing namespace testing::ext;
3137a09cd7Sopenharmony_ciusing namespace OHOS::PowerMgr;
3237a09cd7Sopenharmony_ciusing namespace OHOS;
3337a09cd7Sopenharmony_ciusing namespace std;
3437a09cd7Sopenharmony_ciconst int32_t INVAILID_VALUE = -1;
3537a09cd7Sopenharmony_cinamespace {
3637a09cd7Sopenharmony_ciint32_t MockEventCb(const HdfThermalCallbackInfo& event)
3737a09cd7Sopenharmony_ci{
3837a09cd7Sopenharmony_ci    return 0;
3937a09cd7Sopenharmony_ci}
4037a09cd7Sopenharmony_ci
4137a09cd7Sopenharmony_cibool MockStatusCb(const OHOS::HDI::ServiceManager::V1_0::ServiceStatus&)
4237a09cd7Sopenharmony_ci{
4337a09cd7Sopenharmony_ci    return true;
4437a09cd7Sopenharmony_ci}
4537a09cd7Sopenharmony_ci
4637a09cd7Sopenharmony_ci/**
4737a09cd7Sopenharmony_ci * @tc.name: ThermalClientTest001
4837a09cd7Sopenharmony_ci * @tc.desc: register thermal event
4937a09cd7Sopenharmony_ci * @tc.type: FUNC
5037a09cd7Sopenharmony_ci * @tc.require: issueI5YZQ2
5137a09cd7Sopenharmony_ci */
5237a09cd7Sopenharmony_ciHWTEST_F(ThermalClientTest, ThermalClientTest001, TestSize.Level0)
5337a09cd7Sopenharmony_ci{
5437a09cd7Sopenharmony_ci    THERMAL_HILOGD(LABEL_TEST, "ThermalClientTest001 start.");
5537a09cd7Sopenharmony_ci    ThermalCallback* thermalCb = new ThermalCallback();
5637a09cd7Sopenharmony_ci    HdfThermalCallbackInfo* info = new HdfThermalCallbackInfo();
5737a09cd7Sopenharmony_ci    EXPECT_TRUE(thermalCb->OnThermalDataEvent(*info) == HDF_FAILURE);
5837a09cd7Sopenharmony_ci    using ThermalEventCallback = std::function<int32_t(const HdfThermalCallbackInfo& event)>;
5937a09cd7Sopenharmony_ci    ThermalEventCallback cb = MockEventCb;
6037a09cd7Sopenharmony_ci    EXPECT_TRUE(thermalCb->RegisterThermalEvent(cb) == HDF_SUCCESS);
6137a09cd7Sopenharmony_ci    EXPECT_TRUE(thermalCb->OnThermalDataEvent(*info) != HDF_FAILURE);
6237a09cd7Sopenharmony_ci    delete thermalCb;
6337a09cd7Sopenharmony_ci    thermalCb = nullptr;
6437a09cd7Sopenharmony_ci    delete info;
6537a09cd7Sopenharmony_ci    info = nullptr;
6637a09cd7Sopenharmony_ci    THERMAL_HILOGD(LABEL_TEST, "ThermalClientTest001 end.");
6737a09cd7Sopenharmony_ci}
6837a09cd7Sopenharmony_ci
6937a09cd7Sopenharmony_ci/**
7037a09cd7Sopenharmony_ci * @tc.name: ThermalClientTest002
7137a09cd7Sopenharmony_ci * @tc.desc: listener on receive
7237a09cd7Sopenharmony_ci * @tc.type: FUNC
7337a09cd7Sopenharmony_ci * @tc.require: issueI5YZQ2
7437a09cd7Sopenharmony_ci */
7537a09cd7Sopenharmony_ciHWTEST_F(ThermalClientTest, ThermalClientTest002, TestSize.Level0)
7637a09cd7Sopenharmony_ci{
7737a09cd7Sopenharmony_ci    THERMAL_HILOGD(LABEL_TEST, "ThermalClientTest002 start.");
7837a09cd7Sopenharmony_ci    using StatusCallback = std::function<void(const OHOS::HDI::ServiceManager::V1_0::ServiceStatus&)>;
7937a09cd7Sopenharmony_ci    StatusCallback cb = MockStatusCb;
8037a09cd7Sopenharmony_ci    HdiServiceStatusListener* listener = new HdiServiceStatusListener(cb);
8137a09cd7Sopenharmony_ci    EXPECT_FALSE(listener == nullptr);
8237a09cd7Sopenharmony_ci    OHOS::HDI::ServiceManager::V1_0::ServiceStatus status = {"a", 0, 0, "a"};
8337a09cd7Sopenharmony_ci    listener->OnReceive(status);
8437a09cd7Sopenharmony_ci    delete listener;
8537a09cd7Sopenharmony_ci    listener = nullptr;
8637a09cd7Sopenharmony_ci    THERMAL_HILOGD(LABEL_TEST, "ThermalClientTest002 end.");
8737a09cd7Sopenharmony_ci}
8837a09cd7Sopenharmony_ci
8937a09cd7Sopenharmony_ci/**
9037a09cd7Sopenharmony_ci * @tc.name: ThermalClientTest003
9137a09cd7Sopenharmony_ci * @tc.desc: subscribe callback and unsubscribe callback
9237a09cd7Sopenharmony_ci * @tc.type: FUNC
9337a09cd7Sopenharmony_ci * @tc.require: issueI5YZQ2
9437a09cd7Sopenharmony_ci */
9537a09cd7Sopenharmony_ciHWTEST_F(ThermalClientTest, ThermalClientTest003, TestSize.Level0)
9637a09cd7Sopenharmony_ci{
9737a09cd7Sopenharmony_ci    THERMAL_HILOGD(LABEL_TEST, "ThermalClientTest003 start.");
9837a09cd7Sopenharmony_ci    auto& client = ThermalMgrClient::GetInstance();
9937a09cd7Sopenharmony_ci    std::vector<std::string> typeList;
10037a09cd7Sopenharmony_ci    sptr<IThermalTempCallback> tempCallback = nullptr;
10137a09cd7Sopenharmony_ci    EXPECT_FALSE(client.SubscribeThermalTempCallback(typeList, tempCallback));
10237a09cd7Sopenharmony_ci    EXPECT_FALSE(client.UnSubscribeThermalTempCallback(tempCallback));
10337a09cd7Sopenharmony_ci    sptr<MockThermalRemoteObject> sptrRemoteObj = new MockThermalRemoteObject();
10437a09cd7Sopenharmony_ci    EXPECT_FALSE(sptrRemoteObj == nullptr);
10537a09cd7Sopenharmony_ci    tempCallback = new ThermalTempCallbackProxy(sptrRemoteObj);
10637a09cd7Sopenharmony_ci    EXPECT_FALSE(tempCallback == nullptr);
10737a09cd7Sopenharmony_ci    EXPECT_TRUE(client.SubscribeThermalTempCallback(typeList, tempCallback));
10837a09cd7Sopenharmony_ci    EXPECT_TRUE(client.UnSubscribeThermalTempCallback(tempCallback));
10937a09cd7Sopenharmony_ci    sptr<IThermalLevelCallback> levelCallback = nullptr;
11037a09cd7Sopenharmony_ci    EXPECT_FALSE(client.SubscribeThermalLevelCallback(levelCallback));
11137a09cd7Sopenharmony_ci    EXPECT_FALSE(client.UnSubscribeThermalLevelCallback(levelCallback));
11237a09cd7Sopenharmony_ci    levelCallback = new ThermalLevelCallbackProxy(sptrRemoteObj);
11337a09cd7Sopenharmony_ci    EXPECT_FALSE(levelCallback == nullptr);
11437a09cd7Sopenharmony_ci    EXPECT_TRUE(client.SubscribeThermalLevelCallback(levelCallback));
11537a09cd7Sopenharmony_ci    EXPECT_TRUE(client.UnSubscribeThermalLevelCallback(levelCallback));
11637a09cd7Sopenharmony_ci    sptr<IThermalActionCallback> actionCallback = nullptr;
11737a09cd7Sopenharmony_ci    std::string actionList;
11837a09cd7Sopenharmony_ci    EXPECT_FALSE(client.SubscribeThermalActionCallback(typeList, actionList, actionCallback));
11937a09cd7Sopenharmony_ci    EXPECT_FALSE(client.UnSubscribeThermalActionCallback(actionCallback));
12037a09cd7Sopenharmony_ci    actionCallback = new ThermalActionCallbackProxy(sptrRemoteObj);
12137a09cd7Sopenharmony_ci    EXPECT_FALSE(actionCallback == nullptr);
12237a09cd7Sopenharmony_ci    EXPECT_TRUE(client.SubscribeThermalActionCallback(typeList, actionList, actionCallback));
12337a09cd7Sopenharmony_ci    EXPECT_TRUE(client.UnSubscribeThermalActionCallback(actionCallback));
12437a09cd7Sopenharmony_ci    THERMAL_HILOGD(LABEL_TEST, "ThermalClientTest003 end.");
12537a09cd7Sopenharmony_ci}
12637a09cd7Sopenharmony_ci
12737a09cd7Sopenharmony_ci/**
12837a09cd7Sopenharmony_ci * @tc.name: ThermalClientTest004
12937a09cd7Sopenharmony_ci * @tc.desc: ThermalSrvSensorInfo Marshalling and Unmarshalling test
13037a09cd7Sopenharmony_ci * @tc.type: FUNC
13137a09cd7Sopenharmony_ci * @tc.require: issueI5YZQ2
13237a09cd7Sopenharmony_ci */
13337a09cd7Sopenharmony_ciHWTEST_F(ThermalClientTest, ThermalClientTest004, TestSize.Level0)
13437a09cd7Sopenharmony_ci{
13537a09cd7Sopenharmony_ci    THERMAL_HILOGD(LABEL_TEST, "ThermalClientTest004 start.");
13637a09cd7Sopenharmony_ci    sptr<ThermalSrvSensorInfo> info = new ThermalSrvSensorInfo();
13737a09cd7Sopenharmony_ci    MessageParcel parcel;
13837a09cd7Sopenharmony_ci    info->Unmarshalling(parcel);
13937a09cd7Sopenharmony_ci    EXPECT_TRUE(info->Marshalling(parcel));
14037a09cd7Sopenharmony_ci    THERMAL_HILOGD(LABEL_TEST, "ThermalClientTest004 end.");
14137a09cd7Sopenharmony_ci}
14237a09cd7Sopenharmony_ci
14337a09cd7Sopenharmony_ci/**
14437a09cd7Sopenharmony_ci * @tc.name: ThermalClientTest005
14537a09cd7Sopenharmony_ci * @tc.desc: thermalListener additional test
14637a09cd7Sopenharmony_ci * @tc.type: FUNC
14737a09cd7Sopenharmony_ci * @tc.require: issueI5YZQ2
14837a09cd7Sopenharmony_ci */
14937a09cd7Sopenharmony_ciHWTEST_F(ThermalClientTest, ThermalClientTest005, TestSize.Level0)
15037a09cd7Sopenharmony_ci{
15137a09cd7Sopenharmony_ci    THERMAL_HILOGD(LABEL_TEST, "ThermalClientTest005 start.");
15237a09cd7Sopenharmony_ci    std::shared_ptr<ThermalMgrListener> thermalListener = std::make_shared<ThermalMgrListener>();
15337a09cd7Sopenharmony_ci    ASSERT_NE(thermalListener, nullptr);
15437a09cd7Sopenharmony_ci    int32_t ret = thermalListener->SubscribeLevelEvent(nullptr);
15537a09cd7Sopenharmony_ci    EXPECT_NE(ret, ERR_OK);
15637a09cd7Sopenharmony_ci    ret = thermalListener->UnSubscribeLevelEvent();
15737a09cd7Sopenharmony_ci    EXPECT_NE(ret, ERR_OK);
15837a09cd7Sopenharmony_ci    ThermalLevel level = thermalListener->GetThermalLevel();
15937a09cd7Sopenharmony_ci    EXPECT_NE(static_cast<int32_t>(level), INVAILID_VALUE);
16037a09cd7Sopenharmony_ci    sptr<IThermalLevelCallback> callback = new ThermalMgrListener::ThermalLevelCallback(nullptr);
16137a09cd7Sopenharmony_ci    bool result = callback->OnThermalLevelChanged(level);
16237a09cd7Sopenharmony_ci    EXPECT_EQ(result, false);
16337a09cd7Sopenharmony_ci    THERMAL_HILOGD(LABEL_TEST, "ThermalClientTest005 end.");
16437a09cd7Sopenharmony_ci}
16537a09cd7Sopenharmony_ci} // namespace
166