AudioRoutingPolicyTest.java revision 0d07c76bbc788fba8c77d8e932330ab22ec6ba27
1/*
2 * Copyright (C) 2015 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.hardware.vehicle.V2_0.VehicleAudioContextFlag;
19import android.hardware.vehicle.V2_0.VehicleAudioRoutingPolicyIndex;
20import android.hardware.vehicle.V2_0.VehiclePermissionModel;
21import android.hardware.vehicle.V2_0.VehiclePropValue;
22import android.hardware.vehicle.V2_0.VehicleProperty;
23import android.hardware.vehicle.V2_0.VehiclePropertyAccess;
24import android.test.suitebuilder.annotation.SmallTest;
25
26import com.google.android.collect.Lists;
27
28import com.android.car.vehiclehal.VehiclePropValueBuilder;
29import com.android.car.vehiclehal.test.MockedVehicleHal.FailingPropertyHandler;
30import com.android.car.vehiclehal.test.MockedVehicleHal.VehicleHalPropertyHandler;
31
32import java.util.ArrayList;
33import java.util.LinkedList;
34import java.util.concurrent.Semaphore;
35import java.util.concurrent.TimeUnit;
36
37@SmallTest
38public class AudioRoutingPolicyTest extends MockedCarTestBase {
39    private static final String TAG = AudioRoutingPolicyTest.class.getSimpleName();
40
41    private static final long TIMEOUT_MS = 3000;
42
43    private final VehicleHalPropertyHandler mAudioRoutingPolicyHandler =
44            new FailingPropertyHandler() {
45
46        @Override
47        public void onPropertySet(VehiclePropValue value) {
48            handlePropertySetEvent(value);
49        }
50    };
51
52    private final Semaphore mWaitSemaphore = new Semaphore(0);
53    private final LinkedList<VehiclePropValue> mEvents = new LinkedList<>();
54
55    @Override
56    protected synchronized void setUp() throws Exception {
57        super.setUp();
58    }
59
60    @Override
61    protected synchronized void configureMockedHal() {
62        addProperty(VehicleProperty.AUDIO_ROUTING_POLICY, mAudioRoutingPolicyHandler)
63                .setAccess(VehiclePropertyAccess.WRITE)
64                .setPermissionModel(VehiclePermissionModel.SYSTEM_APP_ONLY);
65    }
66
67    public void testNoHwVariant() throws Exception {
68        checkPolicy0();
69    }
70
71    public void testHwVariant0() throws Exception {
72        addStaticProperty(VehicleProperty.AUDIO_HW_VARIANT,
73                VehiclePropValueBuilder.newBuilder(VehicleProperty.AUDIO_HW_VARIANT)
74                        .addIntValue(0)
75                        .build())
76                .setConfigArray(Lists.newArrayList(0));
77
78        mEvents.clear();
79        reinitializeMockedHal();
80
81        checkPolicy0();
82    }
83
84    public void testHwVariantForTest() throws Exception {
85        addStaticProperty(VehicleProperty.AUDIO_HW_VARIANT,
86                VehiclePropValueBuilder.newBuilder(VehicleProperty.AUDIO_HW_VARIANT)
87                        .addIntValue(-1)
88                        .build())
89                .setConfigArray(Lists.newArrayList(0));
90
91        mEvents.clear();
92        reinitializeMockedHal();
93
94        checkPolicyForTest();
95    }
96
97    private void checkPolicy0() throws Exception {
98        assertTrue(mWaitSemaphore.tryAcquire(TIMEOUT_MS, TimeUnit.MILLISECONDS));
99        ArrayList<Integer> v = mEvents.get(0).value.int32Values;
100        assertEquals(0, v.get(VehicleAudioRoutingPolicyIndex.STREAM).intValue());
101        int contexts = v.get(VehicleAudioRoutingPolicyIndex.CONTEXTS);
102        // check if all contexts are allowed ones.
103        assertTrue((contexts & ~(
104                VehicleAudioContextFlag.ALARM_FLAG |
105                VehicleAudioContextFlag.CALL_FLAG |
106                VehicleAudioContextFlag.MUSIC_FLAG |
107                VehicleAudioContextFlag.RADIO_FLAG |
108                VehicleAudioContextFlag.NAVIGATION_FLAG |
109                VehicleAudioContextFlag.NOTIFICATION_FLAG |
110                VehicleAudioContextFlag.UNKNOWN_FLAG |
111                VehicleAudioContextFlag.VOICE_COMMAND_FLAG |
112                VehicleAudioContextFlag.SYSTEM_SOUND_FLAG |
113                VehicleAudioContextFlag.SAFETY_ALERT_FLAG)) == 0);
114    }
115
116    private void checkPolicyForTest() throws Exception {
117        // write happens twice.
118        assertTrue(mWaitSemaphore.tryAcquire(TIMEOUT_MS, TimeUnit.MILLISECONDS));
119        assertTrue(mWaitSemaphore.tryAcquire(TIMEOUT_MS, TimeUnit.MILLISECONDS));
120        ArrayList<Integer> v = mEvents.get(0).value.int32Values;
121        assertEquals(0, v.get(VehicleAudioRoutingPolicyIndex.STREAM).intValue());
122        assertEquals(
123                VehicleAudioContextFlag.CALL_FLAG |
124                VehicleAudioContextFlag.MUSIC_FLAG |
125                VehicleAudioContextFlag.RADIO_FLAG |
126                VehicleAudioContextFlag.UNKNOWN_FLAG,
127                v.get(VehicleAudioRoutingPolicyIndex.CONTEXTS).intValue());
128        v = mEvents.get(1).value.int32Values;
129        assertEquals(1, v.get(VehicleAudioRoutingPolicyIndex.STREAM).intValue());
130        assertEquals(
131                VehicleAudioContextFlag.ALARM_FLAG |
132                VehicleAudioContextFlag.NAVIGATION_FLAG |
133                VehicleAudioContextFlag.NOTIFICATION_FLAG |
134                VehicleAudioContextFlag.VOICE_COMMAND_FLAG |
135                VehicleAudioContextFlag.SYSTEM_SOUND_FLAG |
136                VehicleAudioContextFlag.SAFETY_ALERT_FLAG,
137                v.get(VehicleAudioRoutingPolicyIndex.CONTEXTS).intValue());
138    }
139
140    private void handlePropertySetEvent(VehiclePropValue value) {
141        mEvents.add(value);
142        mWaitSemaphore.release();
143    }
144}
145