Skip to content

Commit

Permalink
Android: Enable mocking for @NativeMethods in instrumentation tests.
Browse files Browse the repository at this point in the history
It was already implemented, but not enabled.

Change-Id: I4f683032e6c769115cf436544e44564e9501a1d6
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1682316
Reviewed-by: Andrew Grieve <agrieve@chromium.org>
Commit-Queue: Eric Stevenson <estevenson@chromium.org>
Cr-Commit-Position: refs/heads/master@{#674145}
  • Loading branch information
Eric Stevenson authored and Commit Bot committed Jul 2, 2019
1 parent bb0180e commit 9dc6c29
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
13 changes: 4 additions & 9 deletions base/android/jni_generator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,14 @@ class NewStyle {

#### Testing Mockable Natives

(Currently JniMocker only works for junit/robolectric tests, and is not yet
ready for instrumentation tests.)

1. Add the `JniMocker` rule to your test.
2. Call `JniMocker#mock` in a `setUp()` method for each interface you want to
stub out.

Note: Mocking native methods doesn't work in tests that are part of APKs that
use an [`apk_under_test`](https://cs.chromium.org/search/?q=file:BUILD.gn+%22apk_under_test+%3D%22&type=cs).
[This crbug](http://crbug.com/890452) tracks removing the `apk_under_test` variable.

JniMocker will reset the stubs during `tearDown()`.

```java
Expand Down Expand Up @@ -249,9 +250,3 @@ for more about the GN templates.

* Python unit tests live in `jni_generator_tests.py`
* A working demo app exists as `//base/android/jni_generator:sample_jni_apk`

## Known Issues

The `jni_generator` can cause build issues when source files that are used by
JNI are moved due to how `include_dirs` are handled. This is tracked in
[issue 964169](http://crbug.com/964169).
29 changes: 25 additions & 4 deletions build/config/android/rules.gni
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,12 @@ if (enable_java_templates) {
# header_output: Path to the generated .h file (optional).
# sources_blacklist: List of .java files that should be skipped. (optional)
# namespace: Registration functions will be wrapped into this. (optional)
# require_native_mocks: Enforce that any native calls using
# org.chromium.base.annotations.NativeMethods must have a mock set
# (optional).
# enable_native_mocks: Allow native calls using
# org.chromium.base.annotations.NativeMethods to be mocked in tests
# (optional).
#
# Example
# generate_jni_registration("chrome_jni_registration") {
Expand Down Expand Up @@ -414,11 +420,11 @@ if (enable_java_templates) {

if (defined(invoker.enable_native_mocks) && invoker.enable_native_mocks) {
args += [ "--enable_proxy_mocks" ]
}

if (defined(invoker.require_native_mocks) &&
invoker.require_native_mocks) {
args += [ "--require_mocks" ]
if (defined(invoker.require_native_mocks) &&
invoker.require_native_mocks) {
args += [ "--require_mocks" ]
}
}

if (defined(invoker.header_output)) {
Expand Down Expand Up @@ -2029,6 +2035,12 @@ if (enable_java_templates) {
# Optional, default 23.
# max_sdk_version: The maximum Android SDK version this target supports.
# Optional, default not set.
# require_native_mocks: Enforce that any native calls using
# org.chromium.base.annotations.NativeMethods must have a mock set
# (optional).
# enable_native_mocks: Allow native calls using
# org.chromium.base.annotations.NativeMethods to be mocked in tests
# (optional).
template("android_apk_or_module") {
forward_variables_from(invoker, [ "testonly" ])
assert(defined(invoker.final_apk_path) || defined(invoker.name))
Expand Down Expand Up @@ -2639,6 +2651,11 @@ if (enable_java_templates) {

if (_generate_final_jni) {
generate_jni_registration("${_template_name}__final_jni") {
forward_variables_from(invoker,
[
"enable_native_mocks",
"require_native_mocks",
])
target = ":$_template_name"
if (defined(invoker.jni_registration_header)) {
header_output = invoker.jni_registration_header
Expand Down Expand Up @@ -3268,6 +3285,7 @@ if (enable_java_templates) {
"dont_load_shared_libraries",
"enable_chromium_linker_tests",
"enable_multidex",
"enable_native_mocks",
"final_apk_path",
"generate_buildconfig_java",
"generate_final_jni",
Expand Down Expand Up @@ -3302,6 +3320,7 @@ if (enable_java_templates) {
"resource_blacklist_regex",
"resource_ids_provider_dep",
"resources_config_path",
"require_native_mocks",
"secondary_abi_loadable_modules",
"secondary_abi_shared_libraries",
"secondary_native_lib_placeholders",
Expand Down Expand Up @@ -3595,6 +3614,8 @@ if (enable_java_templates) {
]
if (defined(invoker.apk_under_test)) {
data_deps += [ invoker.apk_under_test ]
} else {
enable_native_mocks = true
}
if (defined(invoker.additional_apks)) {
data_deps += invoker.additional_apks
Expand Down

0 comments on commit 9dc6c29

Please sign in to comment.