aboutsummaryrefslogtreecommitdiff
path: root/integration_tests/csuite_crash_detection_test.py
blob: 9dd8a00f587c2639b5b5152ae31b4d25681ad48e (plain)
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
# Lint as: python3
#
# Copyright 2020, 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.
"""Tests C-Suite's crash detection behavior."""

import csuite_test_utils


class CrashDetectionTest(csuite_test_utils.TestCase):

  def setUp(self):
    super(CrashDetectionTest, self).setUp()
    self.adb = csuite_test_utils.Adb()
    self.repo = csuite_test_utils.PackageRepository()
    self.harness = csuite_test_utils.CSuiteHarness()

  def tearDown(self):
    super(CrashDetectionTest, self).tearDown()
    self.harness.cleanup()
    self.repo.cleanup()

  def test_no_crash_test_passes(self):
    test_app_package = 'android.csuite.nocrashtestapp'
    self.adb.run(['logcat', '-c'])

    completed_process = self.run_test(
        test_app_package=test_app_package,
        test_app_module='csuite_no_crash_test_app')

    self.expect_regex(completed_process.stdout, r"""PASSED\s*:\s*1""")
    self.expect_app_launched(test_app_package)
    self.expect_package_not_installed(test_app_package)

  def test_crash_on_launch_test_fails(self):
    test_app_package = 'android.csuite.crashonlaunchtestapp'
    self.adb.run(['logcat', '-c'])

    completed_process = self.run_test(
        test_app_package=test_app_package,
        test_app_module='csuite_crash_on_launch_test_app')

    self.expect_regex(completed_process.stdout, r"""FAILED\s*:\s*1""")
    self.expect_app_launched(test_app_package)
    self.expect_package_not_installed(test_app_package)

  def run_test(self, test_app_package, test_app_module):
    """Set up and run the launcher for a given test app."""

    # We don't check the return code since adb returns non-zero exit code if
    # the package does not exist.
    self.adb.uninstall(test_app_package, check=False)
    self.assert_package_not_installed(test_app_package)

    module_name = self.harness.add_module(test_app_package)
    self.repo.add_package_apks(
        test_app_package, csuite_test_utils.get_test_app_apks(test_app_module))

    file_resolver_class = 'com.android.csuite.config.AppRemoteFileResolver'

    return self.harness.run_and_wait([
        '--serial',
        csuite_test_utils.get_device_serial(),
        'run',
        'commandAndExit',
        'launch',
        '-m',
        module_name,
        '--enable-module-dynamic-download',
        '--dynamic-download-args',
        '%s:uri-template=file://%s/{package}' %
        (file_resolver_class, self.repo.get_path())
    ])

  def expect_regex(self, s, regex):
    with self.subTest():
      self.assertRegex(s, regex)

  def assert_package_not_installed(self, package_name):
    self.assertNotIn(package_name, self.adb.list_packages())

  def expect_package_not_installed(self, package_name):
    with self.subTest():
      self.assert_package_not_installed(package_name)

  def expect_app_launched(self, tag):
    logcat_process = self.adb.run(['logcat', '-d', '-v', 'brief', '-s', tag])
    with self.subTest():
      self.assertIn('App launched', logcat_process.stdout)


if __name__ == '__main__':
  csuite_test_utils.main()