From d092540af68adcbcf8e6a64fe9fcbd9996846b61 Mon Sep 17 00:00:00 2001 From: utzcoz Date: Sat, 28 Oct 2023 16:32:46 +0800 Subject: [PATCH] BasicNativeAndroidTest: Move gtest to a single flavor The junit-gtest library will bring native test library and its c++ shared library into the final package. It's not easy to keep these necessary shared libraries into androidTest flavor only. So this CL adds a new flavor called nativeTest to include native test related libraries and run it with androidTest. For normal usage, we can use core flavor, the new default flavor without any native test related libraries. Signed-off-by: utzcoz --- test_all.sh | 11 ++++++- unit/BasicNativeAndroidTest/app/build.gradle | 30 +++++++++++++++---- .../DefaultInstrumentationTest.kt | 16 ++++++++++ .../android/testing/nativesample/AdderTest.kt | 0 .../app/src/main/cpp/CMakeLists.txt | 14 +++++---- 5 files changed, 59 insertions(+), 12 deletions(-) create mode 100644 unit/BasicNativeAndroidTest/app/src/androidTest/java/com/example/android/testing/nativesample/DefaultInstrumentationTest.kt rename unit/BasicNativeAndroidTest/app/src/{androidTest => androidTestNativeTest}/java/com/example/android/testing/nativesample/AdderTest.kt (100%) diff --git a/test_all.sh b/test_all.sh index b95aabea1..139cca109 100755 --- a/test_all.sh +++ b/test_all.sh @@ -9,7 +9,16 @@ for p in $(cat projects.conf); do echo "=====================================================================" pushd $p > /dev/null # Silent pushd - ./gradlew $@ testDebug nexusOneApi30DebugAndroidTest --info | sed "s@^@$p @" # Prefix every line with directory + + ./gradlew $@ testDebug | sed "s@^@$p @" # Prefix every line with directory + + if [ "$p" == "unit/BasicNativeAndroidTest" ]; then + ./gradlew $@ nexusOneApi30CoreDebugAndroidTest --info | sed "s@^@$p @" # Prefix every line with directory + ./gradlew $@ nexusOneApi30NativeTestDebugAndroidTest --info | sed "s@^@$p @" + else + ./gradlew $@ nexusOneApi30DebugAndroidTest --info | sed "s@^@$p @" # Prefix every line with directory + fi + code=${PIPESTATUS[0]} if [ "$code" -ne "0" ]; then exit $code diff --git a/unit/BasicNativeAndroidTest/app/build.gradle b/unit/BasicNativeAndroidTest/app/build.gradle index bc4c1c1a3..d3b65be63 100644 --- a/unit/BasicNativeAndroidTest/app/build.gradle +++ b/unit/BasicNativeAndroidTest/app/build.gradle @@ -17,10 +17,28 @@ android { testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" externalNativeBuild { cmake { - arguments "-DANDROID_STL=c++_shared" + arguments "-DANDROID_STL=c++_shared", "-DENABLE_NATIVE_TEST=OFF" } } } + + flavorDimensions = ["default"] + + productFlavors { + core { + dimension "default" + applicationId = defaultConfig.applicationId + } + nativeTest { + dimension "default" + externalNativeBuild { + cmake { + arguments "-DENABLE_NATIVE_TEST=ON" + } + } + } + } + externalNativeBuild { cmake { path "src/main/cpp/CMakeLists.txt" @@ -47,7 +65,8 @@ android { testOptions { managedDevices { devices { - // run with ../gradlew nexusOneApi30DebugAndroidTest + // run with ../gradlew nexusOneApi30CoreDebugAndroidTest without native tests + // or ../gradlew nexusOneApi30NativeTestDebugAndroidTest with native tests nexusOneApi30(com.android.build.api.dsl.ManagedVirtualDevice) { // A lower resolution device is used here for better emulator performance device = "Nexus One" @@ -62,9 +81,10 @@ android { } dependencies { - androidTestImplementation "junit:junit:$rootProject.junitVersion" - implementation "androidx.test.ext:junit-gtest:$rootProject.junitGtestVersion" - implementation "com.android.ndk.thirdparty:googletest:$rootProject.googletestVersion" + nativeTestImplementation "androidx.test.ext:junit-gtest:$rootProject.junitGtestVersion" + nativeTestImplementation "com.android.ndk.thirdparty:googletest:$rootProject.googletestVersion" androidTestImplementation "androidx.test:runner:$rootProject.runnerVersion" androidTestImplementation "androidx.test.ext:junit-ktx:$extJUnitVersion" + androidTestImplementation "com.google.truth:truth:rootProject.truthVersion" + androidTestImplementation "junit:junit:$rootProject.junitVersion" } diff --git a/unit/BasicNativeAndroidTest/app/src/androidTest/java/com/example/android/testing/nativesample/DefaultInstrumentationTest.kt b/unit/BasicNativeAndroidTest/app/src/androidTest/java/com/example/android/testing/nativesample/DefaultInstrumentationTest.kt new file mode 100644 index 000000000..c41c2bac6 --- /dev/null +++ b/unit/BasicNativeAndroidTest/app/src/androidTest/java/com/example/android/testing/nativesample/DefaultInstrumentationTest.kt @@ -0,0 +1,16 @@ +package com.example.android.testing.nativesample + +import androidx.test.ext.junit.runners.AndroidJUnit4 +import androidx.test.platform.app.InstrumentationRegistry +import com.google.common.truth.Truth.assertThat +import org.junit.Test +import org.junit.runner.RunWith + +@RunWith(AndroidJUnit4::class) +class DefaultInstrumentationTest { + @Test + fun useAppContext() { + val appContext = InstrumentationRegistry.getInstrumentation().targetContext + assertThat(appContext.packageName).isEqualTo("com.example.android.testing.nativesample") + } +} \ No newline at end of file diff --git a/unit/BasicNativeAndroidTest/app/src/androidTest/java/com/example/android/testing/nativesample/AdderTest.kt b/unit/BasicNativeAndroidTest/app/src/androidTestNativeTest/java/com/example/android/testing/nativesample/AdderTest.kt similarity index 100% rename from unit/BasicNativeAndroidTest/app/src/androidTest/java/com/example/android/testing/nativesample/AdderTest.kt rename to unit/BasicNativeAndroidTest/app/src/androidTestNativeTest/java/com/example/android/testing/nativesample/AdderTest.kt diff --git a/unit/BasicNativeAndroidTest/app/src/main/cpp/CMakeLists.txt b/unit/BasicNativeAndroidTest/app/src/main/cpp/CMakeLists.txt index 7d7da9dff..af74e4674 100644 --- a/unit/BasicNativeAndroidTest/app/src/main/cpp/CMakeLists.txt +++ b/unit/BasicNativeAndroidTest/app/src/main/cpp/CMakeLists.txt @@ -2,18 +2,20 @@ cmake_minimum_required(VERSION 3.10.2) project(junit-gtest-example LANGUAGES CXX) -find_package(googletest REQUIRED CONFIG) -find_package(junit-gtest REQUIRED CONFIG) - include_directories(include) add_library(adder SHARED src/adder.cpp) -add_library(adder-test SHARED test/adder_test.cpp) +if (ENABLE_NATIVE_TEST) + find_package(googletest REQUIRED CONFIG) + find_package(junit-gtest REQUIRED CONFIG) + + add_library(adder-test SHARED test/adder_test.cpp) -target_link_libraries(adder-test + target_link_libraries(adder-test PRIVATE adder googletest::gtest junit-gtest::junit-gtest -) \ No newline at end of file + ) +endif() \ No newline at end of file