1 /*
2 * Copyright (C) 2025 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
16 #include "audio_server_sink_plugin_unittest.h"
17
18 namespace OHOS {
19 namespace Media {
20 namespace Plugins {
21 using namespace std;
22 using namespace testing;
23 using namespace testing::ext;
24 using namespace OHOS::AudioStandard;
25
26 static const int32_t NUM_1 = 1;
27 static const float FLOAT_0 = 0;
28
SetUpTestCase(void)29 void AudioServerSinkPluginUnitTest::SetUpTestCase(void) {}
30
TearDownTestCase(void)31 void AudioServerSinkPluginUnitTest::TearDownTestCase(void) {}
32
SetUp(void)33 void AudioServerSinkPluginUnitTest::SetUp(void)
34 {
35 plugin_ = std::make_shared<AudioServerSinkPlugin>("testAudioServerSinkPlugin");
36 }
37
TearDown(void)38 void AudioServerSinkPluginUnitTest::TearDown(void)
39 {
40 plugin_ = nullptr;
41 }
42
43 /**
44 * @tc.name : Test OnStateChange
45 * @tc.number: OnStateChange_001
46 * @tc.desc : Test cmdType == AudioStandard::StateChangeCmdType::CMD_FROM_SYSTEM
47 */
48 HWTEST_F(AudioServerSinkPluginUnitTest, OnStateChange_001, TestSize.Level0)
49 {
50 auto eventReceiver = std::make_shared<MockEventReceiver>();
51 AudioServerSinkPlugin::AudioRendererCallbackImpl impl(eventReceiver, true);
52 auto state = OHOS::AudioStandard::RendererState::RENDERER_NEW;
53 auto cmdType = OHOS::AudioStandard::StateChangeCmdType::CMD_FROM_SYSTEM;
54 EXPECT_CALL(*eventReceiver, OnEvent(_)).WillOnce(Return());
55 impl.OnStateChange(state, cmdType);
56 }
57
58 /**
59 * @tc.name : Test ChooseVolumeMode
60 * @tc.number:ChooseVolumeMode_001
61 * @tc.desc : Test audioRenderInfo_.volumeMode ==
62 * static_cast<int32_t>(AudioStandard::AudioVolumeMode::AUDIOSTREAM_VOLUMEMODE_APP_INDIVIDUAL)
63 */
64 HWTEST_F(AudioServerSinkPluginUnitTest, ChooseVolumeMode_001, TestSize.Level0)
65 {
66 ASSERT_NE(plugin_, nullptr);
67 plugin_->volumeMode_ =
68 static_cast<int32_t>(AudioStandard::AudioVolumeMode::AUDIOSTREAM_VOLUMEMODE_SYSTEM_GLOBAL);
69 plugin_->audioRenderInfo_.volumeMode =
70 static_cast<int32_t>(AudioStandard::AudioVolumeMode::AUDIOSTREAM_VOLUMEMODE_APP_INDIVIDUAL);
71 auto ret = plugin_->ChooseVolumeMode();
72 EXPECT_EQ(ret, NUM_1);
73 }
74
75 /**
76 * @tc.name : Test ReleaseFile
77 * @tc.number: ReleaseFile_001
78 * @tc.desc : Test entireDumpFile_ != nullptr
79 * Test sliceDumpFile_ != nullptr
80 */
81 HWTEST_F(AudioServerSinkPluginUnitTest, ReleaseFile_001, TestSize.Level0)
82 {
83 ASSERT_NE(plugin_, nullptr);
84 plugin_->entireDumpFile_ = fopen("test_1.txt", "w");
85 ASSERT_NE(plugin_->entireDumpFile_, nullptr);
86 plugin_->sliceDumpFile_ = fopen("test_2.txt", "w");
87 ASSERT_NE(plugin_->sliceDumpFile_, nullptr);
88 plugin_->ReleaseFile();
89 EXPECT_EQ(plugin_->entireDumpFile_, nullptr);
90 EXPECT_EQ(plugin_->sliceDumpFile_, nullptr);
91 }
92
93 /**
94 * @tc.name : Test SetVolumeWithRamp
95 * @tc.number: SetVolumeWithRamp_001
96 * @tc.desc : Test duration != 0
97 */
98 HWTEST_F(AudioServerSinkPluginUnitTest, SetVolumeWithRamp_001, TestSize.Level0)
99 {
100 ASSERT_NE(plugin_, nullptr);
101 auto mockRenderer = new MockAudioRenderer();
102 plugin_->audioRenderer_.reset(mockRenderer);
103 EXPECT_CALL(*mockRenderer, SetVolumeWithRamp(_, _)).WillOnce(Return(1));
104 auto ret = plugin_->SetVolumeWithRamp(FLOAT_0, NUM_1);
105 EXPECT_EQ(ret, NUM_1);
106 }
107
108 /**
109 * @tc.name : Test SetUpAudioRenderSetFlagSetter
110 * @tc.number: SetUpAudioRenderSetFlagSetter_001
111 * @tc.desc : Test Any::IsSameTypeWith<bool>(para) == false
112 */
113 HWTEST_F(AudioServerSinkPluginUnitTest, SetUpAudioRenderSetFlagSetter_001, TestSize.Level0)
114 {
115 ASSERT_NE(plugin_, nullptr);
116 plugin_->SetUpAudioRenderSetFlagSetter();
117 auto it = plugin_->paramsSetterMap_.find(Tag::AUDIO_RENDER_SET_FLAG);
118 EXPECT_NE(it, plugin_->paramsSetterMap_.end());
119 std::function<Status(const ValueType ¶)> func = plugin_->paramsSetterMap_[Tag::AUDIO_RENDER_SET_FLAG];
120 auto eventReceiver = std::make_shared<MockEventReceiver>();
121 plugin_->playerEventReceiver_ = eventReceiver;
122 EXPECT_CALL(*eventReceiver, OnEvent(_)).WillOnce(Return());
123 const ValueType para = OHOS::Media::Any(NUM_1);
124 auto ret = func(para);
125 EXPECT_EQ(ret, Status::ERROR_MISMATCHED_TYPE);
126 }
127
128 } // namespace Plugins
129 } // namespace Media
130 } // namespace OHOS