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 "codeclist_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, "CodecListServiceStub"};
22 std::mutex g_mutex;
23 std::weak_ptr<OHOS::MediaAVCodec::CodecListServiceStubMock> g_mockObject;
24 } // namespace
25
26 namespace OHOS {
27 namespace MediaAVCodec {
RegisterMock(std::shared_ptr<CodecListServiceStubMock> & mock)28 void CodecListServiceStub::RegisterMock(std::shared_ptr<CodecListServiceStubMock> &mock)
29 {
30 std::lock_guard<std::mutex> lock(g_mutex);
31 UNITTEST_INFO_LOG("CodecListServiceStub:0x%" PRIXPTR, FAKE_POINTER(mock.get()));
32 g_mockObject = mock;
33 }
34
Create()35 sptr<CodecListServiceStub> CodecListServiceStub::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
CodecListServiceStub()44 CodecListServiceStub::CodecListServiceStub()
45 {
46 UNITTEST_INFO_LOG("");
47 }
48
~CodecListServiceStub()49 CodecListServiceStub::~CodecListServiceStub()
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->CodecListServiceStubDtor();
56 }
57
DestroyStub()58 int32_t CodecListServiceStub::DestroyStub()
59 {
60 std::lock_guard<std::mutex> lock(g_mutex);
61 UNITTEST_INFO_LOG("");
62 auto mock = g_mockObject.lock();
63 UNITTEST_CHECK_AND_RETURN_RET_LOG(mock != nullptr, AVCS_ERR_UNKNOWN, "mock object is nullptr");
64 return mock->DestroyStub();
65 }
66 } // namespace MediaAVCodec
67 } // namespace OHOS
68