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: Scenario test case. 18""" 19 20import logging 21import os 22import time 23 24import pytest 25 26from aw import Application 27from aw import Utils 28from aw import debugger, runtime 29from aw.api import debugger_api, runtime_api 30 31 32@pytest.mark.debug 33@pytest.mark.timeout(60) 34class TestDebug23: 35 """ 36 测试用例:单实例smartStepinto调试 37 测试步骤: 38 1. 连接 connect server 和主线程 debugger server 39 2. 主线程使能 Runtime 和 Debugger 40 3. 主线程运行等待的调试器 41 4. 主线程解析 entry_ability 和 index 文件 42 5. 主线程 Index.ts 文件设置断点并运行 43 6. 主线程 resume 验证是否正确命中断点 44 7. 主线程 smartStepInto 验证传回的函数信息 45 8. 关闭主线程 debugger server 和 connect server 连接 46 测试代码: 47 function test1() { 48 return taskNumber; 49 } 50 51 function test2() { 52 return taskNumber + 5; 53 } 54 55 function main() { 56 test1(); 57 taskNumber = test1() + test2();// 实例debug23断点处 58 } 59 60 main(); 61 """ 62 63 def setup_method(self): 64 logging.info('Start running TestDebug23: setup') 65 66 self.log_path = rf'{os.path.dirname(__file__)}\..\log' 67 self.hilog_file_name = 'test_debug_23.hilog.txt' 68 self.id_generator = Utils.message_id_generator() 69 70 # receive the hilog before the test start 71 Utils.clear_fault_log() 72 self.hilog_process, self.write_thread = Utils.save_hilog(log_path=self.log_path, 73 file_name=self.hilog_file_name, 74 debug_on=True) 75 76 def teardown_method(self): 77 Application.uninstall(self.config['bundle_name']) 78 79 # terminate the hilog receive process after the test done 80 time.sleep(3) 81 self.hilog_process.stdout.close() 82 self.hilog_process.terminate() 83 self.hilog_process.wait() 84 self.write_thread.join() 85 86 Utils.save_fault_log(log_path=self.log_path) 87 logging.info('TestDebug23 done') 88 89 def test(self, test_suite_main_instance_03_debug): 90 logging.info('Start running TestDebug23: test') 91 self.config = test_suite_main_instance_03_debug 92 websocket = self.config['websocket'] 93 taskpool = self.config['taskpool'] 94 pid = self.config['pid'] 95 self.debugger_impl = debugger_api.DebuggerImpl(self.id_generator, websocket) 96 self.runtime_impl = runtime_api.RuntimeImpl(self.id_generator, websocket) 97 98 taskpool.submit(websocket.main_task(taskpool, self.procedure, pid)) 99 taskpool.await_taskpool() 100 taskpool.task_join() 101 if taskpool.task_exception: 102 raise taskpool.task_exception 103 104 async def procedure(self, websocket): 105 ################################################################################################################ 106 # main thread: connect the debugger server 107 ################################################################################################################ 108 main_thread = await self.debugger_impl.connect_to_debugger_server(self.config['pid'], True) 109 logging.info(f'Connect to the debugger server of instance: {main_thread.instance_id}') 110 ################################################################################################################ 111 # main thread: Runtime.enable 112 ################################################################################################################ 113 await self.runtime_impl.send("Runtime.enable", main_thread) 114 ################################################################################################################ 115 # main thread: Debugger.enable 116 ################################################################################################################ 117 await self.debugger_impl.send("Debugger.enable", main_thread) 118 ################################################################################################################ 119 # main thread: Runtime.runIfWaitingForDebugger 120 ################################################################################################################ 121 await self.runtime_impl.send("Runtime.runIfWaitingForDebugger", main_thread) 122 ################################################################################################################ 123 # main thread: Debugger.scriptParsed 124 ################################################################################################################ 125 response = await self.debugger_impl.recv("Debugger.scriptParsed", main_thread) 126 assert response['params']['url'] == self.config['file_path']['entry_ability'] 127 assert response['params']['endLine'] == 0 128 ################################################################################################################ 129 # main thread: Debugger.paused 130 ################################################################################################################ 131 response = await self.debugger_impl.recv("Debugger.paused", main_thread) 132 assert response['params']['callFrames'][0]['url'] == self.config['file_path']['entry_ability'] 133 assert response['params']['reason'] == 'Break on start' 134 ################################################################################################################ 135 # main thread: Debugger.resume 136 ################################################################################################################ 137 await self.debugger_impl.send("Debugger.resume", main_thread) 138 ################################################################################################################ 139 # main thread: Debugger.scriptParsed 140 ################################################################################################################ 141 response = await self.debugger_impl.recv("Debugger.scriptParsed", main_thread) 142 assert response['params']['url'] == self.config['file_path']['index'] 143 assert response['params']['endLine'] == 0 144 ################################################################################################################ 145 # main thread: Debugger.paused 146 ################################################################################################################ 147 response = await self.debugger_impl.recv("Debugger.paused", main_thread) 148 assert response['params']['callFrames'][0]['url'] == self.config['file_path']['index'] 149 assert response['params']['reason'] == 'Break on start' 150 ################################################################################################################ 151 # main thread: Debugger.removeBreakpointsByUrl 152 ################################################################################################################ 153 params = debugger.RemoveBreakpointsUrl(self.config['file_path']['index']) 154 await self.debugger_impl.send("Debugger.removeBreakpointsByUrl", main_thread, params) 155 ################################################################################################################ 156 # main thread: Debugger.getPossibleAndSetBreakpointByUrl 157 ################################################################################################################ 158 locations = [debugger.BreakLocationUrl(url=self.config['file_path']['index'], line_number=73)] 159 params = debugger.SetBreakpointsLocations(locations) 160 response = await self.debugger_impl.send("Debugger.getPossibleAndSetBreakpointsByUrl", 161 main_thread, params) 162 assert response['result']['locations'][0]['id'] == 'id:73:0:' + self.config['file_path']['index'] 163 ################################################################################################################ 164 # main thread: Debugger.resume 165 ################################################################################################################ 166 await self.debugger_impl.send("Debugger.resume", main_thread) 167 ################################################################################################################ 168 # main thread: Debugger.paused, hit breakpoint 169 ################################################################################################################ 170 response = await self.debugger_impl.recv("Debugger.paused", main_thread) 171 assert response['params']['callFrames'][0]['url'] == self.config['file_path']['index'] 172 assert response['params']['hitBreakpoints'] == ['id:73:17:' + self.config['file_path']['index']] 173 ################################################################################################################ 174 # main thread: Debugger.smartStepInto 175 ################################################################################################################ 176 params = debugger.SmartStepIntoParams(url=self.config['file_path']['index'], line_number=69) 177 await self.debugger_impl.send("Debugger.smartStepInto", main_thread, params) 178 ################################################################################################################ 179 # main thread: Debugger.resume 180 ################################################################################################################ 181 await self.debugger_impl.send("Debugger.resume", main_thread) 182 # main thread: Debugger.paused 183 response = await self.debugger_impl.recv("Debugger.paused", main_thread) 184 assert response['params']['callFrames'][0]['url'] == self.config['file_path']['index'] 185 assert response['params']['callFrames'][0]['functionName'] == 'test2' 186 assert response['params']['reason'] == 'other' 187 assert response['params']['hitBreakpoints'] == ['id:69:11:' + self.config['file_path']['index']] 188 ################################################################################################################ 189 # main thread: Debugger.resume 190 ################################################################################################################ 191 await self.debugger_impl.send("Debugger.resume", main_thread) 192 ################################################################################################################ 193 # main thread: click on the screen 194 ################################################################################################################ 195 Application.click_on_middle() 196 ################################################################################################################ 197 # main thread: Debugger.disable 198 ################################################################################################################ 199 await self.debugger_impl.send("Debugger.disable", main_thread) 200 ################################################################################################################ 201 # close the websocket connections 202 ################################################################################################################ 203 await websocket.send_msg_to_debugger_server(main_thread.instance_id, main_thread.send_msg_queue, 'close') 204 await websocket.send_msg_to_connect_server('close') 205 ################################################################################################################