• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-2025 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 #ifdef UNIFIED_COLLECTOR_GRAPHIC_ENABLE
GetTestProcNameAndPid(std::string & procName,int32_t & pid)37 void GetTestProcNameAndPid(std::string& procName, int32_t& pid)
38 {
39     const std::string systemuiProcName = "com.ohos.systemui";
40     const std::string sceneBoardProcName = "com.ohos.sceneboard";
41     auto systemuiPid = CommonUtils::GetPidByName(systemuiProcName);
42     auto launcherPid = CommonUtils::GetPidByName(sceneBoardProcName);
43     pid = static_cast<int32_t>(systemuiPid > 0 ? systemuiPid : launcherPid);
44     procName = systemuiPid > 0 ? systemuiProcName : sceneBoardProcName;
45 }
46 
47 /**
48  * @tc.name: GraphicMemoryCollectorTest001
49  * @tc.desc: used to test GraphicMemoryCollector.GetGraphicUsage
50  * @tc.type: FUNC
51 */
52 HWTEST_F(GraphicMemoryCollectorTest, GraphicMemoryCollectorTest001, TestSize.Level1)
53 {
54     std::string procName;
55     int32_t pid = 0;
56     GetTestProcNameAndPid(procName, pid);
57     ASSERT_GT(pid, 0);
58     std::shared_ptr<GraphicMemoryCollector> collector = GraphicMemoryCollector::Create();
59     CollectResult<int32_t> data = collector->GetGraphicUsage(pid, GraphicType::TOTAL, false);
60     ASSERT_EQ(data.retCode, UcError::SUCCESS);
61     std::cout << "GetGraphicUsage [pid=" << pid <<", procName=" << procName << "] total result:" << data.data;
62     std::cout << std::endl;
63     ASSERT_GE(data.data, 0);
64 }
65 
66 /**
67  * @tc.name: GraphicMemoryCollectorTest002
68  * @tc.desc: used to test GraphicMemoryCollector.GetGraphicUsage
69  * @tc.type: FUNC
70 */
71 HWTEST_F(GraphicMemoryCollectorTest, GraphicMemoryCollectorTest002, TestSize.Level1)
72 {
73     std::string procName;
74     int32_t pid = 0;
75     GetTestProcNameAndPid(procName, pid);
76     ASSERT_GT(pid, 0);
77     std::shared_ptr<GraphicMemoryCollector> collector = GraphicMemoryCollector::Create();
78     CollectResult<int32_t> glData = collector->GetGraphicUsage(pid, GraphicType::GL, false);
79     ASSERT_EQ(glData.retCode, UcError::SUCCESS);
80     std::cout << "GetGraphicUsage [pid=" << pid <<", procName=" << procName << "] gl result:" << glData.data;
81     std::cout << std::endl;
82     ASSERT_GE(glData.data, 0);
83 }
84 
85 /**
86  * @tc.name: GraphicMemoryCollectorTest003
87  * @tc.desc: used to test GraphicMemoryCollector.GetGraphicUsage
88  * @tc.type: FUNC
89 */
90 HWTEST_F(GraphicMemoryCollectorTest, GraphicMemoryCollectorTest003, TestSize.Level1)
91 {
92     std::string procName;
93     int32_t pid = 0;
94     GetTestProcNameAndPid(procName, pid);
95     ASSERT_GT(pid, 0);
96     std::shared_ptr<GraphicMemoryCollector> collector = GraphicMemoryCollector::Create();
97     CollectResult<int32_t> glData = collector->GetGraphicUsage(pid, GraphicType::GRAPH, false);
98     ASSERT_EQ(glData.retCode, UcError::SUCCESS);
99     std::cout << "GetGraphicUsage [pid=" << pid <<", procName=" << procName << "] gl result:" << glData.data;
100     std::cout << std::endl;
101     ASSERT_GE(glData.data, 0);
102 }
103 
104 /**
105  * @tc.name: GraphicMemoryCollectorTest004
106  * @tc.desc: used to test GraphicMemoryCollector.GetGraphicUsage
107  * @tc.type: FUNC
108 */
109 HWTEST_F(GraphicMemoryCollectorTest, GraphicMemoryCollectorTest004, TestSize.Level1)
110 {
111     std::string procName;
112     int32_t pid = 0;
113     GetTestProcNameAndPid(procName, pid);
114     ASSERT_GT(pid, 0);
115     std::shared_ptr<GraphicMemoryCollector> collector = GraphicMemoryCollector::Create();
116     const int invalidNum = 10; // 10 : invalid number used to cast to GraphicType
117     auto graphicData = collector->GetGraphicUsage(pid, static_cast<GraphicType>(invalidNum), false);
118     ASSERT_EQ(graphicData.retCode, UcError::UNSUPPORT);
119 }
120 #else
121 /**
122  * @tc.name: GraphicMemoryCollectorTest001
123  * @tc.desc: used to test empty GraphicMemoryCollector
124  * @tc.type: FUNC
125 */
126 HWTEST_F(GraphicMemoryCollectorTest, GraphicMemoryCollectorTest001, TestSize.Level1)
127 {
128     std::shared_ptr<GraphicMemoryCollector> collector = GraphicMemoryCollector::Create();
129     CollectResult<int32_t> data = collector->GetGraphicUsage(0, GraphicType::TOTAL, false);
130     ASSERT_EQ(data.retCode, UcError::FEATURE_CLOSED);
131     CollectResult<int32_t> glData = collector->GetGraphicUsage(0, GraphicType::GL, false);
132     ASSERT_EQ(glData.retCode, UcError::FEATURE_CLOSED);
133     CollectResult<int32_t> graphicData = collector->GetGraphicUsage(0, GraphicType::GRAPH, false);
134     ASSERT_EQ(graphicData.retCode, UcError::FEATURE_CLOSED);
135 }
136 #endif
137