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 | |
jbudorick | 25c1738 | 2016-08-03 18:53:07 | [diff] [blame] | 54 | ### Emulator Setup |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 55 | |
jbudorick | 25c1738 | 2016-08-03 18:53:07 | [diff] [blame] | 56 | #### Option 1 |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 57 | |
| 58 | Use an emulator (i.e. Android Virtual Device, AVD): Enabling Intel's |
| 59 | Virtualizaton support provides the fastest, most reliable emulator configuration |
ntfschr | 09699f44 | 2017-01-12 22:20:46 | [diff] [blame] | 60 | available (i.e. x86 emulator with GPU acceleration and KVM support). Remember to |
Nick Harper | 8a13e82 | 2017-05-19 23:45:43 | [diff] [blame] | 61 | build with `target_cpu = "x86"` for x86. Otherwise installing the APKs will fail |
ntfschr | 09699f44 | 2017-01-12 22:20:46 | [diff] [blame] | 62 | with `INSTALL_FAILED_NO_MATCHING_ABIS`. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 63 | |
| 64 | 1. Enable Intel Virtualization support in the BIOS. |
| 65 | |
Andrew Grieve | b747e71 | 2017-11-23 16:58:31 | [diff] [blame^] | 66 | 2. Install emulator deps: |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 67 | |
| 68 | ```shell |
thestig | f51ad75 | 2015-11-11 18:08:49 | [diff] [blame] | 69 | build/android/install_emulator_deps.py --api-level=23 |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 70 | ``` |
| 71 | |
| 72 | This script will download Android SDK and place it a directory called |
| 73 | android\_tools in the same parent directory as your chromium checkout. It |
| 74 | will also download the system-images for the emulators (i.e. arm and x86). |
| 75 | Note that this is a different SDK download than the Android SDK in the |
jbudorick | 25c1738 | 2016-08-03 18:53:07 | [diff] [blame] | 76 | chromium source checkout (i.e. `src/third_party/android_emulator_sdk`). |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 77 | |
Andrew Grieve | b747e71 | 2017-11-23 16:58:31 | [diff] [blame^] | 78 | 3. Run the avd.py script. To start up _num_ emulators use -n. For non-x86 use |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 79 | --abi. |
| 80 | |
| 81 | ```shell |
thestig | f51ad75 | 2015-11-11 18:08:49 | [diff] [blame] | 82 | build/android/avd.py --api-level=23 |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 83 | ``` |
| 84 | |
| 85 | This script will attempt to use GPU emulation, so you must be running the |
ntfschr | 09699f44 | 2017-01-12 22:20:46 | [diff] [blame] | 86 | emulators in an environment with hardware rendering available. See `avd.py |
| 87 | --help` for more details. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 88 | |
jbudorick | 25c1738 | 2016-08-03 18:53:07 | [diff] [blame] | 89 | #### Option 2 |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 90 | |
jbudorick | 25c1738 | 2016-08-03 18:53:07 | [diff] [blame] | 91 | Alternatively, you can create and run your own emulator using the tools provided |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 92 | by the Android SDK. When doing so, be sure to enable GPU emulation in hardware |
| 93 | settings, since Chromium requires it to render. |
| 94 | |
| 95 | ## Building Tests |
| 96 | |
jbudorick | 25c1738 | 2016-08-03 18:53:07 | [diff] [blame] | 97 | 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] | 98 | 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] | 99 | changes, but you may be interested in where your test winds up. In either case, |
| 100 | here are some guidelines for where a test belongs: |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 101 | |
jbudorick | 25c1738 | 2016-08-03 18:53:07 | [diff] [blame] | 102 | ### C++ |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 103 | |
jbudorick | 25c1738 | 2016-08-03 18:53:07 | [diff] [blame] | 104 | C++ test files typically belong in `<top-level directory>_unittests` (e.g. |
| 105 | `base_unittests` for `//base`). There are a few exceptions -- browser tests are |
| 106 | typically their own target (e.g. `content_browsertests` for `//content`, or |
| 107 | `browser_tests` for `//chrome`), and some unit test suites are broken at the |
| 108 | second directory rather than the top-level one. |
| 109 | |
| 110 | ### Java |
| 111 | |
| 112 | Java test files vary a bit more widely than their C++ counterparts: |
| 113 | |
ntfschr | 09699f44 | 2017-01-12 22:20:46 | [diff] [blame] | 114 | - Instrumentation test files -- i.e., tests that will run on a device -- |
| 115 | typically belong in either `<top-level directory>_javatests` or `<top-level |
| 116 | directory>_test_java`. Regardless, they'll wind up getting packaged into one |
| 117 | of a few test APKs: |
ctzsm | 34f54d6 | 2017-04-21 17:04:07 | [diff] [blame] | 118 | - `webview_instrumentation_test_apk` for anything in `//android_webview` |
ntfschr | 09699f44 | 2017-01-12 22:20:46 | [diff] [blame] | 119 | - `content_shell_test_apk` for anything in `//content` or below |
| 120 | - `chrome_public_test_apk` for most things in `//chrome` |
| 121 | - `chrome_sync_shell_test_apk` in a few exceptional cases |
| 122 | - JUnit or Robolectric test files -- i.e., tests that will run on the host -- |
| 123 | typically belong in `<top-level directory>_junit_tests` (e.g. |
| 124 | `base_junit_tests` for `//base`), though here again there are cases |
| 125 | (particularly in `//components`) where suites are split at the second |
| 126 | directory rather than the top-level one. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 127 | |
| 128 | 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] | 129 | else, e.g.: `ninja -C out/Release chrome_public_test_apk` |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 130 | |
| 131 | ## Running Tests |
| 132 | |
jbudorick | 25c1738 | 2016-08-03 18:53:07 | [diff] [blame] | 133 | All functional tests should be runnable via the wrapper scripts generated at |
| 134 | build time: |
| 135 | |
| 136 | ```sh |
| 137 | <output directory>/bin/run_<target_name> [options] |
| 138 | ``` |
| 139 | |
| 140 | Note that tests are sharded across all attached devices unless explicitly told |
| 141 | to do otherwise by `-d/--device`. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 142 | |
| 143 | The commands used by the buildbots are printed in the logs. Look at |
xiaoyin.l | 1003c0b | 2016-12-06 02:51:17 | [diff] [blame] | 144 | https://build.chromium.org/ to duplicate the same test command as a particular |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 145 | builder. |
| 146 | |
jbudorick | 25c1738 | 2016-08-03 18:53:07 | [diff] [blame] | 147 | ### INSTALL\_FAILED\_CONTAINER\_ERROR or INSTALL\_FAILED\_INSUFFICIENT\_STORAGE |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 148 | |
jbudorick | 25c1738 | 2016-08-03 18:53:07 | [diff] [blame] | 149 | 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] | 150 | binaries to the AVD emulator, you may need to resize your userdata partition |
| 151 | with the following commands: |
| 152 | |
| 153 | ```shell |
davve | 7ae32cd | 2015-09-22 06:54:09 | [diff] [blame] | 154 | # Resize userdata partition to be 1G |
mikecase | 7c26305 | 2017-03-30 23:46:11 | [diff] [blame] | 155 | resize2fs android_emulator_sdk/sdk/system-images/android-25/x86/userdata.img 1G |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 156 | |
| 157 | # Set filesystem parameter to continue on errors; Android doesn't like some |
| 158 | # things e2fsprogs does. |
mikecase | 7c26305 | 2017-03-30 23:46:11 | [diff] [blame] | 159 | tune2fs -e continue android_emulator_sdk/sdk/system-images/android-25/x86/userdata.img |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 160 | ``` |
| 161 | |
| 162 | ## Symbolizing Crashes |
| 163 | |
ntfschr | 09699f44 | 2017-01-12 22:20:46 | [diff] [blame] | 164 | Crash stacks are logged and can be viewed using `adb logcat`. To symbolize the |
jyasskin | c1c76ff | 2015-12-18 21:39:52 | [diff] [blame] | 165 | traces, define `CHROMIUM_OUTPUT_DIR=$OUTDIR` where `$OUTDIR` is the argument you |
| 166 | pass to `ninja -C`, and pipe the output through |
| 167 | `third_party/android_platform/development/scripts/stack`. If |
| 168 | `$CHROMIUM_OUTPUT_DIR` is unset, the script will search `out/Debug` and |
| 169 | `out/Release`. For example: |
| 170 | |
| 171 | ```shell |
| 172 | # If you build with |
| 173 | ninja -C out/Debug chrome_public_test_apk |
| 174 | # You can run: |
| 175 | adb logcat -d | third_party/android_platform/development/scripts/stack |
| 176 | |
| 177 | # If you build with |
| 178 | ninja -C out/android chrome_public_test_apk |
| 179 | # You can run: |
| 180 | adb logcat -d | CHROMIUM_OUTPUT_DIR=out/android third_party/android_platform/development/scripts/stack |
| 181 | # or |
| 182 | export CHROMIUM_OUTPUT_DIR=out/android |
| 183 | adb logcat -d | third_party/android_platform/development/scripts/stack |
| 184 | ``` |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 185 | |
mlamouri | 0fbd6cd | 2015-10-26 12:08:11 | [diff] [blame] | 186 | ## JUnit tests |
| 187 | |
| 188 | JUnit tests are Java unittests running on the host instead of the target device. |
| 189 | They are faster to run and therefore are recommended over instrumentation tests |
| 190 | when possible. |
| 191 | |
| 192 | The JUnits tests are usually following the pattern of *target*\_junit\_tests, |
| 193 | for example, `content_junit_tests` and `chrome_junit_tests`. |
| 194 | |
| 195 | When adding a new JUnit test, the associated `BUILD.gn` file must be updated. |
| 196 | For example, adding a test to `chrome_junit_tests` requires to update |
| 197 | `chrome/android/BUILD.gn`. If you are a GYP user, you will not need to do that |
| 198 | step in order to run the test locally but it is still required for GN users to |
| 199 | run the test. |
| 200 | |
| 201 | ```shell |
| 202 | # Build the test suite. |
asvitkine | a7fdb06 | 2017-02-14 20:57:14 | [diff] [blame] | 203 | ninja -C out/my_build chrome_junit_tests |
mlamouri | 0fbd6cd | 2015-10-26 12:08:11 | [diff] [blame] | 204 | |
| 205 | # Run the test suite. |
asvitkine | a7fdb06 | 2017-02-14 20:57:14 | [diff] [blame] | 206 | BUILDTYPE=my_build build/android/test_runner.py junit -s chrome_junit_tests -vvv |
mlamouri | 0fbd6cd | 2015-10-26 12:08:11 | [diff] [blame] | 207 | |
| 208 | # Run a subset of tests. You might need to pass the package name for some tests. |
asvitkine | a7fdb06 | 2017-02-14 20:57:14 | [diff] [blame] | 209 | BUILDTYPE=my_build build/android/test_runner.py junit -s chrome_junit_tests -vvv |
mlamouri | 0fbd6cd | 2015-10-26 12:08:11 | [diff] [blame] | 210 | -f "org.chromium.chrome.browser.media.*" |
| 211 | ``` |
| 212 | |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 213 | ## Gtests |
| 214 | |
| 215 | ```shell |
| 216 | # Build a test suite |
jbudorick | 25c1738 | 2016-08-03 18:53:07 | [diff] [blame] | 217 | ninja -C out/Release content_unittests |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 218 | |
| 219 | # Run a test suite |
jbudorick | 9472f11 | 2016-01-27 16:21:56 | [diff] [blame] | 220 | out/Release/bin/run_content_unittests [-vv] |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 221 | |
| 222 | # Run a subset of tests |
jbudorick | 9472f11 | 2016-01-27 16:21:56 | [diff] [blame] | 223 | out/Release/bin/run_content_unittests [-vv] --gtest-filter ByteStreamTest.* |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 224 | ``` |
| 225 | |
| 226 | ## Instrumentation Tests |
| 227 | |
| 228 | In order to run instrumentation tests, you must leave your device screen ON and |
| 229 | UNLOCKED. Otherwise, the test will timeout trying to launch an intent. |
| 230 | Optionally you can disable screen lock under Settings -> Security -> Screen Lock |
| 231 | -> None. |
| 232 | |
jbudorick | 9472f11 | 2016-01-27 16:21:56 | [diff] [blame] | 233 | Next, you need to build the app, build your tests, and then run your tests |
| 234 | (which will install the APK under test and the test APK automatically). |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 235 | |
| 236 | Examples: |
| 237 | |
| 238 | ContentShell tests: |
| 239 | |
| 240 | ```shell |
| 241 | # Build the code under test |
| 242 | ninja -C out/Release content_shell_apk |
| 243 | |
| 244 | # Build the tests themselves |
| 245 | ninja -C out/Release content_shell_test_apk |
| 246 | |
jbudorick | 9472f11 | 2016-01-27 16:21:56 | [diff] [blame] | 247 | # Run the test (will automagically install the APK under test and the test APK) |
| 248 | out/Release/bin/run_content_shell_test_apk [-vv] |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 249 | ``` |
| 250 | |
newt | 17e4d24 | 2015-08-27 09:07:26 | [diff] [blame] | 251 | ChromePublic tests: |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 252 | |
| 253 | ```shell |
| 254 | # Build the code under test |
newt | 17e4d24 | 2015-08-27 09:07:26 | [diff] [blame] | 255 | ninja -C out/Release chrome_public_apk |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 256 | |
| 257 | # Build the tests themselves |
newt | 17e4d24 | 2015-08-27 09:07:26 | [diff] [blame] | 258 | ninja -C out/Release chrome_public_test_apk |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 259 | |
jbudorick | 9472f11 | 2016-01-27 16:21:56 | [diff] [blame] | 260 | # Run the test (will automagically install the APK under test and the test APK) |
| 261 | out/Release/bin/run_chrome_public_test_apk [-vv] |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 262 | ``` |
| 263 | |
| 264 | AndroidWebView tests: |
| 265 | |
| 266 | ```shell |
ctzsm | 34f54d6 | 2017-04-21 17:04:07 | [diff] [blame] | 267 | ninja -C out/Release webview_instrumentation_apk |
| 268 | ninja -C out/Release webview_instrumentation_test_apk |
| 269 | out/Release/bin/run_webview_instrumentation_test_apk [-vv] |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 270 | ``` |
| 271 | |
ntfschr | 09699f44 | 2017-01-12 22:20:46 | [diff] [blame] | 272 | In order to run a subset of tests, use -f to filter based on test class/method |
| 273 | or -A/-E to filter using annotations. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 274 | |
| 275 | Filtering examples: |
| 276 | |
| 277 | ```shell |
| 278 | # Run a test suite |
jbudorick | 9472f11 | 2016-01-27 16:21:56 | [diff] [blame] | 279 | out/Debug/bin/run_content_shell_test_apk |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 280 | |
| 281 | # Run a specific test class |
jbudorick | 9472f11 | 2016-01-27 16:21:56 | [diff] [blame] | 282 | out/Debug/bin/run_content_shell_test_apk -f AddressDetectionTest.* |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 283 | |
| 284 | # Run a specific test method |
jbudorick | 9472f11 | 2016-01-27 16:21:56 | [diff] [blame] | 285 | out/Debug/bin/run_content_shell_test_apk -f \ |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 286 | AddressDetectionTest#testAddressLimits |
| 287 | |
| 288 | # Run a subset of tests by size (Smoke, SmallTest, MediumTest, LargeTest, |
| 289 | # EnormousTest) |
jbudorick | 9472f11 | 2016-01-27 16:21:56 | [diff] [blame] | 290 | out/Debug/bin/run_content_shell_test_apk -A Smoke |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 291 | |
| 292 | # Run a subset of tests by annotation, such as filtering by Feature |
jbudorick | 9472f11 | 2016-01-27 16:21:56 | [diff] [blame] | 293 | out/Debug/bin/run_content_shell_test_apk -A Feature=Navigation |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 294 | ``` |
| 295 | |
| 296 | You might want to add stars `*` to each as a regular expression, e.g. |
| 297 | `*`AddressDetectionTest`*` |
| 298 | |
| 299 | ## Running Blink Layout Tests |
| 300 | |
qyearsley | ac3af53 | 2016-11-16 22:07:32 | [diff] [blame] | 301 | See [Layout Tests](testing/layout_tests.md). |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 302 | |
| 303 | ## Running GPU tests |
| 304 | |
| 305 | (e.g. the "Android Debug (Nexus 7)" bot on the chromium.gpu waterfall) |
| 306 | |
xiaoyin.l | 1003c0b | 2016-12-06 02:51:17 | [diff] [blame] | 307 | See https://www.chromium.org/developers/testing/gpu-testing for details. Use |
jbudorick | 25c1738 | 2016-08-03 18:53:07 | [diff] [blame] | 308 | `--browser=android-content-shell`. Examine the stdio from the test invocation on |
| 309 | the bots to see arguments to pass to `src/content/test/gpu/run_gpu_test.py`. |