1fa7767c5Sopenharmony_ci/* 2fa7767c5Sopenharmony_ci * Copyright (c) 2021-2021 Huawei Device Co., Ltd. 3fa7767c5Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4fa7767c5Sopenharmony_ci * you may not use this file except in compliance with the License. 5fa7767c5Sopenharmony_ci * You may obtain a copy of the License at 6fa7767c5Sopenharmony_ci * 7fa7767c5Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8fa7767c5Sopenharmony_ci * 9fa7767c5Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10fa7767c5Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11fa7767c5Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12fa7767c5Sopenharmony_ci * See the License for the specific language governing permissions and 13fa7767c5Sopenharmony_ci * limitations under the License. 14fa7767c5Sopenharmony_ci */ 15fa7767c5Sopenharmony_ci 16fa7767c5Sopenharmony_ci#ifdef OHOS_LITE 17fa7767c5Sopenharmony_ci#include <memory> 18fa7767c5Sopenharmony_ci#include "plugin/common/any.h" 19fa7767c5Sopenharmony_ci 20fa7767c5Sopenharmony_ci#include <gtest/gtest.h> 21fa7767c5Sopenharmony_ci#include <mockcpp/mockcpp.hpp> 22fa7767c5Sopenharmony_ci 23fa7767c5Sopenharmony_ci#include <test_helper.h> 24fa7767c5Sopenharmony_ci#include "pipeline/filters/codec/audio_decoder/audio_decoder_filter.h" 25fa7767c5Sopenharmony_ci#include "pipeline/filters/sink/audio_sink/audio_sink_filter.h" 26fa7767c5Sopenharmony_ci#include "scene/player/lite/hiplayer_impl.h" 27fa7767c5Sopenharmony_ci 28fa7767c5Sopenharmony_ciusing namespace testing::ext; 29fa7767c5Sopenharmony_ciusing namespace OHOS::Media::Pipeline; 30fa7767c5Sopenharmony_ci 31fa7767c5Sopenharmony_cinamespace OHOS::Media::Test { 32fa7767c5Sopenharmony_ciclass UtTestHiPlayer : public ::testing::Test { 33fa7767c5Sopenharmony_cipublic: 34fa7767c5Sopenharmony_ci MockObject<MediaSourceFilter> audioSource {}; 35fa7767c5Sopenharmony_ci MockObject<DemuxerFilter> demuxer {}; 36fa7767c5Sopenharmony_ci MockObject<AudioDecoderFilter> audioDecoder {}; 37fa7767c5Sopenharmony_ci MockObject<AudioSinkFilter> audioSink {}; 38fa7767c5Sopenharmony_ci 39fa7767c5Sopenharmony_ci std::shared_ptr<HiPlayerImpl> player = HiPlayerImpl::CreateHiPlayerImpl(); 40fa7767c5Sopenharmony_ci static OHOS::Media::Source source; 41fa7767c5Sopenharmony_ci PInPort emptyInPort = EmptyInPort::GetInstance(); 42fa7767c5Sopenharmony_ci POutPort emptyOutPort = EmptyOutPort::GetInstance(); 43fa7767c5Sopenharmony_ci 44fa7767c5Sopenharmony_ci virtual void SetUp() override 45fa7767c5Sopenharmony_ci { 46fa7767c5Sopenharmony_ci MOCK_METHOD(audioSource, Init).defaults(); 47fa7767c5Sopenharmony_ci MOCK_METHOD(demuxer, Init).defaults(); 48fa7767c5Sopenharmony_ci MOCK_METHOD(audioDecoder, Init).defaults(); 49fa7767c5Sopenharmony_ci MOCK_METHOD(audioSink, Init).defaults(); 50fa7767c5Sopenharmony_ci 51fa7767c5Sopenharmony_ci MOCK_METHOD(audioSource, Prepare).defaults().will(returnValue(ErrorCode::SUCCESS)); 52fa7767c5Sopenharmony_ci MOCK_METHOD(demuxer, Prepare).defaults().will(returnValue(ErrorCode::SUCCESS)); 53fa7767c5Sopenharmony_ci MOCK_METHOD(audioDecoder, Prepare).defaults().will(returnValue(ErrorCode::SUCCESS)); 54fa7767c5Sopenharmony_ci MOCK_METHOD(audioSink, Prepare).defaults().will(returnValue(ErrorCode::SUCCESS)); 55fa7767c5Sopenharmony_ci 56fa7767c5Sopenharmony_ci MOCK_METHOD(audioSource, Start).defaults().will(returnValue(ErrorCode::SUCCESS)); 57fa7767c5Sopenharmony_ci MOCK_METHOD(demuxer, Start).defaults().will(returnValue(ErrorCode::SUCCESS)); 58fa7767c5Sopenharmony_ci MOCK_METHOD(audioDecoder, Start).defaults().will(returnValue(ErrorCode::SUCCESS)); 59fa7767c5Sopenharmony_ci MOCK_METHOD(audioSink, Start).defaults().will(returnValue(ErrorCode::SUCCESS)); 60fa7767c5Sopenharmony_ci 61fa7767c5Sopenharmony_ci MOCK_METHOD(audioSource, Stop).defaults().will(returnValue(ErrorCode::SUCCESS)); 62fa7767c5Sopenharmony_ci MOCK_METHOD(demuxer, Stop).defaults().will(returnValue(ErrorCode::SUCCESS)); 63fa7767c5Sopenharmony_ci MOCK_METHOD(audioDecoder, Stop).defaults().will(returnValue(ErrorCode::SUCCESS)); 64fa7767c5Sopenharmony_ci MOCK_METHOD(audioSink, Stop).defaults().will(returnValue(ErrorCode::SUCCESS)); 65fa7767c5Sopenharmony_ci 66fa7767c5Sopenharmony_ci MOCK_METHOD(audioSource, GetInPort).defaults().will(returnValue(emptyInPort)); 67fa7767c5Sopenharmony_ci MOCK_METHOD(demuxer, GetInPort).defaults().will(returnValue(emptyInPort)); 68fa7767c5Sopenharmony_ci MOCK_METHOD(audioDecoder, GetInPort).defaults().will(returnValue(emptyInPort)); 69fa7767c5Sopenharmony_ci MOCK_METHOD(audioSink, GetInPort).defaults().will(returnValue(emptyInPort)); 70fa7767c5Sopenharmony_ci 71fa7767c5Sopenharmony_ci MOCK_METHOD(audioSource, GetOutPort).defaults().will(returnValue(emptyOutPort)); 72fa7767c5Sopenharmony_ci MOCK_METHOD(demuxer, GetOutPort).defaults().will(returnValue(emptyOutPort)); 73fa7767c5Sopenharmony_ci MOCK_METHOD(audioDecoder, GetOutPort).defaults().will(returnValue(emptyOutPort)); 74fa7767c5Sopenharmony_ci MOCK_METHOD(audioSink, GetOutPort).defaults().will(returnValue(emptyOutPort)); 75fa7767c5Sopenharmony_ci 76fa7767c5Sopenharmony_ci player->audioSource_.reset<MediaSourceFilter>(audioSource); 77fa7767c5Sopenharmony_ci player->demuxer_.reset<DemuxerFilter>(demuxer); 78fa7767c5Sopenharmony_ci player->audioDecoder_.reset<AudioDecoderFilter>(audioDecoder); 79fa7767c5Sopenharmony_ci player->audioSink_.reset<AudioSinkFilter>(audioSink); 80fa7767c5Sopenharmony_ci 81fa7767c5Sopenharmony_ci MOCK_METHOD(audioSource, SetSource).defaults().will(returnValue(ErrorCode::SUCCESS)); 82fa7767c5Sopenharmony_ci MOCK_METHOD(audioSource, SetBufferSize).defaults().will(returnValue(ErrorCode::SUCCESS)); 83fa7767c5Sopenharmony_ci 84fa7767c5Sopenharmony_ci player->Init(); 85fa7767c5Sopenharmony_ci } 86fa7767c5Sopenharmony_ci virtual void TearDown() override 87fa7767c5Sopenharmony_ci { 88fa7767c5Sopenharmony_ci audioSource.verify(); 89fa7767c5Sopenharmony_ci demuxer.verify(); 90fa7767c5Sopenharmony_ci audioDecoder.verify(); 91fa7767c5Sopenharmony_ci audioSink.verify(); 92fa7767c5Sopenharmony_ci 93fa7767c5Sopenharmony_ci audioSource.reset(); 94fa7767c5Sopenharmony_ci demuxer.reset(); 95fa7767c5Sopenharmony_ci audioDecoder.reset(); 96fa7767c5Sopenharmony_ci audioSink.reset(); 97fa7767c5Sopenharmony_ci } 98fa7767c5Sopenharmony_ci}; 99fa7767c5Sopenharmony_ci 100fa7767c5Sopenharmony_ciOHOS::Media::Source UtTestHiPlayer::source("./test.mp3"); 101fa7767c5Sopenharmony_ci 102fa7767c5Sopenharmony_ciHWTEST_F(UtTestHiPlayer, Can_SetSource, TestSize.Level1) 103fa7767c5Sopenharmony_ci{ 104fa7767c5Sopenharmony_ci // 因为player内部根据source参数创建shared_ptr,最终调用 audioSource.SetSource. 这里没法检查该指针参数了。 105fa7767c5Sopenharmony_ci MOCK_METHOD(audioSource, SetSource) 106fa7767c5Sopenharmony_ci .expects(once()) 107fa7767c5Sopenharmony_ci .with(mockcpp::any()) 108fa7767c5Sopenharmony_ci .will(returnValue(ErrorCode::SUCCESS)); 109fa7767c5Sopenharmony_ci ASSERT_EQ(static_cast<int>(ErrorCode::SUCCESS), player->SetSource(source)); 110fa7767c5Sopenharmony_ci player->fsm_.DoTask(); 111fa7767c5Sopenharmony_ci ASSERT_EQ("PreparingState", player->fsm_.curState_->GetName()); 112fa7767c5Sopenharmony_ci} 113fa7767c5Sopenharmony_ci 114fa7767c5Sopenharmony_ciHWTEST_F(UtTestHiPlayer, Can_SetBufferSize, TestSize.Level1) 115fa7767c5Sopenharmony_ci{ 116fa7767c5Sopenharmony_ci size_t size = 100; 117fa7767c5Sopenharmony_ci MOCK_METHOD(audioSource, SetBufferSize) 118fa7767c5Sopenharmony_ci .expects(once()) 119fa7767c5Sopenharmony_ci .with(eq(size)) 120fa7767c5Sopenharmony_ci .will(returnValue(ErrorCode::SUCCESS)); 121fa7767c5Sopenharmony_ci ASSERT_EQ(ErrorCode::SUCCESS, player->SetBufferSize(size)); 122fa7767c5Sopenharmony_ci} 123fa7767c5Sopenharmony_ci} 124fa7767c5Sopenharmony_ci#endif 125