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 TestDebug24: 35 """ 36 测试用例:单实例异常断点的设置/取消 37 测试步骤: 38 1. 连接 connect server 和主线程 debugger server 39 2. 主线程使能 Runtime 和 Debugger 40 3. 主线程运行等待的调试器 41 4. 主线程解析 entry_ability 和 index 文件 42 5. 设置异常捕获状态 ALL 43 6. 主线程 resume(Debugger.resume)命中异常断点,验证异常断点信息 CAUGHT 类型异常 44 7. 主线程 resume(Debugger.resume)命中异常断点,验证异常断点信息 UNCAUGHT 类型异常 45 8. 关闭主线程 debugger server 和 connect server 连接 46 测试代码: 47 function main() { 48 try { 49 throw new Error("This is a caught error"); 50 } catch (error) { 51 console.log("After error"); 52 } 53 throw new Error("This is a uncaught error"); 54 } 55 56 main(); 57 """ 58 59 def setup_method(self): 60 logging.info('Start running TestDebug24: setup') 61 62 self.log_path = rf'{os.path.dirname(__file__)}\..\log' 63 self.hilog_file_name = 'test_debug_24.hilog.txt' 64 self.id_generator = Utils.message_id_generator() 65 66 # receive the hilog before the test start 67 Utils.clear_fault_log() 68 self.hilog_process, self.write_thread = Utils.save_hilog(log_path=self.log_path, 69 file_name=self.hilog_file_name, 70 debug_on=True) 71 72 def teardown_method(self): 73 Application.uninstall(self.config['bundle_name']) 74 75 # terminate the hilog receive process after the test done 76 time.sleep(3) 77 self.hilog_process.stdout.close() 78 self.hilog_process.terminate() 79 self.hilog_process.wait() 80 self.write_thread.join() 81 82 Utils.save_fault_log(log_path=self.log_path) 83 logging.info('TestDebug24 done') 84 85 def test(self, test_suite_main_instance_04_debug): 86 logging.info('Start running TestDebug24: test') 87 self.config = test_suite_main_instance_04_debug 88 websocket = self.config['websocket'] 89 taskpool = self.config['taskpool'] 90 pid = self.config['pid'] 91 self.debugger_impl = debugger_api.DebuggerImpl(self.id_generator, websocket) 92 self.runtime_impl = runtime_api.RuntimeImpl(self.id_generator, websocket) 93 94 taskpool.submit(websocket.main_task(taskpool, self.procedure, pid)) 95 taskpool.await_taskpool() 96 taskpool.task_join() 97 if taskpool.task_exception: 98 raise taskpool.task_exception 99 100 async def procedure(self, websocket): 101 ################################################################################################################ 102 # main thread: connect the debugger server 103 ################################################################################################################ 104 main_thread = await self.debugger_impl.connect_to_debugger_server(self.config['pid'], True) 105 logging.info(f'Connect to the debugger server of instance: {main_thread.instance_id}') 106 ################################################################################################################ 107 # main thread: Runtime.enable 108 ################################################################################################################ 109 await self.runtime_impl.send("Runtime.enable", main_thread) 110 ################################################################################################################ 111 # main thread: Debugger.enable 112 ################################################################################################################ 113 await self.debugger_impl.send("Debugger.enable", main_thread) 114 ################################################################################################################ 115 # main thread: Runtime.runIfWaitingForDebugger 116 ################################################################################################################ 117 await self.runtime_impl.send("Runtime.runIfWaitingForDebugger", main_thread) 118 ################################################################################################################ 119 # main thread: Debugger.scriptParsed 120 ################################################################################################################ 121 response = await self.debugger_impl.recv("Debugger.scriptParsed", main_thread) 122 assert response['params']['url'] == self.config['file_path']['entry_ability'] 123 assert response['params']['endLine'] == 0 124 ################################################################################################################ 125 # main thread: Debugger.paused 126 ################################################################################################################ 127 response = await self.debugger_impl.recv("Debugger.paused", main_thread) 128 assert response['params']['callFrames'][0]['url'] == self.config['file_path']['entry_ability'] 129 assert response['params']['reason'] == 'Break on start' 130 ################################################################################################################ 131 # main thread: Debugger.resume 132 ################################################################################################################ 133 await self.debugger_impl.send("Debugger.resume", main_thread) 134 ################################################################################################################ 135 # main thread: Debugger.scriptParsed 136 ################################################################################################################ 137 response = await self.debugger_impl.recv("Debugger.scriptParsed", main_thread) 138 assert response['params']['url'] == self.config['file_path']['index'] 139 assert response['params']['endLine'] == 0 140 ################################################################################################################ 141 # main thread: Debugger.paused 142 ################################################################################################################ 143 response = await self.debugger_impl.recv("Debugger.paused", main_thread) 144 assert response['params']['callFrames'][0]['url'] == self.config['file_path']['index'] 145 assert response['params']['reason'] == 'Break on start' 146 ################################################################################################################ 147 # main thread: Debugger.setPauseOnExceptions 148 ################################################################################################################ 149 params = debugger.PauseOnExceptionsState.ALL 150 await self.debugger_impl.send("Debugger.setPauseOnExceptions", main_thread, params) 151 ################################################################################################################ 152 # main thread: Debugger.resume 153 ################################################################################################################ 154 await self.debugger_impl.send("Debugger.resume", main_thread) 155 ################################################################################################################ 156 # main thread: Debugger.paused 157 ################################################################################################################ 158 response = await self.debugger_impl.recv("Debugger.paused", main_thread) 159 assert response['params']['callFrames'][0]['url'] == self.config['file_path']['index'] 160 assert response['params']['reason'] == 'exception' 161 assert 'This is a caught error' in response['params']['data']['unserializableValue'] 162 ################################################################################################################ 163 # main thread: Debugger.resume 164 ################################################################################################################ 165 await self.debugger_impl.send("Debugger.resume", main_thread) 166 ################################################################################################################ 167 # main thread: Debugger.paused 168 ################################################################################################################ 169 response = await self.debugger_impl.recv("Debugger.paused", main_thread) 170 assert response['params']['callFrames'][0]['url'] == self.config['file_path']['index'] 171 assert response['params']['reason'] == 'exception' 172 assert 'This is a uncaught error' in response['params']['data']['unserializableValue'] 173 ################################################################################################################ 174 # main thread: Debugger.resume 175 ################################################################################################################ 176 await self.debugger_impl.send("Debugger.resume", main_thread) 177 ################################################################################################################ 178 # close the websocket connections 179 ################################################################################################################ 180 await websocket.send_msg_to_debugger_server(main_thread.instance_id, main_thread.send_msg_queue, 'close') 181 await websocket.send_msg_to_connect_server('close') 182 ################################################################################################################