blob: 6a51945bb9eed48848cd2eb32241590d935a2358 [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
133## Symbolizing Crashes
134
ntfschr09699f442017-01-12 22:20:46135Crash stacks are logged and can be viewed using `adb logcat`. To symbolize the
jyasskinc1c76ff2015-12-18 21:39:52136traces, define `CHROMIUM_OUTPUT_DIR=$OUTDIR` where `$OUTDIR` is the argument you
137pass to `ninja -C`, and pipe the output through
138`third_party/android_platform/development/scripts/stack`. If
139`$CHROMIUM_OUTPUT_DIR` is unset, the script will search `out/Debug` and
140`out/Release`. For example:
141
142```shell
143# If you build with
144ninja -C out/Debug chrome_public_test_apk
145# You can run:
146adb logcat -d | third_party/android_platform/development/scripts/stack
147
148# If you build with
149ninja -C out/android chrome_public_test_apk
150# You can run:
151adb logcat -d | CHROMIUM_OUTPUT_DIR=out/android third_party/android_platform/development/scripts/stack
152# or
153export CHROMIUM_OUTPUT_DIR=out/android
154adb logcat -d | third_party/android_platform/development/scripts/stack
155```
andybons3322f762015-08-24 21:37:09156
mlamouri0fbd6cd2015-10-26 12:08:11157## JUnit tests
158
159JUnit tests are Java unittests running on the host instead of the target device.
160They are faster to run and therefore are recommended over instrumentation tests
161when possible.
162
163The JUnits tests are usually following the pattern of *target*\_junit\_tests,
164for example, `content_junit_tests` and `chrome_junit_tests`.
165
166When adding a new JUnit test, the associated `BUILD.gn` file must be updated.
167For example, adding a test to `chrome_junit_tests` requires to update
Daniel Bratellf73f0df2018-09-24 13:52:49168`chrome/android/BUILD.gn`.
mlamouri0fbd6cd2015-10-26 12:08:11169
170```shell
171# Build the test suite.
Andrew Grieve4fe99742017-11-23 19:43:16172ninja -C out/Default chrome_junit_tests
mlamouri0fbd6cd2015-10-26 12:08:11173
174# Run the test suite.
Andrew Walbranca9cad942020-06-21 14:18:50175out/Default/bin/run_chrome_junit_tests
mlamouri0fbd6cd2015-10-26 12:08:11176
177# Run a subset of tests. You might need to pass the package name for some tests.
Andrew Walbranca9cad942020-06-21 14:18:50178out/Default/bin/run_chrome_junit_tests -f "org.chromium.chrome.browser.media.*"
Andrew Grieve4fe99742017-11-23 19:43:16179```
180
181### Debugging
182
Andrew Grievedb31fb9af2019-05-23 20:53:48183Similar to [debugging apk targets](../android_debugging_instructions.md#debugging-java):
Andrew Grieve4fe99742017-11-23 19:43:16184
185```shell
186out/Default/bin/run_chrome_junit_tests --wait-for-java-debugger
187out/Default/bin/run_chrome_junit_tests --wait-for-java-debugger # Specify custom port via --debug-socket=9999
mlamouri0fbd6cd2015-10-26 12:08:11188```
189
andybons3322f762015-08-24 21:37:09190## Gtests
191
192```shell
193# Build a test suite
jbudorick25c17382016-08-03 18:53:07194ninja -C out/Release content_unittests
andybons3322f762015-08-24 21:37:09195
196# Run a test suite
jbudorick9472f112016-01-27 16:21:56197out/Release/bin/run_content_unittests [-vv]
andybons3322f762015-08-24 21:37:09198
Andrew Grievedb31fb9af2019-05-23 20:53:48199# Run a subset of tests and enable some "please go faster" options:
200out/Release/bin/run_content_unittests --fast-local-dev -f "ByteStreamTest.*"
andybons3322f762015-08-24 21:37:09201```
202
203## Instrumentation Tests
204
205In order to run instrumentation tests, you must leave your device screen ON and
206UNLOCKED. Otherwise, the test will timeout trying to launch an intent.
207Optionally you can disable screen lock under Settings -> Security -> Screen Lock
208-> None.
209
jbudorick9472f112016-01-27 16:21:56210Next, you need to build the app, build your tests, and then run your tests
211(which will install the APK under test and the test APK automatically).
andybons3322f762015-08-24 21:37:09212
213Examples:
214
215ContentShell tests:
216
217```shell
Andrew Grievedb31fb9af2019-05-23 20:53:48218# Build the tests:
andybons3322f762015-08-24 21:37:09219ninja -C out/Release content_shell_test_apk
220
Andrew Grievedb31fb9af2019-05-23 20:53:48221# Run the test suite:
jbudorick9472f112016-01-27 16:21:56222out/Release/bin/run_content_shell_test_apk [-vv]
andybons3322f762015-08-24 21:37:09223
Andrew Grievedb31fb9af2019-05-23 20:53:48224# Run a subset of tests and enable some "please go faster" options:
225out/Release/bin/run_content_shell_test_apk --fast-local-dev -f "*TestClass*"
andybons3322f762015-08-24 21:37:09226```
227
Nate Fischer496fd552019-02-12 13:32:20228Android WebView tests:
andybons3322f762015-08-24 21:37:09229
Nate Fischer496fd552019-02-12 13:32:20230See [WebView's instructions](/android_webview/docs/test-instructions.md).
andybons3322f762015-08-24 21:37:09231
ntfschr09699f442017-01-12 22:20:46232In order to run a subset of tests, use -f to filter based on test class/method
233or -A/-E to filter using annotations.
andybons3322f762015-08-24 21:37:09234
Andrew Grievedb31fb9af2019-05-23 20:53:48235More Filtering examples:
andybons3322f762015-08-24 21:37:09236
237```shell
andybons3322f762015-08-24 21:37:09238# Run a specific test class
Andrew Grievedb31fb9af2019-05-23 20:53:48239out/Debug/bin/run_content_shell_test_apk -f "AddressDetectionTest.*"
andybons3322f762015-08-24 21:37:09240
241# Run a specific test method
Andrew Grievedb31fb9af2019-05-23 20:53:48242out/Debug/bin/run_content_shell_test_apk -f AddressDetectionTest#testAddressLimits
andybons3322f762015-08-24 21:37:09243
244# Run a subset of tests by size (Smoke, SmallTest, MediumTest, LargeTest,
245# EnormousTest)
jbudorick9472f112016-01-27 16:21:56246out/Debug/bin/run_content_shell_test_apk -A Smoke
andybons3322f762015-08-24 21:37:09247
248# Run a subset of tests by annotation, such as filtering by Feature
jbudorick9472f112016-01-27 16:21:56249out/Debug/bin/run_content_shell_test_apk -A Feature=Navigation
andybons3322f762015-08-24 21:37:09250```
251
252You might want to add stars `*` to each as a regular expression, e.g.
253`*`AddressDetectionTest`*`
254
Andrew Grieve4fe99742017-11-23 19:43:16255### Debugging
256
Andrew Grievedb31fb9af2019-05-23 20:53:48257Similar to [debugging apk targets](../android_debugging_instructions.md#debugging-java):
Andrew Grieve4fe99742017-11-23 19:43:16258
259```shell
260out/Debug/bin/run_content_shell_test_apk --wait-for-java-debugger
261```
262
263### Deobfuscating Java Stacktraces
264
265If running with `is_debug=false`, Java stacks from logcat need to be fixed up:
266
267```shell
Andrew Grieve17a59652020-03-19 18:14:53268build/android/stacktrace/java_deobfuscate.py out/Release/apks/ChromePublicTest.apk.mapping < stacktrace.txt
Andrew Grieve4fe99742017-11-23 19:43:16269```
270
271Any stacks produced by test runner output will already be deobfuscated.
272
273
Kent Tamura59ffb022018-11-27 05:30:56274## Running Blink Web Tests
andybons3322f762015-08-24 21:37:09275
Andrew Grievedb31fb9af2019-05-23 20:53:48276See [Web Tests](web_tests.md).
andybons3322f762015-08-24 21:37:09277
278## Running GPU tests
279
280(e.g. the "Android Debug (Nexus 7)" bot on the chromium.gpu waterfall)
281
xiaoyin.l1003c0b2016-12-06 02:51:17282See https://www.chromium.org/developers/testing/gpu-testing for details. Use
jbudorick25c17382016-08-03 18:53:07283`--browser=android-content-shell`. Examine the stdio from the test invocation on
284the bots to see arguments to pass to `src/content/test/gpu/run_gpu_test.py`.