CarRadioManagerTest.java revision e54ac276796c6535558f8444d882adecd19ce2bd
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.car.Car;
19import android.car.test.VehicleHalEmulator;
20import android.car.hardware.radio.CarRadioEvent;
21import android.car.hardware.radio.CarRadioManager;
22import android.car.hardware.radio.CarRadioManager.CarRadioEventListener;
23import android.car.hardware.radio.CarRadioPreset;
24import android.hardware.radio.RadioManager;
25import android.util.Log;
26
27import com.android.car.vehiclenetwork.VehicleNetworkConsts;
28import com.android.car.vehiclenetwork.VehicleNetworkConsts.VehiclePropAccess;
29import com.android.car.vehiclenetwork.VehicleNetworkConsts.VehiclePropChangeMode;
30import com.android.car.vehiclenetwork.VehicleNetworkConsts.VehicleValueType;
31import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropValue;
32import com.android.car.vehiclenetwork.VehiclePropConfigUtil;
33import com.android.car.vehiclenetwork.VehiclePropValueUtil;
34import com.android.support.car.test.MockedCarTestBase;
35
36import java.util.concurrent.Semaphore;
37import java.util.concurrent.TimeUnit;
38import java.util.HashMap;
39
40public class CarRadioManagerTest extends MockedCarTestBase {
41
42    private static final String TAG = CarRadioManagerTest.class.getSimpleName();
43
44    // Use this semaphore to block until the callback is heard of.
45    private Semaphore mAvailable;
46
47    private static final int NUM_PRESETS = 2;
48    private final HashMap<Integer, CarRadioPreset> mRadioPresets =
49        new HashMap<Integer, CarRadioPreset>();
50
51    private CarRadioManager mCarRadioManager;
52
53    private class RadioPresetPropertyHandler
54            implements VehicleHalEmulator.VehicleHalPropertyHandler {
55        public RadioPresetPropertyHandler() { }
56
57        @Override
58        public synchronized void onPropertySet(VehiclePropValue value) {
59            assertEquals(value.getProp(), VehicleNetworkConsts.VEHICLE_PROPERTY_RADIO_PRESET);
60            assertEquals(value.getValueType(), VehicleValueType.VEHICLE_VALUE_TYPE_INT32_VEC4);
61
62            Integer[] valueList = new Integer[4];
63            value.getInt32ValuesList().toArray(valueList);
64            assertFalse(
65                "Index out of range: " + valueList[0] + " (0, " + NUM_PRESETS + ")",
66                valueList[0] < 1);
67            assertFalse(
68                "Index out of range: " + valueList[0] + " (0, " + NUM_PRESETS + ")",
69                valueList[0] > NUM_PRESETS);
70
71            CarRadioPreset preset =
72                new CarRadioPreset(valueList[0], valueList[1], valueList[2], valueList[3]);
73            mRadioPresets.put(valueList[0], preset);
74
75            // The test case must be waiting for the semaphore, if not we should throw exception.
76            if (mAvailable.availablePermits() != 0) {
77                Log.d(TAG, "Lock was free, should have been locked.");
78            }
79            mAvailable.release();
80        }
81
82        @Override
83        public synchronized VehiclePropValue onPropertyGet(VehiclePropValue value) {
84            assertEquals(value.getProp(), VehicleNetworkConsts.VEHICLE_PROPERTY_RADIO_PRESET);
85            assertEquals(value.getValueType(), VehicleValueType.VEHICLE_VALUE_TYPE_INT32_VEC4);
86
87            Integer[] valueList = new Integer[4];
88            value.getInt32ValuesList().toArray(valueList);
89
90            // Get the actual preset.
91            if (valueList[0] < 1 || valueList[0] > NUM_PRESETS) return null;
92            CarRadioPreset preset = mRadioPresets.get(valueList[0]);
93            VehiclePropValue v =
94                VehiclePropValueUtil.createIntVectorValue(
95                    VehicleNetworkConsts.VEHICLE_PROPERTY_RADIO_PRESET,
96                    new int[] {
97                        preset.getPresetNumber(),
98                        preset.getBand(),
99                        preset.getChannel(),
100                        preset.getSubChannel()}, 0);
101            return v;
102        }
103
104        @Override
105        public synchronized void onPropertySubscribe(int property, float sampleRate, int zones) {
106            Log.d(TAG, "onPropertySubscribe property: " + property + " rate: " + sampleRate);
107            if (mAvailable.availablePermits() != 0) {
108                Log.d(TAG, "Lock was free, should have been locked.");
109                return;
110            }
111            mAvailable.release();
112        }
113
114        @Override
115        public synchronized void onPropertyUnsubscribe(int property) {
116        }
117    }
118
119    private class EventListener implements CarRadioEventListener {
120        public EventListener() { }
121
122        @Override
123        public void onEvent(CarRadioEvent event) {
124            // Print the event and release the lock.
125            Log.d(TAG, event.toString());
126            if (mAvailable.availablePermits() != 0) {
127                Log.e(TAG, "Lock should be taken.");
128                // Let the timeout fail the test here.
129                return;
130            }
131            mAvailable.release();
132        }
133    }
134
135    @Override
136    protected void setUp() throws Exception {
137        super.setUp();
138        mAvailable = new Semaphore(0);
139        getVehicleHalEmulator().addProperty(
140                VehiclePropConfigUtil.createProperty(
141                        VehicleNetworkConsts.VEHICLE_PROPERTY_RADIO_PRESET,
142                        VehiclePropAccess.VEHICLE_PROP_ACCESS_READ_WRITE,
143                        VehiclePropChangeMode.VEHICLE_PROP_CHANGE_MODE_ON_CHANGE,
144                        VehicleValueType.VEHICLE_VALUE_TYPE_INT32_VEC4,
145                        NUM_PRESETS),
146                new RadioPresetPropertyHandler());
147        getVehicleHalEmulator().start();
148        mCarRadioManager =
149                (CarRadioManager) getCar().getCarManager(Car.RADIO_SERVICE);
150    }
151
152    public void testPresetCount() throws Exception {
153        int presetCount = mCarRadioManager.getPresetCount();
154        assertEquals("Preset count not same.", NUM_PRESETS, presetCount);
155    }
156
157    public void testSetAndGetPreset() throws Exception {
158        // Create a preset.
159        CarRadioPreset preset = new CarRadioPreset(1, RadioManager.BAND_FM, 1234, -1);
160        assertEquals("Lock should be freed by now.", 0, mAvailable.availablePermits());
161        // mAvailable.acquire(1);
162        mCarRadioManager.setPreset(preset);
163
164        // Wait for acquire to be available again, fail if timeout.
165        boolean success = mAvailable.tryAcquire(5L, TimeUnit.SECONDS);
166        assertEquals("Could not finish setting, timeout!", true, success);
167
168        // Test that get preset gives you the same element.
169        assertEquals(preset, mCarRadioManager.getPreset(1));
170    }
171
172    public void testSubscribe() throws Exception {
173        EventListener l = new EventListener();
174        assertEquals("Lock should be freed by now.", 0, mAvailable.availablePermits());
175        mCarRadioManager.registerListener(l);
176
177        // Wait for acquire to be available again, fail if timeout.
178        boolean success = mAvailable.tryAcquire(5L, TimeUnit.SECONDS);
179        assertEquals("registerListener timeout", true, success);
180
181        // Inject an event and wait for its callback in onPropertySet.
182        CarRadioPreset preset = new CarRadioPreset(2, RadioManager.BAND_AM, 4321, -1);
183        VehiclePropValue v = VehiclePropValueUtil.createIntVectorValue(
184                    VehicleNetworkConsts.VEHICLE_PROPERTY_RADIO_PRESET,
185                    new int[] {
186                        preset.getPresetNumber(),
187                        preset.getBand(),
188                        preset.getChannel(),
189                        preset.getSubChannel()}, 0);
190        getVehicleHalEmulator().injectEvent(v);
191
192        success = mAvailable.tryAcquire(5L, TimeUnit.SECONDS);
193        assertEquals("injectEvent, onEvent timeout!", true, success);
194    }
195}
196