FAFT: Optimizing the tests to check EC power state in shutdown on x86 device

Currently tests confirms the shutdown state by pinging the device. And now
adding the steps to check the EC power state (either S5 or G3) in shutdown.

Checking the shutdown state by verifying the EC power states is reliable
than pinging the device. Because test may consider non-pingable
condition as shutdown state (i.e.,) if the device stuck in FW screens
instead of shutdown.

BUG=None
Branch=None
TEST= Verified all the tests on Soraka board, all tests are passing.

Change-Id: Ibc7c0b0b55308c983b7c25062d2d14c72ebb1d69
Signed-off-by: Lenine Ajagappane <leninex.ajagappane@intel.com>
Reviewed-on: https://chromium-review.googlesource.com/1067617
Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com>
Tested-by: Naresh Solanki <naresh.solanki@intel.com>
Reviewed-by: Naresh Solanki <naresh.solanki@intel.com>
Reviewed-by: Wai-Hong Tam <waihong@google.com>
diff --git a/server/cros/faft/firmware_test.py b/server/cros/faft/firmware_test.py
index bbeacfa..8a04668 100644
--- a/server/cros/faft/firmware_test.py
+++ b/server/cros/faft/firmware_test.py
@@ -1011,9 +1011,24 @@
         # add buffer from the default timeout of 60 seconds.
         self.switcher.wait_for_client_offline(timeout=100, orig_boot_id=boot_id)
         time.sleep(self.faft_config.shutdown)
+        if self.check_ec_capability(['x86'], suppress_warning=True):
+            self.check_shutdown_power_state("G3", pwr_retries=5)
         # Short press power button to boot DUT again.
         self.servo.power_key(self.faft_config.hold_pwr_button_poweron)
 
+    def check_shutdown_power_state(self, power_state, pwr_retries):
+        """Check whether the device entered into requested EC power state
+        after shutdown.
+
+        @param power_state: EC power state has to be checked. Either S5 or G3.
+        @param pwr_retries: Times to check if the DUT in expected power state.
+        @raise TestFail: If device failed to enter into requested power state.
+        """
+        if not self.wait_power_state(power_state, pwr_retries):
+            raise error.TestFail('System not shutdown properly and EC fails '
+                                 'to enter into %s state.' % power_state)
+        logging.info('System entered into %s state..', power_state)
+
     def check_lid_and_power_on(self):
         """
         On devices with EC software sync, system powers on after EC reboots if
@@ -1145,11 +1160,7 @@
                     shutdown_action.__name__)
         except ConnectionError:
             if self.check_ec_capability(['x86'], suppress_warning=True):
-                PWR_RETRIES=5
-                if not self.wait_power_state("G3", PWR_RETRIES):
-                    raise error.TestFail("System not shutdown properly and EC"
-                                         "fails to enter into G3 state.")
-                logging.info('System entered into G3 state..')
+                self.check_shutdown_power_state("G3", pwr_retries=5)
             logging.info(
                 'DUT is surely shutdown. We are going to power it on again...')
 
diff --git a/server/site_tests/firmware_ConsecutiveBoot/firmware_ConsecutiveBoot.py b/server/site_tests/firmware_ConsecutiveBoot/firmware_ConsecutiveBoot.py
index 49da5b0..5fd4723 100644
--- a/server/site_tests/firmware_ConsecutiveBoot/firmware_ConsecutiveBoot.py
+++ b/server/site_tests/firmware_ConsecutiveBoot/firmware_ConsecutiveBoot.py
@@ -62,6 +62,8 @@
         self.faft_client.system.run_shell_command('/sbin/shutdown -P now')
         logging.info('Wait for client to go offline')
         self.switcher.wait_for_client_offline(timeout=100, orig_boot_id=boot_id)
+        if self.check_ec_capability(['x86'], suppress_warning=True):
+            self.check_shutdown_power_state("G3", pwr_retries=13)
         # Retry in case power_short_press was not registered.
         for i in xrange(self.POWER_ON_RETRY):
             logging.info("sleep %d, tap power key to boot.",
diff --git a/server/site_tests/firmware_ECLidSwitch/firmware_ECLidSwitch.py b/server/site_tests/firmware_ECLidSwitch/firmware_ECLidSwitch.py
index a1a82b4..b36619a 100644
--- a/server/site_tests/firmware_ECLidSwitch/firmware_ECLidSwitch.py
+++ b/server/site_tests/firmware_ECLidSwitch/firmware_ECLidSwitch.py
@@ -69,11 +69,15 @@
     def long_delayed_wake(self):
         """Delay for LONG_WAKE_DELAY and then wake DUT with lid switch."""
         time.sleep(self.LONG_WAKE_DELAY)
+        if self.check_ec_capability(['x86'], suppress_warning=True):
+            self.check_shutdown_power_state("G3", pwr_retries=5)
         self._wake_by_lid_switch()
 
     def short_delayed_wake(self):
         """Delay for SHORT_WAKE_DELAY and then wake DUT with lid switch."""
         time.sleep(self.SHORT_WAKE_DELAY)
+        if self.check_ec_capability(['x86'], suppress_warning=True):
+            self.check_shutdown_power_state("G3", pwr_retries=5)
         self._wake_by_lid_switch()
 
     def shutdown_and_wake(self, wake_func):