Fix CFM autotest flakes and cleanup tests.

Session stress and peripheral detect tests have been flaky and failing
randomly for some runs. This CL is an attempt to fix the flakiness and
cleanup some tests.

BUG=None
TEST=Locally

Change-Id: Ia58e8a53045130113544b9ee402add901e3ab377
Reviewed-on: https://chromium-review.googlesource.com/352814
Commit-Ready: Harpreet Grewal <harpreet@chromium.org>
Tested-by: Harpreet Grewal <harpreet@chromium.org>
Reviewed-by: Kalin Stoyanov <kalin@chromium.org>
diff --git a/client/common_lib/cros/cfm_util.py b/client/common_lib/cros/cfm_util.py
index fae2d40..a3cb947 100644
--- a/client/common_lib/cros/cfm_util.py
+++ b/client/common_lib/cros/cfm_util.py
@@ -294,7 +294,7 @@
     @param mic: String with mic name.
     """
     webview_context.ExecuteJavaScript(
-            "window.hrSetAudioInPrefForTest(" + mic + ")")
+            "window.hrSetAudioInPrefForTest('" + mic + "')")
     logging.info('Setting preferred mic to %s.', mic)
 
 
@@ -324,7 +324,7 @@
     @param mic: String with speaker name.
     """
     webview_context.ExecuteJavaScript(
-            "window.hrSetAudioOutPrefForTest(" + speaker + ")")
+            "window.hrSetAudioOutPrefForTest('" + speaker + "')")
     logging.info('Set preferred speaker to %s.', speaker)
 
 
@@ -335,7 +335,7 @@
     @param volume_level: String value ranging from 0-100 to set volume to.
     """
     webview_context.ExecuteJavaScript(
-            "window.hrSetAudioOutVolumeLevelForTest(" + volume_level + ")")
+            "window.hrSetAudioOutVolumeLevelForTest('" + volume_level + "')")
     logging.info('Set speaker volume to %s', volume_level)
 
 
@@ -383,7 +383,7 @@
     @param mic: String with camera name.
     """
     webview_context.ExecuteJavaScript(
-            "window.hrSetVideoCapturePrefForTest(" + camera + ")")
+            "window.hrSetVideoCapturePrefForTest('" + camera + "')")
     logging.info('Set preferred camera to %s.', camera)
 
 
diff --git a/client/site_tests/enterprise_CFM_SessionStress/enterprise_CFM_SessionStress.py b/client/site_tests/enterprise_CFM_SessionStress/enterprise_CFM_SessionStress.py
index 19d3fb9..ed8f521 100644
--- a/client/site_tests/enterprise_CFM_SessionStress/enterprise_CFM_SessionStress.py
+++ b/client/site_tests/enterprise_CFM_SessionStress/enterprise_CFM_SessionStress.py
@@ -8,7 +8,7 @@
 from autotest_lib.client.common_lib import error
 from autotest_lib.client.common_lib.cros import chrome, cfm_util
 
-LONG_TIMEOUT = 10
+LONG_TIMEOUT = 7
 SHORT_TIMEOUT = 2
 EXT_ID = 'ikfcpmgefdpheiiomgmhlmmkihchmdlj'
 
@@ -30,24 +30,10 @@
         hangout_name = 'auto-hangout-' + current_time
         logging.info('Session name: %s', hangout_name)
 
-        if not cfm_util.is_ready_to_start_hangout_session(webview_context):
-            raise error.TestFail('CFM should be ready to start new session.')
-
         cfm_util.start_new_hangout_session(webview_context, hangout_name)
-
-        if not cfm_util.is_in_hangout_session(webview_context):
-            raise error.TestFail('CFM was not able to start hangout session.')
-
-        if cfm_util.is_ready_to_start_hangout_session(webview_context):
-            raise error.TestFail('Is already in hangout session and should not '
-                                 'be able to start another session.')
-
         time.sleep(LONG_TIMEOUT)
         cfm_util.end_hangout_session(webview_context)
 
