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 #include <mutex>
16 #include "avcodec_log.h"
17 #include "mem_mgr_client.h"
18 #define PRINT_HILOG
19 #include "unittest_log.h"
20 namespace {
21 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_TEST, "MemMgrClientMock"};
22 std::mutex g_mutex;
23 std::weak_ptr<OHOS::Memory::MemMgrClientMock> g_mockObject;
24 } // namespace
25 namespace OHOS {
26 namespace Memory {
27 using namespace OHOS::MediaAVCodec;
RegisterMock(std::shared_ptr<MemMgrClientMock> & mock)28 void MemMgrClient::RegisterMock(std::shared_ptr<MemMgrClientMock> &mock)
29 {
30     std::lock_guard<std::mutex> lock(g_mutex);
31     UNITTEST_INFO_LOG("MemMgrClient:0x%" PRIXPTR, FAKE_POINTER(mock.get()));
32     g_mockObject = mock;
33 }
34 
MemMgrClient()35 MemMgrClient::MemMgrClient()
36 {
37     UNITTEST_INFO_LOG("");
38 }
39 
~MemMgrClient()40 MemMgrClient::~MemMgrClient()
41 {
42     UNITTEST_INFO_LOG("");
43 }
44 
GetInstance()45 MemMgrClient& MemMgrClient::GetInstance()
46 {
47     static MemMgrClient instance;
48     return instance;
49 }
50 
NotifyProcessStatus(pid_t pid,int32_t start,int32_t status,int32_t saId)51 int32_t MemMgrClient::NotifyProcessStatus(pid_t pid, int32_t start, int32_t status, int32_t saId)
52 {
53     std::lock_guard<std::mutex> lock(g_mutex);
54     UNITTEST_INFO_LOG("pid:%d, status:%d, start:%d, saId:%d", pid, status, start, saId);
55     auto mock = g_mockObject.lock();
56     UNITTEST_CHECK_AND_RETURN_RET_LOG(mock != nullptr, AVCS_ERR_UNKNOWN, "mock object is nullptr");
57     return mock->NotifyProcessStatus(pid, start, status, saId);
58 }
59 
SetCritical(pid_t pid,bool flag,int32_t saId)60 int32_t MemMgrClient::SetCritical(pid_t pid, bool flag, int32_t saId)
61 {
62     std::lock_guard<std::mutex> lock(g_mutex);
63     UNITTEST_INFO_LOG("pid:%d, flag:%d, saId:%d", pid, flag, saId);
64     auto mock = g_mockObject.lock();
65     UNITTEST_CHECK_AND_RETURN_RET_LOG(mock != nullptr, AVCS_ERR_UNKNOWN, "mock object is nullptr");
66     return mock->SetCritical(pid, flag, saId);
67 }
68 } // namespace Memory
69 } // namespace OHOS
70