blob: c065974b092480d8da1ef1c9ca3f49d6dd486564 [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
9#### ADB Debugging
andybons3322f762015-08-24 21:37:0910
Andrew Grieveb747e712017-11-23 16:58:3111The adb executable exists within the Android SDK:
12
13```shell
14third_party/android_tools/sdk/platform-tools/adb
15```
16
andybons3322f762015-08-24 21:37:0917In order to allow the ADB to connect to the device, you must enable USB
18debugging:
andybons22afb312015-08-31 02:24:5119
20* Before Android 4.1 (Jelly Bean):
21 * Go to "System Settings"
22 * Go to "Developer options"
23 * Check "USB debugging".
24 * Un-check "Verify apps over USB".
ntfschr09699f442017-01-12 22:20:4625* On Jelly Bean and above, developer options are hidden by default. To unhide
26 them:
andybons22afb312015-08-31 02:24:5127 * Go to "About phone"
28 * Tap 10 times on "Build number"
29 * The "Developer options" menu will now be available.
30 * Check "USB debugging".
31 * Un-check "Verify apps over USB".
andybons3322f762015-08-24 21:37:0932
jbudorick25c17382016-08-03 18:53:0733#### Screen
andybons3322f762015-08-24 21:37:0934
ntfschr09699f442017-01-12 22:20:4635You **must** ensure that the screen stays on while testing: `adb shell svc power
36stayon usb` Or do this manually on the device: Settings -> Developer options ->
37Stay Awake.
andybons3322f762015-08-24 21:37:0938
39If this option is greyed out, stay awake is probably disabled by policy. In that
40case, get another device or log in with a normal, unmanaged account (because the
41tests will break in exciting ways if stay awake is off).
42
jbudorick25c17382016-08-03 18:53:0743#### Disable Verify Apps
andybons3322f762015-08-24 21:37:0944
ntfschr09699f442017-01-12 22:20:4645You may see a dialog like [this
46one](http://www.samsungmobileusa.com/simulators/ATT_GalaxyMega/mobile/screens/06-02_12.jpg),
andybons3322f762015-08-24 21:37:0947which states, _Google may regularly check installed apps for potentially harmful
48behavior._ This can interfere with the test runner. To disable this dialog, run:
ntfschr09699f442017-01-12 22:20:4649
50```
51adb shell settings put global package_verifier_enable 0
52```
andybons3322f762015-08-24 21:37:0953
Anthony Berent8d3ce022018-01-15 12:24:3654### Using Emulators
andybons3322f762015-08-24 21:37:0955
Anthony Berent8d3ce022018-01-15 12:24:3656#### Building for emulation
andybons3322f762015-08-24 21:37:0957
Anthony Berent8d3ce022018-01-15 12:24:3658The fast Android emulators use the X86 instruction set, so anything run on such
59an emulator has to be built for X86. Add
60```
61target_cpu = "x86"
62```
63to your args.gn file. You may want use different out directories for your X86
64and ARM builds.
andybons3322f762015-08-24 21:37:0965
Anthony Berent8d3ce022018-01-15 12:24:3666#### Setting up your workstation
andybons3322f762015-08-24 21:37:0967
Anthony Berent8d3ce022018-01-15 12:24:3668The Android emulators support VM acceleration. This, however, needs to be
69enabled on your workstation, as described in
70https://developer.android.com/studio/run/emulator-acceleration.html#accel-vm.
andybons3322f762015-08-24 21:37:0971
Anthony Berent8d3ce022018-01-15 12:24:3672#### Creating and running emulators from Android Studio
andybons3322f762015-08-24 21:37:0973
Anthony Berent8d3ce022018-01-15 12:24:3674The easiest way to create and run an emulator is to use Android Studio's
75Virtual Device Manager. See
76https://developer.android.com/studio/run/managing-avds.html.
andybons3322f762015-08-24 21:37:0977
Anthony Berent8d3ce022018-01-15 12:24:3678Creating emulators in Android Studio will modify the current SDK. If you are
79using the project's SDK then this can cause problems the next time you sync
80the project, so it is normally better to use a different SDK root when
81creating emulators. You can set this up either by creating the Android Studio
82project using generate_gradle.py's --sdk or --sdk-path options or by
83changing the SDK location within AndroidStudio's settings.
andybons3322f762015-08-24 21:37:0984
Anthony Berent8d3ce022018-01-15 12:24:3685#### Starting an emulator from the command line
andybons3322f762015-08-24 21:37:0986
Anthony Berent8d3ce022018-01-15 12:24:3687Once you have created an emulator (using Android Studio or otherwise) you can
88start it from the command line using the
89[emulator](https://developer.android.com/studio/run/emulator-commandline.html)
90command:
andybons3322f762015-08-24 21:37:0991
Anthony Berent8d3ce022018-01-15 12:24:3692```
93{$ANDROID_SDK_ROOT}/tools/emulator @emulatorName
94```
andybons3322f762015-08-24 21:37:0995
Anthony Berent8d3ce022018-01-15 12:24:3696where emulatorName is the name of the emulator you want to start (e.g.
97Nexus_5X_API_27). The command
98
99```
100{$ANDROID_SDK_ROOT}/tools/emulator -list-avds
101```
102
103will list the available emulators.
104
105#### Creating an emulator from the command line
106
107New emulators can be created from the command line using the
108[avdmanager](https://developer.android.com/studio/command-line/avdmanager.html)
109command. This, however, does not provide any way of creating new device types,
110and provides far fewer options than the Android Studio UI for creating new
111emulators.
112
113The device types are configured through a devices.xml file. The devices.xml
114file for standard device types are within Android Studio's install, and that
115for any additional devices you define are in $ANDROID_EMULATOR_HOME (defaulting
116to ~/.android/). The contents of devices.xml is, however, undocumented (and
117presumably subject to change), so this is best modified using Android Studio.
andybons3322f762015-08-24 21:37:09118
119## Building Tests
120
jbudorick25c17382016-08-03 18:53:07121If you're adding a new test file, you'll need to explicitly add it to a gn
ntfschr09699f442017-01-12 22:20:46122target. If you're adding a test to an existing file, you won't need to make gn
jbudorick25c17382016-08-03 18:53:07123changes, but you may be interested in where your test winds up. In either case,
124here are some guidelines for where a test belongs:
andybons3322f762015-08-24 21:37:09125
jbudorick25c17382016-08-03 18:53:07126### C++
andybons3322f762015-08-24 21:37:09127
jbudorick25c17382016-08-03 18:53:07128C++ test files typically belong in `<top-level directory>_unittests` (e.g.
129`base_unittests` for `//base`). There are a few exceptions -- browser tests are
130typically their own target (e.g. `content_browsertests` for `//content`, or
131`browser_tests` for `//chrome`), and some unit test suites are broken at the
132second directory rather than the top-level one.
133
134### Java
135
136Java test files vary a bit more widely than their C++ counterparts:
137
ntfschr09699f442017-01-12 22:20:46138- Instrumentation test files -- i.e., tests that will run on a device --
139 typically belong in either `<top-level directory>_javatests` or `<top-level
140 directory>_test_java`. Regardless, they'll wind up getting packaged into one
141 of a few test APKs:
ctzsm34f54d62017-04-21 17:04:07142 - `webview_instrumentation_test_apk` for anything in `//android_webview`
ntfschr09699f442017-01-12 22:20:46143 - `content_shell_test_apk` for anything in `//content` or below
144 - `chrome_public_test_apk` for most things in `//chrome`
145 - `chrome_sync_shell_test_apk` in a few exceptional cases
146- JUnit or Robolectric test files -- i.e., tests that will run on the host --
147 typically belong in `<top-level directory>_junit_tests` (e.g.
148 `base_junit_tests` for `//base`), though here again there are cases
149 (particularly in `//components`) where suites are split at the second
150 directory rather than the top-level one.
andybons3322f762015-08-24 21:37:09151
152Once you know what to build, just do it like you normally would build anything
newt17e4d242015-08-27 09:07:26153else, e.g.: `ninja -C out/Release chrome_public_test_apk`
andybons3322f762015-08-24 21:37:09154
155## Running Tests
156
jbudorick25c17382016-08-03 18:53:07157All functional tests should be runnable via the wrapper scripts generated at
158build time:
159
160```sh
161<output directory>/bin/run_<target_name> [options]
162```
163
164Note that tests are sharded across all attached devices unless explicitly told
165to do otherwise by `-d/--device`.
andybons3322f762015-08-24 21:37:09166
167The commands used by the buildbots are printed in the logs. Look at
xiaoyin.l1003c0b2016-12-06 02:51:17168https://build.chromium.org/ to duplicate the same test command as a particular
andybons3322f762015-08-24 21:37:09169builder.
170
jbudorick25c17382016-08-03 18:53:07171### INSTALL\_FAILED\_CONTAINER\_ERROR or INSTALL\_FAILED\_INSUFFICIENT\_STORAGE
andybons3322f762015-08-24 21:37:09172
jbudorick25c17382016-08-03 18:53:07173If you see this error when the test runner is attempting to deploy the test
andybons3322f762015-08-24 21:37:09174binaries to the AVD emulator, you may need to resize your userdata partition
175with the following commands:
176
177```shell
davve7ae32cd2015-09-22 06:54:09178# Resize userdata partition to be 1G
mikecase7c263052017-03-30 23:46:11179resize2fs android_emulator_sdk/sdk/system-images/android-25/x86/userdata.img 1G
andybons3322f762015-08-24 21:37:09180
181# Set filesystem parameter to continue on errors; Android doesn't like some
182# things e2fsprogs does.
mikecase7c263052017-03-30 23:46:11183tune2fs -e continue android_emulator_sdk/sdk/system-images/android-25/x86/userdata.img
andybons3322f762015-08-24 21:37:09184```
185
186## Symbolizing Crashes
187
ntfschr09699f442017-01-12 22:20:46188Crash stacks are logged and can be viewed using `adb logcat`. To symbolize the
jyasskinc1c76ff2015-12-18 21:39:52189traces, define `CHROMIUM_OUTPUT_DIR=$OUTDIR` where `$OUTDIR` is the argument you
190pass to `ninja -C`, and pipe the output through
191`third_party/android_platform/development/scripts/stack`. If
192`$CHROMIUM_OUTPUT_DIR` is unset, the script will search `out/Debug` and
193`out/Release`. For example:
194
195```shell
196# If you build with
197ninja -C out/Debug chrome_public_test_apk
198# You can run:
199adb logcat -d | third_party/android_platform/development/scripts/stack
200
201# If you build with
202ninja -C out/android chrome_public_test_apk
203# You can run:
204adb logcat -d | CHROMIUM_OUTPUT_DIR=out/android third_party/android_platform/development/scripts/stack
205# or
206export CHROMIUM_OUTPUT_DIR=out/android
207adb logcat -d | third_party/android_platform/development/scripts/stack
208```
andybons3322f762015-08-24 21:37:09209
mlamouri0fbd6cd2015-10-26 12:08:11210## JUnit tests
211
212JUnit tests are Java unittests running on the host instead of the target device.
213They are faster to run and therefore are recommended over instrumentation tests
214when possible.
215
216The JUnits tests are usually following the pattern of *target*\_junit\_tests,
217for example, `content_junit_tests` and `chrome_junit_tests`.
218
219When adding a new JUnit test, the associated `BUILD.gn` file must be updated.
220For example, adding a test to `chrome_junit_tests` requires to update
221`chrome/android/BUILD.gn`. If you are a GYP user, you will not need to do that
222step in order to run the test locally but it is still required for GN users to
223run the test.
224
225```shell
226# Build the test suite.
Andrew Grieve4fe99742017-11-23 19:43:16227ninja -C out/Default chrome_junit_tests
mlamouri0fbd6cd2015-10-26 12:08:11228
229# Run the test suite.
Andrew Grieve4fe99742017-11-23 19:43:16230out/Default/run_chrome_junit_tests
mlamouri0fbd6cd2015-10-26 12:08:11231
232# Run a subset of tests. You might need to pass the package name for some tests.
Andrew Grieve4fe99742017-11-23 19:43:16233out/Default/run_chrome_junit_tests -f "org.chromium.chrome.browser.media.*"
234```
235
236### Debugging
237
238Similar to [debugging apk targets](android_debugging_instructions.md#debugging-java):
239
240```shell
241out/Default/bin/run_chrome_junit_tests --wait-for-java-debugger
242out/Default/bin/run_chrome_junit_tests --wait-for-java-debugger # Specify custom port via --debug-socket=9999
mlamouri0fbd6cd2015-10-26 12:08:11243```
244
andybons3322f762015-08-24 21:37:09245## Gtests
246
247```shell
248# Build a test suite
jbudorick25c17382016-08-03 18:53:07249ninja -C out/Release content_unittests
andybons3322f762015-08-24 21:37:09250
251# Run a test suite
jbudorick9472f112016-01-27 16:21:56252out/Release/bin/run_content_unittests [-vv]
andybons3322f762015-08-24 21:37:09253
254# Run a subset of tests
jbudorick9472f112016-01-27 16:21:56255out/Release/bin/run_content_unittests [-vv] --gtest-filter ByteStreamTest.*
andybons3322f762015-08-24 21:37:09256```
257
258## Instrumentation Tests
259
260In order to run instrumentation tests, you must leave your device screen ON and
261UNLOCKED. Otherwise, the test will timeout trying to launch an intent.
262Optionally you can disable screen lock under Settings -> Security -> Screen Lock
263-> None.
264
jbudorick9472f112016-01-27 16:21:56265Next, you need to build the app, build your tests, and then run your tests
266(which will install the APK under test and the test APK automatically).
andybons3322f762015-08-24 21:37:09267
268Examples:
269
270ContentShell tests:
271
272```shell
273# Build the code under test
274ninja -C out/Release content_shell_apk
275
276# Build the tests themselves
277ninja -C out/Release content_shell_test_apk
278
jbudorick9472f112016-01-27 16:21:56279# Run the test (will automagically install the APK under test and the test APK)
280out/Release/bin/run_content_shell_test_apk [-vv]
andybons3322f762015-08-24 21:37:09281```
282
newt17e4d242015-08-27 09:07:26283ChromePublic tests:
andybons3322f762015-08-24 21:37:09284
285```shell
286# Build the code under test
newt17e4d242015-08-27 09:07:26287ninja -C out/Release chrome_public_apk
andybons3322f762015-08-24 21:37:09288
289# Build the tests themselves
newt17e4d242015-08-27 09:07:26290ninja -C out/Release chrome_public_test_apk
andybons3322f762015-08-24 21:37:09291
jbudorick9472f112016-01-27 16:21:56292# Run the test (will automagically install the APK under test and the test APK)
293out/Release/bin/run_chrome_public_test_apk [-vv]
andybons3322f762015-08-24 21:37:09294```
295
296AndroidWebView tests:
297
298```shell
ctzsm34f54d62017-04-21 17:04:07299ninja -C out/Release webview_instrumentation_apk
300ninja -C out/Release webview_instrumentation_test_apk
301out/Release/bin/run_webview_instrumentation_test_apk [-vv]
andybons3322f762015-08-24 21:37:09302```
303
ntfschr09699f442017-01-12 22:20:46304In order to run a subset of tests, use -f to filter based on test class/method
305or -A/-E to filter using annotations.
andybons3322f762015-08-24 21:37:09306
307Filtering examples:
308
309```shell
310# Run a test suite
jbudorick9472f112016-01-27 16:21:56311out/Debug/bin/run_content_shell_test_apk
andybons3322f762015-08-24 21:37:09312
313# Run a specific test class
jbudorick9472f112016-01-27 16:21:56314out/Debug/bin/run_content_shell_test_apk -f AddressDetectionTest.*
andybons3322f762015-08-24 21:37:09315
316# Run a specific test method
jbudorick9472f112016-01-27 16:21:56317out/Debug/bin/run_content_shell_test_apk -f \
andybons3322f762015-08-24 21:37:09318AddressDetectionTest#testAddressLimits
319
320# Run a subset of tests by size (Smoke, SmallTest, MediumTest, LargeTest,
321# EnormousTest)
jbudorick9472f112016-01-27 16:21:56322out/Debug/bin/run_content_shell_test_apk -A Smoke
andybons3322f762015-08-24 21:37:09323
324# Run a subset of tests by annotation, such as filtering by Feature
jbudorick9472f112016-01-27 16:21:56325out/Debug/bin/run_content_shell_test_apk -A Feature=Navigation
andybons3322f762015-08-24 21:37:09326```
327
328You might want to add stars `*` to each as a regular expression, e.g.
329`*`AddressDetectionTest`*`
330
Andrew Grieve4fe99742017-11-23 19:43:16331### Debugging
332
333Similar to [debugging apk targets](android_debugging_instructions.md#debugging-java):
334
335```shell
336out/Debug/bin/run_content_shell_test_apk --wait-for-java-debugger
337```
338
339### Deobfuscating Java Stacktraces
340
341If running with `is_debug=false`, Java stacks from logcat need to be fixed up:
342
343```shell
344out/Release/bin/java_deobfuscate out/Release/apks/ChromePublicTest.apk.mapping < stacktrace.txt
345```
346
347Any stacks produced by test runner output will already be deobfuscated.
348
349
andybons3322f762015-08-24 21:37:09350## Running Blink Layout Tests
351
qyearsleyac3af532016-11-16 22:07:32352See [Layout Tests](testing/layout_tests.md).
andybons3322f762015-08-24 21:37:09353
354## Running GPU tests
355
356(e.g. the "Android Debug (Nexus 7)" bot on the chromium.gpu waterfall)
357
xiaoyin.l1003c0b2016-12-06 02:51:17358See https://www.chromium.org/developers/testing/gpu-testing for details. Use
jbudorick25c17382016-08-03 18:53:07359`--browser=android-content-shell`. Examine the stdio from the test invocation on
360the bots to see arguments to pass to `src/content/test/gpu/run_gpu_test.py`.