andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 1 | # Android Test Instructions |
| 2 | |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 3 | [TOC] |
| 4 | |
jbudorick | 25c1738 | 2016-08-03 18:53:07 | [diff] [blame] | 5 | ## Device Setup |
| 6 | |
| 7 | ### Physical Device Setup |
| 8 | |
Matt Falkenhagen | 3473e2e | 2018-12-03 03:40:50 | [diff] [blame] | 9 | #### Root Access |
| 10 | |
Andrew Grieve | db31fb9af | 2019-05-23 20:53:48 | [diff] [blame] | 11 | Running tests requires root access, which requires using a userdebug build on |
| 12 | your device. |
Matt Falkenhagen | 3473e2e | 2018-12-03 03:40:50 | [diff] [blame] | 13 | |
| 14 | To use a userdebug build, see |
| 15 | [Running Builds](https://source.android.com/setup/build/running.html). Googlers |
| 16 | can refer to [this page](https://goto.google.com/flashdevice). |
| 17 | |
Andrew Grieve | db31fb9af | 2019-05-23 20:53:48 | [diff] [blame] | 18 | If you can't run "adb root", you will get an error when trying to install |
| 19 | the test APKs like "adb: error: failed to copy" and |
| 20 | "remote secure_mkdirs failed: Operation not permitted" (use "adb unroot" to |
| 21 | return adb to normal). |
Matt Falkenhagen | 3473e2e | 2018-12-03 03:40:50 | [diff] [blame] | 22 | |
jbudorick | 25c1738 | 2016-08-03 18:53:07 | [diff] [blame] | 23 | #### ADB Debugging |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 24 | |
Andrew Grieve | b747e71 | 2017-11-23 16:58:31 | [diff] [blame] | 25 | The adb executable exists within the Android SDK: |
| 26 | |
| 27 | ```shell |
Yun Liu | f57cceaf | 2019-03-18 21:31:23 | [diff] [blame] | 28 | third_party/android_sdk/public/platform-tools/adb |
Andrew Grieve | b747e71 | 2017-11-23 16:58:31 | [diff] [blame] | 29 | ``` |
| 30 | |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 31 | In order to allow the ADB to connect to the device, you must enable USB |
| 32 | debugging: |
andybons | 22afb31 | 2015-08-31 02:24:51 | [diff] [blame] | 33 | |
Andrew Grieve | db31fb9af | 2019-05-23 20:53:48 | [diff] [blame] | 34 | * Developer options are hidden by default. To unhide them: |
andybons | 22afb31 | 2015-08-31 02:24:51 | [diff] [blame] | 35 | * 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". |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 40 | |
jbudorick | 25c1738 | 2016-08-03 18:53:07 | [diff] [blame] | 41 | #### Screen |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 42 | |
ntfschr | 09699f44 | 2017-01-12 22:20:46 | [diff] [blame] | 43 | You **must** ensure that the screen stays on while testing: `adb shell svc power |
| 44 | stayon usb` Or do this manually on the device: Settings -> Developer options -> |
| 45 | Stay Awake. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 46 | |
| 47 | If this option is greyed out, stay awake is probably disabled by policy. In that |
| 48 | case, get another device or log in with a normal, unmanaged account (because the |
| 49 | tests will break in exciting ways if stay awake is off). |
| 50 | |
jbudorick | 25c1738 | 2016-08-03 18:53:07 | [diff] [blame] | 51 | #### Disable Verify Apps |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 52 | |
ntfschr | 09699f44 | 2017-01-12 22:20:46 | [diff] [blame] | 53 | You may see a dialog like [this |
| 54 | one](http://www.samsungmobileusa.com/simulators/ATT_GalaxyMega/mobile/screens/06-02_12.jpg), |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 55 | which states, _Google may regularly check installed apps for potentially harmful |
| 56 | behavior._ This can interfere with the test runner. To disable this dialog, run: |
ntfschr | 09699f44 | 2017-01-12 22:20:46 | [diff] [blame] | 57 | |
Nate Fischer | 93c5915 | 2019-02-05 01:25:08 | [diff] [blame] | 58 | ```shell |
ntfschr | 09699f44 | 2017-01-12 22:20:46 | [diff] [blame] | 59 | adb shell settings put global package_verifier_enable 0 |
| 60 | ``` |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 61 | |
Anthony Berent | 8d3ce02 | 2018-01-15 12:24:36 | [diff] [blame] | 62 | ### Using Emulators |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 63 | |
Ryan Sleevi | e059091a | 2019-11-22 19:23:51 | [diff] [blame] | 64 | Running tests on emulators is the same as [on device](#Running-Tests). Refer to |
Andrew Grieve | db31fb9af | 2019-05-23 20:53:48 | [diff] [blame] | 65 | [android_emulator.md](../android_emulator.md) for setting up emulators. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 66 | |
| 67 | ## Building Tests |
| 68 | |
jbudorick | 25c1738 | 2016-08-03 18:53:07 | [diff] [blame] | 69 | If you're adding a new test file, you'll need to explicitly add it to a gn |
ntfschr | 09699f44 | 2017-01-12 22:20:46 | [diff] [blame] | 70 | target. If you're adding a test to an existing file, you won't need to make gn |
jbudorick | 25c1738 | 2016-08-03 18:53:07 | [diff] [blame] | 71 | changes, but you may be interested in where your test winds up. In either case, |
| 72 | here are some guidelines for where a test belongs: |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 73 | |
jbudorick | 25c1738 | 2016-08-03 18:53:07 | [diff] [blame] | 74 | ### C++ |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 75 | |
jbudorick | 25c1738 | 2016-08-03 18:53:07 | [diff] [blame] | 76 | C++ test files typically belong in `<top-level directory>_unittests` (e.g. |
| 77 | `base_unittests` for `//base`). There are a few exceptions -- browser tests are |
| 78 | typically their own target (e.g. `content_browsertests` for `//content`, or |
| 79 | `browser_tests` for `//chrome`), and some unit test suites are broken at the |
| 80 | second directory rather than the top-level one. |
| 81 | |
| 82 | ### Java |
| 83 | |
| 84 | Java test files vary a bit more widely than their C++ counterparts: |
| 85 | |
ntfschr | 09699f44 | 2017-01-12 22:20:46 | [diff] [blame] | 86 | - 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: |
ctzsm | 34f54d6 | 2017-04-21 17:04:07 | [diff] [blame] | 90 | - `webview_instrumentation_test_apk` for anything in `//android_webview` |
ntfschr | 09699f44 | 2017-01-12 22:20:46 | [diff] [blame] | 91 | - `content_shell_test_apk` for anything in `//content` or below |
| 92 | - `chrome_public_test_apk` for most things in `//chrome` |
ntfschr | 09699f44 | 2017-01-12 22:20:46 | [diff] [blame] | 93 | - 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. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 98 | |
| 99 | Once you know what to build, just do it like you normally would build anything |
newt | 17e4d24 | 2015-08-27 09:07:26 | [diff] [blame] | 100 | else, e.g.: `ninja -C out/Release chrome_public_test_apk` |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 101 | |
| 102 | ## Running Tests |
| 103 | |
jbudorick | 25c1738 | 2016-08-03 18:53:07 | [diff] [blame] | 104 | All functional tests should be runnable via the wrapper scripts generated at |
| 105 | build time: |
| 106 | |
| 107 | ```sh |
| 108 | <output directory>/bin/run_<target_name> [options] |
| 109 | ``` |
| 110 | |
| 111 | Note that tests are sharded across all attached devices unless explicitly told |
| 112 | to do otherwise by `-d/--device`. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 113 | |
| 114 | The commands used by the buildbots are printed in the logs. Look at |
xiaoyin.l | 1003c0b | 2016-12-06 02:51:17 | [diff] [blame] | 115 | https://build.chromium.org/ to duplicate the same test command as a particular |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 116 | builder. |
| 117 | |
jbudorick | 25c1738 | 2016-08-03 18:53:07 | [diff] [blame] | 118 | ### INSTALL\_FAILED\_CONTAINER\_ERROR or INSTALL\_FAILED\_INSUFFICIENT\_STORAGE |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 119 | |
jbudorick | 25c1738 | 2016-08-03 18:53:07 | [diff] [blame] | 120 | If you see this error when the test runner is attempting to deploy the test |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 121 | binaries to the AVD emulator, you may need to resize your userdata partition |
| 122 | with the following commands: |
| 123 | |
| 124 | ```shell |
davve | 7ae32cd | 2015-09-22 06:54:09 | [diff] [blame] | 125 | # Resize userdata partition to be 1G |
mikecase | 7c26305 | 2017-03-30 23:46:11 | [diff] [blame] | 126 | resize2fs android_emulator_sdk/sdk/system-images/android-25/x86/userdata.img 1G |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 127 | |
| 128 | # Set filesystem parameter to continue on errors; Android doesn't like some |
| 129 | # things e2fsprogs does. |
mikecase | 7c26305 | 2017-03-30 23:46:11 | [diff] [blame] | 130 | tune2fs -e continue android_emulator_sdk/sdk/system-images/android-25/x86/userdata.img |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 131 | ``` |
| 132 | |
| 133 | ## Symbolizing Crashes |
| 134 | |
ntfschr | 09699f44 | 2017-01-12 22:20:46 | [diff] [blame] | 135 | Crash stacks are logged and can be viewed using `adb logcat`. To symbolize the |
jyasskin | c1c76ff | 2015-12-18 21:39:52 | [diff] [blame] | 136 | traces, define `CHROMIUM_OUTPUT_DIR=$OUTDIR` where `$OUTDIR` is the argument you |
| 137 | pass 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 |
| 144 | ninja -C out/Debug chrome_public_test_apk |
| 145 | # You can run: |
| 146 | adb logcat -d | third_party/android_platform/development/scripts/stack |
| 147 | |
| 148 | # If you build with |
| 149 | ninja -C out/android chrome_public_test_apk |
| 150 | # You can run: |
| 151 | adb logcat -d | CHROMIUM_OUTPUT_DIR=out/android third_party/android_platform/development/scripts/stack |
| 152 | # or |
| 153 | export CHROMIUM_OUTPUT_DIR=out/android |
| 154 | adb logcat -d | third_party/android_platform/development/scripts/stack |
| 155 | ``` |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 156 | |
mlamouri | 0fbd6cd | 2015-10-26 12:08:11 | [diff] [blame] | 157 | ## JUnit tests |
| 158 | |
| 159 | JUnit tests are Java unittests running on the host instead of the target device. |
| 160 | They are faster to run and therefore are recommended over instrumentation tests |
| 161 | when possible. |
| 162 | |
| 163 | The JUnits tests are usually following the pattern of *target*\_junit\_tests, |
| 164 | for example, `content_junit_tests` and `chrome_junit_tests`. |
| 165 | |
| 166 | When adding a new JUnit test, the associated `BUILD.gn` file must be updated. |
| 167 | For example, adding a test to `chrome_junit_tests` requires to update |
Daniel Bratell | f73f0df | 2018-09-24 13:52:49 | [diff] [blame] | 168 | `chrome/android/BUILD.gn`. |
mlamouri | 0fbd6cd | 2015-10-26 12:08:11 | [diff] [blame] | 169 | |
| 170 | ```shell |
| 171 | # Build the test suite. |
Andrew Grieve | 4fe9974 | 2017-11-23 19:43:16 | [diff] [blame] | 172 | ninja -C out/Default chrome_junit_tests |
mlamouri | 0fbd6cd | 2015-10-26 12:08:11 | [diff] [blame] | 173 | |
| 174 | # Run the test suite. |
Andrew Walbran | ca9cad94 | 2020-06-21 14:18:50 | [diff] [blame^] | 175 | out/Default/bin/run_chrome_junit_tests |
mlamouri | 0fbd6cd | 2015-10-26 12:08:11 | [diff] [blame] | 176 | |
| 177 | # Run a subset of tests. You might need to pass the package name for some tests. |
Andrew Walbran | ca9cad94 | 2020-06-21 14:18:50 | [diff] [blame^] | 178 | out/Default/bin/run_chrome_junit_tests -f "org.chromium.chrome.browser.media.*" |
Andrew Grieve | 4fe9974 | 2017-11-23 19:43:16 | [diff] [blame] | 179 | ``` |
| 180 | |
| 181 | ### Debugging |
| 182 | |
Andrew Grieve | db31fb9af | 2019-05-23 20:53:48 | [diff] [blame] | 183 | Similar to [debugging apk targets](../android_debugging_instructions.md#debugging-java): |
Andrew Grieve | 4fe9974 | 2017-11-23 19:43:16 | [diff] [blame] | 184 | |
| 185 | ```shell |
| 186 | out/Default/bin/run_chrome_junit_tests --wait-for-java-debugger |
| 187 | out/Default/bin/run_chrome_junit_tests --wait-for-java-debugger # Specify custom port via --debug-socket=9999 |
mlamouri | 0fbd6cd | 2015-10-26 12:08:11 | [diff] [blame] | 188 | ``` |
| 189 | |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 190 | ## Gtests |
| 191 | |
| 192 | ```shell |
| 193 | # Build a test suite |
jbudorick | 25c1738 | 2016-08-03 18:53:07 | [diff] [blame] | 194 | ninja -C out/Release content_unittests |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 195 | |
| 196 | # Run a test suite |
jbudorick | 9472f11 | 2016-01-27 16:21:56 | [diff] [blame] | 197 | out/Release/bin/run_content_unittests [-vv] |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 198 | |
Andrew Grieve | db31fb9af | 2019-05-23 20:53:48 | [diff] [blame] | 199 | # Run a subset of tests and enable some "please go faster" options: |
| 200 | out/Release/bin/run_content_unittests --fast-local-dev -f "ByteStreamTest.*" |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 201 | ``` |
| 202 | |
| 203 | ## Instrumentation Tests |
| 204 | |
| 205 | In order to run instrumentation tests, you must leave your device screen ON and |
| 206 | UNLOCKED. Otherwise, the test will timeout trying to launch an intent. |
| 207 | Optionally you can disable screen lock under Settings -> Security -> Screen Lock |
| 208 | -> None. |
| 209 | |
jbudorick | 9472f11 | 2016-01-27 16:21:56 | [diff] [blame] | 210 | Next, 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). |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 212 | |
| 213 | Examples: |
| 214 | |
| 215 | ContentShell tests: |
| 216 | |
| 217 | ```shell |
Andrew Grieve | db31fb9af | 2019-05-23 20:53:48 | [diff] [blame] | 218 | # Build the tests: |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 219 | ninja -C out/Release content_shell_test_apk |
| 220 | |
Andrew Grieve | db31fb9af | 2019-05-23 20:53:48 | [diff] [blame] | 221 | # Run the test suite: |
jbudorick | 9472f11 | 2016-01-27 16:21:56 | [diff] [blame] | 222 | out/Release/bin/run_content_shell_test_apk [-vv] |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 223 | |
Andrew Grieve | db31fb9af | 2019-05-23 20:53:48 | [diff] [blame] | 224 | # Run a subset of tests and enable some "please go faster" options: |
| 225 | out/Release/bin/run_content_shell_test_apk --fast-local-dev -f "*TestClass*" |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 226 | ``` |
| 227 | |
Nate Fischer | 496fd55 | 2019-02-12 13:32:20 | [diff] [blame] | 228 | Android WebView tests: |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 229 | |
Nate Fischer | 496fd55 | 2019-02-12 13:32:20 | [diff] [blame] | 230 | See [WebView's instructions](/android_webview/docs/test-instructions.md). |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 231 | |
ntfschr | 09699f44 | 2017-01-12 22:20:46 | [diff] [blame] | 232 | In order to run a subset of tests, use -f to filter based on test class/method |
| 233 | or -A/-E to filter using annotations. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 234 | |
Andrew Grieve | db31fb9af | 2019-05-23 20:53:48 | [diff] [blame] | 235 | More Filtering examples: |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 236 | |
| 237 | ```shell |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 238 | # Run a specific test class |
Andrew Grieve | db31fb9af | 2019-05-23 20:53:48 | [diff] [blame] | 239 | out/Debug/bin/run_content_shell_test_apk -f "AddressDetectionTest.*" |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 240 | |
| 241 | # Run a specific test method |
Andrew Grieve | db31fb9af | 2019-05-23 20:53:48 | [diff] [blame] | 242 | out/Debug/bin/run_content_shell_test_apk -f AddressDetectionTest#testAddressLimits |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 243 | |
| 244 | # Run a subset of tests by size (Smoke, SmallTest, MediumTest, LargeTest, |
| 245 | # EnormousTest) |
jbudorick | 9472f11 | 2016-01-27 16:21:56 | [diff] [blame] | 246 | out/Debug/bin/run_content_shell_test_apk -A Smoke |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 247 | |
| 248 | # Run a subset of tests by annotation, such as filtering by Feature |
jbudorick | 9472f11 | 2016-01-27 16:21:56 | [diff] [blame] | 249 | out/Debug/bin/run_content_shell_test_apk -A Feature=Navigation |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 250 | ``` |
| 251 | |
| 252 | You might want to add stars `*` to each as a regular expression, e.g. |
| 253 | `*`AddressDetectionTest`*` |
| 254 | |
Andrew Grieve | 4fe9974 | 2017-11-23 19:43:16 | [diff] [blame] | 255 | ### Debugging |
| 256 | |
Andrew Grieve | db31fb9af | 2019-05-23 20:53:48 | [diff] [blame] | 257 | Similar to [debugging apk targets](../android_debugging_instructions.md#debugging-java): |
Andrew Grieve | 4fe9974 | 2017-11-23 19:43:16 | [diff] [blame] | 258 | |
| 259 | ```shell |
| 260 | out/Debug/bin/run_content_shell_test_apk --wait-for-java-debugger |
| 261 | ``` |
| 262 | |
| 263 | ### Deobfuscating Java Stacktraces |
| 264 | |
| 265 | If running with `is_debug=false`, Java stacks from logcat need to be fixed up: |
| 266 | |
| 267 | ```shell |
Andrew Grieve | 17a5965 | 2020-03-19 18:14:53 | [diff] [blame] | 268 | build/android/stacktrace/java_deobfuscate.py out/Release/apks/ChromePublicTest.apk.mapping < stacktrace.txt |
Andrew Grieve | 4fe9974 | 2017-11-23 19:43:16 | [diff] [blame] | 269 | ``` |
| 270 | |
| 271 | Any stacks produced by test runner output will already be deobfuscated. |
| 272 | |
| 273 | |
Kent Tamura | 59ffb02 | 2018-11-27 05:30:56 | [diff] [blame] | 274 | ## Running Blink Web Tests |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 275 | |
Andrew Grieve | db31fb9af | 2019-05-23 20:53:48 | [diff] [blame] | 276 | See [Web Tests](web_tests.md). |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 277 | |
| 278 | ## Running GPU tests |
| 279 | |
| 280 | (e.g. the "Android Debug (Nexus 7)" bot on the chromium.gpu waterfall) |
| 281 | |
xiaoyin.l | 1003c0b | 2016-12-06 02:51:17 | [diff] [blame] | 282 | See https://www.chromium.org/developers/testing/gpu-testing for details. Use |
jbudorick | 25c1738 | 2016-08-03 18:53:07 | [diff] [blame] | 283 | `--browser=android-content-shell`. Examine the stdio from the test invocation on |
| 284 | the bots to see arguments to pass to `src/content/test/gpu/run_gpu_test.py`. |