autotest: Separate audio_InternalCardNodes into plug/unplug

Separate audio_InternalCardNodes to the plug and unplug scenarios. This
change can help us to still get test result for either plug or unplug
scenario even if the jack plugger is broken.
This CL also address an issue that we didn't recover the plug status
after the audio_InternalCardNodes run.

BUG=b:145975634
TEST=Run audio_InternalCardNodes.plug and audio_InternalCardNodes.unplug

Change-Id: If61fbaab7fee0fd62b75f79580c44f61179912b3
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/autotest/+/2006848
Tested-by: En-Shuo Hsu <enshuo@chromium.org>
Reviewed-by: Yu-Hsuan Hsu <yuhsuan@chromium.org>
Commit-Queue: En-Shuo Hsu <enshuo@chromium.org>
Auto-Submit: En-Shuo Hsu <enshuo@chromium.org>
diff --git a/server/site_tests/audio_InternalCardNodes/audio_InternalCardNodes.py b/server/site_tests/audio_InternalCardNodes/audio_InternalCardNodes.py
index 07724c3..b9719a7 100644
--- a/server/site_tests/audio_InternalCardNodes/audio_InternalCardNodes.py
+++ b/server/site_tests/audio_InternalCardNodes/audio_InternalCardNodes.py
@@ -15,6 +15,13 @@
 
     """
     version = 1
+    _jack_plugger = None
+
+    def cleanup(self):
+        """Cleanup for this test."""
+        if self._jack_plugger is not None:
+            self._jack_plugger.plug()
+        super(audio_InternalCardNodes, self).cleanup()
 
     def get_expected_nodes(self, plugged):
         """Gets expected nodes should should be created for internal cards.
@@ -42,19 +49,17 @@
             nodes[1].append('ECHO_REFERENCE')
         return nodes
 
-    def run_once(self):
+    def run_once(self, plug=True):
         """Runs InternalCardNodes test."""
         if not audio_test_utils.has_audio_jack(self.host):
             audio_test_utils.check_plugged_nodes(
                     self.facade, self.get_expected_nodes(False))
             return
 
-        jack_plugger = self.host.chameleon.get_audio_board().get_jack_plugger()
+        if not plug:
+            self._jack_plugger = self.host.chameleon.get_audio_board(
+            ).get_jack_plugger()
+            self._jack_plugger.unplug()
 
-        jack_plugger.plug()
         audio_test_utils.check_plugged_nodes(self.facade,
-                                             self.get_expected_nodes(True))
-
-        jack_plugger.unplug()
-        audio_test_utils.check_plugged_nodes(self.facade,
-                                             self.get_expected_nodes(False))
+                                             self.get_expected_nodes(plug))
diff --git a/server/site_tests/audio_InternalCardNodes/control b/server/site_tests/audio_InternalCardNodes/control.plug
similarity index 81%
rename from server/site_tests/audio_InternalCardNodes/control
rename to server/site_tests/audio_InternalCardNodes/control.plug
index 47c7d9c..c76fbc6 100644
--- a/server/site_tests/audio_InternalCardNodes/control
+++ b/server/site_tests/audio_InternalCardNodes/control.plug
@@ -5,9 +5,12 @@
 from autotest_lib.server import utils
 
 AUTHOR = "chromeos-chameleon"
-NAME = "audio_InternalCardNodes"
+NAME = "audio_InternalCardNodes.plug"
 PURPOSE = "Remotely controlled audio test checking audio nodes created for internal card."
-CRITERIA = "This test will fail if the plugged nodes are not expected."
+CRITERIA = """
+This test will fail if the nodes aren't marked as plugged as expected when the
+headset is plugged.
+"""
 TIME = "SHORT"
 TEST_CATEGORY = "Functional"
 TEST_CLASS = "audio"
@@ -28,6 +31,6 @@
 
 def run(machine):
     host = hosts.create_host(machine, chameleon_args=chameleon_args)
-    job.run_test("audio_InternalCardNodes", host=host)
+    job.run_test("audio_InternalCardNodes", host=host, plug=True)
 
 parallel_simple(run, machines)
diff --git a/server/site_tests/audio_InternalCardNodes/control b/server/site_tests/audio_InternalCardNodes/control.unplug
similarity index 80%
copy from server/site_tests/audio_InternalCardNodes/control
copy to server/site_tests/audio_InternalCardNodes/control.unplug
index 47c7d9c..3ffe31c 100644
--- a/server/site_tests/audio_InternalCardNodes/control
+++ b/server/site_tests/audio_InternalCardNodes/control.unplug
@@ -5,9 +5,12 @@
 from autotest_lib.server import utils
 
 AUTHOR = "chromeos-chameleon"
-NAME = "audio_InternalCardNodes"
+NAME = "audio_InternalCardNodes.unplug"
 PURPOSE = "Remotely controlled audio test checking audio nodes created for internal card."
-CRITERIA = "This test will fail if the plugged nodes are not expected."
+CRITERIA = """
+This test will fail if the nodes aren't marked as plugged as expected when the
+headset is unplugged.
+"""
 TIME = "SHORT"
 TEST_CATEGORY = "Functional"
 TEST_CLASS = "audio"
@@ -28,6 +31,6 @@
 
 def run(machine):
     host = hosts.create_host(machine, chameleon_args=chameleon_args)
-    job.run_test("audio_InternalCardNodes", host=host)
+    job.run_test("audio_InternalCardNodes", host=host, plug=False)
 
 parallel_simple(run, machines)