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 29from aw.api import debugger_api, runtime_api 30 31 32@pytest.mark.debug 33@pytest.mark.timeout(60) 34class TestMainMixedDebug: 35 """ 36 测试用例:主线程 debug 混合调试 37 测试步骤: 38 1. 连接 connect server 和主线程 debugger server 39 2. 主线程使能 Runtime 和 Debugger 40 3. 主线程使能混合调试(Debugger.setMixedDebugEnabled) 41 4. 主线程 Index.ets 文件设置断点(Debugger.getPossibleAndSetBreakpointByUrl) 42 5. 触发点击事件,主线程暂停在断点处 43 6. 主线程 stepInto,进入 native 调试(Debugger.stepInto) 44 7. 主线程收到 native 返回的消息后应答,停在下一断点处(Debugger.replyNativeCalling) 45 8. 主线程 stepOver,停在下一行(Debugger.stepOver) 46 9. 主线程 resume(Debugger.resume) 47 10. 主线程去使能 debugger(Debugger.disable) 48 11. 关闭主线程 debugger server 和 connect server 连接 49 关键代码: 50 Index.ets 51 import testNapi from 'libentry.so'; 52 .OnClick(() => { 53 let num = testNapi.add(1, 2) 54 let ans = testNapi.add(num, 3) 55 console.log("testNapi.add run successfully, ans = ", ans) 56 }) 57 """ 58 59 def setup_method(self): 60 logging.info('Start running TestMainMixedDebug: setup') 61 62 self.log_path = rf'{os.path.dirname(__file__)}\..\log' 63 self.hilog_file_name = 'test_main_mixed_debug.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('TestMainMixedDebug done') 84 85 def test(self, test_suite_native_01_debug): 86 logging.info('Start running TestMainMixedDebug: test') 87 self.config = test_suite_native_01_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: Debugger.setMixedDebugEnabled 116 ################################################################################################################ 117 params = debugger.SetMixedDebugEnabledParams(enabled=True, mixed_stack_enabled=False) 118 await self.debugger_impl.send("Debugger.setMixedDebugEnabled", main_thread, params) 119 ################################################################################################################ 120 # main thread: Runtime.runIfWaitingForDebugger 121 ################################################################################################################ 122 await self.runtime_impl.send("Runtime.runIfWaitingForDebugger", main_thread) 123 ################################################################################################################ 124 # main thread: Debugger.scriptParsed 125 ################################################################################################################ 126 response = await self.debugger_impl.recv("Debugger.scriptParsed", main_thread) 127 assert response['params']['url'] == self.config['file_path']['entry_ability'] 128 assert response['params']['endLine'] == 0 129 ################################################################################################################ 130 # main thread: Debugger.paused 131 ################################################################################################################ 132 response = await self.debugger_impl.recv("Debugger.paused", main_thread) 133 assert response['params']['callFrames'][0]['url'] == self.config['file_path']['entry_ability'] 134 assert response['params']['reason'] == 'Break on start' 135 ################################################################################################################ 136 # main thread: Debugger.resume 137 ################################################################################################################ 138 await self.debugger_impl.send("Debugger.resume", main_thread) 139 ################################################################################################################ 140 # main thread: Debugger.scriptParsed 141 ################################################################################################################ 142 response = await self.debugger_impl.recv("Debugger.scriptParsed", main_thread) 143 assert response['params']['url'] == self.config['file_path']['index'] 144 assert response['params']['endLine'] == 0 145 ################################################################################################################ 146 # main thread: Debugger.paused 147 ################################################################################################################ 148 response = await self.debugger_impl.recv("Debugger.paused", main_thread) 149 assert response['params']['callFrames'][0]['url'] == self.config['file_path']['index'] 150 assert response['params']['reason'] == 'Break on start' 151 ################################################################################################################ 152 # main thread: Debugger.removeBreakpointsByUrl 153 ################################################################################################################ 154 params = debugger.RemoveBreakpointsUrl(self.config['file_path']['index']) 155 await self.debugger_impl.send("Debugger.removeBreakpointsByUrl", main_thread, params) 156 ################################################################################################################ 157 # main thread: Debugger.getPossibleAndSetBreakpointByUrl 158 ################################################################################################################ 159 locations = [debugger.BreakLocationUrl(url=self.config['file_path']['index'], line_number=53), 160 debugger.BreakLocationUrl(url=self.config['file_path']['index'], line_number=54)] 161 params = debugger.SetBreakpointsLocations(locations) 162 response = await self.debugger_impl.send("Debugger.getPossibleAndSetBreakpointsByUrl", 163 main_thread, params) 164 assert response['result']['locations'][0]['id'] == 'id:53:0:' + self.config['file_path']['index'] 165 assert response['result']['locations'][1]['id'] == 'id:54:0:' + self.config['file_path']['index'] 166 ################################################################################################################ 167 # main thread: Debugger.resume 168 ################################################################################################################ 169 await self.debugger_impl.send("Debugger.resume", main_thread) 170 ################################################################################################################ 171 # main thread: click on the screen 172 ################################################################################################################ 173 Application.click_on_middle() 174 ################################################################################################################ 175 # main thread: Debugger.paused, hit breakpoint 176 ################################################################################################################ 177 response = await self.debugger_impl.recv("Debugger.paused", main_thread) 178 assert response['params']['callFrames'][0]['url'] == self.config['file_path']['index'] 179 assert response['params']['hitBreakpoints'] == ['id:53:26:' + self.config['file_path']['index']] 180 ################################################################################################################ 181 # main thread: Debugger.stepInto 182 ################################################################################################################ 183 await self.debugger_impl.send("Debugger.stepInto", main_thread) 184 ################################################################################################################ 185 # main thread: Debugger.nativeCalling 186 ################################################################################################################ 187 response = await self.debugger_impl.recv("Debugger.nativeCalling", main_thread) 188 assert response['params']['isStepInto'] is True 189 ################################################################################################################ 190 # main thread: Debugger.replyNativeCalling 191 ################################################################################################################ 192 params = debugger.ReplyNativeCallingParams() 193 await self.debugger_impl.send("Debugger.replyNativeCalling", main_thread, params) 194 # main thread: Debugger.paused 195 response = await self.debugger_impl.recv("Debugger.paused", main_thread) 196 assert response['params']['callFrames'][0]['url'] == self.config['file_path']['index'] 197 assert response['params']['hitBreakpoints'] == ['id:54:26:' + self.config['file_path']['index']] 198 ################################################################################################################ 199 # main thread: Debugger.stepOver 200 ################################################################################################################ 201 await self.debugger_impl.send("Debugger.stepOver", main_thread) 202 # main thread: Debugger.paused 203 response = await self.debugger_impl.recv("Debugger.paused", main_thread) 204 assert response['params']['callFrames'][0]['url'] == self.config['file_path']['index'] 205 assert response['params']['hitBreakpoints'] == [] 206 ################################################################################################################ 207 # main thread: Debugger.resume 208 ################################################################################################################ 209 await self.debugger_impl.send("Debugger.resume", main_thread) 210 ################################################################################################################ 211 # main thread: Debugger.disable 212 ################################################################################################################ 213 await self.debugger_impl.send("Debugger.disable", main_thread) 214 ################################################################################################################ 215 # close the websocket connections 216 ################################################################################################################ 217 await websocket.send_msg_to_debugger_server(main_thread.instance_id, main_thread.send_msg_queue, 'close') 218 await websocket.send_msg_to_connect_server('close') 219 ################################################################################################################