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