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().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().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_regex(completed_process.stdout, 59 r"""Intentional exception""", 60 msg=str(completed_process)) 61 self.expect_package_not_installed(test_app_package, 62 msg=str(completed_process)) 63 64 def run_test(self, test_app_package, test_app_module): 65 """Set up and run the launcher for a given test app.""" 66 67 # We don't check the return code since adb returns non-zero exit code if 68 # the package does not exist. 69 self.adb.uninstall(test_app_package, check=False) 70 self.assert_package_not_installed(test_app_package) 71 72 self.repo.add_package_apks(test_app_package, [test_app_module]) 73 74 file_resolver_class = 'com.android.csuite.config.AppRemoteFileResolver' 75 76 return self.harness.run_and_wait([ 77 '--serial', 78 csuite_test_utils.get_device_serial(), 79 'run', 80 'commandAndExit', 81 'csuite-app-launch', 82 '--enable-module-dynamic-download', 83 '--dynamic-download-args', 84 '%s:uri-template=file://%s/{package}' % 85 (file_resolver_class, self.repo.get_path()), 86 '--package', 87 test_app_package 88 ]) 89 90 def expect_regex(self, s, regex, msg=None): 91 with self.subTest(): 92 self.assertRegex(s, regex, msg=msg) 93 94 def assert_package_not_installed(self, package_name, msg=None): 95 self.assertNotIn(package_name, self.adb.list_packages(), msg=msg) 96 97 def expect_package_not_installed(self, package_name, msg=None): 98 with self.subTest(): 99 self.assert_package_not_installed(package_name, msg=msg) 100 101 def expect_app_launched(self, tag, msg=None): 102 logcat_process = self.adb.run(['logcat', '-d', '-v', 'brief', '-s', tag]) 103 with self.subTest(): 104 self.assertIn('App launched', logcat_process.stdout, msg=msg) 105 106 107if __name__ == '__main__': 108 csuite_test_utils.main() 109