1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3"""
4Copyright (c) 2024 Huawei Device Co., Ltd.
5Licensed under the Apache License, Version 2.0 (the "License");
6you may not use this file except in compliance with the License.
7You may obtain a copy of the License at
8
9    http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
16
17Description: Action words of Application launch.
18"""
19
20import logging
21import subprocess
22import re
23import time
24
25
26class Application(object):
27    @classmethod
28    def stop(cls, bundle_name):
29        stop_cmd = ['hdc', 'shell', 'aa', 'force-stop', bundle_name]
30        logging.info('force stop application: ' + ' '.join(stop_cmd))
31        stop_result = subprocess.run(stop_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
32        logging.info(stop_result.stdout.strip())
33        assert stop_result.returncode == 0
34
35    @classmethod
36    def uninstall(cls, bundle_name):
37        uninstall_cmd = ['hdc', 'uninstall', bundle_name]
38        logging.info('uninstall application: ' + ' '.join(uninstall_cmd))
39        uninstall_result = subprocess.run(uninstall_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
40        logging.info(uninstall_result.stdout.strip())
41        assert uninstall_result.returncode == 0
42
43    @classmethod
44    def install(cls, hap_path):
45        install_cmd = ['hdc', 'install', '-r', hap_path]
46        logging.info('install application: ' + ' '.join(install_cmd))
47        install_result = subprocess.run(install_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
48        logging.info(install_result.stdout)
49        assert 'successfully' in install_result.stdout.decode('utf-8')
50
51    @classmethod
52    def start(cls, bundle_name, start_mode=None):
53        start_cmd = (['hdc', 'shell', 'aa', 'start', '-a', 'EntryAbility', '-b', bundle_name] +
54                     ([start_mode] if start_mode else []))
55        logging.info('start application: ' + ' '.join(start_cmd))
56        start_result = subprocess.run(start_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
57        logging.info(start_result.stdout)
58        assert start_result.stdout.decode('utf-8').strip() == 'start ability successfully.'
59
60    @classmethod
61    def get_pid(cls, bundle_name):
62        ps_cmd = ['hdc', 'shell', 'ps', '-ef']
63        ps_result = subprocess.run(ps_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
64        ps_result_out = ps_result.stdout.decode('utf-8')
65        for line in ps_result_out.strip().split('\r\n'):
66            if bundle_name in line:
67                logging.info(f'pid of {bundle_name}: ' + line)
68                return line.strip().split()[1]
69        return 0
70
71    @classmethod
72    def launch_application(cls, bundle_name, hap_path, start_mode=None):
73        cls.stop(bundle_name)
74        cls.uninstall(bundle_name)
75        cls.install(hap_path)
76        cls.start(bundle_name, start_mode)
77        cls.keep_awake()
78        time.sleep(3)
79        pid = cls.get_pid(bundle_name)
80        return int(pid)
81
82    @classmethod
83    def attach(cls, bundle_name):
84        attach_cmd = (['hdc', 'shell', 'aa', 'attach', '-b', bundle_name])
85        logging.info('start application: ' + ' '.join(attach_cmd))
86        attach_result = subprocess.run(attach_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
87        logging.info(attach_result.stdout)
88        assert attach_result.stdout.decode('utf-8').strip() == 'attach app debug successfully.'
89
90    @classmethod
91    def click_on_middle(cls):
92        '''
93        模拟点击屏幕中间
94        '''
95        get_screen_info_cmd = ['hdc', 'shell', 'hidumper', '-s', 'RenderService', '-a', 'screen']
96        screen_info = subprocess.run(get_screen_info_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
97        match = re.search(r'physical screen resolution: (\d+)x(\d+)', screen_info.stdout.decode('utf-8'))
98        assert match is not None
99
100        middle_x = int(match.group(1)) // 2
101        middle_y = int(match.group(2)) // 2
102        click_cmd = ['hdc', 'shell', 'uinput', '-T', '-c', str(middle_x), str(middle_y)]
103        click_result = subprocess.run(click_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
104        click_result_out = click_result.stdout.decode('utf-8').strip()
105        logging.info(click_result_out)
106        assert "click coordinate" in click_result_out
107
108    @classmethod
109    def keep_awake(cls):
110        keep_awake_cmd = ['hdc', 'shell', 'power-shell', 'setmode', '602']
111        keep_awake_result = subprocess.run(keep_awake_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
112        keep_awake_result_out = keep_awake_result.stdout.decode('utf-8').strip()
113        logging.info(keep_awake_result_out)
114        assert "Set Mode Success!" in keep_awake_result_out