1 /*
2  * Copyright (c) 2022 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 <gtest/gtest.h>
17 #include "background_audio_controller.h"
18 #include "avsession_log.h"
19 #include "audio_info.h"
20 #include "running_process_info.h"
21 #include "app_manager_adapter.h"
22 #include "bundle_mgr_client.h"
23 #include "avsession_descriptor.h"
24 
25 using namespace testing::ext;
26 using namespace OHOS::AVSession;
27 
28 class BkGrAudioControllerTest : public testing::Test {
29 public:
30     static void SetUpTestCase(void);
31     static void TearDownTestCase(void);
32     void SetUp();
33     void TearDown();
34 };
35 
SetUpTestCase()36 void BkGrAudioControllerTest::SetUpTestCase()
37 {}
38 
TearDownTestCase()39 void BkGrAudioControllerTest::TearDownTestCase()
40 {}
41 
SetUp()42 void BkGrAudioControllerTest::SetUp()
43 {}
44 
TearDown()45 void BkGrAudioControllerTest::TearDown()
46 {}
47 
48 /**
49 * @tc.name: OnSessionRelease001
50 * @tc.desc: descriptor.isThirdPartyApp_ = false
51 * @tc.type: FUNC
52 * @tc.require: #I62OZV
53 */
54 static HWTEST(BkGrAudioControllerTest, OnSessionRelease001, TestSize.Level1)
55 {
56     SLOGI("OnSessionRelease001 begin!");
57     AVSessionDescriptor descriptor;
58     descriptor.isThirdPartyApp_ = false;
59     BackgroundAudioController bkgraudiocontroller;
60     bkgraudiocontroller.OnSessionRelease(descriptor);
61     EXPECT_EQ(descriptor.isThirdPartyApp_, false);
62     SLOGI("OnSessionRelease001 end!");
63 }
64 
65 /**
66 * @tc.name: OnSessionRelease002
67 * @tc.desc: descriptor.isThirdPartyApp_ = true&&descriptor.uid_ = -1
68 * @tc.type: FUNC
69 * @tc.require: #I62OZV
70 */
71 static HWTEST(BkGrAudioControllerTest, OnSessionRelease002, TestSize.Level1)
72 {
73     SLOGI("OnSessionRelease002 begin!");
74     AVSessionDescriptor descriptor;
75     descriptor.isThirdPartyApp_ = true;
76     descriptor.uid_ = -1;
77     BackgroundAudioController bkgraudiocontroller;
78     bkgraudiocontroller.OnSessionRelease(descriptor);
79     EXPECT_EQ(descriptor.isThirdPartyApp_, true);
80     SLOGI("OnSessionRelease002 end!");
81 }
82 
83 /**
84 * @tc.name: OnSessionRelease003
85 * @tc.desc: descriptor.isThirdPartyApp_ = true&&descriptor.uid_ = 100
86 * @tc.type: FUNC
87 * @tc.require: #I62OZV
88 */
89 static HWTEST(BkGrAudioControllerTest, OnSessionRelease003, TestSize.Level1)
90 {
91     SLOGI("OnSessionRelease003 begin!");
92     AVSessionDescriptor descriptor;
93     descriptor.isThirdPartyApp_ = true;
94     descriptor.uid_ = 100;
95     BackgroundAudioController bkgraudiocontroller;
96     bkgraudiocontroller.OnSessionRelease(descriptor);
97     EXPECT_EQ(descriptor.isThirdPartyApp_, true);
98     SLOGI("OnSessionRelease032 end!");
99 }
100 
101 /**
102 * @tc.name: HandleAudioStreamRendererStateChange001
103 * @tc.desc: info->rendererState != AudioStandard::RENDERER_RUNNING
104 * @tc.type: FUNC
105 * @tc.require: #I62OZV
106 */
107 static HWTEST(BkGrAudioControllerTest, HandleAudioStreamRendererStateChange001, TestSize.Level1)
108 {
109     SLOGI("HandleAudioStreamRendererStateChange001 begin!");
110     std::unique_ptr<OHOS::AudioStandard::AudioRendererChangeInfo> info =
111         std::make_unique<OHOS::AudioStandard::AudioRendererChangeInfo>();
112     std::vector<std::unique_ptr<OHOS::AudioStandard::AudioRendererChangeInfo>> audioRendererChangeInfos;
113     info->rendererState = OHOS::AudioStandard::RENDERER_NEW;
114     audioRendererChangeInfos.push_back(move(info));
115     BackgroundAudioController backgroundaudiocontroller;
116     backgroundaudiocontroller.HandleAudioStreamRendererStateChange(audioRendererChangeInfos);
117     EXPECT_EQ(audioRendererChangeInfos[0]->rendererState, OHOS::AudioStandard::RENDERER_NEW);
118 }
119 
120 /**
121 * @tc.name: HandleAudioStreamRendererStateChange002
122 * @tc.desc: !AppManagerAdapter::GetInstance().IsAppBackground(info->clientUID)
123 * @tc.type: FUNC
124 * @tc.require: #I62OZV
125 */
126 static HWTEST(BkGrAudioControllerTest, HandleAudioStreamRendererStateChange002, TestSize.Level1)
127 {
128     SLOGI("HandleAudioStreamRendererStateChange002 begin!");
129     std::unique_ptr<OHOS::AudioStandard::AudioRendererChangeInfo> info =
130         std::make_unique<OHOS::AudioStandard::AudioRendererChangeInfo>();
131     std::vector<std::unique_ptr<OHOS::AudioStandard::AudioRendererChangeInfo>> audioRendererChangeInfos;
132     info->rendererState = OHOS::AudioStandard::RENDERER_RUNNING;
133     info->clientUID = -1;
134     info->clientPid = -1;
135     audioRendererChangeInfos.push_back(move(info));
136     BackgroundAudioController backgroundaudiocontroller;
137     backgroundaudiocontroller.HandleAudioStreamRendererStateChange(audioRendererChangeInfos);
138     EXPECT_EQ(audioRendererChangeInfos[0]->clientUID, -1);
139 }
140 
141 /**
142 * @tc.name: HandleAppBackgroundState001
143 * @tc.desc: int32_t uid = -1;
144 * @tc.type: FUNC
145 * @tc.require: #I62OZV
146 */
147 static HWTEST(BkGrAudioControllerTest, HandleAppBackgroundState001, TestSize.Level1)
148 {
149     SLOGI("HandleAppBackgroundState001 begin!");
150     int32_t uid = -1;
151     int32_t pid = -1;
152     BackgroundAudioController backgroundaudiocontroller;
153     backgroundaudiocontroller.HandleAppBackgroundState(uid, pid);
154     EXPECT_EQ(uid, -1);
155 }