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