Skip to content

Commit

Permalink
Update static_ref_test.cc to use new fake_test_constants.h.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 512154070
  • Loading branch information
jwhpryor authored and copybara-github committed Feb 24, 2023
1 parent 00c559a commit 8241a8d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 42 deletions.
1 change: 1 addition & 0 deletions implementation/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,7 @@ cc_test(
name = "static_ref_test",
srcs = ["static_ref_test.cc"],
deps = [
":fake_test_constants",
"//:jni_bind",
"//:jni_test",
"@googletest//:gtest_main",
Expand Down
75 changes: 33 additions & 42 deletions implementation/static_ref_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "implementation/fake_test_constants.h"
#include "jni_bind.h"
#include "jni_test.h"

Expand All @@ -29,6 +30,7 @@ using ::jni::Params;
using ::jni::Rank;
using ::jni::Static;
using ::jni::StaticRef;
using ::jni::test::Fake;
using ::jni::test::JniTest;
using ::testing::_;
using ::testing::Return;
Expand Down Expand Up @@ -58,130 +60,119 @@ static constexpr Class kClass{
// clang-format on

TEST_F(JniTest, StaticBooleanField) {
jfieldID static_field_id{reinterpret_cast<jfieldID>(0XDEDEDE)};
EXPECT_CALL(*env_, GetStaticFieldID(_, StrEq("booleanField"), StrEq("Z")))
.WillOnce(Return(static_field_id));
EXPECT_CALL(*env_, GetStaticBooleanField(_, static_field_id))
.WillOnce(Return(Fake<jfieldID>()));
EXPECT_CALL(*env_, GetStaticBooleanField(_, Fake<jfieldID>()))
.WillOnce(Return(true));
EXPECT_CALL(*env_, SetStaticBooleanField(_, static_field_id, true));
EXPECT_CALL(*env_, SetStaticBooleanField(_, Fake<jfieldID>(), true));

LocalObject<kClass> obj;
EXPECT_TRUE(StaticRef<kClass>{}["booleanField"].Get());
StaticRef<kClass>{}["booleanField"].Set(true);
}

TEST_F(JniTest, StaticByteField) {
jfieldID static_field_id{reinterpret_cast<jfieldID>(0XDEDEDE)};
EXPECT_CALL(*env_, GetStaticFieldID(_, StrEq("byteField"), StrEq("B")))
.WillOnce(Return(static_field_id));
EXPECT_CALL(*env_, GetStaticByteField(_, static_field_id))
.WillOnce(Return(Fake<jfieldID>()));
EXPECT_CALL(*env_, GetStaticByteField(_, Fake<jfieldID>()))
.WillOnce(Return(true));
EXPECT_CALL(*env_, SetStaticByteField(_, static_field_id, true));
EXPECT_CALL(*env_, SetStaticByteField(_, Fake<jfieldID>(), true));

LocalObject<kClass> obj;
EXPECT_TRUE(StaticRef<kClass>{}["byteField"].Get());
StaticRef<kClass>{}["byteField"].Set(true);
}

TEST_F(JniTest, StaticCharField) {
jfieldID static_field_id{reinterpret_cast<jfieldID>(0XDEDEDE)};
EXPECT_CALL(*env_, GetStaticFieldID(_, StrEq("charField"), StrEq("C")))
.WillOnce(Return(static_field_id));
EXPECT_CALL(*env_, GetStaticCharField(_, static_field_id))
.WillOnce(Return(Fake<jfieldID>()));
EXPECT_CALL(*env_, GetStaticCharField(_, Fake<jfieldID>()))
.WillOnce(Return(true));
EXPECT_CALL(*env_, SetStaticCharField(_, static_field_id, true));
EXPECT_CALL(*env_, SetStaticCharField(_, Fake<jfieldID>(), true));

LocalObject<kClass> obj;
EXPECT_TRUE(StaticRef<kClass>{}["charField"].Get());
StaticRef<kClass>{}["charField"].Set(true);
}

TEST_F(JniTest, StaticShortField) {
jfieldID static_field_id{reinterpret_cast<jfieldID>(0XDEDEDE)};
EXPECT_CALL(*env_, GetStaticFieldID(_, StrEq("shortField"), StrEq("S")))
.WillOnce(Return(static_field_id));
EXPECT_CALL(*env_, GetStaticShortField(_, static_field_id))
.WillOnce(Return(Fake<jfieldID>()));
EXPECT_CALL(*env_, GetStaticShortField(_, Fake<jfieldID>()))
.WillOnce(Return(true));
EXPECT_CALL(*env_, SetStaticShortField(_, static_field_id, true));
EXPECT_CALL(*env_, SetStaticShortField(_, Fake<jfieldID>(), true));

LocalObject<kClass> obj;
EXPECT_TRUE(StaticRef<kClass>{}["shortField"].Get());
StaticRef<kClass>{}["shortField"].Set(true);
}

TEST_F(JniTest, StaticIntField) {
jfieldID static_field_id{reinterpret_cast<jfieldID>(0XDEDEDE)};
EXPECT_CALL(*env_, GetStaticFieldID(_, StrEq("intField"), StrEq("I")))
.WillOnce(Return(static_field_id));
EXPECT_CALL(*env_, GetStaticIntField(_, static_field_id))
.WillOnce(Return(Fake<jfieldID>()));
EXPECT_CALL(*env_, GetStaticIntField(_, Fake<jfieldID>()))
.WillOnce(Return(true));
EXPECT_CALL(*env_, SetStaticIntField(_, static_field_id, true));
EXPECT_CALL(*env_, SetStaticIntField(_, Fake<jfieldID>(), true));

LocalObject<kClass> obj;
EXPECT_TRUE(StaticRef<kClass>{}["intField"].Get());
StaticRef<kClass>{}["intField"].Set(true);
}

TEST_F(JniTest, StaticLongField) {
jfieldID static_field_id{reinterpret_cast<jfieldID>(0XDEDEDE)};
EXPECT_CALL(*env_, GetStaticFieldID(_, StrEq("longField"), StrEq("J")))
.WillOnce(Return(static_field_id));
EXPECT_CALL(*env_, GetStaticLongField(_, static_field_id))
.WillOnce(Return(Fake<jfieldID>()));
EXPECT_CALL(*env_, GetStaticLongField(_, Fake<jfieldID>()))
.WillOnce(Return(true));
EXPECT_CALL(*env_, SetStaticLongField(_, static_field_id, true));
EXPECT_CALL(*env_, SetStaticLongField(_, Fake<jfieldID>(), true));

LocalObject<kClass> obj;
EXPECT_TRUE(StaticRef<kClass>{}["longField"].Get());
StaticRef<kClass>{}["longField"].Set(true);
}

TEST_F(JniTest, StaticFloatField) {
jfieldID static_field_id{reinterpret_cast<jfieldID>(0XDEDEDE)};
EXPECT_CALL(*env_, GetStaticFieldID(_, StrEq("floatField"), StrEq("F")))
.WillOnce(Return(static_field_id));
EXPECT_CALL(*env_, GetStaticFloatField(_, static_field_id))
.WillOnce(Return(Fake<jfieldID>()));
EXPECT_CALL(*env_, GetStaticFloatField(_, Fake<jfieldID>()))
.WillOnce(Return(true));
EXPECT_CALL(*env_, SetStaticFloatField(_, static_field_id, true));
EXPECT_CALL(*env_, SetStaticFloatField(_, Fake<jfieldID>(), true));

LocalObject<kClass> obj;
EXPECT_TRUE(StaticRef<kClass>{}["floatField"].Get());
StaticRef<kClass>{}["floatField"].Set(true);
}

TEST_F(JniTest, StaticDoubleField) {
jfieldID static_field_id{reinterpret_cast<jfieldID>(0XDEDEDE)};
EXPECT_CALL(*env_, GetStaticFieldID(_, StrEq("doubleField"), StrEq("D")))
.WillOnce(Return(static_field_id));
EXPECT_CALL(*env_, GetStaticDoubleField(_, static_field_id))
.WillOnce(Return(Fake<jfieldID>()));
EXPECT_CALL(*env_, GetStaticDoubleField(_, Fake<jfieldID>()))
.WillOnce(Return(true));
EXPECT_CALL(*env_, SetStaticDoubleField(_, static_field_id, true));
EXPECT_CALL(*env_, SetStaticDoubleField(_, Fake<jfieldID>(), true));

LocalObject<kClass> obj;
EXPECT_TRUE(StaticRef<kClass>{}["doubleField"].Get());
StaticRef<kClass>{}["doubleField"].Set(true);
}

TEST_F(JniTest, StaticField_ObjectGet) {
jfieldID field_id{reinterpret_cast<jfieldID>(0XDEDEDE)};
jobject fake_obj{reinterpret_cast<jobject>(0XCDCDCD)};
EXPECT_CALL(*env_,
GetStaticFieldID(_, StrEq("classField"), StrEq("LkClass2;")))
.WillOnce(Return(field_id));
EXPECT_CALL(*env_, GetStaticObjectField(_, field_id))
.WillOnce(Return(fake_obj));
.WillOnce(Return(Fake<jfieldID>()));
EXPECT_CALL(*env_, GetStaticObjectField(_, Fake<jfieldID>()))
.WillOnce(Return(Fake<jobject>()));

jni::LocalObject<kClass2> obj = StaticRef<kClass>{}["classField"].Get();
}

TEST_F(JniTest, StaticField_ObjectSet) {
jfieldID field_id{reinterpret_cast<jfieldID>(0XDEDEDE)};
jobject fake_obj{reinterpret_cast<jobject>(0XCDCDCD)};
EXPECT_CALL(*env_,
GetStaticFieldID(_, StrEq("classField"), StrEq("LkClass2;")))
.WillOnce(Return(field_id));
EXPECT_CALL(*env_, SetStaticObjectField(_, field_id, fake_obj));
.WillOnce(Return(Fake<jfieldID>()));
EXPECT_CALL(*env_,
SetStaticObjectField(_, Fake<jfieldID>(), Fake<jobject>()));

StaticRef<kClass>{}["classField"].Set(LocalObject<kClass2>{fake_obj});
StaticRef<kClass>{}["classField"].Set(LocalObject<kClass2>{Fake<jobject>()});
}

////////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 8241a8d

Please sign in to comment.