Andrew Grieve | b5b3bb3 | 2023-10-04 13:55:51 | [diff] [blame] | 1 | # Android Testing in Chromium |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 2 | |
Andrew Grieve | b5b3bb3 | 2023-10-04 13:55:51 | [diff] [blame] | 3 | [TOC] |
| 4 | |
| 5 | ## Test Types |
| 6 | |
| 7 | - **[gtests]**: For writing unit tests in C++. Most tests are cross-platform. |
| 8 | - **Browser Tests**: Built on top of gtest. Used to write integration tests. |
| 9 | - **[Robolectric]**: JUnit tests that run on the host machine by emulating (or |
| 10 | mocking) Android APIs. |
| 11 | - **[Instrumentation Tests]**: JUnit tests that run on devices / emulators. |
| 12 | - **Unit Instrumentation Tests**: Unit tests that do not require |
Ted Choc | a4550c64 | 2024-11-19 19:59:30 | [diff] [blame] | 13 | initializing the Content layer. These use [BaseActivityTestRule] often |
| 14 | using [BlankUiTestActivity]. |
Andrew Grieve | b5b3bb3 | 2023-10-04 13:55:51 | [diff] [blame] | 15 | - **Integration Instrumentation Tests**: Instrumentation tests that require |
| 16 | initializing the Content layer to test a certain feature in the end-to-end |
| 17 | flow. These typically use more specialized test rules such as |
| 18 | [ContentShellActivityTestRule] or [ChromeActivityTestRule]. |
| 19 | |
| 20 | [gtests]: android_gtests.md |
| 21 | [Robolectric]: android_robolectric_tests.md |
| 22 | [Instrumentation Tests]: android_instrumentation_tests.md |
| 23 | [BaseActivityTestRule]: https://source.chromium.org/chromium/chromium/src/+/main:base/test/android/javatests/src/org/chromium/base/test/BaseActivityTestRule.java |
Ted Choc | a4550c64 | 2024-11-19 19:59:30 | [diff] [blame] | 24 | [BlankUiTestActivity]: https://source.chromium.org/chromium/chromium/src/+/main:ui/android/javatests/src/org/chromium/ui/test/util/BlankUiTestActivity.java |
Andrew Grieve | b5b3bb3 | 2023-10-04 13:55:51 | [diff] [blame] | 25 | [ContentShellActivityTestRule]: https://source.chromium.org/chromium/chromium/src/+/main:content/shell/android/javatests/src/org/chromium/content_shell_apk/ContentShellActivityTestRule.java |
| 26 | [ChromeActivityTestRule]: https://source.chromium.org/chromium/chromium/src/+/main:chrome/test/android/javatests/src/org/chromium/chrome/test/ChromeActivityTestRule.java |
| 27 | |
| 28 | ## Device Setup |
| 29 | |
| 30 | ### Physical Device Setup |
| 31 | |
| 32 | #### Root Access |
| 33 | |
| 34 | Running tests requires root access, which requires using a userdebug build on |
| 35 | your device. |
| 36 | |
| 37 | To use a userdebug build, see |
| 38 | [Running Builds](https://source.android.com/setup/build/running.html). Googlers |
| 39 | can refer to [this page](https://goto.google.com/flashdevice). |
| 40 | |
| 41 | If you can't run "adb root", you will get an error when trying to install |
| 42 | the test APKs like "adb: error: failed to copy" and |
| 43 | "remote secure_mkdirs failed: Operation not permitted" (use "adb unroot" to |
| 44 | return adb to normal). |
| 45 | |
| 46 | #### ADB Debugging |
| 47 | |
| 48 | The adb executable exists within the Android SDK: |
| 49 | |
| 50 | ```shell |
| 51 | third_party/android_sdk/public/platform-tools/adb |
| 52 | ``` |
| 53 | |
| 54 | In order to allow the ADB to connect to the device, you must enable USB |
| 55 | debugging: |
| 56 | |
| 57 | * Developer options are hidden by default. To unhide them: |
| 58 | * Go to "About phone" |
| 59 | * Tap 10 times on "Build number" |
| 60 | * The "Developer options" menu will now be available. |
| 61 | * Check "USB debugging". |
| 62 | * Un-check "Verify apps over USB". |
| 63 | |
| 64 | #### Screen |
| 65 | |
| 66 | You **must** ensure that the screen stays on while testing: `adb shell svc power |
| 67 | stayon usb` Or do this manually on the device: Settings -> Developer options -> |
| 68 | Stay Awake. |
| 69 | |
| 70 | If this option is greyed out, stay awake is probably disabled by policy. In that |
| 71 | case, get another device or log in with a normal, unmanaged account (because the |
| 72 | tests will break in exciting ways if stay awake is off). |
| 73 | |
| 74 | #### Disable Verify Apps |
| 75 | |
| 76 | You may see a dialog like [this |
| 77 | one](http://www.samsungmobileusa.com/simulators/ATT_GalaxyMega/mobile/screens/06-02_12.jpg), |
| 78 | which states, _Google may regularly check installed apps for potentially harmful |
| 79 | behavior._ This can interfere with the test runner. To disable this dialog, run: |
| 80 | |
| 81 | ```shell |
| 82 | adb shell settings put global package_verifier_enable 0 |
| 83 | ``` |
| 84 | |
| 85 | ### Using Emulators |
| 86 | |
| 87 | Running tests on emulators is the same as [on device](#Running-Tests). Refer to |
| 88 | [android_emulator.md](/docs/android_emulator.md) for setting up emulators. |
| 89 | |
| 90 | ## Building Tests |
| 91 | |
| 92 | If you're adding a new test file, you'll need to explicitly add it to a gn |
| 93 | target. If you're adding a test to an existing file, you won't need to make gn |
| 94 | changes, but you may be interested in where your test winds up. In either case, |
| 95 | here are some guidelines for where a test belongs: |
| 96 | |
| 97 | ### C++ |
| 98 | |
| 99 | C++ test files typically belong in `<top-level directory>_unittests` (e.g. |
| 100 | `base_unittests` for `//base`). There are a few exceptions -- browser tests are |
| 101 | typically their own target (e.g. `content_browsertests` for `//content`, or |
| 102 | `browser_tests` for `//chrome`), and some unit test suites are broken at the |
| 103 | second directory rather than the top-level one. |
| 104 | |
| 105 | ### Java |
| 106 | |
| 107 | Java test files vary a bit more widely than their C++ counterparts: |
| 108 | |
| 109 | - Instrumentation test files -- i.e., tests that will run on a device -- |
| 110 | typically belong in either `<top-level directory>_javatests` or `<top-level |
| 111 | directory>_test_java`. Regardless, they'll wind up getting packaged into one |
| 112 | of a few test APKs: |
| 113 | - `webview_instrumentation_test_apk` for anything in `//android_webview` |
| 114 | - `content_shell_test_apk` for anything in `//content` or below |
| 115 | - `chrome_public_test_apk` for most things in `//chrome` |
| 116 | - JUnit or Robolectric test files -- i.e., tests that will run on the host -- |
| 117 | typically belong in `<top-level directory>_junit_tests` (e.g. |
| 118 | `base_junit_tests` for `//base`), though here again there are cases |
| 119 | (particularly in `//components`) where suites are split at the second |
| 120 | directory rather than the top-level one. |
| 121 | |
| 122 | Once you know what to build, just do it like you normally would build anything |
| 123 | else, e.g.: `ninja -C out/Release chrome_public_test_apk` |
| 124 | |
| 125 | ### Determining Test Target |
| 126 | |
| 127 | If you do not know what target a test file belongs to, you can use |
| 128 | `//tools/autotest.py` to figure it out fo you: |
| 129 | |
| 130 | ```sh |
| 131 | # Builds relevant test target and then runs the test: |
| 132 | tools/autotest.py -C <output directory> TestClassName |
| 133 | ``` |
| 134 | |
| 135 | ## Running Tests |
| 136 | |
| 137 | All functional tests should be runnable via the wrapper scripts generated at |
| 138 | build time: |
| 139 | |
| 140 | ```sh |
| 141 | <output directory>/bin/run_<target_name> [options] |
| 142 | ``` |
| 143 | |
| 144 | Note that tests are sharded across all attached devices unless explicitly told |
| 145 | to do otherwise by `-d/--device`. |
| 146 | |
| 147 | The commands used by the buildbots are printed in the logs. Look at |
| 148 | https://build.chromium.org/ to duplicate the same test command as a particular |
| 149 | builder. |
| 150 | |
| 151 | ### Listing Available Tests |
| 152 | |
| 153 | Use `--list-tests` to list what tests are available. |
| 154 | |
| 155 | ```sh |
| 156 | # Prints out all available tests: |
| 157 | <output directory>/bin/run_<target_name> --list-tests |
| 158 | |
| 159 | # Prints out all available tests that match a filter: |
| 160 | <output directory>/bin/run_<target_name> --list-tests -f "*MyFilter*" |
| 161 | ``` |
| 162 | |
| 163 | ### INSTALL\_FAILED\_CONTAINER\_ERROR or INSTALL\_FAILED\_INSUFFICIENT\_STORAGE |
| 164 | |
| 165 | If you see this error when the test runner is attempting to deploy the test |
| 166 | binaries to the AVD emulator, you may need to resize your userdata partition |
| 167 | with the following commands: |
| 168 | |
| 169 | ```shell |
| 170 | # Resize userdata partition to be 1G |
| 171 | resize2fs android_emulator_sdk/sdk/system-images/android-25/x86/userdata.img 1G |
| 172 | |
| 173 | # Set filesystem parameter to continue on errors; Android doesn't like some |
| 174 | # things e2fsprogs does. |
| 175 | tune2fs -e continue android_emulator_sdk/sdk/system-images/android-25/x86/userdata.img |
| 176 | ``` |
| 177 | |
| 178 | ### AdbCommandFailedError: failed to stat remote object |
| 179 | |
| 180 | There's a known issue (https://crbug.com/1094062) where the unit test binaries can fail on |
| 181 | Android R and later: if you see this error, try rerunning on an Android version |
| 182 | with API level <= 29 (Android <= Q). |
| 183 | |
| 184 | ## Symbolizing Crashes |
| 185 | |
| 186 | Crash stacks are logged and can be viewed using `adb logcat`. To symbolize the |
| 187 | traces, define `CHROMIUM_OUTPUT_DIR=$OUTDIR` where `$OUTDIR` is the argument you |
| 188 | pass to `ninja -C`, and pipe the output through |
| 189 | `third_party/android_platform/development/scripts/stack`. If |
| 190 | `$CHROMIUM_OUTPUT_DIR` is unset, the script will search `out/Debug` and |
| 191 | `out/Release`. For example: |
| 192 | |
| 193 | ```shell |
| 194 | # If you build with |
| 195 | ninja -C out/Debug chrome_public_test_apk |
| 196 | # You can run: |
| 197 | adb logcat -d | third_party/android_platform/development/scripts/stack |
| 198 | |
| 199 | # If you build with |
| 200 | ninja -C out/android chrome_public_test_apk |
| 201 | # You can run: |
| 202 | adb logcat -d | CHROMIUM_OUTPUT_DIR=out/android third_party/android_platform/development/scripts/stack |
| 203 | # or |
| 204 | export CHROMIUM_OUTPUT_DIR=out/android |
| 205 | adb logcat -d | third_party/android_platform/development/scripts/stack |
| 206 | ``` |
| 207 | |
| 208 | ## Robolectric Tests |
| 209 | |
| 210 | JUnit tests are Java unittests running on the host instead of the target device. |
| 211 | They are faster to run and therefore are recommended over instrumentation tests |
| 212 | when possible. |
| 213 | |
| 214 | The JUnits tests are usually following the pattern of *target*\_junit\_tests, |
| 215 | for example, `content_junit_tests` and `chrome_junit_tests`. |
| 216 | |
| 217 | When adding a new JUnit test, the associated `BUILD.gn` file must be updated. |
| 218 | For example, adding a test to `chrome_junit_tests` requires to update |
| 219 | `chrome/android/BUILD.gn`. |
| 220 | |
| 221 | ```shell |
| 222 | # Build the test suite. |
| 223 | ninja -C out/Default chrome_junit_tests |
| 224 | |
| 225 | # Run the test suite. |
| 226 | out/Default/bin/run_chrome_junit_tests |
| 227 | |
| 228 | # Run a subset of tests. You might need to pass the package name for some tests. |
| 229 | out/Default/bin/run_chrome_junit_tests -f "org.chromium.chrome.browser.media.*" |
| 230 | ``` |
| 231 | |
| 232 | ### Debugging |
| 233 | |
| 234 | Similar to [debugging apk targets](/docs/android_debugging_instructions.md#debugging-java): |
| 235 | |
| 236 | ```shell |
| 237 | out/Default/bin/run_chrome_junit_tests --wait-for-java-debugger |
| 238 | out/Default/bin/run_chrome_junit_tests --wait-for-java-debugger # Specify custom port via --debug-socket=9999 |
| 239 | ``` |
| 240 | |
| 241 | ## Gtests |
| 242 | |
| 243 | ```shell |
| 244 | # Build a test suite |
| 245 | ninja -C out/Release content_unittests |
| 246 | |
| 247 | # Run a test suite |
| 248 | out/Release/bin/run_content_unittests [-vv] |
| 249 | |
| 250 | # Run a subset of tests and enable some "please go faster" options: |
| 251 | out/Release/bin/run_content_unittests --fast-local-dev -f "ByteStreamTest.*" |
| 252 | ``` |
| 253 | |
| 254 | ## Instrumentation Tests |
| 255 | |
| 256 | In order to run instrumentation tests, you must leave your device screen ON and |
| 257 | UNLOCKED. Otherwise, the test will timeout trying to launch an intent. |
| 258 | Optionally you can disable screen lock under Settings -> Security -> Screen Lock |
| 259 | -> None. |
| 260 | |
| 261 | Next, you need to build the app, build your tests, and then run your tests |
| 262 | (which will install the APK under test and the test APK automatically). |
| 263 | |
| 264 | Examples: |
| 265 | |
| 266 | ContentShell tests: |
| 267 | |
| 268 | ```shell |
| 269 | # Build the tests: |
| 270 | ninja -C out/Release content_shell_test_apk |
| 271 | |
| 272 | # Run the test suite: |
| 273 | out/Release/bin/run_content_shell_test_apk [-vv] |
| 274 | |
| 275 | # Run a subset of tests and enable some "please go faster" options: |
| 276 | out/Release/bin/run_content_shell_test_apk --fast-local-dev -f "*TestClass*" |
| 277 | ``` |
| 278 | |
| 279 | Android WebView tests: |
| 280 | |
| 281 | See [WebView's instructions](/android_webview/docs/test-instructions.md). |
| 282 | |
| 283 | In order to run a subset of tests, use -f to filter based on test class/method |
| 284 | or -A/-E to filter using annotations. |
| 285 | |
| 286 | More Filtering examples: |
| 287 | |
| 288 | ```shell |
| 289 | # Run a specific test class |
| 290 | out/Debug/bin/run_content_shell_test_apk -f "AddressDetectionTest.*" |
| 291 | |
| 292 | # Run a specific test method |
| 293 | out/Debug/bin/run_content_shell_test_apk -f AddressDetectionTest#testAddressLimits |
| 294 | |
| 295 | # Run a subset of tests by size (Smoke, SmallTest, MediumTest, LargeTest, |
| 296 | # EnormousTest) |
| 297 | out/Debug/bin/run_content_shell_test_apk -A Smoke |
| 298 | |
| 299 | # Run a subset of tests by annotation, such as filtering by Feature |
| 300 | out/Debug/bin/run_content_shell_test_apk -A Feature=Navigation |
| 301 | ``` |
| 302 | |
| 303 | You might want to add stars `*` to each as a regular expression, e.g. |
| 304 | `*`AddressDetectionTest`*` |
| 305 | |
| 306 | ### Debugging |
| 307 | |
| 308 | Similar to [debugging apk targets](/docs/android_debugging_instructions.md#debugging-java): |
| 309 | |
| 310 | ```shell |
| 311 | out/Debug/bin/run_content_shell_test_apk --wait-for-java-debugger |
| 312 | ``` |
| 313 | |
| 314 | ### Deobfuscating Java Stacktraces |
| 315 | |
| 316 | If running with `is_debug=false`, Java stacks from logcat need to be fixed up: |
| 317 | |
| 318 | ```shell |
| 319 | build/android/stacktrace/java_deobfuscate.py out/Release/apks/ChromePublicTest.apk.mapping < stacktrace.txt |
| 320 | ``` |
| 321 | |
| 322 | Any stacks produced by test runner output will already be deobfuscated. |
| 323 | |
| 324 | |
| 325 | ## Running Blink Web Tests |
| 326 | |
| 327 | See [Web Tests](web_tests.md). |
| 328 | |
| 329 | ## Running Telemetry (Perf) Tests |
| 330 | |
| 331 | See [Telemetry](https://chromium.googlesource.com/catapult/+/HEAD/telemetry/README.md). |
| 332 | |
| 333 | ## Running GPU tests |
| 334 | |
| 335 | (e.g. the "Android Debug (Nexus 7)" bot on the chromium.gpu waterfall) |
| 336 | |
| 337 | See https://www.chromium.org/developers/testing/gpu-testing for details. Use |
| 338 | `--browser=android-content-shell`. Examine the stdio from the test invocation on |
| 339 | the bots to see arguments to pass to `src/content/test/gpu/run_gpu_test.py`. |