• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Lint as: python3
2#
3# Copyright 2020, The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16"""Tests C-Suite's crash detection behavior."""
17
18import csuite_test_utils
19
20
21class CrashDetectionTest(csuite_test_utils.TestCase):
22
23  def setUp(self):
24    super(CrashDetectionTest, self).setUp()
25    self.adb = csuite_test_utils.Adb()
26    self.repo = csuite_test_utils.PackageRepository()
27    self.harness = csuite_test_utils.CSuiteHarness()
28
29  def tearDown(self):
30    super(CrashDetectionTest, self).tearDown()
31    self.harness.cleanup()
32    self.repo.cleanup()
33
34  def test_no_crash_test_passes(self):
35    test_app_package = 'android.csuite.nocrashtestapp'
36    self.adb.run(['logcat', '-c'])
37
38    completed_process = self.run_test(
39        test_app_package=test_app_package,
40        test_app_module='csuite_no_crash_test_app')
41
42    self.expect_regex(completed_process.stdout, r"""PASSED\s*:\s*1""",
43                      msg=str(completed_process))
44    self.expect_app_launched(test_app_package, msg=str(completed_process))
45    self.expect_package_not_installed(test_app_package,
46                                      msg=str(completed_process))
47
48  def test_crash_on_launch_test_fails(self):
49    test_app_package = 'android.csuite.crashonlaunchtestapp'
50    self.adb.run(['logcat', '-c'])
51
52    completed_process = self.run_test(
53        test_app_package=test_app_package,
54        test_app_module='csuite_crash_on_launch_test_app')
55
56    self.expect_regex(completed_process.stdout, r"""FAILED\s*:\s*1""",
57                      msg=str(completed_process))
58    self.expect_app_launched(test_app_package, msg=str(completed_process))
59    self.expect_package_not_installed(test_app_package,
60                                      msg=str(completed_process))
61
62  def run_test(self, test_app_package, test_app_module):
63    """Set up and run the launcher for a given test app."""
64
65    # We don't check the return code since adb returns non-zero exit code if
66    # the package does not exist.
67    self.adb.uninstall(test_app_package, check=False)
68    self.assert_package_not_installed(test_app_package)
69
70    self.repo.add_package_apks(
71        test_app_package, csuite_test_utils.get_test_app_apks(test_app_module))
72
73    file_resolver_class = 'com.android.csuite.config.AppRemoteFileResolver'
74
75    return self.harness.run_and_wait([
76        '--serial',
77        csuite_test_utils.get_device_serial(),
78        'run',
79        'commandAndExit',
80        'csuite-app-launch',
81        '--enable-module-dynamic-download',
82        '--dynamic-download-args',
83        '%s:uri-template=file://%s/{package}' %
84        (file_resolver_class, self.repo.get_path()),
85        '--package',
86        test_app_package
87    ])
88
89  def expect_regex(self, s, regex, msg=None):
90    with self.subTest():
91      self.assertRegex(s, regex, msg=msg)
92
93  def assert_package_not_installed(self, package_name, msg=None):
94    self.assertNotIn(package_name, self.adb.list_packages(), msg=msg)
95
96  def expect_package_not_installed(self, package_name, msg=None):
97    with self.subTest():
98      self.assert_package_not_installed(package_name, msg=msg)
99
100  def expect_app_launched(self, tag, msg=None):
101    logcat_process = self.adb.run(['logcat', '-d', '-v', 'brief', '-s', tag])
102    with self.subTest():
103      self.assertIn('App launched', logcat_process.stdout, msg=msg)
104
105
106if __name__ == '__main__':
107  csuite_test_utils.main()
108