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 #ifdef UNIFIED_COLLECTOR_GRAPHIC_ENABLE 37 /** 38 * @tc.name: GraphicMemoryCollectorTest001 39 * @tc.desc: used to test GraphicMemoryCollector.GetGraphicUsage 40 * @tc.type: FUNC 41 */ 42 HWTEST_F(GraphicMemoryCollectorTest, GraphicMemoryCollectorTest001, TestSize.Level1) 43 { 44 const std::string systemuiProcName = "com.ohos.systemui"; 45 const std::string sceneBoardProcName = "com.ohos.sceneboard"; 46 auto systemuiPid = CommonUtils::GetPidByName(systemuiProcName); 47 auto launcherPid = CommonUtils::GetPidByName(sceneBoardProcName); 48 auto pid = static_cast<int32_t>(systemuiPid > 0 ? systemuiPid : launcherPid); 49 const std::string procName = systemuiPid > 0 ? systemuiProcName : sceneBoardProcName; 50 if (pid <= 0) { 51 std::cout << "Get pid failed" << std::endl; 52 return; 53 } 54 std::shared_ptr<GraphicMemoryCollector> collector = GraphicMemoryCollector::Create(); 55 CollectResult<int32_t> data = collector->GetGraphicUsage(pid, GraphicType::TOTAL, false); 56 ASSERT_EQ(data.retCode, UcError::SUCCESS); 57 std::cout << "GetGraphicUsage [pid=" << pid <<", procName=" << procName << "] total result:" << data.data; 58 std::cout << std::endl; 59 ASSERT_GE(data.data, 0); 60 CollectResult<int32_t> glData = collector->GetGraphicUsage(pid, GraphicType::GL, false); 61 ASSERT_EQ(glData.retCode, UcError::SUCCESS); 62 std::cout << "GetGraphicUsage [pid=" << pid <<", procName=" << procName << "] gl result:" << glData.data; 63 std::cout << std::endl; 64 ASSERT_GE(glData.data, 0); 65 CollectResult<int32_t> graphicData = collector->GetGraphicUsage(pid, GraphicType::GRAPH, false); 66 ASSERT_EQ(graphicData.retCode, UcError::SUCCESS); 67 std::cout << "GetGraphicUsage [pid=" << pid <<", procName=" << procName << "] graphic result:" << graphicData.data; 68 std::cout << std::endl; 69 ASSERT_GE(graphicData.data, 0); 70 } 71 #else 72 /** 73 * @tc.name: GraphicMemoryCollectorTest001 74 * @tc.desc: used to test empty GraphicMemoryCollector 75 * @tc.type: FUNC 76 */ 77 HWTEST_F(GraphicMemoryCollectorTest, GraphicMemoryCollectorTest001, TestSize.Level1) 78 { 79 std::shared_ptr<GraphicMemoryCollector> collector = GraphicMemoryCollector::Create(); 80 CollectResult<int32_t> data = collector->GetGraphicUsage(0, GraphicType::TOTAL, false); 81 ASSERT_EQ(data.retCode, UcError::FEATURE_CLOSED); 82 CollectResult<int32_t> glData = collector->GetGraphicUsage(0, GraphicType::GL, false); 83 ASSERT_EQ(glData.retCode, UcError::FEATURE_CLOSED); 84 CollectResult<int32_t> graphicData = collector->GetGraphicUsage(0, GraphicType::GRAPH, false); 85 ASSERT_EQ(graphicData.retCode, UcError::FEATURE_CLOSED); 86 } 87 #endif 88