-        if cfm_util.is_in_hangout_session(webview_context):
-            raise error.TestFail('CFM should not be in hangout session.')
-
 
     def run_once(self, repeat):
         """Runs the test."""
diff --git a/client/site_tests/enterprise_CFM_USBPeripheralDetect/enterprise_CFM_USBPeripheralDetect.py b/client/site_tests/enterprise_CFM_USBPeripheralDetect/enterprise_CFM_USBPeripheralDetect.py
index 0d56070..ef9f77f 100644
--- a/client/site_tests/enterprise_CFM_USBPeripheralDetect/enterprise_CFM_USBPeripheralDetect.py
+++ b/client/site_tests/enterprise_CFM_USBPeripheralDetect/enterprise_CFM_USBPeripheralDetect.py
@@ -19,14 +19,11 @@
     version = 1
 
 
-    def peripheral_detection(self, webview_context):
-        """Get attached peripheral information.
+    def set_preferred_peripherals(self, webview_context, cros_peripherals):
+        """Set perferred peripherals.
 
         @param webview_context: Context for hangouts window.
         """
-        cfm_peripheral_dict = {'Microphone': None, 'Speaker': None,
-                               'Camera': None}
-
         cfm_util.wait_for_telemetry_commands(webview_context)
         cfm_util.wait_for_oobe_start_page(webview_context)
 
@@ -36,6 +33,29 @@
         cfm_util.skip_oobe_screen(webview_context)
         time.sleep(SHORT_TIMEOUT)
 
+        avail_mics = cfm_util.get_mic_devices(webview_context)
+        avail_speakers = cfm_util.get_speaker_devices(webview_context)
+        avail_cameras = cfm_util.get_camera_devices(webview_context)
+
+        if cros_peripherals.get('Microphone') in avail_mics:
+            cfm_util.set_preferred_mic(
+                    webview_context, cros_peripherals.get('Microphone'))
+        if cros_peripherals.get('Speaker') in avail_speakers:
+            cfm_util.set_preferred_speaker(
+                    webview_context, cros_peripherals.get('Speaker'))
+        if cros_peripherals.get('Camera') in avail_cameras:
+            cfm_util.set_preferred_camera(
+                    webview_context, cros_peripherals.get('Camera'))
+
+
+    def peripheral_detection(self, webview_context):
+        """Get attached peripheral information.
+
+        @param webview_context: Context for hangouts window.
+        """
+        cfm_peripheral_dict = {'Microphone': None, 'Speaker': None,
+                               'Camera': None}
+
         cfm_peripheral_dict['Microphone'] = cfm_util.get_preferred_mic(
                 webview_context)
         cfm_peripheral_dict['Speaker'] = cfm_util.get_preferred_speaker(
@@ -59,6 +79,8 @@
                            auto_login=False) as cr:
             cfm_webview_context = cfm_util.get_cfm_webview_context(
                     cr.browser, EXT_ID)
+            self.set_preferred_peripherals(cfm_webview_context,
+                                           cros_peripheral_dict)
             cfm_peripheral_dict = self.peripheral_detection(cfm_webview_context)
             logging.debug('Peripherals detected by hotrod: %s',
                           cfm_peripheral_dict)
diff --git a/client/site_tests/enterprise_CFM_VolumeChangeClient/enterprise_CFM_VolumeChangeClient.py b/client/site_tests/enterprise_CFM_VolumeChangeClient/enterprise_CFM_VolumeChangeClient.py
index 99f468b..2f3b578 100644
--- a/client/site_tests/enterprise_CFM_VolumeChangeClient/enterprise_CFM_VolumeChangeClient.py
+++ b/client/site_tests/enterprise_CFM_VolumeChangeClient/enterprise_CFM_VolumeChangeClient.py
@@ -11,8 +11,6 @@
 LONG_TIMEOUT = 10
 SHORT_TIMEOUT = 2
 EXT_ID = 'ikfcpmgefdpheiiomgmhlmmkihchmdlj'
-CMD = "cras_test_client --dump_server_info | awk '/Output Nodes:/," \
-      "/Input Devices:/' | grep -E 'USB' | awk -v N=3 '{print $N}'"
 
 
 class enterprise_CFM_VolumeChangeClient(test.test):
@@ -61,7 +59,7 @@
             raise error.TestFail('CFM should not be in hangout session.')
 
 
-    def run_once(self, repeat):
+    def run_once(self, repeat, cmd):
         """Runs the test."""
         with chrome.Chrome(clear_enterprise_policy=False,
                            dont_override_profile=True,
@@ -90,7 +88,7 @@
                 cfm_util.set_speaker_volume(cfm_webview_context, cfm_volume)
                 time.sleep(SHORT_TIMEOUT)
 
-                cras_volume = utils.system_output(CMD)
+                cras_volume = utils.system_output(cmd)
                 if cras_volume != cfm_volume:
                     raise error.TestFail('Cras volume (%s) does not match '
                                          'volume set by CFM (%s).' %
diff --git a/server/site_tests/enterprise_CFM_USBPeripheralHotplugDetect/control.c920 b/server/site_tests/enterprise_CFM_USBPeripheralHotplugDetect/control.c920
index 8442cc4..fba2260 100644
--- a/server/site_tests/enterprise_CFM_USBPeripheralHotplugDetect/control.c920
+++ b/server/site_tests/enterprise_CFM_USBPeripheralHotplugDetect/control.c920
@@ -30,9 +30,8 @@
     host = hosts.create_host(machine, servo_args=servo_args)
     peripheral_whitelist_dict = {'0b0e:0412': 'Jabra SPEAK 410',
                                  '046d:082d': 'HD Pro Webcam C920 (046d:082d)'}
-    job.run_test('enterprise_CFM_USBPeripheralHotplugDetect',
-                 host=host,
-                 peripheral_whitelist_dict=peripheral_whitelist_dict)
+    job.run_test('enterprise_CFM_USBPeripheralHotplugDetect', host=host,
+            peripheral_whitelist_dict=peripheral_whitelist_dict, tag="c920")
 
 
 parallel_simple(run_test, machines)
diff --git a/server/site_tests/enterprise_CFM_USBPeripheralHotplugDetect/control.c930 b/server/site_tests/enterprise_CFM_USBPeripheralHotplugDetect/control.c930
index 935e3d4..c1b08b3 100644
--- a/server/site_tests/enterprise_CFM_USBPeripheralHotplugDetect/control.c930
+++ b/server/site_tests/enterprise_CFM_USBPeripheralHotplugDetect/control.c930
@@ -30,9 +30,8 @@
     host = hosts.create_host(machine, servo_args=servo_args)
     peripheral_whitelist_dict = {'0b0e:0412': 'Jabra SPEAK 410',
             '046d:0843': 'Logitech Webcam C930e (046d:0843)'}
-    job.run_test('enterprise_CFM_USBPeripheralHotplugDetect',
-                 host=host,
-                 peripheral_whitelist_dict=peripheral_whitelist_dict)
+    job.run_test('enterprise_CFM_USBPeripheralHotplugDetect', host=host,
+            peripheral_whitelist_dict=peripheral_whitelist_dict, tag="c930")
 
 
 parallel_simple(run_test, machines)
diff --git a/server/site_tests/enterprise_CFM_VolumeChange/control b/server/site_tests/enterprise_CFM_VolumeChange/control.internal_speaker
similarity index 73%
copy from server/site_tests/enterprise_CFM_VolumeChange/control
copy to server/site_tests/enterprise_CFM_VolumeChange/control.internal_speaker
index 37ca625..8f384b2 100644
--- a/server/site_tests/enterprise_CFM_VolumeChange/control
+++ b/server/site_tests/enterprise_CFM_VolumeChange/control.internal_speaker
@@ -5,12 +5,13 @@
 from autotest_lib.server import utils
 
 AUTHOR = "harpreet@chromium.org"
-NAME = "enterprise_CFM_VolumeChange"
+NAME = "enterprise_CFM_VolumeChange.internal_speaker"
 TIME = "SHORT"
 TEST_CATEGORY = "Functional"
 TEST_CLASS = "enterprise"
 TEST_TYPE = "server"
 ATTRIBUTES = "suite:hotrod"
+DEPENDENCIES = "internal_speaker"
 
 DOC = """
 This test clears the TPM and enables the appropriate usb port on the servo
