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: Fixtures of pytest.
18"""
19
20import logging
21import os
22
23import pytest
24
25from aw import Application, Fport, WebSocket, TaskPool
26
27
28@pytest.fixture(scope='class')
29def test_suite_worker_01():
30    logging.info('running worker_01 in default mode')
31    config = {
32        'start_mode': None,
33        'connect_server_port': 15678,
34        'debugger_server_port': 15679,
35        'bundle_name': 'com.example.multiWorker01',
36        'hap_name': 'MultiWorker01.hap',
37        'hap_path': rf'{os.path.dirname(__file__)}\..\resource\MultiWorker01.hap',
38        'file_path': {
39            'entry_ability': 'entry|entry|1.0.0|src/main/ets/entryability/EntryAbility.ts',
40            'index': 'entry|entry|1.0.0|src/main/ets/pages/Index.ts',
41            'worker': 'entry|entry|1.0.0|src/main/ets/workers/Worker.ts'
42        }
43    }
44    launch_hap(config)
45    return config
46
47
48@pytest.fixture(scope='class')
49def test_suite_worker_01_debug():
50    logging.info('running worker_01 in debug mode')
51    config = {
52        'start_mode': '-D',
53        'connect_server_port': 15678,
54        'debugger_server_port': 15679,
55        'bundle_name': 'com.example.multiWorker01',
56        'hap_name': 'MultiWorker01.hap',
57        'hap_path': rf'{os.path.dirname(__file__)}\..\resource\MultiWorker01.hap',
58        'file_path': {
59            'entry_ability': 'entry|entry|1.0.0|src/main/ets/entryability/EntryAbility.ts',
60            'index': 'entry|entry|1.0.0|src/main/ets/pages/Index.ts',
61            'worker': 'entry|entry|1.0.0|src/main/ets/workers/Worker.ts'
62        }
63    }
64    launch_hap(config)
65    return config
66
67
68@pytest.fixture(scope='class')
69def test_suite_worker_02():
70    logging.info('running worker_02 in default mode')
71    config = {
72        'start_mode': None,
73        'connect_server_port': 15680,
74        'debugger_server_port': 15681,
75        'bundle_name': 'com.example.multiWorker02',
76        'hap_name': 'MultiWorker02.hap',
77        'hap_path': rf'{os.path.dirname(__file__)}\..\resource\MultiWorker02.hap'
78    }
79    launch_hap(config)
80    return config
81
82
83@pytest.fixture(scope='class')
84def test_suite_worker_02_profile():
85    logging.info('running worker_02 in profile mode')
86    config = {
87        'start_mode': '-p profile jsperf',
88        'connect_server_port': 15680,
89        'debugger_server_port': 15681,
90        'bundle_name': 'com.example.multiWorker02',
91        'hap_name': 'MultiWorker02.hap',
92        'hap_path': rf'{os.path.dirname(__file__)}\..\resource\MultiWorker02.hap'
93    }
94    launch_hap(config)
95    return config
96
97
98@pytest.fixture(scope='class')
99def test_suite_taskpool_01():
100    logging.info('running taskpool_01 in default mode')
101    config = {
102        'start_mode': None,
103        'connect_server_port': 15682,
104        'debugger_server_port': 15683,
105        'bundle_name': 'com.example.taskPool01',
106        'hap_name': 'TaskPool01.hap',
107        'hap_path': rf'{os.path.dirname(__file__)}\..\resource\TaskPool01.hap',
108        'file_path': {
109            'entry_ability': 'entry|entry|1.0.0|src/main/ets/entryability/EntryAbility.ts',
110            'index': 'entry|entry|1.0.0|src/main/ets/pages/Index.ts',
111        }
112    }
113    launch_hap(config)
114    return config
115
116
117@pytest.fixture(scope='class')
118def test_suite_taskpool_01_debug():
119    logging.info('running taskpool_01 in debug mode')
120    config = {
121        'start_mode': '-D',
122        'connect_server_port': 15682,
123        'debugger_server_port': 15683,
124        'bundle_name': 'com.example.taskPool01',
125        'hap_name': 'TaskPool01.hap',
126        'hap_path': rf'{os.path.dirname(__file__)}\..\resource\TaskPool01.hap',
127        'file_path': {
128            'entry_ability': 'entry|entry|1.0.0|src/main/ets/entryability/EntryAbility.ts',
129            'index': 'entry|entry|1.0.0|src/main/ets/pages/Index.ts',
130        }
131    }
132    launch_hap(config)
133    return config
134
135
136def launch_hap(config):
137    pid = Application.launch_application(config['bundle_name'], config['hap_path'], start_mode=config['start_mode'])
138    assert pid != 0, logging.error(f'Pid of {config["hap_name"]} is 0!')
139    config['pid'] = pid
140
141    Fport.clear_fport()
142    connect_server_port = Fport.fport_connect_server(config['connect_server_port'],
143                                                     config['pid'],
144                                                     config['bundle_name'])
145    assert connect_server_port > 0, logging.error('Failed to fport connect server for 3 times, '
146                                                  'the port is very likely occupied')
147    config['connect_server_port'] = connect_server_port
148
149    config['websocket'] = WebSocket(config['connect_server_port'], config['debugger_server_port'])
150    config['taskpool'] = TaskPool()