Nate Fischer | 867117c3 | 2019-01-16 02:24:33 | [diff] [blame] | 1 | # WebView Test Instructions |
| 2 | |
Nate Fischer | 67d7bf2 | 2019-02-12 17:01:57 | [diff] [blame] | 3 | [TOC] |
| 4 | |
Nate Fischer | 867117c3 | 2019-01-16 02:24:33 | [diff] [blame] | 5 | ## Android Instructions |
| 6 | |
| 7 | Please follow the instructions at |
Ken Rockot | 35028ca | 2019-05-30 18:50:45 | [diff] [blame] | 8 | [android_test_instructions.md](/docs/testing/android_test_instructions.md). |
Nate Fischer | 867117c3 | 2019-01-16 02:24:33 | [diff] [blame] | 9 | This guide is an extension with WebView-specific content. |
| 10 | |
| 11 | *** note |
| 12 | **Note:** except where otherwise noted, all tests require a device or emulator. |
| 13 | *** |
| 14 | |
| 15 | ## Chromium-side tests |
| 16 | |
| 17 | ### Instrumentation tests |
| 18 | |
| 19 | These tests live under `//android_webview/javatests/`, and are mostly |
| 20 | end-to-end (*with the exception of the `//android_webview/glue/` layer*). |
| 21 | |
| 22 | ```sh |
| 23 | # Build |
| 24 | $ autoninja -C out/Default webview_instrumentation_test_apk |
| 25 | |
| 26 | # Run tests (any of these commands): |
| 27 | $ out/Default/bin/run_webview_instrumentation_test_apk # All tests |
| 28 | $ out/Default/bin/run_webview_instrumentation_test_apk -f AwContentsTest#* # A particular test suite |
| 29 | $ out/Default/bin/run_webview_instrumentation_test_apk -f AwContentsTest#testClearCacheInQuickSuccession # A single test |
| 30 | $ out/Default/bin/run_webview_instrumentation_test_apk -f AwContentsTest#*Succession # Any glob pattern matching 1 or more tests |
Nate Fischer | 4bd3cc9 | 2019-12-06 02:14:42 | [diff] [blame] | 31 | |
| 32 | # Print both Java and C++ log messages to the console (optional): |
| 33 | $ adb logcat |
Nate Fischer | 867117c3 | 2019-01-16 02:24:33 | [diff] [blame] | 34 | ``` |
| 35 | |
Nate Fischer | 19d894a4 | 2019-01-24 18:55:22 | [diff] [blame] | 36 | *** aside |
| 37 | You can optionally use `ClassName.methodName` instead of `ClassName#methodName`; |
| 38 | the chromium test runner understands either syntax. |
| 39 | *** |
| 40 | |
Drew Stonebraker | 9ceff9a | 2019-06-13 20:04:49 | [diff] [blame] | 41 | ### Java unittests |
| 42 | |
| 43 | These tests live under `//android_webview/junit/` and use Robolectric. |
| 44 | |
Nate Fischer | 4bd3cc9 | 2019-12-06 02:14:42 | [diff] [blame] | 45 | *** promo |
| 46 | **Tip:** Robolectric tests run on workstation and do not need a device or |
| 47 | emulator. These generally run much faster than on-device tests. |
| 48 | *** |
| 49 | |
Drew Stonebraker | 9ceff9a | 2019-06-13 20:04:49 | [diff] [blame] | 50 | ```sh |
| 51 | # Build |
| 52 | $ autoninja -C out/Default android_webview_junit_tests |
| 53 | |
| 54 | # Run tests (any of these commands): |
| 55 | $ out/Default/bin/run_android_webview_junit_tests # All tests |
| 56 | $ out/Default/bin/run_android_webview_junit_tests -f *FindAddressTest#* # Same glob patterns work here |
Nate Fischer | 4bd3cc9 | 2019-12-06 02:14:42 | [diff] [blame] | 57 | |
| 58 | # Print both Java and C++ log messages to the console (optional) by passing "-v" |
| 59 | # to the test runner. Example: |
| 60 | $ out/Default/bin/run_android_webview_unittests -v # All tests, including logs |
Drew Stonebraker | 9ceff9a | 2019-06-13 20:04:49 | [diff] [blame] | 61 | ``` |
| 62 | |
| 63 | *** note |
| 64 | For junit tests, filter (`-f`) arguments require fully qualified class names |
| 65 | (e.g. `org.chromium.android_webview.robolectric.FindAddressTest`), but replacing |
| 66 | the package name with a glob wildcard (`*`), as in the example above, will work |
| 67 | if the class name is unique. |
| 68 | *** |
| 69 | |
Nate Fischer | 867117c3 | 2019-01-16 02:24:33 | [diff] [blame] | 70 | ### Native unittests |
| 71 | |
| 72 | These are any `*_test.cc` or `*_unittest.cc` test under `//android_webview/`. |
| 73 | Currently, we only have tests under `//android_webview/browser/` and |
| 74 | `//android_webview/lib/`. |
| 75 | |
| 76 | ```sh |
| 77 | # Build |
| 78 | $ autoninja -C out/Default android_webview_unittests |
| 79 | |
| 80 | # Run tests (any of these commands): |
| 81 | $ out/Default/bin/run_android_webview_unittests # All tests |
| 82 | $ out/Default/bin/run_android_webview_unittests -f AndroidStreamReaderURLRequestJobTest.* # Same glob patterns work here |
Nate Fischer | 4bd3cc9 | 2019-12-06 02:14:42 | [diff] [blame] | 83 | |
| 84 | # Print both Java and C++ log messages to the console (optional): |
| 85 | $ adb logcat |
Nate Fischer | 867117c3 | 2019-01-16 02:24:33 | [diff] [blame] | 86 | ``` |
| 87 | |
Nate Fischer | 19d894a4 | 2019-01-24 18:55:22 | [diff] [blame] | 88 | ### Layout tests and page cycler tests |
| 89 | |
Nate Fischer | b3e39ac | 2021-05-05 14:22:25 | [diff] [blame] | 90 | WebView's layout tests and page cycler tests exercise the **WebView installed on |
| 91 | the system** and instrument the [system WebView shell app](webview-shell.md) |
| 92 | (`system_webview_shell_apk`). These test cases are defined in |
Nate Fischer | 19d894a4 | 2019-01-24 18:55:22 | [diff] [blame] | 93 | `//android_webview/tools/system_webview_shell/`. |
| 94 | |
Nate Fischer | b3e39ac | 2021-05-05 14:22:25 | [diff] [blame] | 95 | *** note |
Nate Fischer | 5a6d48a3 | 2021-11-18 03:19:36 | [diff] [blame] | 96 | **Important:** these tests compile and install both `system_webview_apk` and |
| 97 | `system_webview_shell_apk`. |
Nate Fischer | b3e39ac | 2021-05-05 14:22:25 | [diff] [blame] | 98 | |
Nate Fischer | 5a6d48a3 | 2021-11-18 03:19:36 | [diff] [blame] | 99 | You will need to configure GN args to make sure `system_webview_apk` is a valid |
| 100 | WebView provider for your system. Please see the [full build |
| 101 | instructions](build-instructions.md). |
| 102 | |
| 103 | If you are using an **emulator**, you will also need to configure the |
| 104 | `system_webview_shell_package_name` GN arg. See [WebView shell |
| 105 | docs](webview-shell.md#setting-up-the-build) for details. |
Nate Fischer | b3e39ac | 2021-05-05 14:22:25 | [diff] [blame] | 106 | *** |
| 107 | |
Nate Fischer | 19d894a4 | 2019-01-24 18:55:22 | [diff] [blame] | 108 | ```sh |
Nate Fischer | b3e39ac | 2021-05-05 14:22:25 | [diff] [blame] | 109 | # Build (this also compiles system_webview_shell_apk and system_webview_apk) |
Nate Fischer | 19d894a4 | 2019-01-24 18:55:22 | [diff] [blame] | 110 | $ autoninja -C out/Default system_webview_shell_layout_test_apk |
| 111 | |
Nate Fischer | b3e39ac | 2021-05-05 14:22:25 | [diff] [blame] | 112 | # Run layout tests (installs the test APK, WebView shell, and |
| 113 | # system_webview_apk, and also switches your WebView provider) |
Nate Fischer | 19d894a4 | 2019-01-24 18:55:22 | [diff] [blame] | 114 | $ out/Default/bin/run_system_webview_shell_layout_test_apk |
Nate Fischer | 4bd3cc9 | 2019-12-06 02:14:42 | [diff] [blame] | 115 | |
| 116 | # Print both Java and C++ log messages to the console (optional): |
| 117 | $ adb logcat |
Nate Fischer | 19d894a4 | 2019-01-24 18:55:22 | [diff] [blame] | 118 | ``` |
| 119 | |
| 120 | To run page cycler tests instead, use the `system_webview_shell_page_cycler_apk` |
| 121 | target and test runner in the steps above. |
| 122 | |
Nate Fischer | 0791cf999 | 2021-05-13 15:57:31 | [diff] [blame] | 123 | ### UI tests |
| 124 | |
| 125 | Like [layout and page cycler tests](#Layout-tests-and-page-cycler-tests), |
| 126 | WebView UI tests use the WebView installed on the system (and will automatically |
| 127 | compile and install the `system_webview_apk` target). Unlike those tests |
| 128 | however, this test suite _does not_ depend on the system WebView shell app, so |
| 129 | the setup is simpler. You will still need to follow the [full build |
| 130 | instructions](build-instructions.md) to correctly configure the |
| 131 | `system_webview_apk` target, but will not need to worry about compiling the |
| 132 | WebView shell (and do not need to worry about https://crbug.com/1205665). |
| 133 | |
| 134 | ```sh |
| 135 | # Build (this also compiles system_webview_apk) |
| 136 | $ autoninja -C out/Default webview_ui_test_app_test_apk |
| 137 | |
| 138 | # Run layout tests (installs the test APK and system_webview_apk and also |
| 139 | # switches your WebView provider) |
| 140 | $ out/Default/bin/run_webview_ui_test_app_test_apk |
| 141 | |
| 142 | # Print both Java and C++ log messages to the console (optional): |
| 143 | $ adb logcat |
| 144 | ``` |
| 145 | |
Nate Fischer | 867117c3 | 2019-01-16 02:24:33 | [diff] [blame] | 146 | ### Useful test runner options |
| 147 | |
| 148 | #### Debugging flaky tests |
| 149 | |
| 150 | ```sh |
| 151 | $ out/Default/bin/run_webview_instrumentation_test_apk \ # Any test runner |
| 152 | --num_retries=0 \ # Tests normally retry-on-failure; disable for easier repo |
Nate Fischer | 3286faa | 2021-07-15 21:03:56 | [diff] [blame] | 153 | --repeat=100 \ # Repeat up to 100 times for a failure |
Nate Fischer | 867117c3 | 2019-01-16 02:24:33 | [diff] [blame] | 154 | --break-on-failure \ # Stop repeating once we see the first failure |
| 155 | -f=AwContentsTest#testClearCacheInQuickSuccession |
| 156 | ``` |
| 157 | |
| 158 | #### Enable a Feature for a local run |
| 159 | |
| 160 | ```sh |
| 161 | $ out/Default/bin/run_webview_instrumentation_test_apk \ # Any test runner |
| 162 | # Desired Features; see commandline-flags.md for more information |
Nate Fischer | 1531d358 | 2019-08-06 21:31:17 | [diff] [blame] | 163 | --enable-features="MyFeature,MyOtherFeature" \ |
Nate Fischer | 867117c3 | 2019-01-16 02:24:33 | [diff] [blame] | 164 | -f=AwContentsTest#testClearCacheInQuickSuccession |
| 165 | ``` |
| 166 | |
John Abd-El-Malek | 1de03a6 | 2021-05-19 22:53:42 | [diff] [blame] | 167 | #### Debugging hangs in instrumentation tests |
| 168 | |
| 169 | If an instrumentation test is hanging, it's possible to get a callstack from the |
| 170 | browser process. This requires running on a device with root. |
| 171 | |
| 172 | It's not possible to get a callstack from the renderer because the sandbox will |
| 173 | prevent the trace file from being written. A workaround if you want to see the |
| 174 | renderer threads is to run in single-process mode by adding |
| 175 | `@OnlyRunIn(SINGLE_PROCESS)` above the test. |
| 176 | |
| 177 | ##### conventions |
| 178 | |
| 179 | | Label | | |
| 180 | |-----------|----------------------------------------------------------------| |
| 181 | | (shell) | in your workstation's shell | |
| 182 | | (phone) | inside the phone's shell which you entered through `adb shell` | |
| 183 | |
| 184 | ```sh |
| 185 | # Find the pid |
| 186 | $ (shell) adb root |
| 187 | $ (shell) adb shell |
| 188 | |
| 189 | # Get the main WebView Shell pid, e.g. org.chromium.android_webview.shell and |
| 190 | # not org.chromium.android_webview.shell:sandboxed_process0 |
| 191 | $ (phone) ps -A | grep org.chromium.android_webview.shell |
| 192 | # Generate a callstack (this won't kill the process) |
| 193 | $ (phone) kill -3 pid |
| 194 | # Look for the latest trace |
| 195 | $ (phone) ls /data/anr/ -l |
| 196 | # Copy the trace locally |
| 197 | $ (shell) adb pull /data/anr/trace_01 /tmp/t1 |
| 198 | # Generate a callstack. Run this from the source directory. |
| 199 | $ (shell) third_party/android_platform/development/scripts/stack --output-directory=out/Release /tmp/t1 |
| 200 | ``` |
| 201 | |
| 202 | |
Nate Fischer | 867117c3 | 2019-01-16 02:24:33 | [diff] [blame] | 203 | ## External tests |
| 204 | |
| 205 | As WebView is an Android system component, we have some tests defined outside of |
| 206 | the chromium repository, but which the team still maintains. For some of these |
| 207 | tests, we have scripts to help chromium developers check these tests. |
| 208 | |
| 209 | *** promo |
| 210 | All of these tests are end-to-end, so they exercise whatever WebView |
| 211 | implementation you've installed and selected on your device. This also means you |
| 212 | can enable Features and commandline flags the same way [as you would for |
Nate Fischer | 19d894a4 | 2019-01-24 18:55:22 | [diff] [blame] | 213 | production](./commandline-flags.md). |
Nate Fischer | 867117c3 | 2019-01-16 02:24:33 | [diff] [blame] | 214 | *** |
| 215 | |
| 216 | ### CTS |
| 217 | |
| 218 | WebView has [CTS](https://source.android.com/compatibility/cts) tests, testing |
Nate Fischer | 19d894a4 | 2019-01-24 18:55:22 | [diff] [blame] | 219 | end-to-end behavior (using the WebView installed on the system). These tests |
| 220 | live in the Android source tree (under `//platform/cts/tests/tests/webkit/`). |
Nate Fischer | 867117c3 | 2019-01-16 02:24:33 | [diff] [blame] | 221 | |
Nate Fischer | 19d894a4 | 2019-01-24 18:55:22 | [diff] [blame] | 222 | Chromium developers can download and run pre-built APKs for these test cases |
| 223 | with: |
Nate Fischer | 867117c3 | 2019-01-16 02:24:33 | [diff] [blame] | 224 | |
| 225 | ```sh |
Nate Fischer | 19d894a4 | 2019-01-24 18:55:22 | [diff] [blame] | 226 | # Install the desired WebView APK |
| 227 | ... |
| 228 | |
| 229 | # Run pre-built WebView CTS tests: |
Nate Fischer | 867117c3 | 2019-01-16 02:24:33 | [diff] [blame] | 230 | $ android_webview/tools/run_cts.py \ |
Nate Fischer | 867117c3 | 2019-01-16 02:24:33 | [diff] [blame] | 231 | --verbose \ # Optional |
| 232 | -f=android.webkit.cts.WebViewTest#* # Supports similar test filters |
Nate Fischer | 4bd3cc9 | 2019-12-06 02:14:42 | [diff] [blame] | 233 | |
| 234 | # Print both Java and C++ log messages to the console (optional): |
| 235 | $ adb logcat |
Nate Fischer | 867117c3 | 2019-01-16 02:24:33 | [diff] [blame] | 236 | ``` |
| 237 | |
Nate Fischer | f8e32dbd | 2020-07-20 19:31:47 | [diff] [blame] | 238 | *** promo |
| 239 | **Tip:** make sure your device locale is **English (United States)** per |
| 240 | [CTS setup requirements](https://source.android.com/compatibility/cts/setup). |
| 241 | *** |
| 242 | |
Andrew Luo | a710799 | 2019-09-25 19:58:11 | [diff] [blame] | 243 | To disable failing CTS tests, please see the cts_config |
| 244 | [README](../tools/cts_config/README.md) file. |
| 245 | |
Nate Fischer | 867117c3 | 2019-01-16 02:24:33 | [diff] [blame] | 246 | If you'd like to edit these tests, see internal documentation at |
| 247 | http://go/clank-webview for working with Android checkouts. |
| 248 | |
| 249 | ### AndroidX (Support Library) |
| 250 | |
| 251 | WebView also has an AndroidX module, which has its own tests (similar to CTS |
| 252 | tests). These tests live under the AOSP source tree, under |
| 253 | `//platform/frameworks/support/`. |
| 254 | |
Nate Fischer | 84c05a4 | 2021-11-29 22:29:39 | [diff] [blame] | 255 | TODO(ntfschr): document the solution for https://crbug.com/891102, when that's |
Nate Fischer | 867117c3 | 2019-01-16 02:24:33 | [diff] [blame] | 256 | fixed. |