• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 TestDebug21:
35    """
36    测试用例:单实例Sendable对象成员方法上普通断点的设置/取消
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.  关闭主线程 debugger server 和 connect server 连接
45    测试代码:
46    @Sendable
47    class TestClass {
48      desc: string = "XXXXXXXX";// 自定义设置类成员
49      taskNum: number = XXXXXXXX;
50    get getTaskNum(): number {// 自定义设置类成员方法
51      return this.taskNum;// 实例debug21断点
52    }
53    }
54    const testInstance = new TestClass();
55    let taskNumber = testInstance.getTaskNum;
56    """
57
58    def setup_method(self):
59        logging.info('Start running TestDebug21: setup')
60
61        self.log_path = rf'{os.path.dirname(__file__)}\..\log'
62        self.hilog_file_name = 'test_debug_21.hilog.txt'
63        self.id_generator = Utils.message_id_generator()
64
65        # receive the hilog before the test start
66        Utils.clear_fault_log()
67        self.hilog_process, self.write_thread = Utils.save_hilog(log_path=self.log_path,
68                                                                 file_name=self.hilog_file_name,
69                                                                 debug_on=True)
70
71    def teardown_method(self):
72        Application.uninstall(self.config['bundle_name'])
73
74        # terminate the hilog receive process after the test done
75        time.sleep(3)
76        self.hilog_process.stdout.close()
77        self.hilog_process.terminate()
78        self.hilog_process.wait()
79        self.write_thread.join()
80
81        Utils.save_fault_log(log_path=self.log_path)
82        logging.info('TestDebug21 done')
83
84    def test(self, test_suite_main_instance_02_debug):
85        logging.info('Start running TestDebug21: test')
86        self.config = test_suite_main_instance_02_debug
87        websocket = self.config['websocket']
88        taskpool = self.config['taskpool']
89        pid = self.config['pid']
90        self.debugger_impl = debugger_api.DebuggerImpl(self.id_generator, websocket)
91        self.runtime_impl = runtime_api.RuntimeImpl(self.id_generator, websocket)
92
93        taskpool.submit(websocket.main_task(taskpool, self.procedure, pid))
94        taskpool.await_taskpool()
95        taskpool.task_join()
96        if taskpool.task_exception:
97            raise taskpool.task_exception
98
99    async def procedure(self, websocket):
100        ################################################################################################################
101        # main thread: connect the debugger server
102        ################################################################################################################
103        main_thread = await self.debugger_impl.connect_to_debugger_server(self.config['pid'], True)
104        logging.info(f'Connect to the debugger server of instance: {main_thread.instance_id}')
105        ################################################################################################################
106        # main thread: Runtime.enable
107        ################################################################################################################
108        await self.runtime_impl.send("Runtime.enable", main_thread)
109        ################################################################################################################
110        # main thread: Debugger.enable
111        ################################################################################################################
112        await self.debugger_impl.send("Debugger.enable", main_thread)
113        ################################################################################################################
114        # main thread: Runtime.runIfWaitingForDebugger
115        ################################################################################################################
116        await self.runtime_impl.send("Runtime.runIfWaitingForDebugger", main_thread)
117        ################################################################################################################
118        # main thread: Debugger.scriptParsed
119        ################################################################################################################
120        response = await self.debugger_impl.recv("Debugger.scriptParsed", main_thread)
121        assert response['params']['url'] == self.config['file_path']['entry_ability']
122        assert response['params']['endLine'] == 0
123        ################################################################################################################
124        # main thread: Debugger.paused
125        ################################################################################################################
126        response = await self.debugger_impl.recv("Debugger.paused", main_thread)
127        assert response['params']['callFrames'][0]['url'] == self.config['file_path']['entry_ability']
128        assert response['params']['reason'] == 'Break on start'
129        ################################################################################################################
130        # main thread: Debugger.resume
131        ################################################################################################################
132        await self.debugger_impl.send("Debugger.resume", main_thread)
133        ################################################################################################################
134        # main thread: Debugger.scriptParsed
135        ################################################################################################################
136        response = await self.debugger_impl.recv("Debugger.scriptParsed", main_thread)
137        assert response['params']['url'] == self.config['file_path']['index']
138        assert response['params']['endLine'] == 0
139        ################################################################################################################
140        # main thread: Debugger.paused
141        ################################################################################################################
142        response = await self.debugger_impl.recv("Debugger.paused", main_thread)
143        assert response['params']['callFrames'][0]['url'] == self.config['file_path']['index']
144        assert response['params']['reason'] == 'Break on start'
145        ################################################################################################################
146        # main thread: Debugger.removeBreakpointsByUrl
147        ################################################################################################################
148        params = debugger.RemoveBreakpointsUrl(self.config['file_path']['index'])
149        await self.debugger_impl.send("Debugger.removeBreakpointsByUrl", main_thread, params)
150        ################################################################################################################
151        # main thread: Debugger.getPossibleAndSetBreakpointByUrl
152        ################################################################################################################
153        locations = [debugger.BreakLocationUrl(url=self.config['file_path']['index'], line_number=74)]
154        params = debugger.SetBreakpointsLocations(locations)
155        response = await self.debugger_impl.send("Debugger.getPossibleAndSetBreakpointsByUrl",
156                                                 main_thread, params)
157        assert response['result']['locations'][0]['id'] == 'id:74:0:' + self.config['file_path']['index']
158        ################################################################################################################
159        # main thread: Debugger.resume
160        ################################################################################################################
161        await self.debugger_impl.send("Debugger.resume", main_thread)
162        ################################################################################################################
163        # main thread: Debugger.paused, hit breakpoint
164        ################################################################################################################
165        response = await self.debugger_impl.recv("Debugger.paused", main_thread)
166        assert response['params']['callFrames'][0]['url'] == self.config['file_path']['index']
167        assert response['params']['hitBreakpoints'] == ['id:74:15:' + self.config['file_path']['index']]
168        ################################################################################################################
169        # main thread: Debugger.resume
170        ################################################################################################################
171        await self.debugger_impl.send("Debugger.resume", main_thread)
172        ###############################################################################################################
173        # main thread: click on the screen
174        ################################################################################################################
175        Application.click_on_middle()
176        ################################################################################################################
177        # main thread: Debugger.disable
178        ################################################################################################################
179        await self.debugger_impl.send("Debugger.disable", main_thread)
180        ################################################################################################################
181        # close the websocket connections
182        ################################################################################################################
183        await websocket.send_msg_to_debugger_server(main_thread.instance_id, main_thread.send_msg_queue, 'close')
184        await websocket.send_msg_to_connect_server('close')
185        ################################################################################################################