~aleteoryx/muditaos

fac27c747a44786c0fd971e7748e386380165723 — mkamonMdt 5 years ago 9cc633b
[EGD-4955] Add on-off test harness for simulator

This PR provides CI test for turning on and off PurePhone
simulator.
M .github/workflows/main.yml => .github/workflows/main.yml +14 -0
@@ 102,3 102,17 @@ jobs:
      - name: print emulator logs
        run: cat emulator.log
        if: always()

  emulator_on_off_test:
    name: emulator on-off test
    if: github.event.pull_request.draft == false
    runs-on: self-hosted
    needs:
      - build_linux_binary_and_run_tests
    steps:
      - name: start emulator on-off test
        run: ./phone_on_off_test.sh
        working-directory: test
      - name: print emulator logs
        run: cat emulator.log
        if: always()

M source/main.cpp => source/main.cpp +1 -1
@@ 165,5 165,5 @@ int main()

    cpp_freertos::Thread::StartScheduler();

    return 1;
    return 0;
}

M test/README.md => test/README.md +11 -9
@@ 73,6 73,7 @@ methods:
    * `wait` - timeout
    
#### Example

```python
from harness.harness import Harness
from harness.interface.defs import key_codes


@@ 81,20 82,21 @@ from harness.interface.defs import key_codes
harness = Harness.from_detect()

# get current application name
current_window = harness.get_window_name()
current_window = harness.get_application_name()


#open messages when phone is unlocked
# open messages when phone is unlocked
@harness.with_phone_unlocked
def do_after_unlock(connection):
    # open menu
    connection.send_key_code(key_codes["enter"])
  # open menu
  connection.send_key_code(key_codes["enter"])

    harness.open_application("messages")
    # send joystick down keypress
    connection.send_key_code(key_codes["down"])
  harness.open_application("messages")
  # send joystick down keypress
  connection.send_key_code(key_codes["down"])

    # open a thread
    connection.send_key_code(key_codes["enter"])
  # open a thread
  connection.send_key_code(key_codes["enter"])
```

### pyTest running

M test/harness/harness.py => test/harness/harness.py +23 -6
@@ 6,6 6,7 @@ from enum import Enum
from harness import utils, log
from harness.interface import CDCSerial as serial
from harness.interface.defs import key_codes, endpoint, method
from harness.interface.CDCSerial import Keytype
from harness.utils import send_keystoke, application_keypath, send_char
from harness.interface.error import TestError, Error
import random


@@ 37,17 38,18 @@ class Harness:
    def get_connection(self):
        return self.connection

    def get_window_name(self):
        return self.connection.get_window_name()
    def get_application_name(self):
        return self.connection.get_application_name()

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


@@ 90,3 92,18 @@ class Harness:
        })
        return ret

    def turn_phone_off(self):
        log.info("Turning phone off...")
        app_desktop = "ApplicationDesktop"
        end_loop_counter = 10

        while not self.get_application_name() == app_desktop:
            if not end_loop_counter > 0:
                raise LookupError("Filed to switch to {}".format(app_desktop))
            log.info("Not on the Application Desktop, fnRight.")
            self.connection.send_key_code(key_codes["fnRight"], Keytype.long_press)
            end_loop_counter -=  1

        self.connection.send_key_code(key_codes["fnRight"], Keytype.long_press)
        self.connection.send_key_code(key_codes["right"])
        self.connection.send_key_code(key_codes["enter"])

M test/harness/interface/CDCSerial.py => test/harness/interface/CDCSerial.py +1 -1
@@ 114,7 114,7 @@ class CDCSerial:
        print(ret)
        return ret["body"]["ATResponse"]

    def get_window_name(self):
    def get_application_name(self):
        body = {
            "focus": True
        }

A test/phone_off.py => test/phone_off.py +39 -0
@@ 0,0 1,39 @@
# Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
# For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

# Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
# For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

import sys

from harness import log
from harness.harness import Harness
from harness.interface.error import TestError, Error

def main():
    if len(sys.argv) == 1 or "/dev" not in sys.argv[1]:
        log.warning("Port name not passed, trying port name filename from simulator...")
        try:
            file = open("/tmp/purephone_pts_name", "r")
        except FileNotFoundError:
            raise TestError(Error.PORT_FILE_NOT_FOUND)

        port_name = file.readline()
        if port_name.isascii():
            log.debug("found {} entry!".format(port_name))
        else:
            print(f'Please pass port name as the parameter: python {sys.argv[0]} /dev/ttyACM0 number duration')
            raise TestError(Error.PORT_NOT_FOUND)
    else:
        port_name = sys.argv[1]

    harness = Harness(port_name)
    harness.unlock_phone()
    harness.turn_phone_off()

if __name__ == "__main__":
    try:
        main()
    except TestError as err:
        log.error(err)
        exit(err.get_error_code())

A test/phone_on_off_test.sh => test/phone_on_off_test.sh +30 -0
@@ 0,0 1,30 @@
#!/usr/bin/env bash
# Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
# For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

declare JOB

## run command in the background
background() {
  eval $1
  JOB=$!
}

## returns jobs status if job failed
reap() {
  local status=0
    wait ${JOB}
    status=$?
    if [[ $status -ne 0 ]]; then
      echo -e "[${JOB}] Exited with status: ${status}"
    fi
  exit ${status}
}

pushd ..
background './run_emulator_on_filesystem_image.sh 2>&1 > emulator.log & echo $! > emulator.pid'
popd
sleep 1
eval 'python3 phone_off.py -rP --port=simulator --timeout=20 -m "not rt1051"'

reap

M test/pytest/test_call_back.py => test/pytest/test_call_back.py +2 -2
@@ 19,9 19,9 @@ def test_call(harness, call_duration):
    # enter menu
    harness.connection.send_key_code(key_codes["enter"])
    harness.open_application("calllog")
    if harness.connection.get_window_name() != "ApplicationCallLog":
    if harness.connection.get_application_name() != "ApplicationCallLog":
        time.sleep(2)
        assert harness.connection.get_window_name() == "ApplicationCallLog"
        assert harness.connection.get_application_name() == "ApplicationCallLog"

    # call
    harness.connection.send_key_code(key_codes["fnLeft"])

M test/pytest/test_send_message.py => test/pytest/test_send_message.py +2 -2
@@ 19,9 19,9 @@ def test_send_message(harness, phone_number, sms_text):
    # enter menu
    harness.connection.send_key_code(key_codes["enter"])
    harness.open_application("messages")
    if harness.connection.get_window_name() != "ApplicationMessages":
    if harness.connection.get_application_name() != "ApplicationMessages":
        time.sleep(2)
        assert harness.connection.get_window_name() == "ApplicationMessages"
        assert harness.connection.get_application_name() == "ApplicationMessages"

    # create new message
    harness.connection.send_key_code(key_codes["left"])