• 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
28
29
30@pytest.mark.hot_reload
31@pytest.mark.timeout(120)
32class TestHotReload01:
33    """
34    测试用例:热重载基础功能, 重载文件impact HAR, 重载文件import HSP
35    测试步骤:
36        1、启动应用
37        2、点击屏幕中央,调用foo函数,输出日志:
38            Test Application HotReloadHarHsp01 => foo(): base function foo
39            Test Application HotReloadHarHsp01 => calc(): 8
40        3、触发热重载
41        4、Hilog输出:Load patch success!
42        5、点击屏幕中央,调用foo函数,输出日志:
43            Test Application HotReloadHarHsp01 => foo(): patch function foo
44            Test Application HotReloadHarHsp01 => calc(): 48
45    测试应用代码示例:
46        import {Sum} from '@ohos/HarLibrary'
47        import {Multiply} from '@ohos/HspDiscovery'
48        修改前:
49            .onClick(() => {
50              console.log('Test HotReload Application 01 => foo():', foo())
51              console.log('Test HotReload Application 01 => calc():', calc(1, 2))
52            })
53            function foo() {
54              return "base function foo"
55            }
56            function calc(x: number, y: number): number {
57              return Multiply(Sum(x, x), Sum(y, y))
58            }
59        修改后:HotReloadHarHsp01.hap
60            .onClick(() => {
61              console.log('Test HotReload Application 01 => foo():', foo())
62              console.log('Test HotReload Application 01 => calc():', calc(3, 4))
63            })
64            function foo() {
65              return "patch function foo"
66            }
67            function calc(x: number, y: number): number {
68              return Multiply(Sum(x, x), Sum(y, y))
69            }
70    """
71
72    def setup_method(self):
73        logging.info('Start running test_hot_reload_01: setup')
74
75        self.log_path = rf'{os.path.dirname(__file__)}\..\log'
76        self.hilog_file_name = 'test_hot_reload_01.hilog.txt'
77        self.hilog_file_path = os.path.join(self.log_path, self.hilog_file_name)
78        self.id_generator = Utils.message_id_generator()
79
80        # receive the hilog before the test start
81        Utils.clear_fault_log()
82        self.hilog_process, self.write_thread = Utils.save_hilog(log_path=self.log_path,
83                                                                 file_name=self.hilog_file_name,
84                                                                 debug_on=False)
85
86    def teardown_method(self):
87        Application.uninstall(self.config['bundle_name'])
88
89        # terminate the hilog receive process after the test done
90        time.sleep(3)
91        self.hilog_process.stdout.close()
92        self.hilog_process.terminate()
93        self.hilog_process.wait()
94        self.write_thread.join()
95
96        Utils.save_fault_log(log_path=self.log_path)
97        logging.info('test_hot_reload_01 done')
98
99    def test(self, test_suite_hotreload_har_hsp_01):
100        logging.info('Start running test_hot_reload_01: test')
101        self.config = test_suite_hotreload_har_hsp_01
102        ################################################################################################################
103        # trigger onclick event
104        ################################################################################################################
105        logging.info(f'{"=" * 30} Before Hot Reload {"=" * 30}')
106        Application.click_on_middle()
107        time.sleep(3)
108        matched_log = Utils.search_hilog(self.hilog_file_path, key_world=b'base function foo')
109        logging.info(matched_log)
110        assert len(matched_log) == 1
111        matched_log = Utils.search_hilog(self.hilog_file_path,
112                                         key_world=b'Test Application HotReloadHarHsp01 => calc(): 8')
113        logging.info(matched_log)
114        assert len(matched_log) == 1
115        ################################################################################################################
116        # perform Hot Reload
117        ################################################################################################################
118        logging.info(f'{"=" * 30} Perform Hot Reload {"=" * 30}')
119        Utils.hdc_file_send(source=self.config['local_hqf_entry_path'], sink=self.config['remote_hqf_entry_path'])
120        Application.hot_reload(self.config['remote_hqf_entry_path'])
121        time.sleep(3)
122        matched_log = Utils.search_hilog(self.hilog_file_path, key_world=b'Load patch success!')
123        logging.info(matched_log)
124        assert len(matched_log) == 1
125        ################################################################################################################
126        # after hot reload
127        ################################################################################################################
128        logging.info(f'{"=" * 30} After Hot Reload {"=" * 30}')
129        Application.click_on_middle()   # trigger onclick event
130        time.sleep(3)
131        matched_log = Utils.search_hilog(self.hilog_file_path, key_world=b'patch function foo')
132        logging.info(matched_log)
133        assert len(matched_log) == 1
134        matched_log = Utils.search_hilog(self.hilog_file_path,
135                                         key_world=b'Test Application HotReloadHarHsp01 => calc(): 48')
136        logging.info(matched_log)
137        assert len(matched_log) == 1
138        ################################################################################################################
139        # check if the application is running normally
140        ################################################################################################################
141        pid = Application.get_pid(self.config['bundle_name'])
142        assert pid == self.config['pid'], logging.error(f'App is no longer running with pid: {pid}')
143