15c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung/*
25c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung * Copyright (C) 2015 The Android Open Source Project
35c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung *
45c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung * Licensed under the Apache License, Version 2.0 (the "License");
55c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung * you may not use this file except in compliance with the License.
65c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung * You may obtain a copy of the License at
75c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung *
85c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung *      http://www.apache.org/licenses/LICENSE-2.0
95c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung *
105c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung * Unless required by applicable law or agreed to in writing, software
115c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung * distributed under the License is distributed on an "AS IS" BASIS,
125c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung * See the License for the specific language governing permissions and
145c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung * limitations under the License.
155c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung */
16e54ac276796c6535558f8444d882adecd19ce2bdKeun-young Parkpackage com.android.car.test;
175c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung
18cfe93105f637c2822da113308f113ed418d0b319Pavel Maltsevimport android.hardware.automotive.vehicle.V2_0.VehicleAudioContextFlag;
19cfe93105f637c2822da113308f113ed418d0b319Pavel Maltsevimport android.hardware.automotive.vehicle.V2_0.VehicleAudioRoutingPolicyIndex;
20cfe93105f637c2822da113308f113ed418d0b319Pavel Maltsevimport android.hardware.automotive.vehicle.V2_0.VehiclePropValue;
21cfe93105f637c2822da113308f113ed418d0b319Pavel Maltsevimport android.hardware.automotive.vehicle.V2_0.VehicleProperty;
22cfe93105f637c2822da113308f113ed418d0b319Pavel Maltsevimport android.hardware.automotive.vehicle.V2_0.VehiclePropertyAccess;
232906c4461afa9ac0c5865768a3238281e5ecf26dPavel Maltsevimport android.test.suitebuilder.annotation.SmallTest;
2475623ce0736848d0b27f08c65d843a031b325027Pavel Maltsev
250d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsevimport com.google.android.collect.Lists;
265c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung
270d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsevimport com.android.car.vehiclehal.VehiclePropValueBuilder;
280d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsevimport com.android.car.vehiclehal.test.MockedVehicleHal.FailingPropertyHandler;
290d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsevimport com.android.car.vehiclehal.test.MockedVehicleHal.VehicleHalPropertyHandler;
300d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev
310d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsevimport java.util.ArrayList;
325c7cb266f8900450c2ab41802b079b02f14d707fkeunyoungimport java.util.LinkedList;
335c7cb266f8900450c2ab41802b079b02f14d707fkeunyoungimport java.util.concurrent.Semaphore;
345c7cb266f8900450c2ab41802b079b02f14d707fkeunyoungimport java.util.concurrent.TimeUnit;
355c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung
362906c4461afa9ac0c5865768a3238281e5ecf26dPavel Maltsev@SmallTest
375c7cb266f8900450c2ab41802b079b02f14d707fkeunyoungpublic class AudioRoutingPolicyTest extends MockedCarTestBase {
380d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev    private static final String TAG = AudioRoutingPolicyTest.class.getSimpleName();
395c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung
405c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung    private static final long TIMEOUT_MS = 3000;
415c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung
42a74b9caa2fb6435f1c01c5e8766b89235c4e3d5akeunyoung    private final VehicleHalPropertyHandler mAudioRoutingPolicyHandler =
430d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev            new FailingPropertyHandler() {
445c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung
455c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung        @Override
465c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung        public void onPropertySet(VehiclePropValue value) {
475c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung            handlePropertySetEvent(value);
485c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung        }
495c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung    };
505c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung
515c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung    private final Semaphore mWaitSemaphore = new Semaphore(0);
520d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev    private final LinkedList<VehiclePropValue> mEvents = new LinkedList<>();
535c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung
545c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung    @Override
550d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev    protected synchronized void setUp() throws Exception {
565c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung        super.setUp();
570d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev    }
585c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung
590d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev    @Override
600d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev    protected synchronized void configureMockedHal() {
610d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev        addProperty(VehicleProperty.AUDIO_ROUTING_POLICY, mAudioRoutingPolicyHandler)
6217470d61d554eda65eac04bdf9635bdc95d29e5ePavel Maltsev                .setAccess(VehiclePropertyAccess.WRITE);
635c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung    }
645c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung
650d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev    public void testNoHwVariant() throws Exception {
665c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung        checkPolicy0();
675c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung    }
685c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung
695c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung    public void testHwVariant0() throws Exception {
700d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev        addStaticProperty(VehicleProperty.AUDIO_HW_VARIANT,
710d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev                VehiclePropValueBuilder.newBuilder(VehicleProperty.AUDIO_HW_VARIANT)
720d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev                        .addIntValue(0)
730d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev                        .build())
740d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev                .setConfigArray(Lists.newArrayList(0));
750d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev
760d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev        mEvents.clear();
770d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev        reinitializeMockedHal();
780d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev
795c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung        checkPolicy0();
805c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung    }
815c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung
8232b6382264510577126f9723337a7f48332b2ae3Keun-young Park    public void testHwVariantForTest() throws Exception {
830d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev        addStaticProperty(VehicleProperty.AUDIO_HW_VARIANT,
840d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev                VehiclePropValueBuilder.newBuilder(VehicleProperty.AUDIO_HW_VARIANT)
850d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev                        .addIntValue(-1)
860d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev                        .build())
870d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev                .setConfigArray(Lists.newArrayList(0));
880d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev
890d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev        mEvents.clear();
900d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev        reinitializeMockedHal();
910d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev
9232b6382264510577126f9723337a7f48332b2ae3Keun-young Park        checkPolicyForTest();
935c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung    }
945c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung
955c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung    private void checkPolicy0() throws Exception {
965c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung        assertTrue(mWaitSemaphore.tryAcquire(TIMEOUT_MS, TimeUnit.MILLISECONDS));
970d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev        ArrayList<Integer> v = mEvents.get(0).value.int32Values;
980d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev        assertEquals(0, v.get(VehicleAudioRoutingPolicyIndex.STREAM).intValue());
990d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev        int contexts = v.get(VehicleAudioRoutingPolicyIndex.CONTEXTS);
10032b6382264510577126f9723337a7f48332b2ae3Keun-young Park        // check if all contexts are allowed ones.
10132b6382264510577126f9723337a7f48332b2ae3Keun-young Park        assertTrue((contexts & ~(
102962ba77e2b928466dd7a4652bc468127adb871a0Scott Randolph                VehicleAudioContextFlag.RINGTONE_FLAG |
1030d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev                VehicleAudioContextFlag.ALARM_FLAG |
1040d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev                VehicleAudioContextFlag.CALL_FLAG |
1050d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev                VehicleAudioContextFlag.MUSIC_FLAG |
1060d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev                VehicleAudioContextFlag.RADIO_FLAG |
1070d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev                VehicleAudioContextFlag.NAVIGATION_FLAG |
1080d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev                VehicleAudioContextFlag.NOTIFICATION_FLAG |
1090d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev                VehicleAudioContextFlag.UNKNOWN_FLAG |
1100d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev                VehicleAudioContextFlag.VOICE_COMMAND_FLAG |
1110d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev                VehicleAudioContextFlag.SYSTEM_SOUND_FLAG |
1120d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev                VehicleAudioContextFlag.SAFETY_ALERT_FLAG)) == 0);
1135c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung    }
1145c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung
11532b6382264510577126f9723337a7f48332b2ae3Keun-young Park    private void checkPolicyForTest() throws Exception {
116a74b9caa2fb6435f1c01c5e8766b89235c4e3d5akeunyoung        // write happens twice.
1175c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung        assertTrue(mWaitSemaphore.tryAcquire(TIMEOUT_MS, TimeUnit.MILLISECONDS));
1185c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung        assertTrue(mWaitSemaphore.tryAcquire(TIMEOUT_MS, TimeUnit.MILLISECONDS));
1190d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev        ArrayList<Integer> v = mEvents.get(0).value.int32Values;
1200d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev        assertEquals(0, v.get(VehicleAudioRoutingPolicyIndex.STREAM).intValue());
1215c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung        assertEquals(
1220d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev                VehicleAudioContextFlag.CALL_FLAG |
1230d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev                VehicleAudioContextFlag.MUSIC_FLAG |
1240d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev                VehicleAudioContextFlag.RADIO_FLAG |
1250d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev                VehicleAudioContextFlag.UNKNOWN_FLAG,
1260d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev                v.get(VehicleAudioRoutingPolicyIndex.CONTEXTS).intValue());
1270d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev        v = mEvents.get(1).value.int32Values;
1280d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev        assertEquals(1, v.get(VehicleAudioRoutingPolicyIndex.STREAM).intValue());
1295c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung        assertEquals(
1300d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev                VehicleAudioContextFlag.ALARM_FLAG |
1310d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev                VehicleAudioContextFlag.NAVIGATION_FLAG |
1320d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev                VehicleAudioContextFlag.NOTIFICATION_FLAG |
1330d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev                VehicleAudioContextFlag.VOICE_COMMAND_FLAG |
1340d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev                VehicleAudioContextFlag.SYSTEM_SOUND_FLAG |
1350d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev                VehicleAudioContextFlag.SAFETY_ALERT_FLAG,
1360d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev                v.get(VehicleAudioRoutingPolicyIndex.CONTEXTS).intValue());
1375c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung    }
1385c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung
1395c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung    private void handlePropertySetEvent(VehiclePropValue value) {
1405c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung        mEvents.add(value);
1415c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung        mWaitSemaphore.release();
1425c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung    }
1435c7cb266f8900450c2ab41802b079b02f14d707fkeunyoung}
144