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.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 return fgets(buffer, sizeof(buffer), fp) != nullptr ? true : false;
35 }
36
37 class MemoryCollectorClientTest : public testing::Test {
38 public:
SetUp()39 void SetUp() {};
TearDown()40 void TearDown() {};
SetUpTestCase()41 static void SetUpTestCase() {};
TearDownTestCase()42 static void TearDownTestCase() {};
43 };
44
45 /**
46 * @tc.name: MemoryCollectorClientTest001
47 * @tc.desc: used to test the function of MemoryCollector.SetAppResourceLimit;
48 * @tc.type: FUNC
49 */
50 HWTEST_F(MemoryCollectorClientTest, MemoryCollectorClientTest001, TestSize.Level1)
51 {
52 if (!IsXpowerPluginExist()) {
53 std::cout << "Xpower plugin not exist" << std::endl;
54 return;
55 }
56 std::shared_ptr<MemoryCollector> collector = MemoryCollector::Create();
57 MemoryCaller caller = {.pid = 100, .resourceType = "pss_memory", .limitValue = 100, .enabledDebugLog = false};
58 auto collectResult = collector->SetAppResourceLimit(caller);
59 ASSERT_EQ(collectResult.retCode, UcError::SUCCESS);
60 ASSERT_EQ(collectResult.data, 0);
61 }
62
63 /**
64 * @tc.name: MemoryCollectorClientTest002
65 * @tc.desc: used to test the function of MemoryCollector.GetGraphicUsage;
66 * @tc.type: FUNC
67 */
68 HWTEST_F(MemoryCollectorClientTest, MemoryCollectorClientTest002, TestSize.Level1)
69 {
70 std::shared_ptr<MemoryCollector> collector = MemoryCollector::Create();
71 auto collectResult = collector->GetGraphicUsage();
72 std::cout << "GetGraphicUsage result:" << collectResult.data << std::endl;
73 ASSERT_EQ(collectResult.retCode, UcError::SUCCESS);
74 }