1 /*
2  * Copyright (c) 2021 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 "hiview_sa_test.h"
17 
18 #include <string>
19 #include <unistd.h>
20 
21 #include "common_utils.h"
22 #include "hiview_service_ability_proxy.h"
23 #include "if_system_ability_manager.h"
24 #include "ipc_skeleton.h"
25 #include "iservice_registry.h"
26 #include "system_ability_definition.h"
27 
28 namespace OHOS {
29 namespace HiviewDFX {
SetUpTestCase()30 void HiviewSATest::SetUpTestCase() {}
31 
TearDownTestCase()32 void HiviewSATest::TearDownTestCase() {}
33 
SetUp()34 void HiviewSATest::SetUp() {}
35 
TearDown()36 void HiviewSATest::TearDown() {}
37 
GetHiviewRemoteObject()38 sptr<IRemoteObject> GetHiviewRemoteObject()
39 {
40     sptr<ISystemAbilityManager> serviceManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
41     if (serviceManager == nullptr) {
42         printf("serviceManager == nullptr");
43         return nullptr;
44     }
45     sptr<IRemoteObject> abilityObject = serviceManager->CheckSystemAbility(DFX_SYS_HIVIEW_ABILITY_ID);
46     if (abilityObject == nullptr) {
47         printf("abilityObjext == nullptr");
48         return nullptr;
49     }
50     return abilityObject;
51 }
52 
53 /**
54  * @tc.name: CommonTest001
55  * @tc.desc: Check whether the SA is successfully obtained.
56  * @tc.type: FUNC
57  * @tc.require: AR000FJLO2
58  */
59 HWTEST_F(HiviewSATest, CommonTest001, testing::ext::TestSize.Level3)
60 {
61     sptr<IRemoteObject> abilityObjext = GetHiviewRemoteObject();
62     if (abilityObjext == nullptr) {
63         printf("CheckSystemAbility error \r\n");
64         assert(false);
65     }
66     auto hiviewSAProxy = new HiviewServiceAbilityProxy(abilityObjext);
67     if (hiviewSAProxy == nullptr) {
68         printf("hiviewSAProxy == nullptr");
69         ASSERT_NE(hiviewSAProxy, nullptr);
70     }
71 
72     printf("end \r\n");
73 }
74 
75 /**
76  * @tc.name: CommonTest002
77  * @tc.desc: Check hidumper -s 1201.
78  * @tc.type: FUNC
79  * @tc.require: AR000FJLO2
80  */
81 HWTEST_F(HiviewSATest, CommonTest002, testing::ext::TestSize.Level3)
82 {
83     printf("system(\"hidumper -s 1201\") \r\n");
84     char buffer[256];
85     FILE* fp = popen("hidumper -s 1201", "r");
86 
87     ASSERT_NE(fp, nullptr);
88     fgets(buffer, sizeof(buffer), fp);
89     printf("%s", buffer);
90     pclose(fp);
91     std::string str(buffer);
92     if (str.find("Error") != std::string::npos) {
93         printf("hidumper -s 1201 fail!\r\n");
94         FAIL();
95     }
96 }
97 
98 /**
99  * @tc.name: CommonTest003
100  * @tc.desc: Check GetGraphicUsage
101  * @tc.type: FUNC
102  */
103 HWTEST_F(HiviewSATest, CommonTest003, testing::ext::TestSize.Level3)
104 {
105     sptr<IRemoteObject> abilityObjext = GetHiviewRemoteObject();
106     if (abilityObjext == nullptr) {
107         printf("CheckSystemAbility error \r\n");
108         return;
109     }
110     auto hiviewSAProxy = new HiviewServiceAbilityProxy(abilityObjext);
111     auto systemuiPid = CommonUtils::GetPidByName("com.ohos.systemui");
112     auto launcherPid = CommonUtils::GetPidByName("com.ohos.sceneboard");
113     auto pid = static_cast<int32_t>(systemuiPid > 0 ? systemuiPid : launcherPid);
114     if (pid <= 0) {
115         std::cout << "Get pid failed" << std::endl;
116         return;
117     }
118     CollectResult<int32_t> data = hiviewSAProxy->GetGraphicUsage().result_;
119     ASSERT_EQ(data.retCode, UCollect::UcError::SUCCESS);
120     std::cout << "GetGraphicUsage result:" << data.data << std::endl;
121     ASSERT_GE(data.data, 0);
122 }
123 }
124 }