• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (C) 2025 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#      http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15set -e -o pipefail
16
17# we pass "--adb_flags=<>" option from Bazel test invocation, e.g.:
18# bazel test :my_instrumentation_test --test_arg --adb_flags="-s emulator-5554"
19# Bazel passes this option into the args of this script.
20# We support only one option, named "adb_flags" and validate it here.
21ADB_FLAGS=""
22if [[ -n "$1" ]]; then
23  ARG_PREFIX="--adb_flags="
24  ARG_PREFIX_REGEX="^$ARG_PREFIX"
25  if [[ "$1" =~ $ARG_PREFIX_REGEX ]]; then
26    ARG="$1"
27    ADB_FLAGS=${ARG#*"$ARG_PREFIX"}
28  else
29    echo "Unexpected script argument '$1'"
30    exit 1
31  fi
32fi
33
34readonly ADB_FLAGS
35
36readonly adb_tool_path="%%adb_tool_path%%"
37readonly appt2_tool_path="%%appt2_tool_path%%"
38
39readonly apk_path="%%apk_path%%"
40readonly test_apk_path="%%test_apk_path%%"
41
42readonly TEST_RUNNER_CLASS="androidx.test.runner.AndroidJUnitRunner"
43
44function call_adb() {
45  # we don't wrap $ADB_FLAGS in quotes, since it may contains a list of strings
46  # (e.g "-s emulator-5554"), and we want it to expand to the list of strings
47  # when passing it as args to the adb tool.
48  # shellcheck disable=SC2086
49  "${adb_tool_path}" $ADB_FLAGS "$@"
50}
51
52apk_package=$("${appt2_tool_path}" dump packagename "${apk_path}")
53test_apk_package=$("${appt2_tool_path}" dump packagename "${test_apk_path}")
54
55function filter_adb_output() {
56  # Version of adb used in tests may differ from the system one, so we filter
57  # out lines related to the version mismatch and successfully daemon restarts.
58  # The output can as follows, we want to keep only the last line:
59  # adb server version (41) doesn't match this client (39); killing...
60  # * daemon started successfully *
61  # adb server version (41) doesn't match this client (39); killing...
62  # * daemon started successfully *
63  # Failure [DELETE_FAILED_INTERNAL_ERROR]
64  echo -n "$1" | grep -v "^adb server version (" | grep -v "^* daemon started successfully"
65}
66
67function call_adb_uninstall() {
68  local NO_PACKAGE_INSTALLED="Failure [DELETE_FAILED_INTERNAL_ERROR]"
69  readonly NO_PACKAGE_INSTALLED
70  local apk_pkg="$1"
71
72  local OUTPUT
73  # adb uninstall exits with error if there is no package 'apk_pkg' installed,
74  # this is fine for us: we try to delete a package first even if it is not
75  # installed.
76  set +e
77  OUTPUT=$(call_adb uninstall "${apk_pkg}" 2>&1)
78  set -e
79
80  local FILTERED_OUTPUT
81  FILTERED_OUTPUT=$(filter_adb_output "$OUTPUT")
82  if [[ "$FILTERED_OUTPUT" != "$NO_PACKAGE_INSTALLED" ]] && [[ "$FILTERED_OUTPUT" != "Success" ]]; then
83    # Print the whole output
84    echo "adb uninstall error: '${OUTPUT}'"
85    exit 1
86  fi
87}
88
89function check_connected_devices() {
90  local DEVICES_OUTPUT
91  local FILTERED_DEVICES_OUTPUT
92  DEVICES_OUTPUT=$(call_adb devices)
93  FILTERED_DEVICES_OUTPUT=$(filter_adb_output "$DEVICES_OUTPUT")
94  # Expected filtered output:
95  # List of devices attached
96  # emulator-5554   device
97  if [[ "$FILTERED_DEVICES_OUTPUT" == "List of devices attached" ]]; then
98    # If output contains only header line then there is no connected devices
99    echo "Test Error: No connected devices"
100    exit 1
101  fi
102}
103
104check_connected_devices
105
106call_adb_uninstall "${apk_package}"
107
108call_adb_uninstall "${test_apk_package}"
109
110call_adb install "${apk_path}"
111
112call_adb install "${test_apk_path}"
113
114TEST_OUTPUT=$(call_adb shell am instrument -w "${test_apk_package}/${TEST_RUNNER_CLASS}")
115
116echo "'adb shell am instrument' output:"
117echo "${TEST_OUTPUT}"
118
119# TODO(ktimofeev): Run 'am instrument' with '-r' flag and parse output.
120if [[ "${TEST_OUTPUT}" =~ "FAILURES!!!" ]] ||
121   [[ "${TEST_OUTPUT}" =~ "INSTRUMENTATION_RESULT: shortMsg=Process crashed" ]] ||
122   [[ "${TEST_OUTPUT}" =~ "INSTRUMENTATION_FAILED" ]]; then
123  exit 1
124fi