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