Home | History | Annotate | Download | only in cts
      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 
     17 package android.media.cts;
     18 
     19 import android.content.Context;
     20 import android.content.pm.PackageManager;
     21 
     22 import android.media.AudioDeviceCallback;
     23 import android.media.AudioDeviceInfo;
     24 import android.media.AudioManager;
     25 
     26 import android.os.Handler;
     27 import android.os.Looper;
     28 
     29 import android.test.AndroidTestCase;
     30 
     31 import android.util.Log;
     32 
     33 /**
     34  * TODO: Insert description here. (generated by pmclean)
     35  */
     36 public class EnumDevicesTest extends AndroidTestCase {
     37     private static final String TAG = "EnumDevicesTest";
     38 
     39     private AudioManager mAudioManager;
     40 
     41     boolean mAddCallbackCalled = false;
     42     boolean mRemoveCallbackCalled = false;
     43 
     44     @Override
     45     protected void setUp() throws Exception {
     46         super.setUp();
     47 
     48         // get the AudioManager
     49         mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
     50         assertNotNull(mAudioManager);
     51     }
     52 
     53     public void test_getDevices() {
     54         AudioDeviceInfo[] deviceList;
     55 
     56         // test an empty flags set
     57         deviceList = mAudioManager.getDevices(0);
     58         assertTrue(deviceList != null);
     59         assertTrue(deviceList.length == 0);
     60 
     61         PackageManager pkgMgr = mContext.getPackageManager();
     62 
     63         boolean isTvDevice = DeviceUtils.isTVDevice(mContext);
     64 
     65         int numOutputDevices = 0;
     66         if (pkgMgr.hasSystemFeature(PackageManager.FEATURE_AUDIO_OUTPUT)) {
     67             // test OUTPUTS
     68             deviceList = mAudioManager.getDevices(AudioManager.GET_DEVICES_OUTPUTS);
     69             assertTrue(deviceList != null);
     70 
     71             numOutputDevices = deviceList.length;
     72             if (numOutputDevices == 0) {
     73                 boolean isHDMIConnected = DeviceUtils.isHDMIConnected(mContext);
     74                 if (isTvDevice && !isHDMIConnected) {
     75                     Log.w(TAG, "getDevices test: failure due to missing reported output " +
     76                                "or the test is run on a TV device with no HDMI connected");
     77                 }
     78                 assertTrue("getDevices test: failure due to missing HDMI connection " +
     79                            "or missing output", false);
     80             }
     81 
     82             // any reported output devices should be "sinks"
     83             for(int index = 0; index < numOutputDevices; index++) {
     84                 assertTrue(deviceList[index].isSink());
     85             }
     86         }
     87 
     88         int numInputDevices = 0;
     89         if (pkgMgr.hasSystemFeature(PackageManager.FEATURE_MICROPHONE)) {
     90             // test INPUTS
     91             deviceList = mAudioManager.getDevices(AudioManager.GET_DEVICES_INPUTS);
     92             assertTrue(deviceList != null);
     93 
     94             numInputDevices = deviceList.length;
     95             assertTrue(numInputDevices != 0);
     96 
     97             // all should be "sources"
     98             for(int index = 0; index < numInputDevices; index++) {
     99                 assertTrue(deviceList[index].isSource());
    100             }
    101         }
    102 
    103         // INPUTS & OUTPUTS
    104         if (mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_AUDIO_OUTPUT) &&
    105                 mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_MICROPHONE)) {
    106             deviceList = mAudioManager.getDevices(AudioManager.GET_DEVICES_ALL);
    107             assertTrue(deviceList != null);
    108             assertTrue(deviceList.length == (numOutputDevices + numInputDevices));
    109         }
    110     }
    111 
    112     public void test_devicesInfoFields() {
    113         AudioDeviceInfo[] deviceList;
    114         deviceList = mAudioManager.getDevices(AudioManager.GET_DEVICES_ALL);
    115         for (int index = 0; index < deviceList.length; index++) {
    116             AudioDeviceInfo deviceInfo = deviceList[index];
    117 
    118             // we don't say anything about the returned value.
    119             int id = deviceInfo.getId();
    120 
    121             // Product Name
    122             CharSequence productName = deviceInfo.getProductName();
    123             assertNotNull(productName);
    124             assertTrue(productName.length() != 0);
    125 
    126             // Address
    127             String address = deviceInfo.getAddress();
    128             assertNotNull(address);
    129             // address may be empty
    130 
    131             // isSource() XOR isSink()
    132             assertTrue(deviceInfo.isSource() != deviceInfo.isSink());
    133 
    134             // Sample Rates
    135             int[] sampleRates = deviceInfo.getSampleRates();
    136             assertNotNull(sampleRates);
    137             // Note: an empty array indicates that the device supports arbitrary sample rates.
    138 
    139             // Channel Masks
    140             int[] channelMasks = deviceInfo.getChannelMasks();
    141             assertNotNull(channelMasks);
    142             // Note: an empty array indicates that the device supports arbitrary channel masks.
    143 
    144             // Channel Index Masks
    145             int[] indexMasks = deviceInfo.getChannelIndexMasks();
    146             assertNotNull(indexMasks);
    147             // Note: an empty array indicates that the device supports arbitrary channel index
    148             // masks.
    149 
    150             // Channel Counts
    151             int[] channelCounts = deviceInfo.getChannelCounts();
    152             assertNotNull(channelCounts);
    153             // Note: an empty array indicates that the device supports arbitrary channel counts.
    154 
    155             // Encodings
    156             int[] encodings = deviceInfo.getEncodings();
    157             assertNotNull(encodings);
    158             // Note: an empty array indicates that the device supports arbitrary encodings.
    159 
    160             int type = deviceInfo.getType();
    161             assertTrue(type != AudioDeviceInfo.TYPE_UNKNOWN);
    162         }
    163     }
    164 
    165     private class EmptyDeviceCallback extends AudioDeviceCallback {
    166         public void onAudioDevicesAdded(AudioDeviceInfo[] addedDevices) {
    167             mAddCallbackCalled = true;
    168         }
    169 
    170         public void onAudioDevicesRemoved(AudioDeviceInfo[] removedDevices) {
    171             mRemoveCallbackCalled = true;
    172         }
    173     }
    174 
    175     /*
    176      * tests if the Looper for the current thread has been prepared,
    177      * If not, it makes one, prepares it and returns it.
    178      * If this returns non-null, the caller is reponsible for calling quit()
    179      * on the returned Looper.
    180      */
    181     private Looper prepareIfNeededLooper() {
    182         // non-null Handler
    183         Looper myLooper = null;
    184         if (Looper.myLooper() == null) {
    185             Looper.prepare();
    186             myLooper = Looper.myLooper();
    187             assertNotNull(myLooper);
    188         }
    189         return myLooper;
    190     }
    191 
    192     public void test_deviceCallback() {
    193         // null callback?
    194         mAudioManager.registerAudioDeviceCallback(null,null);
    195 
    196         AudioDeviceCallback callback =  new EmptyDeviceCallback();
    197         AudioDeviceCallback someOtherCallback =  new EmptyDeviceCallback();
    198         // null Handler
    199         mAudioManager.registerAudioDeviceCallback(callback, null);
    200 
    201         // unregister null callback
    202         mAudioManager.unregisterAudioDeviceCallback(null);
    203         // unregister callback not registered
    204         mAudioManager.unregisterAudioDeviceCallback(someOtherCallback);
    205         // nominal case
    206         mAudioManager.unregisterAudioDeviceCallback(callback);
    207         // remove twice
    208         mAudioManager.unregisterAudioDeviceCallback(callback);
    209 
    210         Looper myLooper = prepareIfNeededLooper();
    211 
    212         mAudioManager.registerAudioDeviceCallback(callback, new Handler());
    213         // unregister null callback
    214         mAudioManager.unregisterAudioDeviceCallback(null);
    215         // unregister callback not registered
    216         mAudioManager.unregisterAudioDeviceCallback(someOtherCallback);
    217         // nominal case
    218         mAudioManager.unregisterAudioDeviceCallback(callback);
    219         // remove twice
    220         mAudioManager.unregisterAudioDeviceCallback(callback);
    221 
    222         if (myLooper != null) {
    223             myLooper.quit();
    224         }
    225     }
    226 
    227     //TODO - Need tests for device connect/disconnect callbacks
    228 }
    229