Skip to content

Commit

Permalink
Added android test harness (#617)
Browse files Browse the repository at this point in the history
* Added android test harness

* Added function resolution/running

* Reset static state on argparse tests

* Only include log test utils, no log tests on android
  • Loading branch information
Justin Boswell authored Apr 3, 2020
1 parent ec3e191 commit 79cb3f5
Show file tree
Hide file tree
Showing 18 changed files with 558 additions and 38 deletions.
14 changes: 14 additions & 0 deletions AWSCRTAndroidTestRunner/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
*.iml
.gradle
/local.properties
/.idea/caches
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
.DS_Store
/build
/captures
.externalNativeBuild
.cxx
3 changes: 3 additions & 0 deletions AWSCRTAndroidTestRunner/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/build
src/androidTest/**/tests
src/main/res
49 changes: 49 additions & 0 deletions AWSCRTAndroidTestRunner/app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
ndkVersion "21.0.6113669"

defaultConfig {
applicationId "software.amazon.awssdk.crt.awscrtandroidtestrunner"
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

externalNativeBuild {
cmake {
cppFlags ""
}
}
}

buildTypes {
release {
minifyEnabled false
}
}

externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
version "3.10.2"
}
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package software.amazon.awssdk.crt.awscrtandroidtestrunner.tests

import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.Test
import org.junit.Assert
import software.amazon.awssdk.crt.awscrtandroidtestrunner.NativeTestFixture

class Test_@TEST_NAME@ : NativeTestFixture() {
@Test
public fun test_@TEST_NAME@() {
Assert.assertEquals(0, runTest("@TEST_NAME@"))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package software.amazon.awssdk.crt.awscrtandroidtestrunner

open class NativeTestFixture {
companion object {
// Used to load the 'native-lib' library on application startup.
init {
System.loadLibrary("native-lib")
}
}

external fun runTest(name: String): Int
}
5 changes: 5 additions & 0 deletions AWSCRTAndroidTestRunner/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="software.amazon.awssdk.crt.awscrtandroidtestrunner">

</manifest>
58 changes: 58 additions & 0 deletions AWSCRTAndroidTestRunner/app/src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.4.1)

# AWS lib
set(path_to_common "${CMAKE_CURRENT_LIST_DIR}/../../../../..")
get_filename_component(path_to_common ${path_to_common} ABSOLUTE)

# This is required in order to append /lib/cmake to each element in CMAKE_PREFIX_PATH
set(AWS_MODULE_DIR "/${CMAKE_INSTALL_LIBDIR}/cmake")
string(REPLACE ";" "${AWS_MODULE_DIR};" AWS_MODULE_PATH "${CMAKE_PREFIX_PATH}${AWS_MODULE_DIR}")
# Append that generated list to the module search path
list(APPEND CMAKE_MODULE_PATH ${AWS_MODULE_PATH})

list(APPEND CMAKE_MODULE_PATH "${path_to_common}/cmake")
include(AwsFindPackage)
set(IN_SOURCE_BUILD ON)
set(BUILD_SHARED_LIBS ON)

# We will generate our own tests, the tests that are there depend on CTest
set(ALLOW_CROSS_COMPILED_TESTS ON)
set(BUILD_TESTING ON)
add_subdirectory(${path_to_common} ${CMAKE_CURRENT_BINARY_DIR}/aws-c-common)
aws_use_package(aws-c-common)

function(import_tests test_cmakelists)
get_property(TEST_CASES GLOBAL PROPERTY AWS_TEST_CASES)

# Generate Kotlin test classes
get_filename_component(testrunner_path "../../androidTest/java/software/amazon/awssdk/crt/awscrtandroidtestrunner" ABSOLUTE)
foreach(name IN LISTS TEST_CASES)
set(TEST_NAME "${name}")
configure_file(
"${testrunner_path}/NativeTest.kt.in"
"${testrunner_path}/tests/NativeTest_${name}.kt"
)
endforeach()
endfunction()

file(GLOB test_src "${path_to_common}/tests/*.c")
file(GLOB test_logging_src
"${path_to_common}/tests/logging/logging_test_utilities.c"
"${path_to_common}/tests/logging/test_logger.c")
set(test_src ${test_src} ${test_logging_src})
import_tests(${path_to_common}/tests/CMakeLists.txt)

# JNI Lib
add_library(native-lib SHARED native-lib.cpp ${test_src})
find_library(log-lib log)
target_include_directories(native-lib PUBLIC
"${path_to_common}/include"
"${path_to_common}/tests"
"${CMAKE_CURRENT_BINARY_DIR}/aws-c-common/generated/include")
target_compile_definitions(native-lib PRIVATE AWS_UNSTABLE_TESTING_API=1)
target_link_libraries(native-lib ${log-lib} ${DEP_AWS_LIBS})
31 changes: 31 additions & 0 deletions AWSCRTAndroidTestRunner/app/src/main/cpp/native-lib.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <jni.h>
#include <string>
#include <sstream>

#include <dlfcn.h>

#include <android/log.h>

typedef int(test_fn_t)(int, char**);

extern "C" JNIEXPORT jint JNICALL
Java_software_amazon_awssdk_crt_awscrtandroidtestrunner_NativeTestFixture_runTest(
JNIEnv *env,
jobject /* this */,
jstring jni_name) {
const char *test_name = env->GetStringUTFChars(jni_name, nullptr);
__android_log_print(ANDROID_LOG_INFO, "native-test", "RUNNING %s", test_name);

test_fn_t *test_fn = (test_fn_t*)dlsym(RTLD_DEFAULT, test_name);
if (!test_fn) {
__android_log_print(ANDROID_LOG_WARN, "native-test", "%s NOT FOUND", test_name);
return -1;
}

int result = test_fn(0, nullptr);
__android_log_print(
result ? ANDROID_LOG_FATAL : ANDROID_LOG_INFO,
"native-test",
"%s %s", test_name, result ? "FAILED" : "OK");
return result;
}
29 changes: 29 additions & 0 deletions AWSCRTAndroidTestRunner/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.3.71'
repositories {
google()
jcenter()

}
dependencies {
classpath 'com.android.tools.build:gradle:3.6.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}

allprojects {
repositories {
google()
jcenter()

}
}

task clean(type: Delete) {
delete rootProject.buildDir
}
23 changes: 23 additions & 0 deletions AWSCRTAndroidTestRunner/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx1536m
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
# AndroidX package structure to make it clearer which packages are bundled with the
# Android operating system, and which are packaged with your app's APK
# https://developer.android.com/topic/libraries/support-library/androidx-rn
android.useAndroidX=true
# Automatically convert third-party libraries to use AndroidX
android.enableJetifier=true
# Kotlin code style for this project: "official" or "obsolete":
kotlin.code.style=official
org.gradle.daemon=true
org.gradle.parallel=true
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#Wed Apr 01 11:18:00 PDT 2020
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip
Loading

0 comments on commit 79cb3f5

Please sign in to comment.