autotest: Add new power_WaitForCoolDown test

This test waits for DUT to cool down (usually after
running thermal test in test suite) while collecting
power/thermal related stat.

BUG=b:140967161
TEST=ThermalQual.fast

Change-Id: I53ca6cd5a088c098c4853b1102004917511a7f8b
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/autotest/+/2018467
Reviewed-by: Mengqi Guo <mqg@chromium.org>
Tested-by: Puthikorn Voravootivat <puthik@chromium.org>
Auto-Submit: Puthikorn Voravootivat <puthik@chromium.org>
Commit-Queue: Puthikorn Voravootivat <puthik@chromium.org>
diff --git a/client/site_tests/power_WaitForCoolDown/control b/client/site_tests/power_WaitForCoolDown/control
new file mode 100644
index 0000000..a7436a3
--- /dev/null
+++ b/client/site_tests/power_WaitForCoolDown/control
@@ -0,0 +1,21 @@
+# Copyright 2020 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+AUTHOR = "puthik"
+NAME = "power_WaitForCoolDown"
+PURPOSE = "Transition test to wait for DUT to cool down after thermal test."
+TIME = "SHORT"
+TEST_CATEGORY = "Benchmark"
+TEST_CLASS = "power"
+TEST_TYPE = "client"
+
+DOC = """
+Transition test to wait for DUT to cool down after thermal test while collecting
+power / thermal releated log.
+"""
+
+args_dict = utils.args_to_dict(args)
+pdash_note = args_dict.get('pdash_note', '')
+job.run_test('power_WaitForCoolDown', pdash_note=pdash_note,
+             force_discharge=True)
diff --git a/client/site_tests/power_WaitForCoolDown/power_WaitForCoolDown.py b/client/site_tests/power_WaitForCoolDown/power_WaitForCoolDown.py
new file mode 100755
index 0000000..c017a56
--- /dev/null
+++ b/client/site_tests/power_WaitForCoolDown/power_WaitForCoolDown.py
@@ -0,0 +1,49 @@
+# Copyright 2020 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+import logging
+
+from autotest_lib.client.cros.power import power_test
+from autotest_lib.client.cros.power import power_utils
+
+
+class power_WaitForCoolDown(power_test.power_Test):
+    """class for power_WaitForCoolDown test."""
+    version = 1
+
+    def initialize(self, pdash_note='', seconds_period=20.,
+                   force_discharge=False):
+        power_utils.set_display_power(power_utils.DISPLAY_POWER_ALL_OFF)
+        super(power_WaitForCoolDown, self).initialize(
+                seconds_period=seconds_period,
+                pdash_note=pdash_note,
+                force_discharge=force_discharge)
+
+    def run_once(self, target_temp=48, max_runtime=600):
+        """"Look at temperature every |seconds_period| seconds for at most
+        |max_runtime| seconds until reported temps do not exceed |target_temp|.
+
+        @param target_temp: Target temperature in celsius.
+        @param max_runtime: Maximum runtime in seconds.
+        """
+        loop_secs = max(1, int(self._seconds_period))
+        num_loop = int(max_runtime / loop_secs)
+        self.start_measurements()
+
+        for i in range(num_loop):
+            max_temp = max(self._tlog.refresh())
+            if max_temp <= target_temp:
+                logging.info('Cooldown at %d seconds, temp: %.1fC',
+                             i * self._seconds_period, max_temp)
+                return
+            self.loop_sleep(i, loop_secs)
+
+        max_temp = max(self._tlog.refresh())
+        logging.warn(
+            'Fail to cool down after %d seconds, temp: %.1fC, target: %dC',
+            num_loop * loop_secs, max_temp, target_temp)
+
+    def cleanup(self):
+        power_utils.set_display_power(power_utils.DISPLAY_POWER_ALL_ON)
+        super(power_WaitForCoolDown, self).cleanup()