@@ -26,10 +27,11 @@
 def run_test(machine):
     host = hosts.create_host(machine, servo_args=servo_args)
     repeat = int(args_dict.get('repeat', 10))
+    cmd = "cras_test_client --dump_server_info | awk '/Output Nodes:/," \
+          "/Input Devices:/' | grep -E 'INTERNAL_*' | awk -v N=2 '{print $N}'"
 
-    job.run_test('enterprise_CFM_VolumeChange',
-                 host=host,
-                 repeat=repeat)
+    job.run_test('enterprise_CFM_VolumeChange', host=host, repeat=repeat,
+            cmd=cmd, tag="internal_speaker")
 
 
 parallel_simple(run_test, machines)
diff --git a/server/site_tests/enterprise_CFM_VolumeChange/control b/server/site_tests/enterprise_CFM_VolumeChange/control.usb_speaker
similarity index 74%
rename from server/site_tests/enterprise_CFM_VolumeChange/control
rename to server/site_tests/enterprise_CFM_VolumeChange/control.usb_speaker
index 37ca625..94172ca 100644
--- a/server/site_tests/enterprise_CFM_VolumeChange/control
+++ b/server/site_tests/enterprise_CFM_VolumeChange/control.usb_speaker
@@ -5,12 +5,13 @@
 from autotest_lib.server import utils
 
 AUTHOR = "harpreet@chromium.org"
