1ba5c3796Sopenharmony_ci/*
2ba5c3796Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3ba5c3796Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4ba5c3796Sopenharmony_ci * you may not use this file except in compliance with the License.
5ba5c3796Sopenharmony_ci * You may obtain a copy of the License at
6ba5c3796Sopenharmony_ci *
7ba5c3796Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8ba5c3796Sopenharmony_ci *
9ba5c3796Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10ba5c3796Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11ba5c3796Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12ba5c3796Sopenharmony_ci * See the License for the specific language governing permissions and
13ba5c3796Sopenharmony_ci * limitations under the License.
14ba5c3796Sopenharmony_ci */
15ba5c3796Sopenharmony_ci
16ba5c3796Sopenharmony_ci#include "gtest/gtest.h"
17ba5c3796Sopenharmony_ci
18ba5c3796Sopenharmony_ci#include "utils.h"
19ba5c3796Sopenharmony_ci
20ba5c3796Sopenharmony_ci#define private public
21ba5c3796Sopenharmony_ci#define protected public
22ba5c3796Sopenharmony_ci#include "mem_mgr_client.h"
23ba5c3796Sopenharmony_ci#include "bundle_priority_list.h"
24ba5c3796Sopenharmony_ci#include "mem_mgr_constant.h"
25ba5c3796Sopenharmony_ci#include "app_state_subscriber.h"
26ba5c3796Sopenharmony_ci#undef private
27ba5c3796Sopenharmony_ci#undef protected
28ba5c3796Sopenharmony_ci
29ba5c3796Sopenharmony_cinamespace OHOS {
30ba5c3796Sopenharmony_cinamespace Memory {
31ba5c3796Sopenharmony_ciusing namespace testing;
32ba5c3796Sopenharmony_ciusing namespace testing::ext;
33ba5c3796Sopenharmony_ci
34ba5c3796Sopenharmony_ciconstexpr int IPC_STUB_ERR = 300;
35ba5c3796Sopenharmony_ciclass AppStateSubscriberTest : public AppStateSubscriber {
36ba5c3796Sopenharmony_cipublic:
37ba5c3796Sopenharmony_ci    void OnConnected() override;
38ba5c3796Sopenharmony_ci    void OnDisconnected() override;
39ba5c3796Sopenharmony_ci    void OnAppStateChanged(int32_t pid, int32_t uid, int32_t state) override;
40ba5c3796Sopenharmony_ci    void ForceReclaim(int32_t pid, int32_t uid) override;
41ba5c3796Sopenharmony_ci    void OnTrim(SystemMemoryLevel level) override;
42ba5c3796Sopenharmony_ci    void OnRemoteDied(const wptr<IRemoteObject> &object) override;
43ba5c3796Sopenharmony_ci};
44ba5c3796Sopenharmony_ci
45ba5c3796Sopenharmony_civoid AppStateSubscriberTest::OnConnected()
46ba5c3796Sopenharmony_ci{
47ba5c3796Sopenharmony_ci}
48ba5c3796Sopenharmony_ci
49ba5c3796Sopenharmony_civoid AppStateSubscriberTest::OnDisconnected()
50ba5c3796Sopenharmony_ci{
51ba5c3796Sopenharmony_ci}
52ba5c3796Sopenharmony_ci
53ba5c3796Sopenharmony_civoid AppStateSubscriberTest::OnAppStateChanged(int32_t pid, int32_t uid, int32_t state)
54ba5c3796Sopenharmony_ci{
55ba5c3796Sopenharmony_ci}
56ba5c3796Sopenharmony_ci
57ba5c3796Sopenharmony_civoid AppStateSubscriberTest::ForceReclaim(int32_t pid, int32_t uid)
58ba5c3796Sopenharmony_ci{
59ba5c3796Sopenharmony_ci}
60ba5c3796Sopenharmony_ci
61ba5c3796Sopenharmony_civoid AppStateSubscriberTest::OnTrim(SystemMemoryLevel level)
62ba5c3796Sopenharmony_ci{
63ba5c3796Sopenharmony_ci}
64ba5c3796Sopenharmony_ci
65ba5c3796Sopenharmony_civoid AppStateSubscriberTest::OnRemoteDied(const wptr<IRemoteObject> &object)
66ba5c3796Sopenharmony_ci{
67ba5c3796Sopenharmony_ci}
68ba5c3796Sopenharmony_ci
69ba5c3796Sopenharmony_ciclass InnerkitsTest : public testing::Test {
70ba5c3796Sopenharmony_cipublic:
71ba5c3796Sopenharmony_ci    static void SetUpTestCase();
72ba5c3796Sopenharmony_ci    static void TearDownTestCase();
73ba5c3796Sopenharmony_ci    void SetUp();
74ba5c3796Sopenharmony_ci    void TearDown();
75ba5c3796Sopenharmony_ci    static std::shared_ptr<MemMgrClient> memMgrClient_;
76ba5c3796Sopenharmony_ci    static std::vector<std::shared_ptr<AppStateSubscriberTest>> appStateSubscriberTests;
77ba5c3796Sopenharmony_ci};
78ba5c3796Sopenharmony_cistd::vector<std::shared_ptr<AppStateSubscriberTest>> InnerkitsTest::appStateSubscriberTests =
79ba5c3796Sopenharmony_ci    std::vector<std::shared_ptr<AppStateSubscriberTest>>();
80ba5c3796Sopenharmony_ci
81ba5c3796Sopenharmony_civoid InnerkitsTest::SetUpTestCase()
82ba5c3796Sopenharmony_ci{
83ba5c3796Sopenharmony_ci}
84ba5c3796Sopenharmony_ci
85ba5c3796Sopenharmony_civoid InnerkitsTest::TearDownTestCase()
86ba5c3796Sopenharmony_ci{
87ba5c3796Sopenharmony_ci}
88ba5c3796Sopenharmony_ci
89ba5c3796Sopenharmony_civoid InnerkitsTest::SetUp()
90ba5c3796Sopenharmony_ci{
91ba5c3796Sopenharmony_ci}
92ba5c3796Sopenharmony_ci
93ba5c3796Sopenharmony_civoid InnerkitsTest::TearDown()
94ba5c3796Sopenharmony_ci{
95ba5c3796Sopenharmony_ci    for (auto appStateSubscriberTest_ : appStateSubscriberTests) {
96ba5c3796Sopenharmony_ci        MemMgrClient::GetInstance().UnsubscribeAppState(*appStateSubscriberTest_);
97ba5c3796Sopenharmony_ci    }
98ba5c3796Sopenharmony_ci    appStateSubscriberTests.clear();
99ba5c3796Sopenharmony_ci}
100ba5c3796Sopenharmony_ci
101ba5c3796Sopenharmony_ciHWTEST_F(InnerkitsTest, GetBundlePriorityList_Test, TestSize.Level1)
102ba5c3796Sopenharmony_ci{
103ba5c3796Sopenharmony_ci    BundlePriorityList bundlePrioList;
104ba5c3796Sopenharmony_ci    int32_t ret = MemMgrClient::GetInstance().GetBundlePriorityList(bundlePrioList);
105ba5c3796Sopenharmony_ci    EXPECT_EQ(ret, 0);
106ba5c3796Sopenharmony_ci    bundlePrioList.Show();
107ba5c3796Sopenharmony_ci}
108ba5c3796Sopenharmony_ci
109ba5c3796Sopenharmony_ciHWTEST_F(InnerkitsTest, GetPriorityDescTest, TestSize.Level1)
110ba5c3796Sopenharmony_ci{
111ba5c3796Sopenharmony_ci    auto ptr = ReclaimPriorityMapping.find(RECLAIM_PRIORITY_SYSTEM);
112ba5c3796Sopenharmony_ci    EXPECT_EQ(ptr->second, "System");
113ba5c3796Sopenharmony_ci}
114ba5c3796Sopenharmony_ci
115ba5c3796Sopenharmony_ciHWTEST_F(InnerkitsTest, NotifyDistDevStatus_Test, TestSize.Level1)
116ba5c3796Sopenharmony_ci{
117ba5c3796Sopenharmony_ci    int32_t ret = MemMgrClient::GetInstance().NotifyDistDevStatus(123, 456, "dist_dev_test", true);
118ba5c3796Sopenharmony_ci    EXPECT_EQ(ret, 0);
119ba5c3796Sopenharmony_ci}
120ba5c3796Sopenharmony_ci
121ba5c3796Sopenharmony_ciHWTEST_F(InnerkitsTest, GetKillLevelOfLmkd_Test, TestSize.Level1)
122ba5c3796Sopenharmony_ci{
123ba5c3796Sopenharmony_ci    int32_t killLevel;
124ba5c3796Sopenharmony_ci    int32_t ret = MemMgrClient::GetInstance().GetKillLevelOfLmkd(killLevel);
125ba5c3796Sopenharmony_ci    EXPECT_EQ(ret, 0);
126ba5c3796Sopenharmony_ci    printf("lmkd kill level: %d\n", killLevel);
127ba5c3796Sopenharmony_ci}
128ba5c3796Sopenharmony_ci
129ba5c3796Sopenharmony_ci#ifdef USE_PURGEABLE_MEMORY
130ba5c3796Sopenharmony_ci
131ba5c3796Sopenharmony_ciHWTEST_F(InnerkitsTest, RegisterActiveApps_Test, TestSize.Level1)
132ba5c3796Sopenharmony_ci{
133ba5c3796Sopenharmony_ci    int32_t pid = 1234;
134ba5c3796Sopenharmony_ci    int32_t uid = 20012001;
135ba5c3796Sopenharmony_ci    int32_t ret = MemMgrClient::GetInstance().RegisterActiveApps(pid, uid);
136ba5c3796Sopenharmony_ci    EXPECT_EQ(ret, 0);
137ba5c3796Sopenharmony_ci}
138ba5c3796Sopenharmony_ci
139ba5c3796Sopenharmony_ciHWTEST_F(InnerkitsTest, DeregisterActiveApps_Test, TestSize.Level1)
140ba5c3796Sopenharmony_ci{
141ba5c3796Sopenharmony_ci    int32_t pid = 1234;
142ba5c3796Sopenharmony_ci    int32_t uid = 20012001;
143ba5c3796Sopenharmony_ci    int32_t ret = MemMgrClient::GetInstance().DeregisterActiveApps(pid, uid);
144ba5c3796Sopenharmony_ci    EXPECT_EQ(ret, 0);
145ba5c3796Sopenharmony_ci}
146ba5c3796Sopenharmony_ci
147ba5c3796Sopenharmony_ciHWTEST_F(InnerkitsTest, SubscribeAppState_Test, TestSize.Level1)
148ba5c3796Sopenharmony_ci{
149ba5c3796Sopenharmony_ci    std::shared_ptr<AppStateSubscriberTest> appStateSubscriberTest_1 = std::make_shared<AppStateSubscriberTest>();
150ba5c3796Sopenharmony_ci    EXPECT_NE(appStateSubscriberTest_1, nullptr);
151ba5c3796Sopenharmony_ci    int32_t ret = MemMgrClient::GetInstance().SubscribeAppState(*appStateSubscriberTest_1);
152ba5c3796Sopenharmony_ci    appStateSubscriberTests.emplace_back(appStateSubscriberTest_1);
153ba5c3796Sopenharmony_ci    EXPECT_EQ(ret, 0);
154ba5c3796Sopenharmony_ci}
155ba5c3796Sopenharmony_ci
156ba5c3796Sopenharmony_ciHWTEST_F(InnerkitsTest, UnsubscribeAppState_Test, TestSize.Level1)
157ba5c3796Sopenharmony_ci{
158ba5c3796Sopenharmony_ci    std::shared_ptr<AppStateSubscriberTest> appStateSubscriberTest_1 = std::make_shared<AppStateSubscriberTest>();
159ba5c3796Sopenharmony_ci    EXPECT_NE(appStateSubscriberTest_1, nullptr);
160ba5c3796Sopenharmony_ci    MemMgrClient::GetInstance().SubscribeAppState(*appStateSubscriberTest_1);
161ba5c3796Sopenharmony_ci    appStateSubscriberTests.emplace_back(appStateSubscriberTest_1);
162ba5c3796Sopenharmony_ci    int32_t ret = MemMgrClient::GetInstance().UnsubscribeAppState(*appStateSubscriberTest_1);
163ba5c3796Sopenharmony_ci    EXPECT_EQ(ret, 0);
164ba5c3796Sopenharmony_ci}
165ba5c3796Sopenharmony_ci
166ba5c3796Sopenharmony_ciHWTEST_F(InnerkitsTest, GetAvailableMemory_Test, TestSize.Level1)
167ba5c3796Sopenharmony_ci{
168ba5c3796Sopenharmony_ci    int32_t ret = MemMgrClient::GetInstance().GetAvailableMemory();
169ba5c3796Sopenharmony_ci    EXPECT_NE(ret, 0);
170ba5c3796Sopenharmony_ci}
171ba5c3796Sopenharmony_ci#else
172ba5c3796Sopenharmony_ciHWTEST_F(InnerkitsTest, RegisterActiveApps_Test, TestSize.Level1)
173ba5c3796Sopenharmony_ci{
174ba5c3796Sopenharmony_ci    int32_t pid = 1234;
175ba5c3796Sopenharmony_ci    int32_t uid = 20012001;
176ba5c3796Sopenharmony_ci    int32_t ret = MemMgrClient::GetInstance().RegisterActiveApps(pid, uid);
177ba5c3796Sopenharmony_ci    EXPECT_EQ(ret, -1);
178ba5c3796Sopenharmony_ci}
179ba5c3796Sopenharmony_ci
180ba5c3796Sopenharmony_ciHWTEST_F(InnerkitsTest, DeregisterActiveApps_Test, TestSize.Level1)
181ba5c3796Sopenharmony_ci{
182ba5c3796Sopenharmony_ci    int32_t pid = 1234;
183ba5c3796Sopenharmony_ci    int32_t uid = 20012001;
184ba5c3796Sopenharmony_ci    int32_t ret = MemMgrClient::GetInstance().DeregisterActiveApps(pid, uid);
185ba5c3796Sopenharmony_ci    EXPECT_EQ(ret, -1);
186ba5c3796Sopenharmony_ci}
187ba5c3796Sopenharmony_ci
188ba5c3796Sopenharmony_ciHWTEST_F(InnerkitsTest, SubscribeAppState_Test, TestSize.Level1)
189ba5c3796Sopenharmony_ci{
190ba5c3796Sopenharmony_ci    std::shared_ptr<AppStateSubscriberTest> appStateSubscriberTest_1 = std::make_shared<AppStateSubscriberTest>();
191ba5c3796Sopenharmony_ci    EXPECT_NE(appStateSubscriberTest_1, nullptr);
192ba5c3796Sopenharmony_ci    int32_t ret = MemMgrClient::GetInstance().SubscribeAppState(*appStateSubscriberTest_1);
193ba5c3796Sopenharmony_ci    appStateSubscriberTests.emplace_back(appStateSubscriberTest_1);
194ba5c3796Sopenharmony_ci    EXPECT_EQ(ret, -1);
195ba5c3796Sopenharmony_ci}
196ba5c3796Sopenharmony_ci
197ba5c3796Sopenharmony_ciHWTEST_F(InnerkitsTest, UnsubscribeAppState_Test, TestSize.Level1)
198ba5c3796Sopenharmony_ci{
199ba5c3796Sopenharmony_ci    std::shared_ptr<AppStateSubscriberTest> appStateSubscriberTest_1 = std::make_shared<AppStateSubscriberTest>();
200ba5c3796Sopenharmony_ci    EXPECT_NE(appStateSubscriberTest_1, nullptr);
201ba5c3796Sopenharmony_ci    MemMgrClient::GetInstance().SubscribeAppState(*appStateSubscriberTest_1);
202ba5c3796Sopenharmony_ci    appStateSubscriberTests.emplace_back(appStateSubscriberTest_1);
203ba5c3796Sopenharmony_ci    int32_t ret = MemMgrClient::GetInstance().UnsubscribeAppState(*appStateSubscriberTest_1);
204ba5c3796Sopenharmony_ci    EXPECT_EQ(ret, -1);
205ba5c3796Sopenharmony_ci}
206ba5c3796Sopenharmony_ci
207ba5c3796Sopenharmony_ciHWTEST_F(InnerkitsTest, GetAvailableMemory_Test, TestSize.Level1)
208ba5c3796Sopenharmony_ci{
209ba5c3796Sopenharmony_ci    int32_t ret = MemMgrClient::GetInstance().GetAvailableMemory();
210ba5c3796Sopenharmony_ci    EXPECT_EQ(ret, -1);
211ba5c3796Sopenharmony_ci}
212ba5c3796Sopenharmony_ci#endif // USE_PURGEABLE_MEMORY
213ba5c3796Sopenharmony_ci
214ba5c3796Sopenharmony_ciHWTEST_F(InnerkitsTest, GetReclaimPriorityByPid_Test, TestSize.Level1)
215ba5c3796Sopenharmony_ci{
216ba5c3796Sopenharmony_ci    int32_t pid = 1;
217ba5c3796Sopenharmony_ci    int32_t priority = 1001;
218ba5c3796Sopenharmony_ci    int32_t ret = MemMgrClient::GetInstance().GetReclaimPriorityByPid(pid, priority);
219ba5c3796Sopenharmony_ci    EXPECT_EQ(ret, IPC_STUB_ERR);
220ba5c3796Sopenharmony_ci    EXPECT_EQ(priority, 1001);
221ba5c3796Sopenharmony_ci}
222ba5c3796Sopenharmony_ci
223ba5c3796Sopenharmony_ciHWTEST_F(InnerkitsTest, NotifyProcessStateChangedSync_Test, TestSize.Level1)
224ba5c3796Sopenharmony_ci{
225ba5c3796Sopenharmony_ci    MemMgrProcessStateInfo processStateInfo;
226ba5c3796Sopenharmony_ci    processStateInfo.pid_ = 1014;
227ba5c3796Sopenharmony_ci    processStateInfo.uid_ = 20100001;
228ba5c3796Sopenharmony_ci    processStateInfo.reason_ = ProcPriorityUpdateReason::START_ABILITY;
229ba5c3796Sopenharmony_ci    int32_t ret = MemMgrClient::GetInstance().NotifyProcessStateChangedSync(processStateInfo);
230ba5c3796Sopenharmony_ci    EXPECT_EQ(ret, IPC_STUB_ERR);
231ba5c3796Sopenharmony_ci}
232ba5c3796Sopenharmony_ci
233ba5c3796Sopenharmony_ciHWTEST_F(InnerkitsTest, NotifyProcessStateChangedAsync_Test, TestSize.Level1)
234ba5c3796Sopenharmony_ci{
235ba5c3796Sopenharmony_ci    MemMgrProcessStateInfo processStateInfo;
236ba5c3796Sopenharmony_ci    processStateInfo.pid_ = 1034;
237ba5c3796Sopenharmony_ci    processStateInfo.uid_ = 20300001;
238ba5c3796Sopenharmony_ci    processStateInfo.reason_ = ProcPriorityUpdateReason::START_ABILITY;
239ba5c3796Sopenharmony_ci    int32_t ret = MemMgrClient::GetInstance().NotifyProcessStateChangedAsync(processStateInfo);
240ba5c3796Sopenharmony_ci    EXPECT_EQ(ret, IPC_STUB_ERR);
241ba5c3796Sopenharmony_ci}
242ba5c3796Sopenharmony_ci
243ba5c3796Sopenharmony_ciHWTEST_F(InnerkitsTest, NotifyProcessStatus_Test, TestSize.Level1)
244ba5c3796Sopenharmony_ci{
245ba5c3796Sopenharmony_ci    pid_t pid = 1035;
246ba5c3796Sopenharmony_ci    int type = 1;
247ba5c3796Sopenharmony_ci    int status = 1;
248ba5c3796Sopenharmony_ci    int saId = 1000;
249ba5c3796Sopenharmony_ci    int ret = MemMgrClient::GetInstance().NotifyProcessStatus(pid, type, status, saId);
250ba5c3796Sopenharmony_ci    EXPECT_EQ(ret, 0);
251ba5c3796Sopenharmony_ci}
252ba5c3796Sopenharmony_ci
253ba5c3796Sopenharmony_ciHWTEST_F(InnerkitsTest, SetCritical_Test, TestSize.Level1)
254ba5c3796Sopenharmony_ci{
255ba5c3796Sopenharmony_ci    pid_t pid = 1036;
256ba5c3796Sopenharmony_ci    int saId = 1000;
257ba5c3796Sopenharmony_ci    int ret = MemMgrClient::GetInstance().SetCritical(pid, true, saId);
258ba5c3796Sopenharmony_ci    EXPECT_EQ(ret, 0);
259ba5c3796Sopenharmony_ci}
260ba5c3796Sopenharmony_ci}
261ba5c3796Sopenharmony_ci}
262