"Fixing" EnumDevicesTest.test_getDevices() to allow for TV-style devices.

Some devices will report support for FEATURE_AUDIO_OUTPUT
but not have an ACTUAL device attached when the test is run.

The CTS protocol REQUIRES TV devices to have an HDMI device connect.
So, still fail the test but warn in the log about TV devices w/o HDMI.

Added utility to determine HDMI connection state.

Bug: 31524241
Change-Id: If2a9a7a6a4c374fd9a1161f796d119dbc72d8edf
diff --git a/tests/tests/media/src/android/media/cts/DeviceUtils.java b/tests/tests/media/src/android/media/cts/DeviceUtils.java
index 41afce1..c2b1c32 100644
--- a/tests/tests/media/src/android/media/cts/DeviceUtils.java
+++ b/tests/tests/media/src/android/media/cts/DeviceUtils.java
@@ -16,14 +16,21 @@
 
 package android.media.cts;
 
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.content.pm.PackageManager;
+
 import android.media.AudioDeviceInfo;
 import android.media.AudioManager;
 
+import android.util.Log;
+
 /* package */ class DeviceUtils {
     private static final String TAG = "DeviceUtils";
 
     /* package */ static boolean hasOutputDevice(AudioManager audioMgr) {
-
         AudioDeviceInfo[] devices = audioMgr.getDevices(AudioManager.GET_DEVICES_OUTPUTS);
         return devices.length != 0;
     }
@@ -32,4 +39,27 @@
         AudioDeviceInfo[] devices = audioMgr.getDevices(AudioManager.GET_DEVICES_INPUTS);
         return devices.length != 0;
     }
+
+    /* package */ static boolean isTVDevice(Context context) {
+        return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_LEANBACK);
+    }
+
+    /*
+     * HDMI
+     */
+    /* package */ static boolean isHDMIConnected(Context context) {
+        // configure the IntentFilter
+        IntentFilter intentFilter = new IntentFilter();
+        intentFilter.addAction(AudioManager.ACTION_HDMI_AUDIO_PLUG);
+        Intent intent = context.registerReceiver(null, intentFilter);
+
+        String action = intent.getAction();
+        boolean isHDMIConnected = false;
+        if (action.equals(AudioManager.ACTION_HDMI_AUDIO_PLUG)) {
+            isHDMIConnected =
+                    intent.getIntExtra(AudioManager.EXTRA_AUDIO_PLUG_STATE, 0) != 0;
+        }
+
+        return isHDMIConnected;
+    }
 }
diff --git a/tests/tests/media/src/android/media/cts/EnumDevicesTest.java b/tests/tests/media/src/android/media/cts/EnumDevicesTest.java
index 94af087..e3ed453 100644
--- a/tests/tests/media/src/android/media/cts/EnumDevicesTest.java
+++ b/tests/tests/media/src/android/media/cts/EnumDevicesTest.java
@@ -28,10 +28,14 @@
 
 import android.test.AndroidTestCase;
 
+import android.util.Log;
+
 /**
  * TODO: Insert description here. (generated by pmclean)
  */
 public class EnumDevicesTest extends AndroidTestCase {
+    private static final String TAG = "EnumDevicesTest";
+
     private AudioManager mAudioManager;
 
     boolean mAddCallbackCalled = false;
@@ -54,27 +58,41 @@
         assertTrue(deviceList != null);
         assertTrue(deviceList.length == 0);
 
+        PackageManager pkgMgr = mContext.getPackageManager();
+
+        boolean isTvDevice = DeviceUtils.isTVDevice(mContext);
+
         int numOutputDevices = 0;
-        if (mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_AUDIO_OUTPUT)) {
+        if (pkgMgr.hasSystemFeature(PackageManager.FEATURE_AUDIO_OUTPUT)) {
             // test OUTPUTS
             deviceList = mAudioManager.getDevices(AudioManager.GET_DEVICES_OUTPUTS);
             assertTrue(deviceList != null);
-            numOutputDevices = deviceList.length;
-            assertTrue(numOutputDevices != 0);
 
-            // all should be "sinks"
+            numOutputDevices = deviceList.length;
+            if (numOutputDevices == 0) {
+                boolean isHDMIConnected = DeviceUtils.isHDMIConnected(mContext);
+                if (isTvDevice && !isHDMIConnected) {
+                    Log.w(TAG, "getDevices test: failure due to missing reported output " +
+                               "or the test is run on a TV device with no HDMI connected");
+                }
+                assertTrue("getDevices test: failure due to missing HDMI connection " +
+                           "or missing output", false);
+            }
+
+            // any reported output devices should be "sinks"
             for(int index = 0; index < numOutputDevices; index++) {
                 assertTrue(deviceList[index].isSink());
             }
         }
 
         int numInputDevices = 0;
-        if (mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_MICROPHONE)) {
+        if (pkgMgr.hasSystemFeature(PackageManager.FEATURE_MICROPHONE)) {
             // test INPUTS
             deviceList = mAudioManager.getDevices(AudioManager.GET_DEVICES_INPUTS);
             assertTrue(deviceList != null);
+
             numInputDevices = deviceList.length;
-            assertTrue(numInputDevices != 0);
+            assertTrue(numOutputDevices != 0);
 
             // all should be "sources"
             for(int index = 0; index < numInputDevices; index++) {
@@ -86,6 +104,7 @@
         if (mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_AUDIO_OUTPUT) &&
                 mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_MICROPHONE)) {
             deviceList = mAudioManager.getDevices(AudioManager.GET_DEVICES_ALL);
+            assertTrue(deviceList != null);
             assertTrue(deviceList.length == (numOutputDevices + numInputDevices));
         }
     }