-NAME = "enterprise_CFM_VolumeChange"
+NAME = "enterprise_CFM_VolumeChange.usb_speaker"
 TIME = "SHORT"
 TEST_CATEGORY = "Functional"
 TEST_CLASS = "enterprise"
 TEST_TYPE = "server"
 ATTRIBUTES = "suite:hotrod"
+DEPENDENCIES = "usb_speaker"
 
 DOC = """
 This test clears the TPM and enables the appropriate usb port on the servo
@@ -26,10 +27,11 @@
 def run_test(machine):
     host = hosts.create_host(machine, servo_args=servo_args)
     repeat = int(args_dict.get('repeat', 10))
+    cmd = "cras_test_client --dump_server_info | awk '/Output Nodes:/," \
+          "/Input Devices:/' | grep -E 'USB' | awk -v N=3 '{print $N}'"
 
-    job.run_test('enterprise_CFM_VolumeChange',
-                 host=host,
-                 repeat=repeat)
+    job.run_test('enterprise_CFM_VolumeChange', host=host, repeat=repeat,
+            cmd=cmd, tag="usb_speaker")
 
 
 parallel_simple(run_test, machines)
diff --git a/server/site_tests/enterprise_CFM_VolumeChange/enterprise_CFM_VolumeChange.py b/server/site_tests/enterprise_CFM_VolumeChange/enterprise_CFM_VolumeChange.py
index 1d8eca7..6a0f9d7 100644
--- a/server/site_tests/enterprise_CFM_VolumeChange/enterprise_CFM_VolumeChange.py
+++ b/server/site_tests/enterprise_CFM_VolumeChange/enterprise_CFM_VolumeChange.py
@@ -11,7 +11,7 @@
     """
     version = 1
 
-    def run_once(self, host, repeat):
+    def run_once(self, host, repeat, cmd):
         self.client = host
 
         tpm_utils.ClearTPMOwnerRequest(self.client)
@@ -26,6 +26,7 @@
         autotest.Autotest(self.client).run_test(
                 'enterprise_CFM_VolumeChangeClient',
                 repeat=repeat,
+                cmd=cmd,
                 check_client_result=True)
 
         tpm_utils.ClearTPMOwnerRequest(self.client)