Home | History | Annotate | Download | only in test
      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  */
     16 package com.android.car.test;
     17 
     18 import android.car.test.VehicleHalEmulator.VehicleHalPropertyHandler;
     19 import android.test.suitebuilder.annotation.SmallTest;
     20 
     21 import com.android.car.vehiclenetwork.VehicleNetworkConsts;
     22 import com.android.car.vehiclenetwork.VehicleNetworkConsts.VehicleAudioContextFlag;
     23 import com.android.car.vehiclenetwork.VehicleNetworkConsts.VehicleAudioRoutingPolicyIndex;
     24 import com.android.car.vehiclenetwork.VehicleNetworkConsts.VehiclePermissionModel;
     25 import com.android.car.vehiclenetwork.VehicleNetworkConsts.VehiclePropAccess;
     26 import com.android.car.vehiclenetwork.VehicleNetworkConsts.VehiclePropChangeMode;
     27 import com.android.car.vehiclenetwork.VehicleNetworkConsts.VehicleValueType;
     28 import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropValue;
     29 import com.android.car.vehiclenetwork.VehiclePropConfigUtil;
     30 import com.android.car.vehiclenetwork.VehiclePropValueUtil;
     31 
     32 import java.util.LinkedList;
     33 import java.util.concurrent.Semaphore;
     34 import java.util.concurrent.TimeUnit;
     35 
     36 @SmallTest
     37 public class AudioRoutingPolicyTest extends MockedCarTestBase {
     38 
     39     private static final long TIMEOUT_MS = 3000;
     40 
     41     private final VehicleHalPropertyHandler mAudioRoutingPolicyHandler =
     42             new VehicleHalPropertyHandler() {
     43 
     44         @Override
     45         public void onPropertySet(VehiclePropValue value) {
     46             handlePropertySetEvent(value);
     47         }
     48 
     49         @Override
     50         public VehiclePropValue onPropertyGet(VehiclePropValue value) {
     51             fail("cannot get");
     52             return null;
     53         }
     54 
     55         @Override
     56         public void onPropertySubscribe(int property, float sampleRate, int zones) {
     57             fail("cannot subscribe");
     58         }
     59 
     60         @Override
     61         public void onPropertyUnsubscribe(int property) {
     62             fail("cannot unsubscribe");
     63         }
     64     };
     65 
     66     private final Semaphore mWaitSemaphore = new Semaphore(0);
     67     private final LinkedList<VehiclePropValue> mEvents = new LinkedList<VehiclePropValue>();
     68 
     69     @Override
     70     protected void setUp() throws Exception {
     71         super.setUp();
     72 
     73         getVehicleHalEmulator().addProperty(
     74                 VehiclePropConfigUtil.getBuilder(
     75                         VehicleNetworkConsts.VEHICLE_PROPERTY_AUDIO_ROUTING_POLICY,
     76                         VehiclePropAccess.VEHICLE_PROP_ACCESS_WRITE,
     77                         VehiclePropChangeMode.VEHICLE_PROP_CHANGE_MODE_ON_CHANGE,
     78                         VehicleValueType.VEHICLE_VALUE_TYPE_INT32_VEC2,
     79                         VehiclePermissionModel.VEHICLE_PERMISSION_SYSTEM_APP_ONLY,
     80                         0 /*configFlags*/, 0 /*sampleRateMax*/, 0 /*sampleRateMin*/).build(),
     81                 mAudioRoutingPolicyHandler);
     82     }
     83 
     84     public void testNoHwVaraint() throws Exception {
     85         getVehicleHalEmulator().removeProperty(
     86                 VehicleNetworkConsts.VEHICLE_PROPERTY_AUDIO_HW_VARIANT);
     87         getVehicleHalEmulator().start();
     88         checkPolicy0();
     89     }
     90 
     91     public void testHwVariant0() throws Exception {
     92         getVehicleHalEmulator().addStaticProperty(
     93                 VehiclePropConfigUtil.createStaticStringProperty(
     94                         VehicleNetworkConsts.VEHICLE_PROPERTY_AUDIO_HW_VARIANT),
     95                 VehiclePropValueUtil.createIntValue(
     96                         VehicleNetworkConsts.VEHICLE_PROPERTY_AUDIO_HW_VARIANT, 0, 0));
     97         getVehicleHalEmulator().start();
     98         checkPolicy0();
     99     }
    100 
    101     public void testHwVariant1() throws Exception {
    102         getVehicleHalEmulator().addStaticProperty(
    103                 VehiclePropConfigUtil.createStaticStringProperty(
    104                         VehicleNetworkConsts.VEHICLE_PROPERTY_AUDIO_HW_VARIANT),
    105                 VehiclePropValueUtil.createIntValue(
    106                         VehicleNetworkConsts.VEHICLE_PROPERTY_AUDIO_HW_VARIANT, 1, 0));
    107         getVehicleHalEmulator().start();
    108         checkPolicy1();
    109     }
    110 
    111     private void checkPolicy0() throws Exception {
    112         assertTrue(mWaitSemaphore.tryAcquire(TIMEOUT_MS, TimeUnit.MILLISECONDS));
    113         VehiclePropValue v = mEvents.get(0);
    114         assertEquals(0, v.getInt32Values(
    115                 VehicleAudioRoutingPolicyIndex.VEHICLE_AUDIO_ROUTING_POLICY_INDEX_STREAM));
    116         assertEquals(
    117                 VehicleAudioContextFlag.VEHICLE_AUDIO_CONTEXT_ALARM_FLAG |
    118                 VehicleAudioContextFlag.VEHICLE_AUDIO_CONTEXT_CALL_FLAG |
    119                 VehicleAudioContextFlag.VEHICLE_AUDIO_CONTEXT_MUSIC_FLAG |
    120                 VehicleAudioContextFlag.VEHICLE_AUDIO_CONTEXT_NAVIGATION_FLAG |
    121                 VehicleAudioContextFlag.VEHICLE_AUDIO_CONTEXT_NOTIFICATION_FLAG |
    122                 VehicleAudioContextFlag.VEHICLE_AUDIO_CONTEXT_UNKNOWN_FLAG |
    123                 VehicleAudioContextFlag.VEHICLE_AUDIO_CONTEXT_VOICE_COMMAND_FLAG |
    124                 VehicleAudioContextFlag.VEHICLE_AUDIO_CONTEXT_SYSTEM_SOUND_FLAG |
    125                 VehicleAudioContextFlag.VEHICLE_AUDIO_CONTEXT_SAFETY_ALERT_FLAG,
    126                 v.getInt32Values(
    127                         VehicleAudioRoutingPolicyIndex.VEHICLE_AUDIO_ROUTING_POLICY_INDEX_CONTEXTS)
    128                         );
    129     }
    130 
    131     private void checkPolicy1() throws Exception {
    132         // write happens twice.
    133         assertTrue(mWaitSemaphore.tryAcquire(TIMEOUT_MS, TimeUnit.MILLISECONDS));
    134         assertTrue(mWaitSemaphore.tryAcquire(TIMEOUT_MS, TimeUnit.MILLISECONDS));
    135         VehiclePropValue v = mEvents.get(0);
    136         assertEquals(0, v.getInt32Values(
    137                 VehicleAudioRoutingPolicyIndex.VEHICLE_AUDIO_ROUTING_POLICY_INDEX_STREAM));
    138         assertEquals(
    139                 VehicleAudioContextFlag.VEHICLE_AUDIO_CONTEXT_CALL_FLAG |
    140                 VehicleAudioContextFlag.VEHICLE_AUDIO_CONTEXT_MUSIC_FLAG |
    141                 VehicleAudioContextFlag.VEHICLE_AUDIO_CONTEXT_UNKNOWN_FLAG,
    142                 v.getInt32Values(
    143                         VehicleAudioRoutingPolicyIndex.VEHICLE_AUDIO_ROUTING_POLICY_INDEX_CONTEXTS)
    144                         );
    145         v = mEvents.get(1);
    146         assertEquals(1, v.getInt32Values(
    147                 VehicleAudioRoutingPolicyIndex.VEHICLE_AUDIO_ROUTING_POLICY_INDEX_STREAM));
    148         assertEquals(
    149                 VehicleAudioContextFlag.VEHICLE_AUDIO_CONTEXT_ALARM_FLAG |
    150                 VehicleAudioContextFlag.VEHICLE_AUDIO_CONTEXT_NAVIGATION_FLAG |
    151                 VehicleAudioContextFlag.VEHICLE_AUDIO_CONTEXT_NOTIFICATION_FLAG |
    152                 VehicleAudioContextFlag.VEHICLE_AUDIO_CONTEXT_VOICE_COMMAND_FLAG |
    153                 VehicleAudioContextFlag.VEHICLE_AUDIO_CONTEXT_SYSTEM_SOUND_FLAG |
    154                 VehicleAudioContextFlag.VEHICLE_AUDIO_CONTEXT_SAFETY_ALERT_FLAG,
    155                 v.getInt32Values(
    156                         VehicleAudioRoutingPolicyIndex.VEHICLE_AUDIO_ROUTING_POLICY_INDEX_CONTEXTS)
    157                         );
    158     }
    159 
    160     private void handlePropertySetEvent(VehiclePropValue value) {
    161         mEvents.add(value);
    162         mWaitSemaphore.release();
    163     }
    164 }
    165