• 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 
18 #include "memory_collector_client.h"
19 
20 using namespace testing::ext;
21 using namespace OHOS::HiviewDFX;
22 using namespace OHOS::HiviewDFX::UCollectClient;
23 using namespace OHOS::HiviewDFX::UCollect;
24 
IsXpowerPluginExist()25 bool IsXpowerPluginExist()
26 {
27     std::string cmd = "ps -T $(pidof hiview) | grep -i Xpower";
28     FILE* fp = popen(cmd.c_str(), "r");
29     if (fp == nullptr) {
30         std::cout << "popen failed" << std::endl;
31         return false;
32     }
33     char buffer[256];
34     bool res = (fgets(buffer, sizeof(buffer), fp) != nullptr);
35     pclose(fp);
36     return res;
37 }
38 
39 class MemoryCollectorClientTest : public testing::Test {
40 public:
SetUp()41     void SetUp() {};
TearDown()42     void TearDown() {};
SetUpTestCase()43     static void SetUpTestCase() {};
TearDownTestCase()44     static void TearDownTestCase() {};
45 };
46 
47 /**
48  * @tc.name: MemoryCollectorClientTest001
49  * @tc.desc: used to test the function of MemoryCollector.SetAppResourceLimit;
50  * @tc.type: FUNC
51 */
52 HWTEST_F(MemoryCollectorClientTest, MemoryCollectorClientTest001, TestSize.Level1)
53 {
54     std::shared_ptr<MemoryCollector> collector = MemoryCollector::Create();
55     MemoryCaller caller = {.pid = 100, .resourceType = "pss_memory", .limitValue = 100, .enabledDebugLog = false};
56     auto collectResult = collector->SetAppResourceLimit(caller);
57     if (!IsXpowerPluginExist()) {
58         std::cout << "Xpower plugin not exist" << std::endl;
59         ASSERT_NE(collectResult.retCode, UcError::SUCCESS);
60     } else {
61         ASSERT_EQ(collectResult.retCode, UcError::SUCCESS);
62         ASSERT_EQ(collectResult.data, 0);
63     }
64 }
65 
66 /**
67  * @tc.name: MemoryCollectorClientTest002
68  * @tc.desc: used to test the function of MemoryCollector.GetGraphicUsage;
69  * @tc.type: FUNC
70 */
71 HWTEST_F(MemoryCollectorClientTest, MemoryCollectorClientTest002, TestSize.Level1)
72 {
73     std::shared_ptr<MemoryCollector> collector = MemoryCollector::Create();
74     auto collectResult = collector->GetGraphicUsage();
75     std::cout << "GetGraphicUsage result:" << collectResult.data << std::endl;
76 #ifdef UNIFIED_COLLECTOR_GRAPHIC_ENABLE
77 #ifdef UNIFIED_COLLECTOR_MEMORY_ENABLE
78     ASSERT_EQ(collectResult.retCode, UcError::SUCCESS);
79 #else
80     ASSERT_EQ(collectResult.retCode, UcError::FEATURE_CLOSED);
81 #endif
82 #else
83     ASSERT_EQ(collectResult.retCode, UcError::FEATURE_CLOSED);
84 #endif
85 }