Implements JavaToNativeStringMap and adds tests for native API.
Implements JavaToNativeStringMap that is a replacement for JavaToStdMapStrings. It uses a new template method JavaToNativeMap. Also adds testing support for native API and a test for JavaToNativeStringMap. Bug: webrtc:8769 Change-Id: I580d4992a899ebe02da39af450fa51d52ee9b88b Reviewed-on: https://webrtc-review.googlesource.com/48060 Reviewed-by: Magnus Jedvert <magjed@webrtc.org> Reviewed-by: Patrik Höglund <phoglund@webrtc.org> Commit-Queue: Sami Kalliomäki <sakal@webrtc.org> Cr-Commit-Position: refs/heads/master@{#21967}
This commit is contained in:

committed by
Commit Bot

parent
1bc1ec43a3
commit
78498cf5ee
9
BUILD.gn
9
BUILD.gn
@ -446,7 +446,14 @@ if (rtc_include_tests) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (is_android) {
|
if (is_android) {
|
||||||
deps += [ "//testing/android/native_test:native_test_support" ]
|
# Do not use Chromium's launcher. native_unittests defines its own JNI_OnLoad.
|
||||||
|
use_default_launcher = false
|
||||||
|
|
||||||
|
deps += [
|
||||||
|
"sdk/android:native_unittests",
|
||||||
|
"sdk/android:native_unittests_java",
|
||||||
|
"//testing/android/native_test:native_test_support",
|
||||||
|
]
|
||||||
shard_timeout = 900
|
shard_timeout = 900
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -892,3 +892,41 @@ rtc_static_library("native_api_codecs") {
|
|||||||
"//rtc_base:rtc_base_approved",
|
"//rtc_base:rtc_base_approved",
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
generate_jni("generated_native_unittests_jni") {
|
||||||
|
testonly = true
|
||||||
|
|
||||||
|
sources = [
|
||||||
|
"native_unittests/org/webrtc/JavaTypesTestHelper.java",
|
||||||
|
]
|
||||||
|
jni_package = ""
|
||||||
|
jni_generator_include = "//sdk/android/src/jni/jni_generator_helper.h"
|
||||||
|
}
|
||||||
|
|
||||||
|
rtc_android_library("native_unittests_java") {
|
||||||
|
testonly = true
|
||||||
|
|
||||||
|
java_files = [ "native_unittests/org/webrtc/JavaTypesTestHelper.java" ]
|
||||||
|
|
||||||
|
deps = [
|
||||||
|
":libjingle_peerconnection_java",
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
rtc_source_set("native_unittests") {
|
||||||
|
testonly = true
|
||||||
|
|
||||||
|
sources = [
|
||||||
|
"native_unittests/java_types_unittest.cc",
|
||||||
|
"native_unittests/test_jni_onload.cc",
|
||||||
|
]
|
||||||
|
|
||||||
|
deps = [
|
||||||
|
":generated_native_unittests_jni",
|
||||||
|
":native_api_base",
|
||||||
|
":native_api_jni",
|
||||||
|
"//rtc_base:checks",
|
||||||
|
"//test:test_support",
|
||||||
|
"//testing/gtest",
|
||||||
|
]
|
||||||
|
}
|
||||||
|
@ -30,6 +30,8 @@ namespace webrtc {
|
|||||||
Iterable::Iterable(JNIEnv* jni, const JavaRef<jobject>& iterable)
|
Iterable::Iterable(JNIEnv* jni, const JavaRef<jobject>& iterable)
|
||||||
: jni_(jni), iterable_(jni, iterable) {}
|
: jni_(jni), iterable_(jni, iterable) {}
|
||||||
|
|
||||||
|
Iterable::Iterable(Iterable&& other) = default;
|
||||||
|
|
||||||
Iterable::~Iterable() = default;
|
Iterable::~Iterable() = default;
|
||||||
|
|
||||||
// Creates an iterator representing the end of any collection.
|
// Creates an iterator representing the end of any collection.
|
||||||
@ -103,6 +105,22 @@ std::string GetJavaEnumName(JNIEnv* jni, const JavaRef<jobject>& j_enum) {
|
|||||||
return JavaToStdString(jni, JNI_Enum::Java_Enum_name(jni, j_enum));
|
return JavaToStdString(jni, JNI_Enum::Java_Enum_name(jni, j_enum));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Iterable GetJavaMapEntrySet(JNIEnv* jni, const JavaRef<jobject>& j_map) {
|
||||||
|
return Iterable(jni, JNI_Map::Java_Map_entrySet(jni, j_map));
|
||||||
|
}
|
||||||
|
|
||||||
|
ScopedJavaLocalRef<jobject> GetJavaMapEntryKey(
|
||||||
|
JNIEnv* jni,
|
||||||
|
const JavaRef<jobject>& j_entry) {
|
||||||
|
return Java_JniHelper_getKey(jni, j_entry);
|
||||||
|
}
|
||||||
|
|
||||||
|
ScopedJavaLocalRef<jobject> GetJavaMapEntryValue(
|
||||||
|
JNIEnv* jni,
|
||||||
|
const JavaRef<jobject>& j_entry) {
|
||||||
|
return Java_JniHelper_getValue(jni, j_entry);
|
||||||
|
}
|
||||||
|
|
||||||
int64_t JavaToNativeLong(JNIEnv* env, const JavaRef<jobject>& j_long) {
|
int64_t JavaToNativeLong(JNIEnv* env, const JavaRef<jobject>& j_long) {
|
||||||
return JNI_Long::Java_Long_longValue(env, j_long);
|
return JNI_Long::Java_Long_longValue(env, j_long);
|
||||||
}
|
}
|
||||||
@ -136,6 +154,19 @@ std::string JavaToNativeString(JNIEnv* jni, const JavaRef<jstring>& j_string) {
|
|||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::map<std::string, std::string> JavaToNativeStringMap(
|
||||||
|
JNIEnv* jni,
|
||||||
|
const JavaRef<jobject>& j_map) {
|
||||||
|
return JavaToNativeMap<std::string, std::string>(
|
||||||
|
jni, j_map,
|
||||||
|
[](JNIEnv* env, JavaRef<jobject> const& key,
|
||||||
|
JavaRef<jobject> const& value) {
|
||||||
|
return std::make_pair(
|
||||||
|
JavaToNativeString(env, static_java_ref_cast<jstring>(env, key)),
|
||||||
|
JavaToNativeString(env, static_java_ref_cast<jstring>(env, value)));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
ScopedJavaLocalRef<jobject> NativeToJavaBoolean(JNIEnv* env, bool b) {
|
ScopedJavaLocalRef<jobject> NativeToJavaBoolean(JNIEnv* env, bool b) {
|
||||||
return JNI_Boolean::Java_Boolean_ConstructorJLB_Z(env, b);
|
return JNI_Boolean::Java_Boolean_ConstructorJLB_Z(env, b);
|
||||||
}
|
}
|
||||||
@ -242,18 +273,4 @@ std::vector<std::string> JavaToStdVectorStrings(JNIEnv* jni,
|
|||||||
return converted_list;
|
return converted_list;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::map<std::string, std::string> JavaToStdMapStrings(
|
|
||||||
JNIEnv* jni,
|
|
||||||
const JavaRef<jobject>& j_map) {
|
|
||||||
const JavaRef<jobject>& j_entry_set = JNI_Map::Java_Map_entrySet(jni, j_map);
|
|
||||||
std::map<std::string, std::string> result;
|
|
||||||
for (const JavaRef<jobject>& j_entry : Iterable(jni, j_entry_set)) {
|
|
||||||
result.insert(std::make_pair(
|
|
||||||
JavaToStdString(jni, Java_JniHelper_getKey(jni, j_entry)),
|
|
||||||
JavaToStdString(jni, Java_JniHelper_getValue(jni, j_entry))));
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace webrtc
|
} // namespace webrtc
|
||||||
|
@ -50,6 +50,8 @@ namespace webrtc {
|
|||||||
class Iterable {
|
class Iterable {
|
||||||
public:
|
public:
|
||||||
Iterable(JNIEnv* jni, const JavaRef<jobject>& iterable);
|
Iterable(JNIEnv* jni, const JavaRef<jobject>& iterable);
|
||||||
|
Iterable(Iterable&& other);
|
||||||
|
|
||||||
~Iterable();
|
~Iterable();
|
||||||
|
|
||||||
class Iterator {
|
class Iterator {
|
||||||
@ -111,6 +113,13 @@ bool IsNull(JNIEnv* jni, const JavaRef<jobject>& obj);
|
|||||||
// Returns the name of a Java enum.
|
// Returns the name of a Java enum.
|
||||||
std::string GetJavaEnumName(JNIEnv* jni, const JavaRef<jobject>& j_enum);
|
std::string GetJavaEnumName(JNIEnv* jni, const JavaRef<jobject>& j_enum);
|
||||||
|
|
||||||
|
Iterable GetJavaMapEntrySet(JNIEnv* jni, const JavaRef<jobject>& j_map);
|
||||||
|
ScopedJavaLocalRef<jobject> GetJavaMapEntryKey(JNIEnv* jni,
|
||||||
|
const JavaRef<jobject>& j_entry);
|
||||||
|
ScopedJavaLocalRef<jobject> GetJavaMapEntryValue(
|
||||||
|
JNIEnv* jni,
|
||||||
|
const JavaRef<jobject>& j_entry);
|
||||||
|
|
||||||
// --------------------------------------------------------
|
// --------------------------------------------------------
|
||||||
// -- Methods for converting Java types to native types. --
|
// -- Methods for converting Java types to native types. --
|
||||||
// --------------------------------------------------------
|
// --------------------------------------------------------
|
||||||
@ -141,6 +150,23 @@ std::vector<T> JavaToNativeVector(JNIEnv* env,
|
|||||||
return container;
|
return container;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename Key, typename T, typename Convert>
|
||||||
|
std::map<Key, T> JavaToNativeMap(JNIEnv* env,
|
||||||
|
const JavaRef<jobject>& j_map,
|
||||||
|
Convert convert) {
|
||||||
|
std::map<Key, T> container;
|
||||||
|
for (auto const& j_entry : GetJavaMapEntrySet(env, j_map)) {
|
||||||
|
container.emplace(convert(env, GetJavaMapEntryKey(env, j_entry),
|
||||||
|
GetJavaMapEntryValue(env, j_entry)));
|
||||||
|
}
|
||||||
|
return container;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Converts Map<String, String> to std::map<std::string, std::string>.
|
||||||
|
std::map<std::string, std::string> JavaToNativeStringMap(
|
||||||
|
JNIEnv* env,
|
||||||
|
const JavaRef<jobject>& j_map);
|
||||||
|
|
||||||
// --------------------------------------------------------
|
// --------------------------------------------------------
|
||||||
// -- Methods for converting native types to Java types. --
|
// -- Methods for converting native types to Java types. --
|
||||||
// --------------------------------------------------------
|
// --------------------------------------------------------
|
||||||
@ -262,10 +288,13 @@ inline std::string JavaToStdString(JNIEnv* jni, jstring j_string) {
|
|||||||
std::vector<std::string> JavaToStdVectorStrings(JNIEnv* jni,
|
std::vector<std::string> JavaToStdVectorStrings(JNIEnv* jni,
|
||||||
const JavaRef<jobject>& list);
|
const JavaRef<jobject>& list);
|
||||||
|
|
||||||
|
// Deprecated. Use JavaToNativeStringMap instead.
|
||||||
// Parses Map<String, String> to std::map<std::string, std::string>.
|
// Parses Map<String, String> to std::map<std::string, std::string>.
|
||||||
std::map<std::string, std::string> JavaToStdMapStrings(
|
inline std::map<std::string, std::string> JavaToStdMapStrings(
|
||||||
JNIEnv* jni,
|
JNIEnv* jni,
|
||||||
const JavaRef<jobject>& j_map);
|
const JavaRef<jobject>& j_map) {
|
||||||
|
return JavaToNativeStringMap(jni, j_map);
|
||||||
|
}
|
||||||
|
|
||||||
// Deprecated. Use scoped jobjects instead.
|
// Deprecated. Use scoped jobjects instead.
|
||||||
inline std::map<std::string, std::string> JavaToStdMapStrings(JNIEnv* jni,
|
inline std::map<std::string, std::string> JavaToStdMapStrings(JNIEnv* jni,
|
||||||
|
@ -197,6 +197,13 @@ class ScopedJavaGlobalRef : public JavaRef<T> {
|
|||||||
RTC_DISALLOW_COPY_AND_ASSIGN(ScopedJavaGlobalRef);
|
RTC_DISALLOW_COPY_AND_ASSIGN(ScopedJavaGlobalRef);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline ScopedJavaLocalRef<T> static_java_ref_cast(JNIEnv* env,
|
||||||
|
JavaRef<jobject> const& ref) {
|
||||||
|
ScopedJavaLocalRef<jobject> owned_ref(env, ref);
|
||||||
|
return ScopedJavaLocalRef<T>(env, static_cast<T>(owned_ref.Release()));
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace webrtc
|
} // namespace webrtc
|
||||||
|
|
||||||
#endif // SDK_ANDROID_NATIVE_API_JNI_SCOPED_JAVA_REF_H_
|
#endif // SDK_ANDROID_NATIVE_API_JNI_SCOPED_JAVA_REF_H_
|
||||||
|
35
sdk/android/native_unittests/java_types_unittest.cc
Normal file
35
sdk/android/native_unittests/java_types_unittest.cc
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
|
||||||
|
*
|
||||||
|
* Use of this source code is governed by a BSD-style license
|
||||||
|
* that can be found in the LICENSE file in the root of the source
|
||||||
|
* tree. An additional intellectual property rights grant can be found
|
||||||
|
* in the file PATENTS. All contributing project authors may
|
||||||
|
* be found in the AUTHORS file in the root of the source tree.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "sdk/android/generated_native_unittests_jni/jni/JavaTypesTestHelper_jni.h"
|
||||||
|
#include "sdk/android/native_api/jni/java_types.h"
|
||||||
|
#include "test/gtest.h"
|
||||||
|
|
||||||
|
namespace webrtc {
|
||||||
|
namespace test {
|
||||||
|
namespace {
|
||||||
|
TEST(JavaTypesTest, TestJavaToNativeStringMap) {
|
||||||
|
JNIEnv* env = AttachCurrentThreadIfNeeded();
|
||||||
|
ScopedJavaLocalRef<jobject> j_map =
|
||||||
|
Java_JavaTypesTestHelper_createTestStringMap(env);
|
||||||
|
|
||||||
|
std::map<std::string, std::string> output = JavaToNativeStringMap(env, j_map);
|
||||||
|
|
||||||
|
std::map<std::string, std::string> expected{
|
||||||
|
{"one", "1"}, {"two", "2"}, {"three", "3"},
|
||||||
|
};
|
||||||
|
EXPECT_EQ(expected, output);
|
||||||
|
}
|
||||||
|
} // namespace
|
||||||
|
} // namespace test
|
||||||
|
} // namespace webrtc
|
@ -0,0 +1,25 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
|
||||||
|
*
|
||||||
|
* Use of this source code is governed by a BSD-style license
|
||||||
|
* that can be found in the LICENSE file in the root of the source
|
||||||
|
* tree. An additional intellectual property rights grant can be found
|
||||||
|
* in the file PATENTS. All contributing project authors may
|
||||||
|
* be found in the AUTHORS file in the root of the source tree.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.webrtc;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class JavaTypesTestHelper {
|
||||||
|
@CalledByNative
|
||||||
|
public static Map createTestStringMap() {
|
||||||
|
Map<String, String> testMap = new HashMap<String, String>();
|
||||||
|
testMap.put("one", "1");
|
||||||
|
testMap.put("two", "2");
|
||||||
|
testMap.put("three", "3");
|
||||||
|
return testMap;
|
||||||
|
}
|
||||||
|
}
|
23
sdk/android/native_unittests/test_jni_onload.cc
Normal file
23
sdk/android/native_unittests/test_jni_onload.cc
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
|
||||||
|
*
|
||||||
|
* Use of this source code is governed by a BSD-style license
|
||||||
|
* that can be found in the LICENSE file in the root of the source
|
||||||
|
* tree. An additional intellectual property rights grant can be found
|
||||||
|
* in the file PATENTS. All contributing project authors may
|
||||||
|
* be found in the AUTHORS file in the root of the source tree.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <jni.h>
|
||||||
|
#undef JNIEXPORT
|
||||||
|
#define JNIEXPORT __attribute__((visibility("default")))
|
||||||
|
|
||||||
|
#include "rtc_base/checks.h"
|
||||||
|
#include "sdk/android/native_api/base/init.h"
|
||||||
|
#include "sdk/android/native_api/jni/java_types.h"
|
||||||
|
|
||||||
|
// This is called by the VM when the shared library is first loaded.
|
||||||
|
JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) {
|
||||||
|
webrtc::InitAndroid(vm);
|
||||||
|
return JNI_VERSION_1_4;
|
||||||
|
}
|
@ -36,13 +36,13 @@ class JniHelper {
|
|||||||
|
|
||||||
// TODO(bugs.webrtc.org/8606): Remove.
|
// TODO(bugs.webrtc.org/8606): Remove.
|
||||||
@CalledByNative
|
@CalledByNative
|
||||||
static String getKey(Map.Entry<String, String> entry) {
|
static Object getKey(Map.Entry entry) {
|
||||||
return entry.getKey();
|
return entry.getKey();
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO(bugs.webrtc.org/8606): Remove.
|
// TODO(bugs.webrtc.org/8606): Remove.
|
||||||
@CalledByNative
|
@CalledByNative
|
||||||
static String getValue(Map.Entry<String, String> entry) {
|
static Object getValue(Map.Entry entry) {
|
||||||
return entry.getValue();
|
return entry.getValue();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,8 +23,8 @@ static jboolean JNI_HardwareVideoEncoderFactory_IsSameH264Profile(
|
|||||||
const JavaParamRef<jclass>&,
|
const JavaParamRef<jclass>&,
|
||||||
const JavaParamRef<jobject>& params1,
|
const JavaParamRef<jobject>& params1,
|
||||||
const JavaParamRef<jobject>& params2) {
|
const JavaParamRef<jobject>& params2) {
|
||||||
return H264::IsSameH264Profile(JavaToStdMapStrings(jni, params1),
|
return H264::IsSameH264Profile(JavaToNativeStringMap(jni, params1),
|
||||||
JavaToStdMapStrings(jni, params2));
|
JavaToNativeStringMap(jni, params2));
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace jni
|
} // namespace jni
|
||||||
|
@ -20,8 +20,8 @@ namespace jni {
|
|||||||
SdpVideoFormat VideoCodecInfoToSdpVideoFormat(JNIEnv* jni,
|
SdpVideoFormat VideoCodecInfoToSdpVideoFormat(JNIEnv* jni,
|
||||||
const JavaRef<jobject>& j_info) {
|
const JavaRef<jobject>& j_info) {
|
||||||
return SdpVideoFormat(
|
return SdpVideoFormat(
|
||||||
JavaToStdString(jni, Java_VideoCodecInfo_getName(jni, j_info)),
|
JavaToNativeString(jni, Java_VideoCodecInfo_getName(jni, j_info)),
|
||||||
JavaToStdMapStrings(jni, Java_VideoCodecInfo_getParams(jni, j_info)));
|
JavaToNativeStringMap(jni, Java_VideoCodecInfo_getParams(jni, j_info)));
|
||||||
}
|
}
|
||||||
|
|
||||||
ScopedJavaLocalRef<jobject> SdpVideoFormatToVideoCodecInfo(
|
ScopedJavaLocalRef<jobject> SdpVideoFormatToVideoCodecInfo(
|
||||||
|
Reference in New Issue
Block a user