blob: 6d4a8fe43ca6eaaff9203fa0927ad54adbcc1bd4 [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
11Running tests requires being able to run "adb root", which requires using a
12userdebug build on your device.
13
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
18If you can't run "adb root", you will get an error when trying to install the
19test APKs like "adb: error: failed to copy" and "remote secure_mkdirs failed:
20Operation not permitted".
21
jbudorick25c17382016-08-03 18:53:0722#### ADB Debugging
andybons3322f762015-08-24 21:37:0923
Andrew Grieveb747e712017-11-23 16:58:3124The adb executable exists within the Android SDK:
25
26```shell
27third_party/android_tools/sdk/platform-tools/adb
28```
29
andybons3322f762015-08-24 21:37:0930In order to allow the ADB to connect to the device, you must enable USB
31debugging:
andybons22afb312015-08-31 02:24:5132
33* Before Android 4.1 (Jelly Bean):
34 * Go to "System Settings"
35 * Go to "Developer options"
36 * Check "USB debugging".
37 * Un-check "Verify apps over USB".
ntfschr09699f442017-01-12 22:20:4638* On Jelly Bean and above, developer options are hidden by default. To unhide
39 them:
andybons22afb312015-08-31 02:24:5140 * Go to "About phone"
41 * Tap 10 times on "Build number"
42 * The "Developer options" menu will now be available.
43 * Check "USB debugging".
44 * Un-check "Verify apps over USB".
andybons3322f762015-08-24 21:37:0945
jbudorick25c17382016-08-03 18:53:0746#### Screen
andybons3322f762015-08-24 21:37:0947
ntfschr09699f442017-01-12 22:20:4648You **must** ensure that the screen stays on while testing: `adb shell svc power
49stayon usb` Or do this manually on the device: Settings -> Developer options ->
50Stay Awake.
andybons3322f762015-08-24 21:37:0951
52If this option is greyed out, stay awake is probably disabled by policy. In that
53case, get another device or log in with a normal, unmanaged account (because the
54tests will break in exciting ways if stay awake is off).
55
jbudorick25c17382016-08-03 18:53:0756#### Disable Verify Apps
andybons3322f762015-08-24 21:37:0957
ntfschr09699f442017-01-12 22:20:4658You may see a dialog like [this
59one](http://www.samsungmobileusa.com/simulators/ATT_GalaxyMega/mobile/screens/06-02_12.jpg),
andybons3322f762015-08-24 21:37:0960which states, _Google may regularly check installed apps for potentially harmful
61behavior._ This can interfere with the test runner. To disable this dialog, run:
ntfschr09699f442017-01-12 22:20:4662
Nate Fischer93c59152019-02-05 01:25:0863```shell
ntfschr09699f442017-01-12 22:20:4664adb shell settings put global package_verifier_enable 0
65```
andybons3322f762015-08-24 21:37:0966
Anthony Berent8d3ce022018-01-15 12:24:3667### Using Emulators
andybons3322f762015-08-24 21:37:0968
Andrew Grieveae094e392018-06-15 16:10:2269Running tests on emulators is the same as on device. Refer to
Raphael Kubo da Costafe411ff2018-06-20 08:16:5070[android_emulator.md](android_emulator.md) for setting up emulators.
andybons3322f762015-08-24 21:37:0971
72## Building Tests
73
jbudorick25c17382016-08-03 18:53:0774If you're adding a new test file, you'll need to explicitly add it to a gn
ntfschr09699f442017-01-12 22:20:4675target. If you're adding a test to an existing file, you won't need to make gn
jbudorick25c17382016-08-03 18:53:0776changes, but you may be interested in where your test winds up. In either case,
77here are some guidelines for where a test belongs:
andybons3322f762015-08-24 21:37:0978
jbudorick25c17382016-08-03 18:53:0779### C++
andybons3322f762015-08-24 21:37:0980
jbudorick25c17382016-08-03 18:53:0781C++ test files typically belong in `<top-level directory>_unittests` (e.g.
82`base_unittests` for `//base`). There are a few exceptions -- browser tests are
83typically their own target (e.g. `content_browsertests` for `//content`, or
84`browser_tests` for `//chrome`), and some unit test suites are broken at the
85second directory rather than the top-level one.
86
87### Java
88
89Java test files vary a bit more widely than their C++ counterparts:
90
ntfschr09699f442017-01-12 22:20:4691- Instrumentation test files -- i.e., tests that will run on a device --
92 typically belong in either `<top-level directory>_javatests` or `<top-level
93 directory>_test_java`. Regardless, they'll wind up getting packaged into one
94 of a few test APKs:
ctzsm34f54d62017-04-21 17:04:0795 - `webview_instrumentation_test_apk` for anything in `//android_webview`
ntfschr09699f442017-01-12 22:20:4696 - `content_shell_test_apk` for anything in `//content` or below
97 - `chrome_public_test_apk` for most things in `//chrome`
ntfschr09699f442017-01-12 22:20:4698- JUnit or Robolectric test files -- i.e., tests that will run on the host --
99 typically belong in `<top-level directory>_junit_tests` (e.g.
100 `base_junit_tests` for `//base`), though here again there are cases
101 (particularly in `//components`) where suites are split at the second
102 directory rather than the top-level one.
andybons3322f762015-08-24 21:37:09103
104Once you know what to build, just do it like you normally would build anything
newt17e4d242015-08-27 09:07:26105else, e.g.: `ninja -C out/Release chrome_public_test_apk`
andybons3322f762015-08-24 21:37:09106
107## Running Tests
108
jbudorick25c17382016-08-03 18:53:07109All functional tests should be runnable via the wrapper scripts generated at
110build time:
111
112```sh
113<output directory>/bin/run_<target_name> [options]
114```
115
116Note that tests are sharded across all attached devices unless explicitly told
117to do otherwise by `-d/--device`.
andybons3322f762015-08-24 21:37:09118
119The commands used by the buildbots are printed in the logs. Look at
xiaoyin.l1003c0b2016-12-06 02:51:17120https://build.chromium.org/ to duplicate the same test command as a particular
andybons3322f762015-08-24 21:37:09121builder.
122
jbudorick25c17382016-08-03 18:53:07123### INSTALL\_FAILED\_CONTAINER\_ERROR or INSTALL\_FAILED\_INSUFFICIENT\_STORAGE
andybons3322f762015-08-24 21:37:09124
jbudorick25c17382016-08-03 18:53:07125If you see this error when the test runner is attempting to deploy the test
andybons3322f762015-08-24 21:37:09126binaries to the AVD emulator, you may need to resize your userdata partition
127with the following commands:
128
129```shell
davve7ae32cd2015-09-22 06:54:09130# Resize userdata partition to be 1G
mikecase7c263052017-03-30 23:46:11131resize2fs android_emulator_sdk/sdk/system-images/android-25/x86/userdata.img 1G
andybons3322f762015-08-24 21:37:09132
133# Set filesystem parameter to continue on errors; Android doesn't like some
134# things e2fsprogs does.
mikecase7c263052017-03-30 23:46:11135tune2fs -e continue android_emulator_sdk/sdk/system-images/android-25/x86/userdata.img
andybons3322f762015-08-24 21:37:09136```
137
138## Symbolizing Crashes
139
ntfschr09699f442017-01-12 22:20:46140Crash stacks are logged and can be viewed using `adb logcat`. To symbolize the
jyasskinc1c76ff2015-12-18 21:39:52141traces, define `CHROMIUM_OUTPUT_DIR=$OUTDIR` where `$OUTDIR` is the argument you
142pass to `ninja -C`, and pipe the output through
143`third_party/android_platform/development/scripts/stack`. If
144`$CHROMIUM_OUTPUT_DIR` is unset, the script will search `out/Debug` and
145`out/Release`. For example:
146
147```shell
148# If you build with
149ninja -C out/Debug chrome_public_test_apk
150# You can run:
151adb logcat -d | third_party/android_platform/development/scripts/stack
152
153# If you build with
154ninja -C out/android chrome_public_test_apk
155# You can run:
156adb logcat -d | CHROMIUM_OUTPUT_DIR=out/android third_party/android_platform/development/scripts/stack
157# or
158export CHROMIUM_OUTPUT_DIR=out/android
159adb logcat -d | third_party/android_platform/development/scripts/stack
160```
andybons3322f762015-08-24 21:37:09161
mlamouri0fbd6cd2015-10-26 12:08:11162## JUnit tests
163
164JUnit tests are Java unittests running on the host instead of the target device.
165They are faster to run and therefore are recommended over instrumentation tests
166when possible.
167
168The JUnits tests are usually following the pattern of *target*\_junit\_tests,
169for example, `content_junit_tests` and `chrome_junit_tests`.
170
171When adding a new JUnit test, the associated `BUILD.gn` file must be updated.
172For example, adding a test to `chrome_junit_tests` requires to update
Daniel Bratellf73f0df2018-09-24 13:52:49173`chrome/android/BUILD.gn`.
mlamouri0fbd6cd2015-10-26 12:08:11174
175```shell
176# Build the test suite.
Andrew Grieve4fe99742017-11-23 19:43:16177ninja -C out/Default chrome_junit_tests
mlamouri0fbd6cd2015-10-26 12:08:11178
179# Run the test suite.
Andrew Grieve4fe99742017-11-23 19:43:16180out/Default/run_chrome_junit_tests
mlamouri0fbd6cd2015-10-26 12:08:11181
182# Run a subset of tests. You might need to pass the package name for some tests.
Andrew Grieve4fe99742017-11-23 19:43:16183out/Default/run_chrome_junit_tests -f "org.chromium.chrome.browser.media.*"
184```
185
186### Debugging
187
188Similar to [debugging apk targets](android_debugging_instructions.md#debugging-java):
189
190```shell
191out/Default/bin/run_chrome_junit_tests --wait-for-java-debugger
192out/Default/bin/run_chrome_junit_tests --wait-for-java-debugger # Specify custom port via --debug-socket=9999
mlamouri0fbd6cd2015-10-26 12:08:11193```
194
andybons3322f762015-08-24 21:37:09195## Gtests
196
197```shell
198# Build a test suite
jbudorick25c17382016-08-03 18:53:07199ninja -C out/Release content_unittests
andybons3322f762015-08-24 21:37:09200
201# Run a test suite
jbudorick9472f112016-01-27 16:21:56202out/Release/bin/run_content_unittests [-vv]
andybons3322f762015-08-24 21:37:09203
204# Run a subset of tests
jbudorick9472f112016-01-27 16:21:56205out/Release/bin/run_content_unittests [-vv] --gtest-filter ByteStreamTest.*
andybons3322f762015-08-24 21:37:09206```
207
208## Instrumentation Tests
209
210In order to run instrumentation tests, you must leave your device screen ON and
211UNLOCKED. Otherwise, the test will timeout trying to launch an intent.
212Optionally you can disable screen lock under Settings -> Security -> Screen Lock
213-> None.
214
jbudorick9472f112016-01-27 16:21:56215Next, you need to build the app, build your tests, and then run your tests
216(which will install the APK under test and the test APK automatically).
andybons3322f762015-08-24 21:37:09217
218Examples:
219
220ContentShell tests:
221
222```shell
223# Build the code under test
224ninja -C out/Release content_shell_apk
225
226# Build the tests themselves
227ninja -C out/Release content_shell_test_apk
228
jbudorick9472f112016-01-27 16:21:56229# Run the test (will automagically install the APK under test and the test APK)
230out/Release/bin/run_content_shell_test_apk [-vv]
andybons3322f762015-08-24 21:37:09231```
232
newt17e4d242015-08-27 09:07:26233ChromePublic tests:
andybons3322f762015-08-24 21:37:09234
235```shell
236# Build the code under test
newt17e4d242015-08-27 09:07:26237ninja -C out/Release chrome_public_apk
andybons3322f762015-08-24 21:37:09238
239# Build the tests themselves
newt17e4d242015-08-27 09:07:26240ninja -C out/Release chrome_public_test_apk
andybons3322f762015-08-24 21:37:09241
jbudorick9472f112016-01-27 16:21:56242# Run the test (will automagically install the APK under test and the test APK)
243out/Release/bin/run_chrome_public_test_apk [-vv]
andybons3322f762015-08-24 21:37:09244```
245
Nate Fischer496fd552019-02-12 13:32:20246Android WebView tests:
andybons3322f762015-08-24 21:37:09247
Nate Fischer496fd552019-02-12 13:32:20248See [WebView's instructions](/android_webview/docs/test-instructions.md).
andybons3322f762015-08-24 21:37:09249
ntfschr09699f442017-01-12 22:20:46250In order to run a subset of tests, use -f to filter based on test class/method
251or -A/-E to filter using annotations.
andybons3322f762015-08-24 21:37:09252
253Filtering examples:
254
255```shell
256# Run a test suite
jbudorick9472f112016-01-27 16:21:56257out/Debug/bin/run_content_shell_test_apk
andybons3322f762015-08-24 21:37:09258
259# Run a specific test class
jbudorick9472f112016-01-27 16:21:56260out/Debug/bin/run_content_shell_test_apk -f AddressDetectionTest.*
andybons3322f762015-08-24 21:37:09261
262# Run a specific test method
jbudorick9472f112016-01-27 16:21:56263out/Debug/bin/run_content_shell_test_apk -f \
andybons3322f762015-08-24 21:37:09264AddressDetectionTest#testAddressLimits
265
266# Run a subset of tests by size (Smoke, SmallTest, MediumTest, LargeTest,
267# EnormousTest)
jbudorick9472f112016-01-27 16:21:56268out/Debug/bin/run_content_shell_test_apk -A Smoke
andybons3322f762015-08-24 21:37:09269
270# Run a subset of tests by annotation, such as filtering by Feature
jbudorick9472f112016-01-27 16:21:56271out/Debug/bin/run_content_shell_test_apk -A Feature=Navigation
andybons3322f762015-08-24 21:37:09272```
273
274You might want to add stars `*` to each as a regular expression, e.g.
275`*`AddressDetectionTest`*`
276
Andrew Grieve4fe99742017-11-23 19:43:16277### Debugging
278
279Similar to [debugging apk targets](android_debugging_instructions.md#debugging-java):
280
281```shell
282out/Debug/bin/run_content_shell_test_apk --wait-for-java-debugger
283```
284
285### Deobfuscating Java Stacktraces
286
287If running with `is_debug=false`, Java stacks from logcat need to be fixed up:
288
289```shell
290out/Release/bin/java_deobfuscate out/Release/apks/ChromePublicTest.apk.mapping < stacktrace.txt
291```
292
293Any stacks produced by test runner output will already be deobfuscated.
294
295
Kent Tamura59ffb022018-11-27 05:30:56296## Running Blink Web Tests
andybons3322f762015-08-24 21:37:09297
Kent Tamura59ffb022018-11-27 05:30:56298See [Web Tests](testing/web_tests.md).
andybons3322f762015-08-24 21:37:09299
300## Running GPU tests
301
302(e.g. the "Android Debug (Nexus 7)" bot on the chromium.gpu waterfall)
303
xiaoyin.l1003c0b2016-12-06 02:51:17304See https://www.chromium.org/developers/testing/gpu-testing for details. Use
jbudorick25c17382016-08-03 18:53:07305`--browser=android-content-shell`. Examine the stdio from the test invocation on
306the bots to see arguments to pass to `src/content/test/gpu/run_gpu_test.py`.