scripts: deploy_chrome: add --unlock-password option

If provided the unlock password will be entered into the unlock screen
after deployment and restart, once the unlock screen is displayed.

BUG=none
TEST=./run_tests scripts/deploy_chrome_unittest.py
TEST=manual

Change-Id: Icddba9567f5a11ea58d64fd5176828568cb127a1
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/chromite/+/4338996
Tested-by: Joel Hockey <joelhockey@chromium.org>
Commit-Queue: Achuith Bhandarkar <achuith@chromium.org>
Reviewed-by: Achuith Bhandarkar <achuith@chromium.org>
Auto-Submit: Joel Hockey <joelhockey@chromium.org>
diff --git a/scripts/deploy_chrome.py b/scripts/deploy_chrome.py
index f84aa58..ec3f16b 100644
--- a/scripts/deploy_chrome.py
+++ b/scripts/deploy_chrome.py
@@ -54,6 +54,8 @@
 LSOF_COMMAND_CHROME = "lsof %s/chrome"
 LSOF_COMMAND = "lsof %s"
 DBUS_RELOAD_COMMAND = "killall -HUP dbus-daemon"
+LAST_LOGIN_COMMAND = "bootstat_get_last login-prompt-visible"
+UNLOCK_PASSWORD_COMMAND = "inject-keys.py -s %s -k enter"
 
 _ANDROID_DIR = "/system/chrome"
 _ANDROID_DIR_EXTRACT_PATH = "system/chrome/*"
@@ -460,9 +462,27 @@
         self.device.run(DBUS_RELOAD_COMMAND, check=False)
 
         if self.options.startui and self._stopped_ui:
+            last_login = self._GetLastLogin()
             logging.info("Starting UI...")
             self.device.run("start ui")
 
+            if self.options.unlock_password:
+                logging.info("Unlocking...")
+
+                @retry_util.WithRetry(max_retry=5, sleep=1)
+                def WaitForUnlockScreen():
+                    if self._GetLastLogin() == last_login:
+                        raise DeployFailure("Unlock screen not shown")
+
+                WaitForUnlockScreen()
+                self.device.run(
+                    UNLOCK_PASSWORD_COMMAND % self.options.unlock_password
+                )
+
+    def _GetLastLogin(self):
+        """Returns last login time"""
+        return self.device.run(LAST_LOGIN_COMMAND).stdout.strip()
+
     def _DeployTestBinaries(self):
         """Deploys any local test binary to _CHROME_TEST_BIN_DIR on the device.
 
@@ -753,6 +773,11 @@
         help="Don't restart the ui daemon after deployment.",
     )
     parser.add_argument(
+        "--unlock-password",
+        default=None,
+        help="Password to use to unlock after deployment and restart.",
+    )
+    parser.add_argument(
         "--nostrip",
         action="store_false",
         dest="dostrip",