• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <string>
20 #include <unordered_set>
21 
22 #include <gtest/gtest.h>
23 
24 #include <AudioPolicyConfig.h>
25 #include <media/AudioSystem.h>
26 #include <media/TypeConverter.h>
27 #include <system/audio.h>
28 #include <utils/Log.h>
29 
30 #include "AudioPolicyManagerTestClient.h"
31 #include "AudioPolicyTestManager.h"
32 
33 using namespace android;
34 
TEST(AudioHealthTest,AttachedDeviceFound)35 TEST(AudioHealthTest, AttachedDeviceFound) {
36     unsigned int numPorts;
37     unsigned int generation1;
38     unsigned int generation;
39     struct audio_port_v7 *audioPorts = nullptr;
40     int attempts = 10;
41     do {
42         if (attempts-- < 0) {
43             free(audioPorts);
44             GTEST_FAIL() << "Query audio ports time out";
45         }
46         numPorts = 0;
47         ASSERT_EQ(NO_ERROR, AudioSystem::listAudioPorts(
48                 AUDIO_PORT_ROLE_NONE, AUDIO_PORT_TYPE_DEVICE, &numPorts, nullptr, &generation1));
49         if (numPorts == 0) {
50             free(audioPorts);
51             GTEST_FAIL() << "Number of audio ports should not be zero";
52         }
53 
54         audioPorts = (struct audio_port_v7 *)realloc(
55                 audioPorts, numPorts * sizeof(struct audio_port_v7));
56         status_t status = AudioSystem::listAudioPorts(
57                 AUDIO_PORT_ROLE_NONE, AUDIO_PORT_TYPE_DEVICE, &numPorts, audioPorts, &generation);
58         if (status != NO_ERROR) {
59             free(audioPorts);
60             GTEST_FAIL() << "Query audio ports failed";
61         }
62     } while (generation1 != generation);
63     std::unordered_set<audio_devices_t> attachedDevices;
64     for (int i = 0 ; i < numPorts; i++) {
65         attachedDevices.insert(audioPorts[i].ext.device.type);
66     }
67     free(audioPorts);
68 
69     auto config = AudioPolicyConfig::loadFromApmXmlConfigWithFallback();
70     ASSERT_NE(AudioPolicyConfig::kDefaultConfigSource, config->getSource());
71 
72     for (const auto& desc : config->getInputDevices()) {
73         if (attachedDevices.find(desc->type()) == attachedDevices.end()) {
74             std::string deviceType;
75             (void)DeviceConverter::toString(desc->type(), deviceType);
76             ADD_FAILURE() << "Input device \"" << deviceType << "\" not found";
77         }
78     }
79     for (const auto& desc : config->getOutputDevices()) {
80         if (attachedDevices.find(desc->type()) == attachedDevices.end()) {
81             std::string deviceType;
82             (void)DeviceConverter::toString(desc->type(), deviceType);
83             ADD_FAILURE() << "Output device \"" << deviceType << "\" not found";
84         }
85     }
86 }
87 
TEST(AudioHealthTest,ConnectSupportedDevice)88 TEST(AudioHealthTest, ConnectSupportedDevice) {
89     auto config = AudioPolicyConfig::loadFromApmXmlConfigWithFallback();
90     ASSERT_NE(AudioPolicyConfig::kDefaultConfigSource, config->getSource());
91     AudioPolicyManagerTestClient client;
92     AudioPolicyTestManager manager(config, &client);
93 
94     DeviceVector devices;
95     for (const auto& hwModule : config->getHwModules()) {
96         for (const auto& profile : hwModule->getOutputProfiles()) {
97             devices.merge(profile->getSupportedDevices());
98         }
99         for (const auto& profile : hwModule->getInputProfiles()) {
100             devices.merge(profile->getSupportedDevices());
101         }
102     }
103     for (const auto& device : devices) {
104         if (!audio_is_bluetooth_out_sco_device(device->type()) &&
105             !audio_is_bluetooth_in_sco_device(device->type())) {
106             // There are two reasons to only test connecting BT devices.
107             // 1) It is easier to construct a fake address.
108             // 2) This test will be run in presubmit. In that case, it makes sense to make the test
109             //    processing time short.
110             continue;
111         }
112         std::string address = "11:22:33:44:55:66";
113         media::AudioPortFw aidlPort;
114         ASSERT_EQ(OK, manager.deviceToAudioPort(device->type(), address.c_str(), "" /*name*/,
115                                                  &aidlPort));
116         ASSERT_EQ(AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
117                 AudioSystem::getDeviceConnectionState(device->type(), address.c_str()));
118         ASSERT_EQ(NO_ERROR, AudioSystem::setDeviceConnectionState(
119                 AUDIO_POLICY_DEVICE_STATE_AVAILABLE, aidlPort.hal, AUDIO_FORMAT_DEFAULT));
120         ASSERT_EQ(AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
121                 AudioSystem::getDeviceConnectionState(device->type(), address.c_str()));
122         ASSERT_EQ(NO_ERROR, AudioSystem::setDeviceConnectionState(
123                 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, aidlPort.hal, AUDIO_FORMAT_DEFAULT));
124         ASSERT_EQ(AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
125                 AudioSystem::getDeviceConnectionState(device->type(), address.c_str()));
126     }
127 }
128