~aleteoryx/muditaos

ref: 50f28e8f19b23384aac9efd5c22cc155d451cace muditaos/test/harness/harness.py -rw-r--r-- 1.8 KiB
50f28e8f — Bartosz Cichocki [EGD-4270] updated test API, added sending message case in test harness (#1042) 5 years ago
                                                                                
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
# Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
# For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md
import time

from harness import utils
from harness.interface import CDCSerial as serial
from harness.interface.defs import key_codes, endpoint, method
from harness.utils import send_keystoke, application_keypath, send_char
import random


class Harness:
    connection = None
    is_phone_unlocked = False

    def __init__(self, port='/dev/ttyACM2'):
        self.connection = serial.CDCSerial(port)

    def get_connection(self):
        return self.connection

    def unlock_phone(self):
        if self.connection.is_phone_locked():
            self.connection.send_key(key_codes["enter"])
            self.connection.send_key(key_codes["#"])
            self.connection.send_key(3)
            self.connection.send_key(3)
            self.connection.send_key(3)
            self.connection.send_key(3)
            print("Phone unlocked")
        else:
            print("Phone already unlocked")

    def with_phone_unlocked(self, func):
        if not self.is_phone_unlocked:
            self.unlock_phone()
            self.is_phone_unlocked = True

        func(self.connection)

    def open_application(self, app):
        send_keystoke(application_keypath[app], self.connection)

    def send_text(self, text: str):
        for letter in text:
            send_char(letter, self.connection)

    def send_number(self, number: str):
        utils.send_number(number, self.connection)

    def endpoint_request(self, ep_name: str, met: str, body: dict) -> dict:
        ret = self.connection.write({
            "endpoint": endpoint[ep_name],
            "method": method[met],
            "uuid": random.randint(1, 32000),
            "body": body
        })
        return ret["body"]