Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / mojo / android / javatests / validation_test_util.cc
blob1fb0abf89f1bc82eed98c5b3c1a426c49ed7315d
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "mojo/android/javatests/validation_test_util.h"
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h"
9 #include "base/android/scoped_java_ref.h"
10 #include "base/test/test_support_android.h"
11 #include "jni/ValidationTestUtil_jni.h"
12 #include "mojo/public/cpp/bindings/tests/validation_test_input_parser.h"
14 namespace mojo {
15 namespace android {
17 bool RegisterValidationTestUtil(JNIEnv* env) {
18 return RegisterNativesImpl(env);
21 ScopedJavaLocalRef<jobject> ParseData(
22 JNIEnv* env,
23 const JavaParamRef<jclass>& jcaller,
24 const JavaParamRef<jstring>& data_as_string) {
25 std::string input =
26 base::android::ConvertJavaStringToUTF8(env, data_as_string);
27 std::vector<uint8_t> data;
28 size_t num_handles;
29 std::string error_message;
30 if (!test::ParseValidationTestInput(
31 input, &data, &num_handles, &error_message)) {
32 ScopedJavaLocalRef<jstring> j_error_message =
33 base::android::ConvertUTF8ToJavaString(env, error_message);
34 return Java_ValidationTestUtil_buildData(env, NULL, 0,
35 j_error_message.obj());
37 void* data_ptr = &data[0];
38 if (!data_ptr) {
39 DCHECK(!data.size());
40 data_ptr = &data;
42 jobject byte_buffer =
43 env->NewDirectByteBuffer(data_ptr, data.size());
44 return Java_ValidationTestUtil_buildData(env, byte_buffer, num_handles, NULL);
47 } // namespace android
48 } // namespace mojo