blob: 2e3d9a415fbc3bc9791c6e8cac8a269c5b43d2fa [file] [log] [blame] [view]
Nate Fischer867117c32019-01-16 02:24:331# WebView Test Instructions
2
Nate Fischer67d7bf22019-02-12 17:01:573[TOC]
4
Nate Fischer867117c32019-01-16 02:24:335## Android Instructions
6
7Please follow the instructions at
Ken Rockot35028ca2019-05-30 18:50:458[android_test_instructions.md](/docs/testing/android_test_instructions.md).
Nate Fischer867117c32019-01-16 02:24:339This 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
19These tests live under `//android_webview/javatests/`, and are mostly
20end-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 Fischer4bd3cc92019-12-06 02:14:4231
32# Print both Java and C++ log messages to the console (optional):
33$ adb logcat
Nate Fischer867117c32019-01-16 02:24:3334```
35
Nate Fischer19d894a42019-01-24 18:55:2236*** aside
37You can optionally use `ClassName.methodName` instead of `ClassName#methodName`;
38the chromium test runner understands either syntax.
39***
40
Drew Stonebraker9ceff9a2019-06-13 20:04:4941### Java unittests
42
43These tests live under `//android_webview/junit/` and use Robolectric.
44
Nate Fischer4bd3cc92019-12-06 02:14:4245*** promo
46**Tip:** Robolectric tests run on workstation and do not need a device or
47emulator. These generally run much faster than on-device tests.
48***
49
Drew Stonebraker9ceff9a2019-06-13 20:04:4950```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 Fischer4bd3cc92019-12-06 02:14:4257
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 Stonebraker9ceff9a2019-06-13 20:04:4961```
62
63*** note
64For junit tests, filter (`-f`) arguments require fully qualified class names
65(e.g. `org.chromium.android_webview.robolectric.FindAddressTest`), but replacing
66the package name with a glob wildcard (`*`), as in the example above, will work
67if the class name is unique.
68***
69
Nate Fischer867117c32019-01-16 02:24:3370### Native unittests
71
72These are any `*_test.cc` or `*_unittest.cc` test under `//android_webview/`.
73Currently, 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 Fischer4bd3cc92019-12-06 02:14:4283
84# Print both Java and C++ log messages to the console (optional):
85$ adb logcat
Nate Fischer867117c32019-01-16 02:24:3386```
87
Nate Fischer19d894a42019-01-24 18:55:2288### Layout tests and page cycler tests
89
Nate Fischerb3e39ac2021-05-05 14:22:2590WebView's layout tests and page cycler tests exercise the **WebView installed on
91the system** and instrument the [system WebView shell app](webview-shell.md)
92(`system_webview_shell_apk`). These test cases are defined in
Nate Fischer19d894a42019-01-24 18:55:2293`//android_webview/tools/system_webview_shell/`.
94
Nate Fischerb3e39ac2021-05-05 14:22:2595*** note
Nate Fischer5a6d48a32021-11-18 03:19:3696**Important:** these tests compile and install both `system_webview_apk` and
97`system_webview_shell_apk`.
Nate Fischerb3e39ac2021-05-05 14:22:2598
Nate Fischer5a6d48a32021-11-18 03:19:3699You will need to configure GN args to make sure `system_webview_apk` is a valid
100WebView provider for your system. Please see the [full build
101instructions](build-instructions.md).
102
103If you are using an **emulator**, you will also need to configure the
104`system_webview_shell_package_name` GN arg. See [WebView shell
105docs](webview-shell.md#setting-up-the-build) for details.
Nate Fischerb3e39ac2021-05-05 14:22:25106***
107
Nate Fischer19d894a42019-01-24 18:55:22108```sh
Nate Fischerb3e39ac2021-05-05 14:22:25109# Build (this also compiles system_webview_shell_apk and system_webview_apk)
Nate Fischer19d894a42019-01-24 18:55:22110$ autoninja -C out/Default system_webview_shell_layout_test_apk
111
Nate Fischerb3e39ac2021-05-05 14:22:25112# Run layout tests (installs the test APK, WebView shell, and
113# system_webview_apk, and also switches your WebView provider)
Nate Fischer19d894a42019-01-24 18:55:22114$ out/Default/bin/run_system_webview_shell_layout_test_apk
Nate Fischer4bd3cc92019-12-06 02:14:42115
116# Print both Java and C++ log messages to the console (optional):
117$ adb logcat
Nate Fischer19d894a42019-01-24 18:55:22118```
119
120To run page cycler tests instead, use the `system_webview_shell_page_cycler_apk`
121target and test runner in the steps above.
122
Nate Fischer0791cf9992021-05-13 15:57:31123### UI tests
124
125Like [layout and page cycler tests](#Layout-tests-and-page-cycler-tests),
126WebView UI tests use the WebView installed on the system (and will automatically
127compile and install the `system_webview_apk` target). Unlike those tests
128however, this test suite _does not_ depend on the system WebView shell app, so
129the setup is simpler. You will still need to follow the [full build
130instructions](build-instructions.md) to correctly configure the
131`system_webview_apk` target, but will not need to worry about compiling the
132WebView 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 Fischer867117c32019-01-16 02:24:33146### 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 Fischer3286faa2021-07-15 21:03:56153 --repeat=100 \ # Repeat up to 100 times for a failure
Nate Fischer867117c32019-01-16 02:24:33154 --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 Fischer1531d3582019-08-06 21:31:17163 --enable-features="MyFeature,MyOtherFeature" \
Nate Fischer867117c32019-01-16 02:24:33164 -f=AwContentsTest#testClearCacheInQuickSuccession
165```
166
John Abd-El-Malek1de03a62021-05-19 22:53:42167#### Debugging hangs in instrumentation tests
168
169If an instrumentation test is hanging, it's possible to get a callstack from the
170browser process. This requires running on a device with root.
171
172It's not possible to get a callstack from the renderer because the sandbox will
173prevent the trace file from being written. A workaround if you want to see the
174renderer 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 Fischer867117c32019-01-16 02:24:33203## External tests
204
205As WebView is an Android system component, we have some tests defined outside of
206the chromium repository, but which the team still maintains. For some of these
207tests, we have scripts to help chromium developers check these tests.
208
209*** promo
210All of these tests are end-to-end, so they exercise whatever WebView
211implementation you've installed and selected on your device. This also means you
212can enable Features and commandline flags the same way [as you would for
Nate Fischer19d894a42019-01-24 18:55:22213production](./commandline-flags.md).
Nate Fischer867117c32019-01-16 02:24:33214***
215
216### CTS
217
218WebView has [CTS](https://source.android.com/compatibility/cts) tests, testing
Nate Fischer19d894a42019-01-24 18:55:22219end-to-end behavior (using the WebView installed on the system). These tests
220live in the Android source tree (under `//platform/cts/tests/tests/webkit/`).
Nate Fischer867117c32019-01-16 02:24:33221
Nate Fischer19d894a42019-01-24 18:55:22222Chromium developers can download and run pre-built APKs for these test cases
223with:
Nate Fischer867117c32019-01-16 02:24:33224
225```sh
Nate Fischer19d894a42019-01-24 18:55:22226# Install the desired WebView APK
227...
228
229# Run pre-built WebView CTS tests:
Nate Fischer867117c32019-01-16 02:24:33230$ android_webview/tools/run_cts.py \
Nate Fischer867117c32019-01-16 02:24:33231 --verbose \ # Optional
232 -f=android.webkit.cts.WebViewTest#* # Supports similar test filters
Nate Fischer4bd3cc92019-12-06 02:14:42233
234# Print both Java and C++ log messages to the console (optional):
235$ adb logcat
Nate Fischer867117c32019-01-16 02:24:33236```
237
Nate Fischerf8e32dbd2020-07-20 19:31:47238*** 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 Luoa7107992019-09-25 19:58:11243To disable failing CTS tests, please see the cts_config
244[README](../tools/cts_config/README.md) file.
245
Nate Fischer867117c32019-01-16 02:24:33246If you'd like to edit these tests, see internal documentation at
247http://go/clank-webview for working with Android checkouts.
248
249### AndroidX (Support Library)
250
251WebView also has an AndroidX module, which has its own tests (similar to CTS
252tests). These tests live under the AOSP source tree, under
253`//platform/frameworks/support/`.
254
Nate Fischer84c05a42021-11-29 22:29:39255TODO(ntfschr): document the solution for https://crbug.com/891102, when that's
Nate Fischer867117c32019-01-16 02:24:33256fixed.