~aleteoryx/muditaos

ref: ed0d26dc47f77087e9f108ff6df7a6ce4913445d muditaos/test/pytest/test_auto_lock.py -rw-r--r-- 4.6 KiB
ed0d26dc — Wojtek Cichon [EGD-6282] Add marketing-friendly test messages to Pure 4 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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
# Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
# For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

import pytest
from harness.interface.defs import status, key_codes
from harness.interface.CDCSerial import Keytype
from harness import log
import time
import random


def change_auto_lock_timer(harness, value: str):
    body = {"changeAutoLockTimeout": value}
    ret = harness.endpoint_request("developerMode", "put", body)
    assert ret["status"] == status["OK"]

@pytest.fixture(scope='function')
def phone_ends_with_default_auto_lock(harness):
    yield
    timeout = str(30000)
    log.info("Setting back default timeout to {}ms".format(timeout))
    change_auto_lock_timer(harness, timeout)

app_navigation_to_name_mapping = {
    "messages": "ApplicationMessages",
    "calllog": "ApplicationCallLog",
    "contacts": "ApplicationPhonebook",
    "music": "ApplicationMusicPlayer",
    #    "meditation": "ApplicationMeditation",
    #    "settings": "ApplicationSettingsNew",
    "tools": "ApplicationDesktop",  # other MenuPage
    "alarm": "ApplicationAlarmClock",
    "calendar": "ApplicationCalendar"
}


def enter_application(harness, nav_name, app_name):
    harness.connection.send_key_code(key_codes["enter"])
    harness.open_application(nav_name)
    if harness.get_application_name() != app_name:
        time.sleep(1)
        assert harness.get_application_name() == app_name


def open_random_application(harness):
    navigation, name = random.choice(list(app_navigation_to_name_mapping.items()))
    print(navigation)
    print(name)

    # enter_application(harness, navigation, name)
    enter_application(harness, "calendar", "ApplicationCalendar")
    return "ApplicationCalendar"
    # return name


def contains_value(body: dict, attr_name, attr_value):
    if attr_name in body:
        return body[attr_name] == attr_value


def contains_value_recursively(body: dict, attr_name, attr_value):
    if len(body) == 1:
        print(next(iter(body)))
        return contains_value_recursively(next(iter(body.values())), attr_name, attr_value)

    if contains_value(body, attr_name, attr_value):
        return True

    if 'Children' in body:
        for child in body['Children']:
            value_found = contains_value_recursively(child, attr_name, attr_value)
            if value_found:
                return True

    return False


def get_dom(harness):
    body = {"ui": True, "getWindow": True}
    result = harness.endpoint_request("developerMode", "get", body)
    # log.info("data {}".format(find_item_depth_first(result['body']['dom'], 'TopBar')))
    assert result['body']
    assert 'Window' in result['body']['dom']
    return result

@pytest.mark.skip("not fully implemented - should work after EGD-5884")
@pytest.mark.rt1051
@pytest.mark.usefixtures("phone_ends_with_default_auto_lock")
@pytest.mark.usefixtures("phone_unlocked")
def test_auto_lock(harness):
    # change timer lock value
    change_auto_lock_timer(harness, str(5000))
    assert harness.get_application_name() == "ApplicationDesktop"

    time.sleep(6)
    assert harness.connection.is_phone_locked() is True
    harness.unlock_phone()
    app = open_random_application(harness)
    # we should go back to desktop an be locked after 6 seconds
    time.sleep(6)
    assert harness.connection.is_phone_locked() is True
    harness.unlock_phone()
    # we should go back to previously chosen application
    assert harness.get_application_name() == app

    # go back
    harness.connection.send_key_code(key_codes["fnRight"])
    time.sleep(1)
    res = get_dom(harness)
    assert contains_value_recursively(res, 'WindowName', 'MenuWindow') is True

    # go back to main screen
    harness.connection.send_key_code(key_codes["fnRight"], Keytype.long_press)


@pytest.mark.rt1051
@pytest.mark.usefixtures("phone_ends_with_default_auto_lock")
@pytest.mark.usefixtures("phone_unlocked")
def test_no_auto_lock_for_meditation_app(harness):
    # change timer lock value
    change_auto_lock_timer(harness, str(5000))
    assert harness.get_application_name() == "ApplicationDesktop"

    time.sleep(6)
    assert harness.connection.is_phone_locked() is True
    harness.unlock_phone()
    enter_application(harness, "meditation", "ApplicationMeditation")
    # we should go back to desktop an be locked after 4 seconds
    time.sleep(6)
    assert harness.connection.is_phone_locked() is False

    # we should go back to previously chosen application
    assert harness.get_application_name() == "ApplicationMeditation"

    # go back to main screen
    harness.connection.send_key_code(key_codes["fnRight"], Keytype.long_press)