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 <functional>
17 #include <gtest/gtest.h>
18
19 #include "policy/memory_policy.h"
20 #include "work_policy_manager.h"
21 #include "work_scheduler_service.h"
22
23 using namespace testing::ext;
24
25 namespace OHOS {
26 namespace WorkScheduler {
27 class MemoryPolicyTest : public testing::Test {
28 public:
29 static void SetUpTestCase();
TearDownTestCase()30 static void TearDownTestCase() {};
SetUp()31 void SetUp() {};
TearDown()32 void TearDown() {};
33 static std::shared_ptr<WorkPolicyManager> workPolicyManager_;
34 static std::shared_ptr<MemoryPolicy> memoryPolicy_;
35 };
36
37 std::shared_ptr<MemoryPolicy> MemoryPolicyTest::memoryPolicy_ = nullptr;
38 std::shared_ptr<WorkPolicyManager> MemoryPolicyTest::workPolicyManager_ = nullptr;
39
SetUpTestCase()40 void MemoryPolicyTest::SetUpTestCase()
41 {
42 std::shared_ptr<WorkSchedulerService> workSchedulerService_ = std::make_shared<WorkSchedulerService>();
43 workSchedulerService_->Start();
44 workPolicyManager_ = std::make_shared<WorkPolicyManager>(workSchedulerService_);
45 memoryPolicy_ = std::make_shared<MemoryPolicy>(workPolicyManager_);
46 }
47
48 /**
49 * @tc.name: GetPolicyMaxRunning_001
50 * @tc.desc: Test MemoryPolicy GetPolicyMaxRunning.
51 * @tc.type: FUNC
52 * @tc.require: I974IQ
53 */
54 HWTEST_F(MemoryPolicyTest, GetPolicyMaxRunning_001, TestSize.Level1)
55 {
56 memoryPolicy_->workPolicyManager_->SetMemoryByDump(1 * 1024 * 1024);
57 int32_t ret = memoryPolicy_->GetPolicyMaxRunning();
58 EXPECT_EQ(ret, 1);
59 }
60
61 /**
62 * @tc.name: GetPolicyMaxRunning_002
63 * @tc.desc: Test MemoryPolicy GetPolicyMaxRunning.
64 * @tc.type: FUNC
65 * @tc.require: I974IQ
66 */
67 HWTEST_F(MemoryPolicyTest, GetPolicyMaxRunning_002, TestSize.Level1)
68 {
69 memoryPolicy_->workPolicyManager_->SetMemoryByDump(2 * 1024 * 1024);
70 int32_t ret = memoryPolicy_->GetPolicyMaxRunning();
71 EXPECT_EQ(ret, 2);
72 }
73
74 /**
75 * @tc.name: GetPolicyMaxRunning_003
76 * @tc.desc: Test MemoryPolicy GetPolicyMaxRunning.
77 * @tc.type: FUNC
78 * @tc.require: I974IQ
79 */
80 HWTEST_F(MemoryPolicyTest, GetPolicyMaxRunning_003, TestSize.Level1)
81 {
82 memoryPolicy_->workPolicyManager_->SetMemoryByDump(3 * 1024 * 1024);
83 int32_t ret = memoryPolicy_->GetPolicyMaxRunning();
84 EXPECT_EQ(ret, 3);
85 }
86
87 /**
88 * @tc.name: GetPolicyName_001
89 * @tc.desc: Test MemoryPolicy GetPolicyName.
90 * @tc.type: FUNC
91 * @tc.require: I974IQ
92 */
93 HWTEST_F(MemoryPolicyTest, GetPolicyName_001, TestSize.Level1)
94 {
95 std::string ret = memoryPolicy_->GetPolicyName();
96 EXPECT_EQ(ret, "MEMORY_POLICY");
97 }
98 }
99 }
100