File: conftest.py

package info (click to toggle)
python-pure-python-adb 0.3.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,500 kB
  • sloc: python: 2,597; makefile: 8; sh: 1
file content (123 lines) | stat: -rw-r--r-- 3,464 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import time
import telnetlib

import pytest

from ppadb.client import Client as AdbClient
from ppadb.device import Device as AdbDevice
import logging

logger = logging.getLogger(__name__)

adb_host = "emulator"
#adb_host = "127.0.0.1"
#adb_host = "172.20.0.2"
adb_port = 5037
device_serial = "emulator-5554"
emulator_port = 5554

class EmulatorConsole:
    def __init__(self, host, port):
        self._port = port
        self._telnet = telnetlib.Telnet(host=host, port=port)
        print(self._telnet.read_until(b"OK", timeout=5))

    def send(self, data):
        self._telnet.write(data.encode('utf-8') + b'\n')
        return self._telnet.read_until(b"OK", timeout=5).decode('utf-8').strip()

    def is_live(self):
        result = self.send("ping")
        if "I am alive!" in result:
            return True
        else:
            return False

    def kill(self):
        self.send("kill")
        return True

def wait_until_true(check_fn, timeout=10, description=None, interval=1):
    start_time = time.time()
    duration = 0
    while True:
        elapsed_seconds = time.time() - start_time
        if elapsed_seconds >= timeout:
            return False

        if check_fn():
            return True
        else:
            if description:
                msg = description
            else:
                msg = "Wait until {} return True...".format(check_fn.__name__ + "()")

            elapsed_seconds = int(elapsed_seconds)
            if duration != elapsed_seconds:
                duration = elapsed_seconds
                logger.info("{}... {}s (timeout:{})".format(msg, elapsed_seconds, timeout))

            time.sleep(interval)

@pytest.fixture(scope="session")
def serial(request):
    return device_serial

@pytest.fixture(scope="session")
def client(request):
    logger.info("Connecting to adb server {}:{}...".format(adb_host, adb_port))
    client = AdbClient(host=adb_host, port=adb_port)

    def try_to_connect_to_adb_server():
        try:
            client.version()
            return True
        except Exception:
            return False

    wait_until_true(try_to_connect_to_adb_server,
                    timeout=60,
                    description="Try to connect to adb server {}:{}".format(adb_host, adb_port))

    logger.info("Adb server version: {}".format(client.version()))

    return client


@pytest.fixture(scope="session")
def device(request, client, serial):
    def emulator_console_is_connectable():
        try:
            console = EmulatorConsole(host=adb_host, port=emulator_port)
            return console
        except Exception as e:
            return None

    def is_boot_completed():
        try:
            adb_device = client.device(serial)
            result = adb_device.shell("getprop sys.boot_completed")
            if not result:
                return False

            result = int(result.strip())

            if result == 1:
                return True
            else:
                return False
        except ValueError as e:
            logger.warning(e)
            return False
        except Exception as e:
            logger.error(e)
            return False

    result = wait_until_true(emulator_console_is_connectable, timeout=60)
    assert result, "Can't connect to the emulator console"

    result = wait_until_true(is_boot_completed, timeout=60)
    assert result, "The emulator doesn't boot"

    return AdbDevice(client, "emulator-5554")