1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef LIBNATIVEHELPER_TESTS_JNI_GTEST_H 18 #define LIBNATIVEHELPER_TESTS_JNI_GTEST_H 19 20 #include <memory> 21 22 #include <gtest/gtest.h> 23 24 #include <jni.h> 25 #include <nativehelper/JniInvocation.h> 26 27 namespace android { 28 29 // Example test setup following googletest docs: 30 // 31 // template <typename Provider> 32 // class TemplatedTest : public JNITestBase<Provider> { 33 // ... 34 // } 35 // 36 // typedef ::testing::Types<MockJNIProvider> Providers; 37 // TYPED_TEST_CASE(TemplatedTest, Providers); 38 // 39 // TYPED_TEST() { 40 // // Test code. Use "this->" to access TemplatedTest members. 41 // } 42 43 44 45 // Provider is a concept that must follow this structure: 46 // 47 // class JNIProvider { 48 // public: 49 // JNIProvider(); 50 // 51 // void SetUp(); 52 // JNIEnv* CreateJNIEnv(); 53 // 54 // void DestroyJNIEnv(JNIEnv* env); 55 // void TearDown(); 56 // } 57 58 template <typename Provider, typename Test = ::testing::Test> 59 class JNITestBase : public Test { 60 protected: JNITestBase()61 JNITestBase() : provider_(), env_(nullptr), java_vm_(nullptr) { 62 } 63 SetUp()64 void SetUp() override { 65 Test::SetUp(); 66 provider_.SetUp(); 67 env_ = provider_.CreateJNIEnv(); 68 ASSERT_TRUE(env_ != nullptr); 69 } 70 TearDown()71 void TearDown() override { 72 provider_->DestroyJNIEnv(env_); 73 provider_->TearDown(); 74 Test::TearDown(); 75 } 76 77 protected: 78 Provider provider_; 79 80 JNIEnv* env_; 81 JavaVM* java_vm_; 82 }; 83 84 // A mockable implementation of the Provider concept. It is the responsibility 85 // of the test to stub out any needed functions (all function pointers will be 86 // null initially). 87 // 88 // TODO: Consider googlemock. 89 class MockJNIProvider { 90 public: MockJNIProvider()91 MockJNIProvider() { 92 } 93 SetUp()94 void SetUp() { 95 // Nothing to here. 96 } 97 98 // TODO: Spawn threads to allow more envs? CreateJNIEnv()99 JNIEnv* CreateJNIEnv() { 100 return CreateMockedJNIEnv().release(); 101 } 102 DestroyJNIEnv(JNIEnv * env)103 void DestroyJNIEnv(JNIEnv* env) { 104 delete env->functions; 105 delete env; 106 } 107 TearDown()108 void TearDown() { 109 // Nothing to do here. 110 } 111 112 protected: CreateMockedJNIEnv()113 std::unique_ptr<JNIEnv> CreateMockedJNIEnv() { 114 JNINativeInterface* inf = new JNINativeInterface(); 115 memset(inf, 0, sizeof(JNINativeInterface)); 116 117 std::unique_ptr<JNIEnv> ret(new JNIEnv{0}); 118 ret->functions = inf; 119 120 return ret; 121 } 122 }; 123 124 } // namespace android 125 126 #endif // LIBNATIVEHELPER_TESTS_JNI_GTEST_H 127