1 /*
2  * Copyright (C) 2019 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 #define LOG_TAG "AudioPolicy_Boot_Test"
18 
19 #include <unordered_set>
20 
21 #include <gtest/gtest.h>
22 
23 #include <media/AudioSystem.h>
24 #include <system/audio.h>
25 #include <utils/Log.h>
26 
27 #include "AudioPolicyManagerTestClient.h"
28 #include "AudioPolicyTestManager.h"
29 
30 using namespace android;
31 
TEST(AudioHealthTest,AttachedDeviceFound)32 TEST(AudioHealthTest, AttachedDeviceFound) {
33     unsigned int numPorts;
34     unsigned int generation1;
35     unsigned int generation;
36     struct audio_port *audioPorts = NULL;
37     int attempts = 10;
38     do {
39         if (attempts-- < 0) {
40             free(audioPorts);
41             GTEST_FAIL() << "Query audio ports time out";
42         }
43         numPorts = 0;
44         ASSERT_EQ(NO_ERROR, AudioSystem::listAudioPorts(
45                 AUDIO_PORT_ROLE_NONE, AUDIO_PORT_TYPE_DEVICE, &numPorts, NULL, &generation1));
46         if (numPorts == 0) {
47             free(audioPorts);
48             GTEST_FAIL() << "Number of audio ports should not be zero";
49         }
50 
51         audioPorts = (struct audio_port *)realloc(audioPorts, numPorts * sizeof(struct audio_port));
52         status_t status = AudioSystem::listAudioPorts(
53                 AUDIO_PORT_ROLE_NONE, AUDIO_PORT_TYPE_DEVICE, &numPorts, audioPorts, &generation);
54         if (status != NO_ERROR) {
55             free(audioPorts);
56             GTEST_FAIL() << "Query audio ports failed";
57         }
58     } while (generation1 != generation);
59     std::unordered_set<audio_devices_t> attachedDevices;
60     for (int i = 0 ; i < numPorts; i++) {
61         attachedDevices.insert(audioPorts[i].ext.device.type);
62     }
63     free(audioPorts);
64 
65     AudioPolicyManagerTestClient client;
66     AudioPolicyTestManager manager(&client);
67     manager.loadConfig();
68     ASSERT_NE("AudioPolicyConfig::setDefault", manager.getConfig().getSource());
69 
70     for (auto desc : manager.getConfig().getInputDevices()) {
71         ASSERT_NE(attachedDevices.end(), attachedDevices.find(desc->type()));
72     }
73     for (auto desc : manager.getConfig().getOutputDevices()) {
74         ASSERT_NE(attachedDevices.end(), attachedDevices.find(desc->type()));
75     }
76 }
77