1 /*
2  * Copyright (c) 2024 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 <gtest/gtest.h>
17 #include <iostream>
18 #include <string>
19 
20 #include "common_utils.h"
21 #include "graphic_memory_collector.h"
22 
23 using namespace testing::ext;
24 using namespace OHOS::HiviewDFX;
25 using namespace OHOS::HiviewDFX::UCollectUtil;
26 using namespace OHOS::HiviewDFX::UCollect;
27 
28 class GraphicMemoryCollectorTest : public testing::Test {
29 public:
SetUp()30     void SetUp() {};
TearDown()31     void TearDown() {};
SetUpTestCase()32     static void SetUpTestCase() {};
TearDownTestCase()33     static void TearDownTestCase() {};
34 };
35 
36 /**
37  * @tc.name: GraphicMemoryCollectorTest001
38  * @tc.desc: used to test GraphicMemoryCollector.GetGraphicUsage
39  * @tc.type: FUNC
40 */
41 HWTEST_F(GraphicMemoryCollectorTest, GraphicMemoryCollectorTest001, TestSize.Level1)
42 {
43     const std::string systemuiProcName = "com.ohos.systemui";
44     const std::string sceneBoardProcName = "com.ohos.sceneboard";
45     auto systemuiPid = CommonUtils::GetPidByName(systemuiProcName);
46     auto launcherPid = CommonUtils::GetPidByName(sceneBoardProcName);
47     auto pid = static_cast<int32_t>(systemuiPid > 0 ? systemuiPid : launcherPid);
48     const std::string procName = systemuiPid > 0 ? systemuiProcName : sceneBoardProcName;
49     if (pid <= 0) {
50         std::cout << "Get pid failed" << std::endl;
51         return;
52     }
53     std::shared_ptr<GraphicMemoryCollector> collector = GraphicMemoryCollector::Create();
54     CollectResult<int32_t> data = collector->GetGraphicUsage(pid);
55     ASSERT_EQ(data.retCode, UcError::SUCCESS);
56     std::cout << "GetGraphicUsage [pid=" << pid <<", procName=" << procName << "] total result:" << data.data;
57     std::cout << std::endl;
58     ASSERT_GE(data.data, 0);
59     CollectResult<int32_t> glData = collector->GetGraphicUsage(pid, GraphicType::GL);
60     ASSERT_EQ(glData.retCode, UcError::SUCCESS);
61     std::cout << "GetGraphicUsage [pid=" << pid <<", procName=" << procName << "] gl result:" << glData.data;
62     std::cout << std::endl;
63     ASSERT_GE(glData.data, 0);
64     CollectResult<int32_t> graphicData = collector->GetGraphicUsage(pid, GraphicType::GRAPH);
65     ASSERT_EQ(graphicData.retCode, UcError::SUCCESS);
66     std::cout << "GetGraphicUsage [pid=" << pid <<", procName=" << procName << "] graphic result:" << graphicData.data;
67     std::cout << std::endl;
68     ASSERT_GE(graphicData.data, 0);
69 }