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 <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 WorkSchedSystemPolicy systemPolicy;
57 memoryPolicy_->workPolicyManager_->SetMemoryByDump(1 * 1024 * 1024);
58 int32_t ret = memoryPolicy_->GetPolicyMaxRunning(systemPolicy);
59 EXPECT_EQ(ret, 1);
60 }
61
62 /**
63 * @tc.name: GetPolicyMaxRunning_002
64 * @tc.desc: Test MemoryPolicy GetPolicyMaxRunning.
65 * @tc.type: FUNC
66 * @tc.require: I974IQ
67 */
68 HWTEST_F(MemoryPolicyTest, GetPolicyMaxRunning_002, TestSize.Level1)
69 {
70 WorkSchedSystemPolicy systemPolicy;
71 memoryPolicy_->workPolicyManager_->SetMemoryByDump(2 * 1024 * 1024);
72 int32_t ret = memoryPolicy_->GetPolicyMaxRunning(systemPolicy);
73 EXPECT_EQ(ret, 2);
74 }
75
76 /**
77 * @tc.name: GetPolicyMaxRunning_003
78 * @tc.desc: Test MemoryPolicy GetPolicyMaxRunning.
79 * @tc.type: FUNC
80 * @tc.require: I974IQ
81 */
82 HWTEST_F(MemoryPolicyTest, GetPolicyMaxRunning_003, TestSize.Level1)
83 {
84 WorkSchedSystemPolicy systemPolicy;
85 memoryPolicy_->workPolicyManager_->SetMemoryByDump(3 * 1024 * 1024);
86 int32_t ret = memoryPolicy_->GetPolicyMaxRunning(systemPolicy);
87 EXPECT_EQ(ret, 3);
88 }
89 }
90 }
91