include multibuild/sdk.mk
include config/config.mk

#####################################
# Build Android static library test #
#####################################
LOCAL_PROJECTS := tests/lib-test
LOCAL_NDK_PATH := $(CONFIG_ANDROID_NDK_PATH)
include $(BUILD_ANDROID)

#######################
# Google test library #
#######################
LOCAL_NAME := gtest
LOCAL_SRC_CPP := multibuild/source/gtest/source/gtest-all.cc \
                 multibuild/source/gtest/source/gtest_main.cc
LOCAL_EXPORT_INCLUDE_DIRS := multibuild/source/gtest/include
LOCAL_INCLUDE_DIRS := $(LOCAL_EXPORT_INCLUDE_DIRS)
LOCAL_LDFLAGS := -lstdc++ -lpthread
include $(BUILD_HOST_STATIC_LIBRARY)

################
# DRK emulator #
################
LOCAL_NAME := host-drk-emulator
LOCAL_SRC_C := $(wildcard tests/drk-emulator/*.c)
LOCAL_INCLUDE_DIRS := multibuild/include/gp-mb thirdparty/drk
LOCAL_CFLAGS := $(CONFIG_CFLAGS)
include $(BUILD_HOST_STATIC_LIBRARY)

##############################
# Activation Server emulator #
##############################
LOCAL_NAME := host-server-emulator
LOCAL_SRC_C := $(wildcard tests/server-emulator/sources/*.c)
LOCAL_EXPORT_INCLUDE_DIRS := tests/server-emulator/include
LOCAL_INCLUDE_DIRS := $(LOCAL_EXPORT_INCLUDE_DIRS)
LOCAL_LDFLAGS := -lssl
include $(BUILD_HOST_STATIC_LIBRARY)

###########################################
# Build host emulator static library test #
###########################################
_emulator_name := emulator-lib-test
_ta_name := activation-ta

LOCAL_NAME := $(_emulator_name)
LOCAL_SRC_CPP := $(wildcard tests/lib-test/*.cpp)
LOCAL_INCLUDE_DIRS := multibuild/include/gp-api multibuild/include/gp-mb
LOCAL_STATIC_LIBRARIES := host-activation-lib host-drk-emulator \
                          host-server-emulator gtest
LOCAL_LDFLAGS := -lstdc++ -lpthread -lssl
LOCAL_LINK_EMULATOR_GP_CLIENT := y
include $(BUILD_HOST)

##########################
# Build Android JNI test #
##########################

_gradle_args := \
  -DbuildToolsVersion='$(CONFIG_ANDROID_SDK_BUILD_TOOLS)' \
  -Dhttp.proxyHost='$(CONFIG_PROXY_HOST)' \
  -Dhttp.proxyPort='$(CONFIG_PROXY_PORT)' \
  -Dhttp.nonProxyHosts='$(subst :,|,$(CONFIG_NON_PROXY_HOSTS))' \
  -Dhttps.proxyHost='$(CONFIG_PROXY_HOST)' \
  -Dhttps.proxyPort='$(CONFIG_PROXY_PORT)' \
  -Dhttps.nonProxyHosts='$(subst :,|,$(CONFIG_NON_PROXY_HOSTS))'

LOCAL_NAME := android-jni-test
LOCAL_PROJECT_DIR := tests/jni-test

LOCAL_SDK_PATH := $(CONFIG_ANDROID_SDK_PATH)
LOCAL_NDK_PATH := $(CONFIG_ANDROID_NDK_PATH)
LOCAL_GRADLE_BIN := $(abspath tests/jni-test/gradlew) $(_gradle_args)
include $(BUILD_GRADLE_APK)

# Java test depends on JAR.
android-jni-test: jar ;

##############
# Test goals #
##############

.PHONY: test
test: test-lib test-jni

.PHONY: test-jni
test-jni:
	adb shell su root setenforce 0
	adb shell am instrument -w com.samsung.android.activation/android.support.test.runner.AndroidJUnitRunner

.PHONY: test-lib
test-lib:
	adb shell su root setenforce 0
	adb shell qsee-lib-test --gtest_color=yes

.PHONY: test-emulator test-lib-emulator
test-emulator test-lib-emulator: ./out/host/$(_emulator_name)/emul_$(CONFIG_TA_UUID)
	@cd ./out/host/$(_emulator_name); \
	./$(_emulator_name);

./out/host/$(_emulator_name)/emul_$(CONFIG_TA_UUID): ./out/emulator/host/$(_ta_name)/$(_ta_name)
	cp -f $(<) $(@)

./out/emulator/host/$(_ta_name)/$(_ta_name): $(_ta_name)-emulator $(_emulator_name) ;
# Empty recipe
