1 /*
2  * Copyright (C) 2023 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 "avcodec_service_stub.h"
18 #define PRINT_HILOG
19 #include "unittest_log.h"
20 namespace {
21 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_TEST, "AVCodecServiceStubMock"};
22 std::mutex g_mutex;
23 std::weak_ptr<OHOS::MediaAVCodec::AVCodecServiceStubMock> g_mockObject;
24 } // namespace
25 
26 namespace OHOS {
27 namespace MediaAVCodec {
RegisterMock(std::shared_ptr<AVCodecServiceStubMock> & mock)28 void AVCodecServiceStub::RegisterMock(std::shared_ptr<AVCodecServiceStubMock> &mock)
29 {
30     std::lock_guard<std::mutex> lock(g_mutex);
31     UNITTEST_INFO_LOG("AVCodecServiceStub:0x%" PRIXPTR, FAKE_POINTER(mock.get()));
32     g_mockObject = mock;
33 }
34 
AVCodecServiceStub()35 AVCodecServiceStub::AVCodecServiceStub()
36 {
37     std::lock_guard<std::mutex> lock(g_mutex);
38     UNITTEST_INFO_LOG("");
39     auto mock = g_mockObject.lock();
40     UNITTEST_CHECK_AND_RETURN_LOG(mock != nullptr, "mock object is nullptr");
41     mock->AVCodecServiceStubCtor();
42 }
43 
~AVCodecServiceStub()44 AVCodecServiceStub::~AVCodecServiceStub()
45 {
46     std::lock_guard<std::mutex> lock(g_mutex);
47     UNITTEST_INFO_LOG("");
48     auto mock = g_mockObject.lock();
49     UNITTEST_CHECK_AND_RETURN_LOG(mock != nullptr, "mock object is nullptr");
50     mock->AVCodecServiceStubDtor();
51 }
52 
GetSubSystemAbility(IStandardAVCodecService::AVCodecSystemAbility subSystemId,const sptr<IRemoteObject> & listener,sptr<IRemoteObject> & stubObject)53 int32_t AVCodecServiceStub::GetSubSystemAbility(IStandardAVCodecService::AVCodecSystemAbility subSystemId,
54                                                 const sptr<IRemoteObject> &listener, sptr<IRemoteObject> &stubObject)
55 {
56     std::lock_guard<std::mutex> lock(g_mutex);
57     UNITTEST_INFO_LOG("subSystemId:%d, listener refCount:%d", static_cast<int32_t>(subSystemId),
58                       listener->GetSptrRefCount());
59     auto mock = g_mockObject.lock();
60     UNITTEST_CHECK_AND_RETURN_RET_LOG(mock != nullptr, AVCS_ERR_UNKNOWN, "mock object is nullptr");
61     return mock->GetSubSystemAbility(subSystemId, listener, stubObject);
62 }
63 
SetDeathListener(const sptr<IRemoteObject> & object)64 int32_t AVCodecServiceStub::SetDeathListener(const sptr<IRemoteObject> &object)
65 {
66     std::lock_guard<std::mutex> lock(g_mutex);
67     UNITTEST_INFO_LOG("object refCount:%d", object->GetSptrRefCount());
68     auto mock = g_mockObject.lock();
69     UNITTEST_CHECK_AND_RETURN_RET_LOG(mock != nullptr, AVCS_ERR_UNKNOWN, "mock object is nullptr");
70     return mock->SetDeathListener(object);
71 }
72 } // namespace MediaAVCodec
73 } // namespace OHOS
74