OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/android/jni_array.h" | 5 #include "base/android/jni_array.h" |
6 | 6 |
7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
8 #include "base/android/scoped_java_ref.h" | 8 #include "base/android/scoped_java_ref.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 | 10 |
11 namespace base { | 11 namespace base { |
12 namespace android { | 12 namespace android { |
13 | 13 |
14 TEST(JniArray, BasicConversions) { | 14 TEST(JniArray, BasicConversions) { |
15 const uint8 kBytes[] = { 0, 1, 2, 3 }; | 15 const uint8 kBytes[] = { 0, 1, 2, 3 }; |
16 const size_t kLen = arraysize(kBytes); | 16 const size_t kLen = arraysize(kBytes); |
17 JNIEnv* env = AttachCurrentThread(); | 17 JNIEnv* env = AttachCurrentThread(); |
18 ScopedJavaLocalRef<jbyteArray> bytes = ToJavaByteArray(env, kBytes, kLen); | 18 ScopedJavaLocalRef<jbyteArray> bytes = ToJavaByteArray(env, kBytes, kLen); |
19 ASSERT_TRUE(bytes.obj()); | 19 ASSERT_TRUE(bytes.obj()); |
20 | 20 |
21 std::vector<uint8> vec(5); | 21 std::vector<uint8> vec(5); |
22 JavaByteArrayToByteVector(env, bytes.obj(), &vec); | 22 JavaByteArrayToByteVector(env, bytes.obj(), &vec); |
23 EXPECT_EQ(4U, vec.size()); | 23 EXPECT_EQ(4U, vec.size()); |
24 EXPECT_EQ(std::vector<uint8>(kBytes, kBytes + kLen), vec); | 24 EXPECT_EQ(std::vector<uint8>(kBytes, kBytes + kLen), vec); |
25 | 25 |
26 AppendJavaByteArrayToByteVector(env, bytes.obj(), &vec); | 26 AppendJavaByteArrayToByteVector(env, bytes.obj(), &vec); |
27 EXPECT_EQ(8U, vec.size()); | 27 EXPECT_EQ(8U, vec.size()); |
28 } | 28 } |
29 | 29 |
| 30 void CheckIntConversion( |
| 31 JNIEnv* env, |
| 32 const int* int_array, |
| 33 const size_t len, |
| 34 const ScopedJavaLocalRef<jintArray>& ints) { |
| 35 ASSERT_TRUE(ints.obj()); |
| 36 |
| 37 jsize java_array_len = env->GetArrayLength(ints.obj()); |
| 38 ASSERT_EQ(static_cast<jsize>(len), java_array_len); |
| 39 |
| 40 jint value; |
| 41 for (size_t i = 0; i < len; ++i) { |
| 42 env->GetIntArrayRegion(ints.obj(), i, 1, &value); |
| 43 ASSERT_EQ(int_array[i], value); |
| 44 } |
| 45 } |
| 46 |
| 47 TEST(JniArray, IntConversions) { |
| 48 const int kInts[] = { 0, 1, -1, kint32min, kint32max}; |
| 49 const size_t kLen = arraysize(kInts); |
| 50 |
| 51 JNIEnv* env = AttachCurrentThread(); |
| 52 CheckIntConversion(env, kInts, kLen, ToJavaIntArray(env, kInts, kLen)); |
| 53 |
| 54 const std::vector<int> vec(kInts, kInts + kLen); |
| 55 CheckIntConversion(env, kInts, kLen, ToJavaIntArray(env, vec)); |
| 56 } |
| 57 |
30 void CheckLongConversion( | 58 void CheckLongConversion( |
31 JNIEnv* env, | 59 JNIEnv* env, |
32 const int64* long_array, | 60 const int64* long_array, |
33 const size_t len, | 61 const size_t len, |
34 const ScopedJavaLocalRef<jlongArray>& longs) { | 62 const ScopedJavaLocalRef<jlongArray>& longs) { |
35 ASSERT_TRUE(longs.obj()); | 63 ASSERT_TRUE(longs.obj()); |
36 | 64 |
37 jsize java_array_len = env->GetArrayLength(longs.obj()); | 65 jsize java_array_len = env->GetArrayLength(longs.obj()); |
38 ASSERT_EQ(static_cast<jsize>(len), java_array_len); | 66 ASSERT_EQ(static_cast<jsize>(len), java_array_len); |
39 | 67 |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 | 167 |
140 EXPECT_EQ(static_cast<size_t>(kMaxItems), vec.size()); | 168 EXPECT_EQ(static_cast<size_t>(kMaxItems), vec.size()); |
141 for (int i = 0; i < kMaxItems; ++i) { | 169 for (int i = 0; i < kMaxItems; ++i) { |
142 snprintf(text, sizeof text, "%d", i); | 170 snprintf(text, sizeof text, "%d", i); |
143 EXPECT_STREQ(text, vec[i].c_str()); | 171 EXPECT_STREQ(text, vec[i].c_str()); |
144 } | 172 } |
145 } | 173 } |
146 | 174 |
147 } // namespace android | 175 } // namespace android |
148 } // namespace base | 176 } // namespace base |
OLD | NEW |