1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3# Copyright (C) 2024 Huawei Device Co., Ltd. 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16import os 17import zipfile 18import subprocess 19import re 20import time 21import json 22 23OUTPUT_PATH = "testModule/output" 24PID_INDEX = 7 25SMALL_FILE_EXPECTED_SIZE = 1 * 1024 26SMALL_FILE_EXPECTED_SIZE_2 = 5 * 1024 27MID_FILE_EXPECTED_SIZE = 100 * 1024 28 29 30def get_path_by_attribute(tree, key, value): 31 attributes = tree['attributes'] 32 if attributes is None: 33 print("tree contains no attributes") 34 return None 35 path = [] 36 if attributes.get(key) == value: 37 return path 38 for index, child in enumerate(tree['children']): 39 child_path = path + [index] 40 result = get_path_by_attribute(child, key, value) 41 if result is not None: 42 return child_path + result 43 return None 44 45 46def get_element_by_path(tree, path): 47 if len(path) == 1: 48 return tree['children'][path[0]] 49 return get_element_by_path(tree['children'][path[0]], path[1:]) 50 51 52def get_location_by_text(tree, text): 53 path = get_path_by_attribute(tree, "text", text) 54 if path is None or len(path) == 0: 55 print("text not found in layout file") 56 element = get_element_by_path(tree, path) 57 locations = element['attributes']['bounds'].replace('[', '').replace(']', ' ').replace(',', ' ').strip().split() 58 return int((int(locations[0]) + int(locations[2])) / 2), int((int(locations[1]) + int(locations[3])) / 2) 59 60 61def touch(dx, dy): 62 output = subprocess.check_output(f"hdc shell uitest uiInput click {dx} {dy}") 63 64 65def get_layout_tree(): 66 output = subprocess.check_output("hdc shell uitest dumpLayout", text=True) 67 path = output.strip().split(":")[-1] 68 subprocess.check_output(f"hdc file recv {path} .\..\inputfiles\layout.json") 69 subprocess.check_output("hdc shell rm " + path) 70 with open(".\..\inputfiles\layout.json", encoding="utf-8") as f: 71 tree = json.load(f) 72 return tree 73 74 75def touch_button(text): 76 layout_tree = get_layout_tree() 77 location = get_location_by_text(layout_tree, text) 78 touch(location[0], location[1]) 79 80 81def get_pid(name): 82 subprocess.check_output("hdc shell ps -ef | grep " + name + " > /data/local/tmp/pids.txt") 83 subprocess.check_output(f"hdc file recv /data/local/tmp/pids.txt .\..\outputfiles\ ", text=True, encoding="utf-8") 84 with open(r'.\..\outputfiles\pids.txt', 'r') as file: 85 lines = file.readlines() 86 for line in lines: 87 if line.split()[PID_INDEX] == name: 88 return line.split()[1] 89 return 0 90 91 92def get_pid_by_process_name(process_name): 93 pid = None 94 cmd = f"hdc shell \"pidof {process_name}\"" 95 try: 96 pid = subprocess.check_output(cmd, encoding="utf-8", text=True) 97 pid = int(pid.strip()) 98 except subprocess.CalledProcessError as e: 99 print(f"Command failed: {cmd}\nError: {e}") 100 except Exception as e: 101 print(f"Unexpected error: {e}") 102 return pid