1 /*
2 * Copyright (c) 2022 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 "utils.h"
19
20 #define private public
21 #define protected public
22 #include "kernel_interface.h"
23 #include "reclaim_priority_manager.h"
24 #include "multi_account_manager.h"
25 #include "oom_score_adj_utils.h"
26 #include "account_bundle_info.h"
27 #undef private
28 #undef protected
29
30 namespace OHOS {
31 namespace Memory {
32 using namespace testing;
33 using namespace testing::ext;
34
35 class OomScoreAdjUtilsTest : public testing::Test {
36 public:
37 static void SetUpTestCase();
38 static void TearDownTestCase();
39 void SetUp();
40 void TearDown();
41 };
42
SetUpTestCase()43 void OomScoreAdjUtilsTest::SetUpTestCase()
44 {
45 }
46
TearDownTestCase()47 void OomScoreAdjUtilsTest::TearDownTestCase()
48 {
49 }
50
SetUp()51 void OomScoreAdjUtilsTest::SetUp()
52 {
53 }
54
TearDown()55 void OomScoreAdjUtilsTest::TearDown()
56 {
57 }
58
59 /**
60 * @tc.name: WriteOomScoreAdjToKernel
61 * @tc.desc: Test the branch into "for" branch
62 * @tc.desc: Test the branch of bundle equals to nullptr
63 * @tc.type: FUNC
64 */
65 HWTEST_F(OomScoreAdjUtilsTest, WriteOomScoreAdjToKernelTest1, TestSize.Level1)
66 {
67 int accountId = 100;
68 std::shared_ptr<BundlePriorityInfo> bundle = std::make_shared<BundlePriorityInfo>("app",
69 accountId * USER_ID_SHIFT + 1, 100);
70
71 // Test the branch into "for" branch
72 ProcessPriorityInfo proc1(1001, bundle->uid_, bundle->priority_);
73 ProcessPriorityInfo proc2(1002, bundle->uid_, bundle->priority_);
74 ProcessPriorityInfo proc3(1003, bundle->uid_, bundle->priority_);
75 ProcessPriorityInfo proc4(1004, bundle->uid_, bundle->priority_);
76 bundle->AddProc(proc1);
77 bundle->AddProc(proc2);
78 bundle->AddProc(proc3);
79 bundle->AddProc(proc4);
80 OomScoreAdjUtils::WriteOomScoreAdjToKernel(bundle);
81 EXPECT_NE(bundle->procs_.begin(), bundle->procs_.end());
82
83 // Test the branch of bundle equals to nullptr
84 bundle = nullptr;
85 bool ret = OomScoreAdjUtils::WriteOomScoreAdjToKernel(bundle);
86 EXPECT_EQ(ret, false);
87 }
88
89 /**
90 * @tc.name: WriteOomScoreAdjToKernel
91 * @tc.type: FUNC
92 */
93 HWTEST_F(OomScoreAdjUtilsTest, WriteOomScoreAdjToKernelTest2, TestSize.Level1)
94 {
95 pid_t pid = 0;
96 int priority = 0;
97 static bool ret = OomScoreAdjUtils::WriteOomScoreAdjToKernel(pid, priority);
98 EXPECT_EQ(ret, true);
99 }
100
101 }
102 }
103