100600bfbSopenharmony_ci/*
200600bfbSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
300600bfbSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
400600bfbSopenharmony_ci * you may not use this file except in compliance with the License.
500600bfbSopenharmony_ci * You may obtain a copy of the License at
600600bfbSopenharmony_ci *
700600bfbSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
800600bfbSopenharmony_ci *
900600bfbSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1000600bfbSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1100600bfbSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1200600bfbSopenharmony_ci * See the License for the specific language governing permissions and
1300600bfbSopenharmony_ci * limitations under the License.
1400600bfbSopenharmony_ci */
1500600bfbSopenharmony_ci#include <gtest/gtest.h>
1600600bfbSopenharmony_ci#include <iservice_registry.h>
1700600bfbSopenharmony_ci#include "dump_manager_cpu_client.h"
1800600bfbSopenharmony_ci#include "dump_manager_cpu_service.h"
1900600bfbSopenharmony_ci#include "dump_common_utils.h"
2000600bfbSopenharmony_ci#include "inner/dump_service_id.h"
2100600bfbSopenharmony_ciusing namespace std;
2200600bfbSopenharmony_ciusing namespace testing::ext;
2300600bfbSopenharmony_ciusing namespace OHOS;
2400600bfbSopenharmony_ciusing namespace OHOS::HiviewDFX;
2500600bfbSopenharmony_cinamespace OHOS {
2600600bfbSopenharmony_cinamespace HiviewDFX {
2700600bfbSopenharmony_ciclass HidumperCpuServiceTest : public testing::Test {
2800600bfbSopenharmony_cipublic:
2900600bfbSopenharmony_ci    static void SetUpTestCase(void);
3000600bfbSopenharmony_ci    static void TearDownTestCase(void);
3100600bfbSopenharmony_ci    void SetUp();
3200600bfbSopenharmony_ci    void TearDown();
3300600bfbSopenharmony_ciprotected:
3400600bfbSopenharmony_ci    static const uint32_t TEST_PID = 1;
3500600bfbSopenharmony_ci};
3600600bfbSopenharmony_ci
3700600bfbSopenharmony_civoid HidumperCpuServiceTest::SetUpTestCase(void)
3800600bfbSopenharmony_ci{
3900600bfbSopenharmony_ci}
4000600bfbSopenharmony_ci
4100600bfbSopenharmony_civoid HidumperCpuServiceTest::TearDownTestCase(void)
4200600bfbSopenharmony_ci{
4300600bfbSopenharmony_ci}
4400600bfbSopenharmony_ci
4500600bfbSopenharmony_civoid HidumperCpuServiceTest::SetUp(void)
4600600bfbSopenharmony_ci{
4700600bfbSopenharmony_ci}
4800600bfbSopenharmony_ci
4900600bfbSopenharmony_civoid HidumperCpuServiceTest::TearDown(void)
5000600bfbSopenharmony_ci{
5100600bfbSopenharmony_ci}
5200600bfbSopenharmony_ci
5300600bfbSopenharmony_ci/**
5400600bfbSopenharmony_ci * @tc.name: HidumperCpuServiceTest001
5500600bfbSopenharmony_ci * @tc.desc: Test DumpManagerCpuService service ready.
5600600bfbSopenharmony_ci * @tc.type: FUNC
5700600bfbSopenharmony_ci */
5800600bfbSopenharmony_ciHWTEST_F(HidumperCpuServiceTest, HidumperCpuServiceTest001, TestSize.Level3)
5900600bfbSopenharmony_ci{
6000600bfbSopenharmony_ci    sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
6100600bfbSopenharmony_ci    ASSERT_TRUE(sam != nullptr) << "HidumperCpuServiceTest001 fail to get GetSystemAbilityManager";
6200600bfbSopenharmony_ci    sptr<IRemoteObject> remoteObject = sam->CheckSystemAbility(DFX_SYS_HIDUMPER_CPU_ABILITY_ID);
6300600bfbSopenharmony_ci    ASSERT_TRUE(remoteObject != nullptr) << "GetCpuSystemAbility failed.";
6400600bfbSopenharmony_ci}
6500600bfbSopenharmony_ci
6600600bfbSopenharmony_ci/**
6700600bfbSopenharmony_ci * @tc.name: HidumperCpuServiceTest002
6800600bfbSopenharmony_ci * @tc.desc: Test of send request to dump spuusage.
6900600bfbSopenharmony_ci * @tc.type: FUNC
7000600bfbSopenharmony_ci */
7100600bfbSopenharmony_ciHWTEST_F(HidumperCpuServiceTest, HidumperCpuServiceTest002, TestSize.Level3)
7200600bfbSopenharmony_ci{
7300600bfbSopenharmony_ci    auto dumpManagerCpuService = std::make_shared<DumpManagerCpuService>();
7400600bfbSopenharmony_ci    dumpManagerCpuService->StartService();
7500600bfbSopenharmony_ci    dumpManagerCpuService->OnStart();
7600600bfbSopenharmony_ci    auto dumpCpuData = std::make_shared<DumpCpuData>();
7700600bfbSopenharmony_ci    int ret = dumpManagerCpuService->Request(*dumpCpuData);
7800600bfbSopenharmony_ci    ASSERT_TRUE(ret == 0) << "dump cpuusage failed.";
7900600bfbSopenharmony_ci}
8000600bfbSopenharmony_ci
8100600bfbSopenharmony_ci/**
8200600bfbSopenharmony_ci * @tc.name: HidumperCpuServiceTest003
8300600bfbSopenharmony_ci * @tc.desc: Test of send request to dump spuusage.
8400600bfbSopenharmony_ci * @tc.type: FUNC
8500600bfbSopenharmony_ci */
8600600bfbSopenharmony_ciHWTEST_F(HidumperCpuServiceTest, HidumperCpuServiceTest003, TestSize.Level3)
8700600bfbSopenharmony_ci{
8800600bfbSopenharmony_ci    auto dumpManagerCpuService = std::make_shared<DumpManagerCpuService>();
8900600bfbSopenharmony_ci    dumpManagerCpuService->StartService();
9000600bfbSopenharmony_ci    double cpuUsage;
9100600bfbSopenharmony_ci    int ret = dumpManagerCpuService->GetCpuUsageByPid(TEST_PID, cpuUsage);
9200600bfbSopenharmony_ci    ASSERT_TRUE(ret == 0) << "dump cpuusage failed.";
9300600bfbSopenharmony_ci}
9400600bfbSopenharmony_ci
9500600bfbSopenharmony_ci/**
9600600bfbSopenharmony_ci * @tc.name: HidumperCpuServiceTest004
9700600bfbSopenharmony_ci * @tc.desc: Test error cpuservice.
9800600bfbSopenharmony_ci * @tc.type: FUNC
9900600bfbSopenharmony_ci */
10000600bfbSopenharmony_ciHWTEST_F(HidumperCpuServiceTest, HidumperCpuServiceTest004, TestSize.Level3)
10100600bfbSopenharmony_ci{
10200600bfbSopenharmony_ci    auto dumpManagerCpuService = std::make_shared<DumpManagerCpuService>();
10300600bfbSopenharmony_ci    dumpManagerCpuService->OnStart();
10400600bfbSopenharmony_ci    dumpManagerCpuService->OnStart();
10500600bfbSopenharmony_ci    dumpManagerCpuService->curCPUInfo_ = nullptr;
10600600bfbSopenharmony_ci    int32_t ret = dumpManagerCpuService->DumpCpuUsageData();
10700600bfbSopenharmony_ci    ASSERT_TRUE(ret != 0) << "dump cpuusage failed.";
10800600bfbSopenharmony_ci}
10900600bfbSopenharmony_ci} // namespace HiviewDFX
11000600bfbSopenharmony_ci} // namespace OHOS