• 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 TestHotReload02:
33    """
34    测试用例:热重载_重载页面未启动
35    测试步骤:
36        1、启动应用,加载Index.ets页面
37        2、修改Index.ets,触发热重载,输出日志:
38            Test Application HotReloadPages01::Index => fontSize: 30
39        3、修改Index.ets和跳转页面Mine.ets,触发热重载,输出日志:
40            Test Application HotReloadPages01::Index => fontSize: 10
41        4、点击Index.ets屏幕中央,跳转到Mine.ets页面,点击Mine.ets页面屏幕中央,输出日志:
42            Test Application HotReloadPages01::Mine => add(): 3
43
44    测试应用代码示例:
45        修改前:
46            Index.ets
47                .fontSize(getFontSize())
48                .onClick(() => {
49                  router.pushUrl({'url': 'pages/Mine'})
50                })
51                function getFontSize() {
52                  let fontSize = 50
53                  console.log("Test Application HotReloadPages01::Index => fontSize:", fontSize);
54                  return fontSize
55                }
56            Mine.ets
57                .onClick(() => {
58                  console.log("Test Application HotReloadPages01::Mine => add():", add(1, 2));
59                })
60        第一次修改:HotReloadPages01.01.hqf
61            Index.ets
62                .fontSize(getFontSize())
63                .onClick(() => {
64                  router.pushUrl({'url': 'pages/Mine'})
65                })
66                function getFontSize() {
67                  let fontSize = 30
68                  console.log("Test Application HotReloadPages01::Index => fontSize:", fontSize);
69                  return fontSize
70                }
71        第二次修改:HotReloadPages01.02.hqf
72            Index.ets
73                .fontSize(getFontSize())
74                .onClick(() => {
75                  router.pushUrl({'url': 'pages/Mine'})
76                })
77                function getFontSize() {
78                  let fontSize = 10
79                  console.log("Test Application HotReloadPages01::Index => fontSize:", fontSize);
80                  return fontSize
81                }
82            Mine.ets
83                .onClick(() => {
84                  console.log("Test Application HotReloadPages01::Mine => add():", add(12, 34));
85                })
86    """
87
88    def setup_method(self):
89        logging.info('Start running test_hot_reload_02: setup')
90
91        self.log_path = rf'{os.path.dirname(__file__)}\..\log'
92        self.hilog_file_name = 'test_hot_reload_02.hilog.txt'
93        self.hilog_file_path = os.path.join(self.log_path, self.hilog_file_name)
94        self.id_generator = Utils.message_id_generator()
95
96        # receive the hilog before the test start
97        Utils.clear_fault_log()
98        self.hilog_process, self.write_thread = Utils.save_hilog(log_path=self.log_path,
99                                                                 file_name=self.hilog_file_name,
100                                                                 debug_on=False)
101
102    def teardown_method(self):
103        Application.uninstall(self.config['bundle_name'])
104
105        # terminate the hilog receive process after the test done
106        time.sleep(3)
107        self.hilog_process.stdout.close()
108        self.hilog_process.terminate()
109        self.hilog_process.wait()
110        self.write_thread.join()
111
112        Utils.save_fault_log(log_path=self.log_path)
113        logging.info('test_hot_reload_02 done')
114
115    def test(self, test_suite_hotreload_pages_01):
116        logging.info('Start running test_hot_reload_02: test')
117        self.config = test_suite_hotreload_pages_01
118        ################################################################################################################
119        # 1st hot reload
120        ################################################################################################################
121        logging.info(f'{"=" * 30} 1st Hot Reload {"=" * 30}')
122        Utils.hdc_file_send(source=self.config['local_hqf_01_path'], sink=self.config['remote_hqf_01_path'])
123        Application.hot_reload(self.config['remote_hqf_01_path'])
124        time.sleep(3)
125        matched_log = Utils.search_hilog(self.hilog_file_path,
126                                         key_world=b"Test Application HotReloadPages01::Index => fontSize: 30")
127        logging.info(matched_log)
128        assert len(matched_log) == 2
129        ################################################################################################################
130        # 2nd hot reload
131        ################################################################################################################
132        logging.info(f'{"=" * 30} 2nd Hot Reload {"=" * 30}')
133        Utils.hdc_file_send(source=self.config['local_hqf_02_path'], sink=self.config['remote_hqf_02_path'])
134        Application.hot_reload(self.config['remote_hqf_02_path'])
135        time.sleep(3)
136        matched_log = Utils.search_hilog(self.hilog_file_path,
137                                         key_world=b"Test Application HotReloadPages01::Index => fontSize: 10")
138        logging.info(matched_log)
139        assert len(matched_log) == 2
140
141        Application.click_on_middle()   # jump to the Mine page
142        time.sleep(3)
143        Application.click_on_middle()   # trigger onclick event
144        time.sleep(3)
145        matched_log = Utils.search_hilog(self.hilog_file_path,
146                                         key_world=b"Test Application HotReloadPages01::Mine => add(): 3")
147        logging.info(matched_log)
148        assert len(matched_log) == 1
149        ################################################################################################################
150        # check if the application is running normally
151        ################################################################################################################
152        pid = Application.get_pid(self.config['bundle_name'])
153        assert pid == self.config['pid'], logging.error(f'App is no longer running with pid: {pid}')
154