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 "codec_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, "CodecServiceStubMock"};
22 std::mutex g_mutex;
23 std::weak_ptr<OHOS::MediaAVCodec::CodecServiceStubMock> g_mockObject;
24 } // namespace
25 
26 namespace OHOS {
27 namespace MediaAVCodec {
RegisterMock(std::shared_ptr<CodecServiceStubMock> & mock)28 void CodecServiceStub::RegisterMock(std::shared_ptr<CodecServiceStubMock> &mock)
29 {
30     std::lock_guard<std::mutex> lock(g_mutex);
31     UNITTEST_INFO_LOG("CodecServiceStub:0x%" PRIXPTR, FAKE_POINTER(mock.get()));
32     g_mockObject = mock;
33 }
34 
Create()35 sptr<CodecServiceStub> CodecServiceStub::Create()
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_RET_LOG(mock != nullptr, nullptr, "mock object is nullptr");
41     return mock->Create();
42 }
43 
CodecServiceStub()44 CodecServiceStub::CodecServiceStub()
45 {
46     UNITTEST_INFO_LOG("");
47 }
48 
~CodecServiceStub()49 CodecServiceStub::~CodecServiceStub()
50 {
51     std::lock_guard<std::mutex> lock(g_mutex);
52     UNITTEST_INFO_LOG("");
53     auto mock = g_mockObject.lock();
54     UNITTEST_CHECK_AND_RETURN_LOG(mock != nullptr, "mock object is nullptr");
55     mock->CodecServiceStubDtor();
56 }
57 
Init(AVCodecType type,bool isMimeType,const std::string & name)58 int32_t CodecServiceStub::Init(AVCodecType type, bool isMimeType, const std::string &name)
59 {
60     std::lock_guard<std::mutex> lock(g_mutex);
61     UNITTEST_INFO_LOG("type:%d, isMimeType:%d, name:%s", static_cast<int32_t>(type), static_cast<int32_t>(isMimeType),
62                       name.c_str());
63     auto mock = g_mockObject.lock();
64     UNITTEST_CHECK_AND_RETURN_RET_LOG(mock != nullptr, AVCS_ERR_UNKNOWN, "mock object is nullptr");
65     return mock->Init(type, isMimeType, name);
66 }
67 
DestroyStub()68 int32_t CodecServiceStub::DestroyStub()
69 {
70     std::lock_guard<std::mutex> lock(g_mutex);
71     auto mock = g_mockObject.lock();
72     UNITTEST_CHECK_AND_RETURN_RET_LOG(mock != nullptr, AVCS_ERR_UNKNOWN, "mock object is nullptr");
73     return mock->DestroyStub();
74 }
75 
Dump(int32_t fd,const std::vector<std::u16string> & args)76 int32_t CodecServiceStub::Dump(int32_t fd, const std::vector<std::u16string>& args)
77 {
78     std::lock_guard<std::mutex> lock(g_mutex);
79     UNITTEST_INFO_LOG("fd:%d", fd);
80     auto mock = g_mockObject.lock();
81     UNITTEST_CHECK_AND_RETURN_RET_LOG(mock != nullptr, AVCS_ERR_UNKNOWN, "mock object is nullptr");
82     return mock->Dump(fd, args);
83 }
84 
85 } // namespace MediaAVCodec
86 } // namespace OHOS
87