CarAudioManagerTest.java revision fe1a8f14e1ac56f095d29336e0986950d8adfc0c
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 */
16package com.android.car.test;
17
18import android.car.Car;
19import android.car.media.CarAudioManager;
20import android.hardware.vehicle.V2_0.VehiclePropValue;
21import android.hardware.vehicle.V2_0.VehicleProperty;
22import android.hardware.vehicle.V2_0.VehiclePropertyAccess;
23import android.os.SystemClock;
24import android.test.suitebuilder.annotation.MediumTest;
25
26import com.android.car.vehiclehal.VehiclePropValueBuilder;
27import com.android.car.vehiclehal.test.MockedVehicleHal.VehicleHalPropertyHandler;
28
29import java.util.concurrent.Semaphore;
30import java.util.concurrent.TimeUnit;
31
32@MediumTest
33public class CarAudioManagerTest extends MockedCarTestBase {
34    private final AudioParametersPropertyHandler mAudioParametersPropertyHandler =
35            new AudioParametersPropertyHandler();
36    CarAudioManager mCarAudioManager;
37
38    @Override
39    protected synchronized void configureMockedHal() {
40        addProperty(VehicleProperty.AUDIO_PARAMETERS, mAudioParametersPropertyHandler)
41	    .setAccess(VehiclePropertyAccess.READ_WRITE)
42	    .setConfigString("com.android.test.param1;com.android.test.param2");
43    }
44
45    @Override
46    protected void setUp() throws Exception {
47        super.setUp();
48        mCarAudioManager = (CarAudioManager) getCar().getCarManager(
49                Car.AUDIO_SERVICE);
50        assertNotNull(mCarAudioManager);
51    }
52
53    public void testAudioParamConfig() throws Exception {
54        String[] keys = mCarAudioManager.getParameterKeys();
55        assertNotNull(keys);
56        assertEquals(2, keys.length);
57        assertEquals("com.android.test.param1", keys[0]);
58        assertEquals("com.android.test.param2", keys[1]);
59    }
60
61    public void testAudioParamSet() throws Exception {
62        try {
63            mCarAudioManager.setParameters(null);
64            fail();
65        } catch (IllegalArgumentException e) {
66            // expected
67        }
68
69        try {
70            mCarAudioManager.setParameters("com.android.test.param3=3");
71            fail();
72        } catch (IllegalArgumentException e) {
73            // expected
74        }
75
76        final String SET_OK1 = "com.android.test.param1=1";
77        mCarAudioManager.setParameters(SET_OK1);
78        mAudioParametersPropertyHandler.waitForSet(DEFAULT_WAIT_TIMEOUT_MS, SET_OK1);
79
80        final String SET_OK2 = "com.android.test.param1=1;com.android.test.param2=2";
81        mCarAudioManager.setParameters(SET_OK2);
82        mAudioParametersPropertyHandler.waitForSet(DEFAULT_WAIT_TIMEOUT_MS, SET_OK2);
83    }
84
85    public void testAudioParamGet() throws Exception {
86        try {
87            mCarAudioManager.getParameters(null);
88            fail();
89        } catch (IllegalArgumentException e) {
90            // expected
91        }
92
93        try {
94            mCarAudioManager.getParameters("com.android.test.param3");
95            fail();
96        } catch (IllegalArgumentException e) {
97            // expected
98        }
99
100        try {
101            mCarAudioManager.getParameters("com.android.test.param1;com.android.test.param3");
102            fail();
103        } catch (IllegalArgumentException e) {
104            // expected
105        }
106
107        final String GET_RESP1 = "com.android.test.param1=1";
108        mAudioParametersPropertyHandler.setValueForGet(GET_RESP1);
109        String get1 = mCarAudioManager.getParameters("com.android.test.param1");
110        assertEquals(GET_RESP1, get1);
111
112        final String GET_RESP2 = "com.android.test.param1=1;com.android.test.param2=2";
113        mAudioParametersPropertyHandler.setValueForGet(GET_RESP2);
114        String get2 = mCarAudioManager.getParameters(
115                "com.android.test.param1;com.android.test.param2");
116        assertEquals(GET_RESP2, get2);
117    }
118
119    public void testAudioParamChangeListener() throws Exception {
120        AudioParamListener listener1 = new AudioParamListener();
121        AudioParamListener listener2 = new AudioParamListener();
122
123        mCarAudioManager.setOnParameterChangeListener(listener1);
124        final String EVENT1 = "com.android.test.param1=10";
125        sendAudioParamChange(EVENT1);
126        listener1.waitForChange(DEFAULT_WAIT_TIMEOUT_MS, EVENT1);
127
128        mCarAudioManager.setOnParameterChangeListener(listener2);
129        listener1.clearParameter();
130        final String EVENT2 = "com.android.test.param1=20;com.android.test.param2=10";
131        sendAudioParamChange(EVENT2);
132        listener2.waitForChange(DEFAULT_WAIT_TIMEOUT_MS, EVENT2);
133        listener1.assertParameter(null);
134
135        mCarAudioManager.setOnParameterChangeListener(null);
136        listener2.clearParameter();
137        sendAudioParamChange(EVENT1);
138        Thread.sleep(200);
139        listener1.assertParameter(null);
140        listener2.assertParameter(null);
141    }
142
143    private void sendAudioParamChange(String params) {
144        getMockedVehicleHal().injectEvent(
145                VehiclePropValueBuilder.newBuilder(VehicleProperty.AUDIO_PARAMETERS)
146                .setTimestamp(SystemClock.elapsedRealtimeNanos())
147                .setStringValue(params)
148                .build());
149    }
150
151    static class AudioParametersPropertyHandler implements VehicleHalPropertyHandler {
152        private final Semaphore mSetWaitSemaphore = new Semaphore(0);
153
154        private String mValueSet;
155        private String mGetResponse;
156
157        public void waitForSet(long waitTimeMs, String expected) throws Exception {
158            mSetWaitSemaphore.tryAcquire(waitTimeMs, TimeUnit.MILLISECONDS);
159            synchronized (this) {
160                assertEquals(expected, mValueSet);
161            }
162        }
163
164        public synchronized void setValueForGet(String value) {
165            mGetResponse = value;
166        }
167
168        @Override
169        public void onPropertySet(VehiclePropValue value) {
170            assertEquals(VehicleProperty.AUDIO_PARAMETERS, value.prop);
171            String setValue = value.value.stringValue;
172            synchronized (this) {
173                mValueSet = setValue;
174            }
175            mSetWaitSemaphore.release();
176        }
177
178        @Override
179        public VehiclePropValue onPropertyGet(VehiclePropValue value) {
180            assertEquals(VehicleProperty.AUDIO_PARAMETERS, value.prop);
181            String response;
182            synchronized (this) {
183                response = mGetResponse;
184            }
185            return VehiclePropValueBuilder.newBuilder(VehicleProperty.AUDIO_PARAMETERS)
186                    .setTimestamp(SystemClock.elapsedRealtimeNanos())
187                    .setStringValue(mGetResponse)
188                    .build();
189        }
190
191        @Override
192        public void onPropertySubscribe(int property, int zones, float sampleRate) {
193            assertEquals(VehicleProperty.AUDIO_PARAMETERS, property);
194        }
195
196        @Override
197        public void onPropertyUnsubscribe(int property) {
198            assertEquals(VehicleProperty.AUDIO_PARAMETERS, property);
199        }
200    }
201
202    static class AudioParamListener implements CarAudioManager.OnParameterChangeListener {
203        private String mParameter;
204        private final Semaphore mChangeWaitSemaphore = new Semaphore(0);
205
206        @Override
207        public void onParameterChange(String parameters) {
208            mParameter = parameters;
209            mChangeWaitSemaphore.release();
210        }
211
212        public void waitForChange(long waitTimeMs, String expected) throws Exception {
213            mChangeWaitSemaphore.tryAcquire(waitTimeMs, TimeUnit.MILLISECONDS);
214            assertEquals(expected, mParameter);
215        }
216
217        public void clearParameter() {
218            mParameter = null;
219        }
220
221        public void assertParameter(String expected) {
222            assertEquals(expected, mParameter);
223        }
224    }
225}
226