1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
# Copyright 2019, The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""Unit tests for the AppRunner."""
import os
import sys
from pathlib import Path
from app_runner import AppRunner, AppRunnerListener
from mock import Mock, call, patch
# The path is "frameworks/base/startop/scripts/"
sys.path.append(Path(os.path.realpath(__file__)).parents[2])
import lib.cmd_utils as cmd_utils
class AppRunnerTestListener(AppRunnerListener):
def preprocess(self) -> None:
cmd_utils.run_shell_command('pre'),
def postprocess(self, pre_launch_timestamp: str) -> None:
cmd_utils.run_shell_command('post'),
def metrics_selector(self, am_start_output: str,
pre_launch_timestamp: str) -> None:
return 'TotalTime=123\n'
RUNNER = AppRunner(package='music',
activity='MainActivity',
compiler_filter='speed',
timeout=None,
simulate=False)
def test_configure_compiler_filter():
with patch('lib.cmd_utils.run_shell_command',
new_callable=Mock) as mock_run_shell_command:
mock_run_shell_command.return_value = (True, 'speed arm64 kUpToDate')
RUNNER.configure_compiler_filter()
calls = [call(os.path.realpath(
os.path.join(RUNNER.DIR,
'../query_compiler_filter.py')) + ' --package music')]
mock_run_shell_command.assert_has_calls(calls)
def test_parse_metrics_output():
input = 'a1=b1\nc1=d1\ne1=f1'
ret = RUNNER.parse_metrics_output(input)
assert ret == [('a1', 'b1'), ('c1', 'd1'), ('e1', 'f1')]
def _mocked_run_shell_command(*args, **kwargs):
if args[0] == 'adb shell "date -u +\'%Y-%m-%d %H:%M:%S.%N\'"':
return (True, "2019-07-02 23:20:06.972674825")
elif args[0] == 'adb shell ps | grep "music" | awk \'{print $2;}\'':
return (True, '9999')
else:
return (True, 'a1=b1\nc1=d1=d2\ne1=f1')
@patch('app_startup.lib.adb_utils.blocking_wait_for_logcat_displayed_time')
@patch('lib.cmd_utils.run_shell_command')
def test_run(mock_run_shell_command,
mock_blocking_wait_for_logcat_displayed_time):
mock_run_shell_command.side_effect = _mocked_run_shell_command
mock_blocking_wait_for_logcat_displayed_time.return_value = 123
test_listener = AppRunnerTestListener()
RUNNER.add_callbacks(test_listener)
result = RUNNER.run()
RUNNER.remove_callbacks(test_listener)
calls = [call('pre'),
call(os.path.realpath(
os.path.join(RUNNER.DIR,
'../query_compiler_filter.py')) +
' --package music'),
call('adb shell "date -u +\'%Y-%m-%d %H:%M:%S.%N\'"'),
call(
'timeout {timeout} "{DIR}/launch_application" "{package}" "{activity}"'
.format(timeout=30,
DIR=os.path.realpath(os.path.dirname(RUNNER.DIR)),
package='music',
activity='MainActivity',
timestamp='2019-07-02 23:20:06.972674825')),
call('post')
]
mock_run_shell_command.assert_has_calls(calls)
assert result == [('TotalTime', '123')]
assert len(RUNNER.listeners) == 0
|