Use WebRTC's Java VM initialization in tests.

WebRTC should not depend on chromium's //base.

Bug: webrtc:13662
Change-Id: Ie660aa0f2477cc747830bba611aa23ed5e732385
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/256364
Reviewed-by: Ilya Nikolaevskiy <ilnik@webrtc.org>
Reviewed-by: Xavier Lepaul‎ <xalep@webrtc.org>
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Reviewed-by: Henrik Andreassson <henrika@webrtc.org>
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Commit-Queue: Björn Terelius <terelius@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#36581}
This commit is contained in:
Björn Terelius
2022-04-19 15:32:37 +02:00
committed by WebRTC LUCI CQ
parent b1dc4e43f0
commit 0c68a7aaa7
14 changed files with 109 additions and 80 deletions

View File

@ -643,7 +643,12 @@ if (rtc_include_tests && !build_with_chromium) {
] ]
data = video_engine_tests_resources data = video_engine_tests_resources
if (is_android) { if (is_android) {
deps += [ "//testing/android/native_test:native_test_native_code" ] use_default_launcher = false
deps += [
"//build/android/gtest_apk:native_test_instrumentation_test_runner_java",
"//testing/android/native_test:native_test_java",
"//testing/android/native_test:native_test_support",
]
shard_timeout = 900 shard_timeout = 900
} }
if (is_ios) { if (is_ios) {
@ -688,7 +693,12 @@ if (rtc_include_tests && !build_with_chromium) {
data = webrtc_perf_tests_resources data = webrtc_perf_tests_resources
if (is_android) { if (is_android) {
deps += [ "//testing/android/native_test:native_test_native_code" ] use_default_launcher = false
deps += [
"//build/android/gtest_apk:native_test_instrumentation_test_runner_java",
"//testing/android/native_test:native_test_java",
"//testing/android/native_test:native_test_support",
]
shard_timeout = 4500 shard_timeout = 4500
} }
if (is_ios) { if (is_ios) {

View File

@ -243,7 +243,12 @@ if (rtc_include_tests) {
"//third_party/abseil-cpp/absl/strings", "//third_party/abseil-cpp/absl/strings",
] ]
if (is_android) { if (is_android) {
deps += [ "//testing/android/native_test:native_test_native_code" ] use_default_launcher = false
deps += [
"//build/android/gtest_apk:native_test_instrumentation_test_runner_java",
"//testing/android/native_test:native_test_java",
"//testing/android/native_test:native_test_support",
]
} }
data = [ data = [
"../resources/voice_engine/audio_tiny16.wav", "../resources/voice_engine/audio_tiny16.wav",

View File

@ -82,12 +82,14 @@ if (rtc_include_tests && !build_with_chromium) {
data = modules_tests_resources data = modules_tests_resources
if (is_android) { if (is_android) {
use_default_launcher = false
deps += [ deps += [
# NOTE(brandtr): Including Java classes seems only to be possible from # NOTE(brandtr): Including Java classes seems only to be possible from
# rtc_test targets. Therefore we include this target here, instead of # rtc_test targets. Therefore we include this target here, instead of
# in video_coding_modules_tests, where it is actually used. # in video_coding_modules_tests, where it is actually used.
"../sdk/android:libjingle_peerconnection_java", "../sdk/android:libjingle_peerconnection_java",
"//testing/android/native_test:native_test_native_code", "//sdk/android:native_test_jni_onload",
"//testing/android/native_test:native_test_support",
] ]
shard_timeout = 900 shard_timeout = 900
} }
@ -231,6 +233,7 @@ if (rtc_include_tests && !build_with_chromium) {
data = modules_unittests_resources data = modules_unittests_resources
if (is_android) { if (is_android) {
use_default_launcher = false
deps += [ deps += [
"../sdk/android:libjingle_peerconnection_java", "../sdk/android:libjingle_peerconnection_java",
"//testing/android/native_test:native_test_support", "//testing/android/native_test:native_test_support",

View File

@ -1543,7 +1543,12 @@ if (rtc_include_tests) {
data = audio_decoder_unittests_resources data = audio_decoder_unittests_resources
if (is_android) { if (is_android) {
deps += [ "//testing/android/native_test:native_test_native_code" ] use_default_launcher = false
deps += [
"//build/android/gtest_apk:native_test_instrumentation_test_runner_java",
"//testing/android/native_test:native_test_java",
"//testing/android/native_test:native_test_support",
]
shard_timeout = 900 shard_timeout = 900
} }
if (is_ios) { if (is_ios) {
@ -1634,7 +1639,12 @@ if (rtc_include_tests) {
data = audio_codec_speed_tests_resources data = audio_codec_speed_tests_resources
if (is_android) { if (is_android) {
deps += [ "//testing/android/native_test:native_test_native_code" ] use_default_launcher = false
deps += [
"//build/android/gtest_apk:native_test_instrumentation_test_runner_java",
"//testing/android/native_test:native_test_java",
"//testing/android/native_test:native_test_support",
]
shard_timeout = 900 shard_timeout = 900
} }

View File

@ -441,11 +441,6 @@ if (rtc_include_tests && !build_with_chromium) {
] ]
} }
if (is_android) { if (is_android) {
# Need to disable error due to the line in
# base/android/jni_android.h triggering it:
# const BASE_EXPORT jobject GetApplicationContext()
# error: type qualifiers ignored on function return type
cflags = [ "-Wno-ignored-qualifiers" ]
sources += [ sources += [
"android/audio_device_unittest.cc", "android/audio_device_unittest.cc",
"android/audio_manager_unittest.cc", "android/audio_manager_unittest.cc",
@ -453,8 +448,10 @@ if (rtc_include_tests && !build_with_chromium) {
"android/ensure_initialized.h", "android/ensure_initialized.h",
] ]
deps += [ deps += [
"../../base", "../../sdk/android:internal_jni",
"../../sdk/android:libjingle_peerconnection_java", "../../sdk/android:libjingle_peerconnection_java",
"../../sdk/android:native_api_jni",
"../../sdk/android:native_test_jni_onload",
] ]
} }
if (!rtc_include_internal_audio_device) { if (!rtc_include_internal_audio_device) {

View File

@ -5,7 +5,7 @@ include_rules = [
specific_include_rules = { specific_include_rules = {
"ensure_initialized\.cc": [ "ensure_initialized\.cc": [
"+base/android", "+sdk/android",
], ],
"audio_device_impl\.cc": [ "audio_device_impl\.cc": [
"+sdk/objc", "+sdk/objc",

View File

@ -10,19 +10,13 @@
#include "modules/audio_device/android/ensure_initialized.h" #include "modules/audio_device/android/ensure_initialized.h"
#include <jni.h>
#include <pthread.h> #include <pthread.h>
#include <stddef.h>
#include "rtc_base/ignore_wundef.h"
// Note: this dependency is dangerous since it reaches into Chromium's base.
// There's a risk of e.g. macro clashes. This file may only be used in tests.
RTC_PUSH_IGNORING_WUNDEF()
#include "base/android/jni_android.h"
RTC_POP_IGNORING_WUNDEF()
#include "modules/audio_device/android/audio_record_jni.h"
#include "modules/audio_device/android/audio_track_jni.h"
#include "modules/utility/include/jvm_android.h" #include "modules/utility/include/jvm_android.h"
#include "rtc_base/checks.h" #include "rtc_base/checks.h"
#include "sdk/android/src/jni/jvm.h"
namespace webrtc { namespace webrtc {
namespace audiodevicemodule { namespace audiodevicemodule {
@ -30,8 +24,9 @@ namespace audiodevicemodule {
static pthread_once_t g_initialize_once = PTHREAD_ONCE_INIT; static pthread_once_t g_initialize_once = PTHREAD_ONCE_INIT;
void EnsureInitializedOnce() { void EnsureInitializedOnce() {
RTC_CHECK(::base::android::IsVMInitialized()); RTC_CHECK(::webrtc::jni::GetJVM() != nullptr);
JNIEnv* jni = ::base::android::AttachCurrentThread();
JNIEnv* jni = ::webrtc::jni::AttachCurrentThreadIfNeeded();
JavaVM* jvm = NULL; JavaVM* jvm = NULL;
RTC_CHECK_EQ(0, jni->GetJavaVM(&jvm)); RTC_CHECK_EQ(0, jni->GetJavaVM(&jvm));

View File

@ -826,13 +826,14 @@ if (rtc_include_tests) {
deps = [ deps = [
"../../api/video_codecs:video_codecs_api", "../../api/video_codecs:video_codecs_api",
"../../modules/utility:utility",
"../../rtc_base:checks", "../../rtc_base:checks",
"../../rtc_base:ignore_wundef", "../../rtc_base:ignore_wundef",
"../../rtc_base:rtc_base_approved", "../../rtc_base:rtc_base_approved",
"../../sdk/android:internal_jni",
"../../sdk/android:native_api_base", "../../sdk/android:native_api_base",
"../../sdk/android:native_api_codecs", "../../sdk/android:native_api_codecs",
"../../sdk/android:native_api_jni", "../../sdk/android:native_api_jni",
"//base",
] ]
} }
} }

View File

@ -10,25 +10,19 @@
#include "modules/video_coding/codecs/test/android_codec_factory_helper.h" #include "modules/video_coding/codecs/test/android_codec_factory_helper.h"
#include <jni.h>
#include <pthread.h> #include <pthread.h>
#include <stddef.h>
#include <memory> #include <memory>
#include "modules/utility/include/jvm_android.h"
#include "rtc_base/checks.h" #include "rtc_base/checks.h"
#include "rtc_base/ignore_wundef.h"
#include "sdk/android/native_api/base/init.h"
#include "sdk/android/native_api/codecs/wrapper.h" #include "sdk/android/native_api/codecs/wrapper.h"
#include "sdk/android/native_api/jni/class_loader.h" #include "sdk/android/native_api/jni/class_loader.h"
#include "sdk/android/native_api/jni/jvm.h" #include "sdk/android/native_api/jni/jvm.h"
#include "sdk/android/native_api/jni/scoped_java_ref.h" #include "sdk/android/native_api/jni/scoped_java_ref.h"
#include "sdk/android/src/jni/jvm.h"
// Note: this dependency is dangerous since it reaches into Chromium's base.
// There's a risk of e.g. macro clashes. This file may only be used in tests.
// Since we use Chrome's build system for creating the gtest binary, this should
// be fine.
RTC_PUSH_IGNORING_WUNDEF()
#include "base/android/jni_android.h"
RTC_POP_IGNORING_WUNDEF()
namespace webrtc { namespace webrtc {
namespace test { namespace test {
@ -37,16 +31,15 @@ namespace {
static pthread_once_t g_initialize_once = PTHREAD_ONCE_INIT; static pthread_once_t g_initialize_once = PTHREAD_ONCE_INIT;
// There can only be one JNI_OnLoad in each binary. So since this is a GTEST
// C++ runner binary, we want to initialize the same global objects we normally
// do if this had been a Java binary.
void EnsureInitializedOnce() { void EnsureInitializedOnce() {
RTC_CHECK(::base::android::IsVMInitialized()); RTC_CHECK(::webrtc::jni::GetJVM() != nullptr);
JNIEnv* env = ::base::android::AttachCurrentThread();
JavaVM* jvm = nullptr;
RTC_CHECK_EQ(0, env->GetJavaVM(&jvm));
InitAndroid(jvm); JNIEnv* jni = ::webrtc::jni::AttachCurrentThreadIfNeeded();
JavaVM* jvm = NULL;
RTC_CHECK_EQ(0, jni->GetJavaVM(&jvm));
// Initialize the Java environment (currently only used by the audio manager).
webrtc::JVM::Initialize(jvm);
} }
} // namespace } // namespace

View File

@ -2150,7 +2150,12 @@ if (rtc_include_tests && !build_with_chromium) {
} }
if (is_android) { if (is_android) {
deps += [ "//testing/android/native_test:native_test_support" ] use_default_launcher = false
deps += [
"//build/android/gtest_apk:native_test_instrumentation_test_runner_java",
"//testing/android/native_test:native_test_java",
"//testing/android/native_test:native_test_support",
]
} }
} }
@ -2393,8 +2398,18 @@ if (rtc_include_tests && !build_with_chromium) {
"//third_party/abseil-cpp/absl/strings", "//third_party/abseil-cpp/absl/strings",
"//third_party/abseil-cpp/absl/types:optional", "//third_party/abseil-cpp/absl/types:optional",
] ]
if (is_android) { if (is_android) {
deps += [ ":android_black_magic" ] use_default_launcher = false
deps += [
":android_black_magic",
# We need to depend on this one directly, or classloads will fail for
# the voice engine BuildInfo, for instance.
"//sdk/android:libjingle_peerconnection_java",
"//sdk/android:native_test_jni_onload",
]
shard_timeout = 900
} }
deps += [ deps += [
@ -2432,33 +2447,23 @@ if (rtc_include_tests && !build_with_chromium) {
"../test:test_main", "../test:test_main",
"../test:test_support", "../test:test_support",
] ]
if (is_android) {
deps += [
"//testing/android/native_test:native_test_support",
# We need to depend on this one directly, or classloads will fail for
# the voice engine BuildInfo, for instance.
"../sdk/android:libjingle_peerconnection_java",
]
shard_timeout = 900
}
} }
if (is_android) { if (is_android) {
rtc_library("android_black_magic") { rtc_library("android_black_magic") {
# The android code uses hacky includes to chromium-base and the ssl code; # The android code uses hacky includes to ssl code. Having this in a
# having this in a separate target enables us to keep the peerconnection # separate target enables us to keep the peerconnection unit tests clean.
# unit tests clean.
check_includes = false
testonly = true testonly = true
sources = [ sources = [
"test/android_test_initializer.cc", "test/android_test_initializer.cc",
"test/android_test_initializer.h", "test/android_test_initializer.h",
] ]
deps = [ deps = [
"../sdk/android:internal_jni",
"../sdk/android:libjingle_peerconnection_jni", "../sdk/android:libjingle_peerconnection_jni",
"//modules/utility:utility",
"//rtc_base:checks",
"//rtc_base:rtc_base",
"//testing/android/native_test:native_test_support", "//testing/android/native_test:native_test_support",
] ]
} }

View File

@ -1,6 +1,5 @@
include_rules = [ include_rules = [
# Allow include of Chrome base/android to allow inclusion of headers needed # Allow include of sdk/android to allow accessing the JVM and Env in tests.
# for accessing the JVM and Application context in gtest. "+sdk/android",
"+base/android",
"+modules/utility/include/jvm_android.h", "+modules/utility/include/jvm_android.h",
] ]

View File

@ -10,22 +10,14 @@
#include "pc/test/android_test_initializer.h" #include "pc/test/android_test_initializer.h"
#include <jni.h>
#include <pthread.h> #include <pthread.h>
#include <stddef.h>
#include "rtc_base/ignore_wundef.h"
// Note: this dependency is dangerous since it reaches into Chromium's base.
// There's a risk of e.g. macro clashes. This file may only be used in tests.
// Since we use Chromes build system for creating the gtest binary, this should
// be fine.
RTC_PUSH_IGNORING_WUNDEF()
#include "base/android/jni_android.h"
RTC_POP_IGNORING_WUNDEF()
#include "modules/utility/include/jvm_android.h" #include "modules/utility/include/jvm_android.h"
#include "rtc_base/checks.h" #include "rtc_base/checks.h"
#include "sdk/android/src/jni/jvm.h"
// TODO(phoglund): This include is also to a target we can't really depend on. // TODO(phoglund): This include is to a target we can't really depend on.
// We need to either break it out into a smaller target or find some way to // We need to either break it out into a smaller target or find some way to
// not use it. // not use it.
#include "rtc_base/ssl_adapter.h" #include "rtc_base/ssl_adapter.h"
@ -40,8 +32,8 @@ static pthread_once_t g_initialize_once = PTHREAD_ONCE_INIT;
// C++ runner binary, we want to initialize the same global objects we normally // C++ runner binary, we want to initialize the same global objects we normally
// do if this had been a Java binary. // do if this had been a Java binary.
void EnsureInitializedOnce() { void EnsureInitializedOnce() {
RTC_CHECK(::base::android::IsVMInitialized()); RTC_CHECK(::webrtc::jni::GetJVM() != nullptr);
JNIEnv* jni = ::base::android::AttachCurrentThread(); JNIEnv* jni = ::webrtc::jni::AttachCurrentThreadIfNeeded();
JavaVM* jvm = NULL; JavaVM* jvm = NULL;
RTC_CHECK_EQ(0, jni->GetJavaVM(&jvm)); RTC_CHECK_EQ(0, jni->GetJavaVM(&jvm));

View File

@ -1599,6 +1599,20 @@ if (is_android) {
] ]
} }
rtc_library("native_test_jni_onload") {
testonly = true
sources = [ "native_unittests/test_jni_onload.cc" ]
deps = [
":base_jni",
":internal_jni",
":native_api_base",
":native_api_jni",
"../../rtc_base:checks",
]
}
rtc_library("native_unittests") { rtc_library("native_unittests") {
testonly = true testonly = true
@ -1611,7 +1625,6 @@ if (is_android) {
"native_unittests/java_types_unittest.cc", "native_unittests/java_types_unittest.cc",
"native_unittests/peerconnection/peer_connection_factory_unittest.cc", "native_unittests/peerconnection/peer_connection_factory_unittest.cc",
"native_unittests/stacktrace/stacktrace_unittest.cc", "native_unittests/stacktrace/stacktrace_unittest.cc",
"native_unittests/test_jni_onload.cc",
"native_unittests/video/video_source_unittest.cc", "native_unittests/video/video_source_unittest.cc",
] ]
@ -1632,6 +1645,7 @@ if (is_android) {
":native_api_peerconnection", ":native_api_peerconnection",
":native_api_stacktrace", ":native_api_stacktrace",
":native_api_video", ":native_api_video",
":native_test_jni_onload",
":opensles_audio_device_module", ":opensles_audio_device_module",
":video_jni", ":video_jni",
"../../api:field_trials_view", "../../api:field_trials_view",

View File

@ -67,7 +67,12 @@ if (rtc_include_tests && !build_with_chromium) {
] ]
if (is_android) { if (is_android) {
deps += [ "//testing/android/native_test:native_test_native_code" ] use_default_launcher = false
deps += [
"//build/android/gtest_apk:native_test_instrumentation_test_runner_java",
"//testing/android/native_test:native_test_java",
"//testing/android/native_test:native_test_support",
]
} }
} }
} }