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
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 if (!IsXpowerPluginExist()) {
55 std::cout << "Xpower plugin not exist" << std::endl;
56 return;
57 }
58 std::shared_ptr<MemoryCollector> collector = MemoryCollector::Create();
59 MemoryCaller caller = {.pid = 100, .resourceType = "pss_memory", .limitValue = 100, .enabledDebugLog = false};
60 auto collectResult = collector->SetAppResourceLimit(caller);
61 ASSERT_EQ(collectResult.retCode, UcError::SUCCESS);
62 ASSERT_EQ(collectResult.data, 0);
63 }
64
65 /**
66 * @tc.name: MemoryCollectorClientTest002
67 * @tc.desc: used to test the function of MemoryCollector.GetGraphicUsage;
68 * @tc.type: FUNC
69 */
70 HWTEST_F(MemoryCollectorClientTest, MemoryCollectorClientTest002, TestSize.Level1)
71 {
72 std::shared_ptr<MemoryCollector> collector = MemoryCollector::Create();
73 auto collectResult = collector->GetGraphicUsage();
74 std::cout << "GetGraphicUsage result:" << collectResult.data << std::endl;
75 #ifdef UNIFIED_COLLECTOR_GRAPHIC_ENABLE
76 #ifdef UNIFIED_COLLECTOR_MEMORY_ENABLE
77 ASSERT_EQ(collectResult.retCode, UcError::SUCCESS);
78 #else
79 ASSERT_EQ(collectResult.retCode, UcError::FEATURE_CLOSED);
80 #endif
81 #else
82 ASSERT_EQ(collectResult.retCode, UcError::FEATURE_CLOSED);
83 #endif
84 }