-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathlocal_array_string_test.cc
208 lines (175 loc) · 7.38 KB
/
local_array_string_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <memory>
#include <utility>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "implementation/jni_helper/fake_test_constants.h"
#include "jni_bind.h"
#include "jni_test.h"
#if __clang__
using ::jni::Array;
using ::jni::ArrayStrip_t;
using ::jni::CDecl_t;
using ::jni::Class;
using ::jni::Fake;
using ::jni::Field;
using ::jni::kJavaLangString;
using ::jni::LocalArray;
using ::jni::LocalObject;
using ::jni::LocalString;
using ::jni::Method;
using ::jni::Params;
using ::jni::Rank;
using ::jni::RegularToArrayTypeMap_t;
using ::jni::test::JniTest;
using ::testing::_;
using ::testing::Return;
using ::testing::StrEq;
namespace {
static constexpr Class kClass{"kClass"};
////////////////////////////////////////////////////////////////////////////////
// Strings are unusual in that they have their own type (jstring) but are
// almost completely objects otherwise.
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Construction / Materialisation.
////////////////////////////////////////////////////////////////////////////////
TEST_F(JniTest, Array_ConstructsFromAnotherStringArray) {
LocalArray<jstring> arr_1{Fake<jobjectArray>()};
LocalArray<jstring> arr_2{std::move(arr_1)};
}
TEST_F(JniTest, Array_CorrectSignatureForStringParams) {
EXPECT_CALL(*env_, FindClass(StrEq("ClassThatReturnsArrays")));
EXPECT_CALL(*env_, FindClass(StrEq("java/lang/String")));
EXPECT_CALL(*env_, GetMethodID(_, StrEq("StringArray"),
StrEq("([Ljava/lang/String;)V")));
static constexpr Class kClass{
"ClassThatReturnsArrays",
Method{"StringArray", jni::Return{}, jni::Params{Array{jstring{}}}},
};
LocalObject<kClass> obj{jobject{nullptr}};
LocalArray<jstring> arr{3};
obj("StringArray", arr);
}
////////////////////////////////////////////////////////////////////////////////
// Caches Length.
////////////////////////////////////////////////////////////////////////////////
TEST_F(JniTest, Array_CachesLengthExactlyOnceOnFirstRequest) {
EXPECT_CALL(*env_, GetArrayLength).Times(0);
LocalArray<jint> obj{Fake<jintArray>()};
EXPECT_CALL(*env_, GetArrayLength).Times(1).WillOnce(Return(5));
EXPECT_EQ(obj.Length(), 5);
EXPECT_EQ(obj.Length(), 5);
EXPECT_EQ(obj.Length(), 5);
}
////////////////////////////////////////////////////////////////////////////////
// Setters.
////////////////////////////////////////////////////////////////////////////////
TEST_F(JniTest, Array_LocalStringRValuesCanBeSet) {
EXPECT_CALL(*env_, DeleteLocalRef(_))
.Times(1); // array (object is moved from)
EXPECT_CALL(*env_, DeleteLocalRef(Fake<jclass>())); // FindClass
EXPECT_CALL(*env_, DeleteLocalRef(Fake<jstring>())).Times(0);
LocalArray<jstring> arr{3};
arr.Set(0, LocalString{Fake<jstring>()});
}
TEST_F(JniTest, Array_LocalVanillaObjectRValuesCanBeSet) {
// Unfortunately this is getting cached separately by `LocalArray`.
// In the future, this should drop to 1.
EXPECT_CALL(*env_, FindClass(StrEq("java/lang/String"))).Times(2);
EXPECT_CALL(*env_, DeleteLocalRef(_)).Times(2); // array, in place obj
EXPECT_CALL(*env_, DeleteLocalRef(Fake<jclass>())).Times(2); // FindClass
EXPECT_CALL(*env_, DeleteLocalRef(Fake<jstring>())).Times(0);
LocalArray<jobject, 1, kJavaLangString> arr{
3, LocalObject<kJavaLangString>{"Foo"}};
arr.Set(0, LocalObject<kJavaLangString>{Fake<jstring>()});
}
////////////////////////////////////////////////////////////////////////////////
// As Return.
////////////////////////////////////////////////////////////////////////////////
TEST_F(JniTest, Array_CorrectReturnSignatureForStrings) {
static constexpr Class kClass{
"kClass",
Method{"StringArray", jni::Return{Array{jstring{}}}},
};
LocalObject<kClass> obj{Fake<jobject>()};
EXPECT_CALL(*env_, GetMethodID(_, StrEq("StringArray"),
StrEq("()[Ljava/lang/String;")));
LocalArray<jstring> arr = obj("StringArray");
}
////////////////////////////////////////////////////////////////////////////////
// As Param.
////////////////////////////////////////////////////////////////////////////////
TEST_F(JniTest, Array_CorrectParamSignatureForStrings) {
static constexpr Class kClass{
"kClass",
Method{"StringArray", jni::Return{}, Params{Array{jstring{}}}},
};
LocalObject<kClass> obj{Fake<jobject>()};
EXPECT_CALL(*env_, GetMethodID(_, StrEq("StringArray"),
StrEq("([Ljava/lang/String;)V")));
LocalArray<jstring> arr{2};
obj("StringArray", arr);
}
TEST_F(JniTest, LocalStringArray_ConstructsObjectsForLValues) {
// Unlike POD, objects are constructed with a size, a jclass, and an init
// object. This makes for a slightly different API then other objects.
EXPECT_CALL(*env_, NewObjectArray);
LocalObject<kClass> default_object{};
LocalArray<jobject, 1, kClass> local_object_array{5, default_object};
}
/*
// TODO(b/143908983): Restore after fixing ubsan failures.
TEST_F(JniTest, Array_StringsCanBeSetOnLocalString) {
EXPECT_CALL(*env_, SetObjectArrayElement(Fake<jobjectArray>(), 0, _));
EXPECT_CALL(*env_, SetObjectArrayElement(Fake<jobjectArray>(), 1, _));
EXPECT_CALL(*env_, SetObjectArrayElement(Fake<jobjectArray>(), 2, _));
EXPECT_CALL(*env_, SetObjectArrayElement(Fake<jobjectArray>(), 3, _));
EXPECT_CALL(*env_, SetObjectArrayElement(Fake<jobjectArray>(), 4, _));
const char* kFoo = "kFoo";
const std::string kNar = "kNar";
LocalArray<jstring> arr{5, LocalObject<kJavaLangString>{"Foo"}};
arr.Set(0, "Bar");
arr.Set(1, std::string{"Baz"});
arr.Set(2, std::string_view{"Bar"});
arr.Set(3, std::string_view{kFoo});
arr.Set(4, std::string_view{kNar});
}
*/
////////////////////////////////////////////////////////////////////////////////
// String Fields.
////////////////////////////////////////////////////////////////////////////////
TEST_F(JniTest, Array_CorrectFieldSignatureForStrings) {
static constexpr Class kClass{
"kClass",
Field{"StringArrayRank1", Array{jstring{}}},
Field{"StringArrayRank2", Array{jstring{}, Rank<2>{}}},
Field{"StringArrayRank3", Array{jstring{}, Rank<3>{}}},
};
LocalObject<kClass> obj{jobject{nullptr}};
EXPECT_CALL(*env_, GetFieldID(_, StrEq("StringArrayRank1"),
StrEq("[Ljava/lang/String;")));
EXPECT_CALL(*env_, GetFieldID(_, StrEq("StringArrayRank2"),
StrEq("[[Ljava/lang/String;")));
EXPECT_CALL(*env_, GetFieldID(_, StrEq("StringArrayRank3"),
StrEq("[[[Ljava/lang/String;")));
LocalArray<jstring> arr1 = obj["StringArrayRank1"].Get();
LocalArray<jstring, 2> arr2 = obj["StringArrayRank2"].Get();
LocalArray<jstring, 3> arr3 = obj["StringArrayRank3"].Get();
}
} // namespace
#endif // __clang__