1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <memory>
18 #include <set>
19 
20 #include <gtest/gtest.h>
21 
22 #include "AudioPolicyTestClient.h"
23 #include "AudioPolicyTestManager.h"
24 
25 using namespace android;
26 
TEST(AudioPolicyManagerTestInit,Failure)27 TEST(AudioPolicyManagerTestInit, Failure) {
28     AudioPolicyTestClient client;
29     AudioPolicyTestManager manager(&client);
30     manager.getConfig().setDefault();
31     // Since the default client fails to open anything,
32     // APM should indicate that the initialization didn't succeed.
33     ASSERT_EQ(NO_INIT, manager.initialize());
34     ASSERT_EQ(NO_INIT, manager.initCheck());
35 }
36 
37 
38 class AudioPolicyManagerTestClient : public AudioPolicyTestClient {
39   public:
40     // AudioPolicyClientInterface implementation
loadHwModule(const char *)41     audio_module_handle_t loadHwModule(const char* /*name*/) override {
42         return mNextModuleHandle++;
43     }
44 
openOutput(audio_module_handle_t module,audio_io_handle_t * output,audio_config_t *,audio_devices_t *,const String8 &,uint32_t *,audio_output_flags_t)45     status_t openOutput(audio_module_handle_t module,
46                         audio_io_handle_t* output,
47                         audio_config_t* /*config*/,
48                         audio_devices_t* /*devices*/,
49                         const String8& /*address*/,
50                         uint32_t* /*latencyMs*/,
51                         audio_output_flags_t /*flags*/) override {
52         if (module >= mNextModuleHandle) {
53             ALOGE("%s: Module handle %d has not been allocated yet (next is %d)",
54                     __func__, module, mNextModuleHandle);
55             return BAD_VALUE;
56         }
57         *output = mNextIoHandle++;
58         return NO_ERROR;
59     }
60 
openInput(audio_module_handle_t module,audio_io_handle_t * input,audio_config_t *,audio_devices_t *,const String8 &,audio_source_t,audio_input_flags_t)61     status_t openInput(audio_module_handle_t module,
62                        audio_io_handle_t* input,
63                        audio_config_t* /*config*/,
64                        audio_devices_t* /*device*/,
65                        const String8& /*address*/,
66                        audio_source_t /*source*/,
67                        audio_input_flags_t /*flags*/) override {
68         if (module >= mNextModuleHandle) {
69             ALOGE("%s: Module handle %d has not been allocated yet (next is %d)",
70                     __func__, module, mNextModuleHandle);
71             return BAD_VALUE;
72         }
73         *input = mNextIoHandle++;
74         return NO_ERROR;
75     }
76 
createAudioPatch(const struct audio_patch *,audio_patch_handle_t * handle,int)77     status_t createAudioPatch(const struct audio_patch* /*patch*/,
78                               audio_patch_handle_t* handle,
79                               int /*delayMs*/) override {
80         *handle = mNextPatchHandle++;
81         mActivePatches.insert(*handle);
82         return NO_ERROR;
83     }
84 
releaseAudioPatch(audio_patch_handle_t handle,int)85     status_t releaseAudioPatch(audio_patch_handle_t handle,
86                                int /*delayMs*/) override {
87         if (mActivePatches.erase(handle) != 1) {
88             if (handle >= mNextPatchHandle) {
89                 ALOGE("%s: Patch handle %d has not been allocated yet (next is %d)",
90                         __func__, handle, mNextPatchHandle);
91             } else {
92                 ALOGE("%s: Attempt to release patch %d twice", __func__, handle);
93             }
94             return BAD_VALUE;
95         }
96         return NO_ERROR;
97     }
98 
99     // Helper methods for tests
getActivePatchesCount() const100     size_t getActivePatchesCount() const { return mActivePatches.size(); }
101 
102   private:
103     audio_module_handle_t mNextModuleHandle = AUDIO_MODULE_HANDLE_NONE + 1;
104     audio_io_handle_t mNextIoHandle = AUDIO_IO_HANDLE_NONE + 1;
105     audio_patch_handle_t mNextPatchHandle = AUDIO_PATCH_HANDLE_NONE + 1;
106     std::set<audio_patch_handle_t> mActivePatches;
107 };
108 
109 class AudioPolicyManagerTest : public testing::Test {
110   protected:
111     virtual void SetUp();
112     virtual void TearDown();
113 
114     std::unique_ptr<AudioPolicyManagerTestClient> mClient;
115     std::unique_ptr<AudioPolicyTestManager> mManager;
116 };
117 
SetUp()118 void AudioPolicyManagerTest::SetUp() {
119     mClient.reset(new AudioPolicyManagerTestClient);
120     mManager.reset(new AudioPolicyTestManager(mClient.get()));
121     mManager->getConfig().setDefault();
122     ASSERT_EQ(NO_ERROR, mManager->initialize());
123     ASSERT_EQ(NO_ERROR, mManager->initCheck());
124 }
125 
TearDown()126 void AudioPolicyManagerTest::TearDown() {
127     mManager.reset();
128     mClient.reset();
129 }
130 
TEST_F(AudioPolicyManagerTest,InitSuccess)131 TEST_F(AudioPolicyManagerTest, InitSuccess) {
132     // SetUp must finish with no assertions.
133 }
134 
TEST_F(AudioPolicyManagerTest,CreateAudioPatchFailure)135 TEST_F(AudioPolicyManagerTest, CreateAudioPatchFailure) {
136     audio_patch patch{};
137     audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE;
138     const size_t patchCountBefore = mClient->getActivePatchesCount();
139     ASSERT_EQ(BAD_VALUE, mManager->createAudioPatch(nullptr, &handle, 0));
140     ASSERT_EQ(BAD_VALUE, mManager->createAudioPatch(&patch, nullptr, 0));
141     ASSERT_EQ(BAD_VALUE, mManager->createAudioPatch(&patch, &handle, 0));
142     patch.num_sources = AUDIO_PATCH_PORTS_MAX + 1;
143     patch.num_sinks = 1;
144     ASSERT_EQ(BAD_VALUE, mManager->createAudioPatch(&patch, &handle, 0));
145     patch.num_sources = 1;
146     patch.num_sinks = AUDIO_PATCH_PORTS_MAX + 1;
147     ASSERT_EQ(BAD_VALUE, mManager->createAudioPatch(&patch, &handle, 0));
148     patch.num_sources = 2;
149     patch.num_sinks = 1;
150     ASSERT_EQ(INVALID_OPERATION, mManager->createAudioPatch(&patch, &handle, 0));
151     patch = {};
152     patch.num_sources = 1;
153     patch.sources[0].role = AUDIO_PORT_ROLE_SINK;
154     patch.num_sinks = 1;
155     patch.sinks[0].role = AUDIO_PORT_ROLE_SINK;
156     ASSERT_EQ(INVALID_OPERATION, mManager->createAudioPatch(&patch, &handle, 0));
157     patch = {};
158     patch.num_sources = 1;
159     patch.sources[0].role = AUDIO_PORT_ROLE_SOURCE;
160     patch.num_sinks = 1;
161     patch.sinks[0].role = AUDIO_PORT_ROLE_SOURCE;
162     ASSERT_EQ(INVALID_OPERATION, mManager->createAudioPatch(&patch, &handle, 0));
163     // Verify that the handle is left unchanged.
164     ASSERT_EQ(AUDIO_PATCH_HANDLE_NONE, handle);
165     ASSERT_EQ(patchCountBefore, mClient->getActivePatchesCount());
166 }
167 
TEST_F(AudioPolicyManagerTest,CreateAudioPatchFromMix)168 TEST_F(AudioPolicyManagerTest, CreateAudioPatchFromMix) {
169     audio_patch patch{};
170     audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE;
171     uid_t uid = 42;
172     const size_t patchCountBefore = mClient->getActivePatchesCount();
173     patch.num_sources = 1;
174     {
175         auto& src = patch.sources[0];
176         src.role = AUDIO_PORT_ROLE_SOURCE;
177         src.type = AUDIO_PORT_TYPE_MIX;
178         src.id = mManager->getConfig().getAvailableInputDevices()[0]->getId();
179         // Note: these are the parameters of the output device.
180         src.sample_rate = 44100;
181         src.format = AUDIO_FORMAT_PCM_16_BIT;
182         src.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
183     }
184     patch.num_sinks = 1;
185     {
186         auto& sink = patch.sinks[0];
187         sink.role = AUDIO_PORT_ROLE_SINK;
188         sink.type = AUDIO_PORT_TYPE_DEVICE;
189         sink.id = mManager->getConfig().getDefaultOutputDevice()->getId();
190     }
191     ASSERT_EQ(NO_ERROR, mManager->createAudioPatch(&patch, &handle, uid));
192     ASSERT_NE(AUDIO_PATCH_HANDLE_NONE, handle);
193     ASSERT_EQ(patchCountBefore + 1, mClient->getActivePatchesCount());
194 }
195 
196 // TODO: Add patch creation tests that involve already existing patch
197