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