blob: 4f1792fd74235af690069b6f4974d22ccb7255a6 [file] [log] [blame] [view]
andybons3322f762015-08-24 21:37:091# Android Test Instructions
2
andybons3322f762015-08-24 21:37:093[TOC]
4
jbudorick25c17382016-08-03 18:53:075## Device Setup
6
7### Physical Device Setup
8
Matt Falkenhagen3473e2e2018-12-03 03:40:509#### Root Access
10
Andrew Grievedb31fb9af2019-05-23 20:53:4811Running tests requires root access, which requires using a userdebug build on
12your device.
Matt Falkenhagen3473e2e2018-12-03 03:40:5013
14To use a userdebug build, see
15[Running Builds](https://source.android.com/setup/build/running.html). Googlers
16can refer to [this page](https://goto.google.com/flashdevice).
17
Andrew Grievedb31fb9af2019-05-23 20:53:4818If you can't run "adb root", you will get an error when trying to install
19the test APKs like "adb: error: failed to copy" and
20"remote secure_mkdirs failed: Operation not permitted" (use "adb unroot" to
21return adb to normal).
Matt Falkenhagen3473e2e2018-12-03 03:40:5022
jbudorick25c17382016-08-03 18:53:0723#### ADB Debugging
andybons3322f762015-08-24 21:37:0924
Andrew Grieveb747e712017-11-23 16:58:3125The adb executable exists within the Android SDK:
26
27```shell
Yun Liuf57cceaf2019-03-18 21:31:2328third_party/android_sdk/public/platform-tools/adb
Andrew Grieveb747e712017-11-23 16:58:3129```
30
andybons3322f762015-08-24 21:37:0931In order to allow the ADB to connect to the device, you must enable USB
32debugging:
andybons22afb312015-08-31 02:24:5133
Andrew Grievedb31fb9af2019-05-23 20:53:4834* Developer options are hidden by default. To unhide them:
andybons22afb312015-08-31 02:24:5135 * Go to "About phone"
36 * Tap 10 times on "Build number"
37 * The "Developer options" menu will now be available.
38 * Check "USB debugging".
39 * Un-check "Verify apps over USB".
andybons3322f762015-08-24 21:37:0940
jbudorick25c17382016-08-03 18:53:0741#### Screen
andybons3322f762015-08-24 21:37:0942
ntfschr09699f442017-01-12 22:20:4643You **must** ensure that the screen stays on while testing: `adb shell svc power
44stayon usb` Or do this manually on the device: Settings -> Developer options ->
45Stay Awake.
andybons3322f762015-08-24 21:37:0946
47If this option is greyed out, stay awake is probably disabled by policy. In that
48case, get another device or log in with a normal, unmanaged account (because the
49tests will break in exciting ways if stay awake is off).
50
jbudorick25c17382016-08-03 18:53:0751#### Disable Verify Apps
andybons3322f762015-08-24 21:37:0952
ntfschr09699f442017-01-12 22:20:4653You may see a dialog like [this
54one](http://www.samsungmobileusa.com/simulators/ATT_GalaxyMega/mobile/screens/06-02_12.jpg),
andybons3322f762015-08-24 21:37:0955which states, _Google may regularly check installed apps for potentially harmful
56behavior._ This can interfere with the test runner. To disable this dialog, run:
ntfschr09699f442017-01-12 22:20:4657
Nate Fischer93c59152019-02-05 01:25:0858```shell
ntfschr09699f442017-01-12 22:20:4659adb shell settings put global package_verifier_enable 0
60```
andybons3322f762015-08-24 21:37:0961
Anthony Berent8d3ce022018-01-15 12:24:3662### Using Emulators
andybons3322f762015-08-24 21:37:0963
Ryan Sleevie059091a2019-11-22 19:23:5164Running tests on emulators is the same as [on device](#Running-Tests). Refer to
Andrew Grievedb31fb9af2019-05-23 20:53:4865[android_emulator.md](../android_emulator.md) for setting up emulators.
andybons3322f762015-08-24 21:37:0966
67## Building Tests
68
jbudorick25c17382016-08-03 18:53:0769If you're adding a new test file, you'll need to explicitly add it to a gn
ntfschr09699f442017-01-12 22:20:4670target. If you're adding a test to an existing file, you won't need to make gn
jbudorick25c17382016-08-03 18:53:0771changes, but you may be interested in where your test winds up. In either case,
72here are some guidelines for where a test belongs:
andybons3322f762015-08-24 21:37:0973
jbudorick25c17382016-08-03 18:53:0774### C++
andybons3322f762015-08-24 21:37:0975
jbudorick25c17382016-08-03 18:53:0776C++ test files typically belong in `<top-level directory>_unittests` (e.g.
77`base_unittests` for `//base`). There are a few exceptions -- browser tests are
78typically their own target (e.g. `content_browsertests` for `//content`, or
79`browser_tests` for `//chrome`), and some unit test suites are broken at the
80second directory rather than the top-level one.
81
82### Java
83
84Java test files vary a bit more widely than their C++ counterparts:
85
ntfschr09699f442017-01-12 22:20:4686- Instrumentation test files -- i.e., tests that will run on a device --
87 typically belong in either `<top-level directory>_javatests` or `<top-level
88 directory>_test_java`. Regardless, they'll wind up getting packaged into one
89 of a few test APKs:
ctzsm34f54d62017-04-21 17:04:0790 - `webview_instrumentation_test_apk` for anything in `//android_webview`
ntfschr09699f442017-01-12 22:20:4691 - `content_shell_test_apk` for anything in `//content` or below
92 - `chrome_public_test_apk` for most things in `//chrome`
ntfschr09699f442017-01-12 22:20:4693- JUnit or Robolectric test files -- i.e., tests that will run on the host --
94 typically belong in `<top-level directory>_junit_tests` (e.g.
95 `base_junit_tests` for `//base`), though here again there are cases
96 (particularly in `//components`) where suites are split at the second
97 directory rather than the top-level one.
andybons3322f762015-08-24 21:37:0998
99Once you know what to build, just do it like you normally would build anything
newt17e4d242015-08-27 09:07:26100else, e.g.: `ninja -C out/Release chrome_public_test_apk`
andybons3322f762015-08-24 21:37:09101
102## Running Tests
103
jbudorick25c17382016-08-03 18:53:07104All functional tests should be runnable via the wrapper scripts generated at
105build time:
106
107```sh
108<output directory>/bin/run_<target_name> [options]
109```
110
111Note that tests are sharded across all attached devices unless explicitly told
112to do otherwise by `-d/--device`.
andybons3322f762015-08-24 21:37:09113
114The commands used by the buildbots are printed in the logs. Look at
xiaoyin.l1003c0b2016-12-06 02:51:17115https://build.chromium.org/ to duplicate the same test command as a particular
andybons3322f762015-08-24 21:37:09116builder.
117
jbudorick25c17382016-08-03 18:53:07118### INSTALL\_FAILED\_CONTAINER\_ERROR or INSTALL\_FAILED\_INSUFFICIENT\_STORAGE
andybons3322f762015-08-24 21:37:09119
jbudorick25c17382016-08-03 18:53:07120If you see this error when the test runner is attempting to deploy the test
andybons3322f762015-08-24 21:37:09121binaries to the AVD emulator, you may need to resize your userdata partition
122with the following commands:
123
124```shell
davve7ae32cd2015-09-22 06:54:09125# Resize userdata partition to be 1G
mikecase7c263052017-03-30 23:46:11126resize2fs android_emulator_sdk/sdk/system-images/android-25/x86/userdata.img 1G
andybons3322f762015-08-24 21:37:09127
128# Set filesystem parameter to continue on errors; Android doesn't like some
129# things e2fsprogs does.
mikecase7c263052017-03-30 23:46:11130tune2fs -e continue android_emulator_sdk/sdk/system-images/android-25/x86/userdata.img
andybons3322f762015-08-24 21:37:09131```
132
David Van Cleve44e5cb52020-10-23 23:57:04133### AdbCommandFailedError: failed to stat remote object
134
135There's a known issue (https://crbug.com/1094062) where the unit test binaries can fail on
136Android R and later: if you see this error, try rerunning on an Android version
137with API level <= 29 (Android <= Q).
138
andybons3322f762015-08-24 21:37:09139## Symbolizing Crashes
140
ntfschr09699f442017-01-12 22:20:46141Crash stacks are logged and can be viewed using `adb logcat`. To symbolize the
jyasskinc1c76ff2015-12-18 21:39:52142traces, define `CHROMIUM_OUTPUT_DIR=$OUTDIR` where `$OUTDIR` is the argument you
143pass to `ninja -C`, and pipe the output through
144`third_party/android_platform/development/scripts/stack`. If
145`$CHROMIUM_OUTPUT_DIR` is unset, the script will search `out/Debug` and
146`out/Release`. For example:
147
148```shell
149# If you build with
150ninja -C out/Debug chrome_public_test_apk
151# You can run:
152adb logcat -d | third_party/android_platform/development/scripts/stack
153
154# If you build with
155ninja -C out/android chrome_public_test_apk
156# You can run:
157adb logcat -d | CHROMIUM_OUTPUT_DIR=out/android third_party/android_platform/development/scripts/stack
158# or
159export CHROMIUM_OUTPUT_DIR=out/android
160adb logcat -d | third_party/android_platform/development/scripts/stack
161```
andybons3322f762015-08-24 21:37:09162
mlamouri0fbd6cd2015-10-26 12:08:11163## JUnit tests
164
165JUnit tests are Java unittests running on the host instead of the target device.
166They are faster to run and therefore are recommended over instrumentation tests
167when possible.
168
169The JUnits tests are usually following the pattern of *target*\_junit\_tests,
170for example, `content_junit_tests` and `chrome_junit_tests`.
171
172When adding a new JUnit test, the associated `BUILD.gn` file must be updated.
173For example, adding a test to `chrome_junit_tests` requires to update
Daniel Bratellf73f0df2018-09-24 13:52:49174`chrome/android/BUILD.gn`.
mlamouri0fbd6cd2015-10-26 12:08:11175
176```shell
177# Build the test suite.
Andrew Grieve4fe99742017-11-23 19:43:16178ninja -C out/Default chrome_junit_tests
mlamouri0fbd6cd2015-10-26 12:08:11179
180# Run the test suite.
Andrew Walbranca9cad942020-06-21 14:18:50181out/Default/bin/run_chrome_junit_tests
mlamouri0fbd6cd2015-10-26 12:08:11182
183# Run a subset of tests. You might need to pass the package name for some tests.
Andrew Walbranca9cad942020-06-21 14:18:50184out/Default/bin/run_chrome_junit_tests -f "org.chromium.chrome.browser.media.*"
Andrew Grieve4fe99742017-11-23 19:43:16185```
186
187### Debugging
188
Andrew Grievedb31fb9af2019-05-23 20:53:48189Similar to [debugging apk targets](../android_debugging_instructions.md#debugging-java):
Andrew Grieve4fe99742017-11-23 19:43:16190
191```shell
192out/Default/bin/run_chrome_junit_tests --wait-for-java-debugger
193out/Default/bin/run_chrome_junit_tests --wait-for-java-debugger # Specify custom port via --debug-socket=9999
mlamouri0fbd6cd2015-10-26 12:08:11194```
195
andybons3322f762015-08-24 21:37:09196## Gtests
197
198```shell
199# Build a test suite
jbudorick25c17382016-08-03 18:53:07200ninja -C out/Release content_unittests
andybons3322f762015-08-24 21:37:09201
202# Run a test suite
jbudorick9472f112016-01-27 16:21:56203out/Release/bin/run_content_unittests [-vv]
andybons3322f762015-08-24 21:37:09204
Andrew Grievedb31fb9af2019-05-23 20:53:48205# Run a subset of tests and enable some "please go faster" options:
206out/Release/bin/run_content_unittests --fast-local-dev -f "ByteStreamTest.*"
andybons3322f762015-08-24 21:37:09207```
208
209## Instrumentation Tests
210
211In order to run instrumentation tests, you must leave your device screen ON and
212UNLOCKED. Otherwise, the test will timeout trying to launch an intent.
213Optionally you can disable screen lock under Settings -> Security -> Screen Lock
214-> None.
215
jbudorick9472f112016-01-27 16:21:56216Next, you need to build the app, build your tests, and then run your tests
217(which will install the APK under test and the test APK automatically).
andybons3322f762015-08-24 21:37:09218
219Examples:
220
221ContentShell tests:
222
223```shell
Andrew Grievedb31fb9af2019-05-23 20:53:48224# Build the tests:
andybons3322f762015-08-24 21:37:09225ninja -C out/Release content_shell_test_apk
226
Andrew Grievedb31fb9af2019-05-23 20:53:48227# Run the test suite:
jbudorick9472f112016-01-27 16:21:56228out/Release/bin/run_content_shell_test_apk [-vv]
andybons3322f762015-08-24 21:37:09229
Andrew Grievedb31fb9af2019-05-23 20:53:48230# Run a subset of tests and enable some "please go faster" options:
231out/Release/bin/run_content_shell_test_apk --fast-local-dev -f "*TestClass*"
andybons3322f762015-08-24 21:37:09232```
233
Nate Fischer496fd552019-02-12 13:32:20234Android WebView tests:
andybons3322f762015-08-24 21:37:09235
Nate Fischer496fd552019-02-12 13:32:20236See [WebView's instructions](/android_webview/docs/test-instructions.md).
andybons3322f762015-08-24 21:37:09237
ntfschr09699f442017-01-12 22:20:46238In order to run a subset of tests, use -f to filter based on test class/method
239or -A/-E to filter using annotations.
andybons3322f762015-08-24 21:37:09240
Andrew Grievedb31fb9af2019-05-23 20:53:48241More Filtering examples:
andybons3322f762015-08-24 21:37:09242
243```shell
andybons3322f762015-08-24 21:37:09244# Run a specific test class
Andrew Grievedb31fb9af2019-05-23 20:53:48245out/Debug/bin/run_content_shell_test_apk -f "AddressDetectionTest.*"
andybons3322f762015-08-24 21:37:09246
247# Run a specific test method
Andrew Grievedb31fb9af2019-05-23 20:53:48248out/Debug/bin/run_content_shell_test_apk -f AddressDetectionTest#testAddressLimits
andybons3322f762015-08-24 21:37:09249
250# Run a subset of tests by size (Smoke, SmallTest, MediumTest, LargeTest,
251# EnormousTest)
jbudorick9472f112016-01-27 16:21:56252out/Debug/bin/run_content_shell_test_apk -A Smoke
andybons3322f762015-08-24 21:37:09253
254# Run a subset of tests by annotation, such as filtering by Feature
jbudorick9472f112016-01-27 16:21:56255out/Debug/bin/run_content_shell_test_apk -A Feature=Navigation
andybons3322f762015-08-24 21:37:09256```
257
258You might want to add stars `*` to each as a regular expression, e.g.
259`*`AddressDetectionTest`*`
260
Andrew Grieve4fe99742017-11-23 19:43:16261### Debugging
262
Andrew Grievedb31fb9af2019-05-23 20:53:48263Similar to [debugging apk targets](../android_debugging_instructions.md#debugging-java):
Andrew Grieve4fe99742017-11-23 19:43:16264
265```shell
266out/Debug/bin/run_content_shell_test_apk --wait-for-java-debugger
267```
268
269### Deobfuscating Java Stacktraces
270
271If running with `is_debug=false`, Java stacks from logcat need to be fixed up:
272
273```shell
Andrew Grieve17a59652020-03-19 18:14:53274build/android/stacktrace/java_deobfuscate.py out/Release/apks/ChromePublicTest.apk.mapping < stacktrace.txt
Andrew Grieve4fe99742017-11-23 19:43:16275```
276
277Any stacks produced by test runner output will already be deobfuscated.
278
279
Kent Tamura59ffb022018-11-27 05:30:56280## Running Blink Web Tests
andybons3322f762015-08-24 21:37:09281
Andrew Grievedb31fb9af2019-05-23 20:53:48282See [Web Tests](web_tests.md).
andybons3322f762015-08-24 21:37:09283
284## Running GPU tests
285
286(e.g. the "Android Debug (Nexus 7)" bot on the chromium.gpu waterfall)
287
xiaoyin.l1003c0b2016-12-06 02:51:17288See https://www.chromium.org/developers/testing/gpu-testing for details. Use
jbudorick25c17382016-08-03 18:53:07289`--browser=android-content-shell`. Examine the stdio from the test invocation on
290the bots to see arguments to pass to `src/content/test/gpu/run_gpu_test.py`.