1# Copyright 2016 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4#
5# This file contains things that are shared by arc.py and arc_util.py.
6
7import logging
8
9from autotest_lib.client.bin import utils
10from autotest_lib.client.common_lib import error
11
12
13# Ask Chrome to start ARC instance and the script will block until ARC's boot
14# completed event.
15ARC_MODE_ENABLED = "enabled"
16# Similar to "enabled", except that it will not block.
17ARC_MODE_ENABLED_ASYNC = "enabled_async"
18# Ask Chrome to not start ARC instance.  This is the default.
19ARC_MODE_DISABLED = "disabled"
20# All available ARC options.
21ARC_MODES = [ARC_MODE_ENABLED, ARC_MODE_ENABLED_ASYNC, ARC_MODE_DISABLED]
22
23_BOOT_CHECK_INTERVAL_SECONDS = 2
24_WAIT_FOR_ANDROID_BOOT_SECONDS = 120
25
26
27def wait_for_android_boot(timeout=None):
28    """Sleep until Android has completed booting or timeout occurs."""
29    if timeout is None:
30        timeout = _WAIT_FOR_ANDROID_BOOT_SECONDS
31
32    def _is_android_booted():
33        output = utils.system_output(
34            'android-sh -c "getprop sys.boot_completed"', ignore_status=True)
35        return output.strip() == '1'
36
37    logging.info('Waiting for Android to boot completely.')
38    utils.poll_for_condition(condition=_is_android_booted,
39                             desc='Android has booted',
40                             timeout=timeout,
41                             exception=error.TestFail('Android did not boot!'),
42                             sleep_interval=_BOOT_CHECK_INTERVAL_SECONDS)
43    logging.info('Android has booted completely.')
44