Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.

Commit

Permalink
Add Android Instrumented Test Environment
Browse files Browse the repository at this point in the history
This commit adds android instrumented test environment.
We need to test and know whether caver-java-android lib
is well-working on real android machine or not.

For this
1 > Add android_instrumented_test sub-project
This sub-project is made for instrumented testing.
This sub-project will use unit test files already written at core.

This sub-project uses AndroidX Test Library for instrumented test.

2 > Add Suite Test for TestClasses containing nested classes
This makes AndroidX Test Library well works on TestClass which contains nested classes.
Without it, test cases of nested classes does not work on emulator.

3 > Update circle-ci config and add shell script
To re-use unit tests written at core, I added a script for it and updated config of circle-ci too.

4 > Update git ignore
To make android_instrumented_test which is Android Project works well, we must not ignore
`gradle.properties` because it contains essentials of AndroidX Test Library.
  • Loading branch information
aeharvlee committed May 26, 2021
1 parent 03d15a1 commit 735c84b
Show file tree
Hide file tree
Showing 42 changed files with 374 additions and 23 deletions.
50 changes: 45 additions & 5 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,20 @@ machine_ubuntu: &machine_ubuntu
android_machine_ubuntu: &android_machine_ubuntu
working_directory: ~/circleci-caver-java-android
machine:
image: ubuntu-1604:201903-01
image: android:202102-01

setup_android_test_files: &setup_android_test_files
run:
name: "Setup android instrumented test environment"
command: |
bash .circleci/setup_android_instrumented_test.sh
pull_klaytn_image: &pull_klaytn_image
run:
name: "Pull and tag Klaytn image"
command: |
docker pull klaytn/build_base:latest
docker image tag klaytn/build_base:latest klaytn
docker pull klaytn/build_base:latest
docker image tag klaytn/build_base:latest klaytn
check_test_network: &check_test_network
run:
Expand Down Expand Up @@ -56,7 +62,15 @@ build_test: &build_test
name: "Build and test"
command: |
./gradlew clean
./gradlew build --debug
./gradlew build --debug -x :android_instrumented_test:build
build_test_android: &build_test_android
run:
name: "Build and test"
command: |
./gradlew clean
./gradlew connectedAndroidTest
./gradlew build --debug -x :android_instrumented_test:build -x test
jobs:
build:
Expand All @@ -76,6 +90,8 @@ jobs:
awk '/version '\''/' build.gradle
fi
- *build_test
- store_test_results:
path: core/build/test-results/test
- persist_to_workspace:
root: ~/
paths:
Expand All @@ -85,6 +101,7 @@ jobs:
<<: *android_machine_ubuntu
steps:
- checkout
- *setup_android_test_files
- *pull_klaytn_image
- *start_test_network
- *check_test_network
Expand All @@ -107,7 +124,30 @@ jobs:
awk '/version '\''/' build.gradle
awk '/ext.web3jVersion = '\''/' build.gradle
- *build_test
- run:
name: Create avd
command: |
SYSTEM_IMAGES="system-images;android-29;default;x86"
sdkmanager "$SYSTEM_IMAGES"
echo "no" | avdmanager --verbose create avd -n test -k "$SYSTEM_IMAGES"
- run:
name: Launch emulator
command: |
emulator -avd test -delay-adb -verbose -no-window -gpu swiftshader_indirect -no-snapshot -noaudio -no-boot-anim
background: true
- run:
name: Wait for emulator to start
command: |
circle-android wait-for-boot
- run:
name: Disable emulator animations
command: |
adb shell settings put global window_animation_scale 0.0
adb shell settings put global transition_animation_scale 0.0
adb shell settings put global animator_duration_scale 0.0
- *build_test_android
- store_test_results:
path: android_instrumented_test/build/outputs/androidTest-results/connected
- persist_to_workspace:
root: ~/
paths:
Expand Down
26 changes: 26 additions & 0 deletions .circleci/setup_android_instrumented_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/sh

PROJECT_DIR=~/circleci-caver-java-android

CORE_DIR=$PROJECT_DIR/core
CORE_TEST_DIR=$PROJECT_DIR/core/src/test
CORE_TEST_CAVER_DIR=$CORE_TEST_DIR/java/com/klaytn/caver

ANDROID_DIR=$PROJECT_DIR/android_instrumented_test
ANDROID_TEST_DIR=$ANDROID_DIR/src/androidTest
ANDROID_TEST_CAVER_DIR=$ANDROID_TEST_DIR/java/com/klaytn/caver/android_instrumented_test

mkdir -p $ANDROID_TEST_CAVER_DIR

## To do instrumented tests on Android Emulator, we use test files we already have at `core/src/test/java/com.klaytn.caver/*`.
# 1. Copy unit test files of core to re-use without re-writing same test-cases just for instrumented test.
cp -r $CORE_TEST_CAVER_DIR/base $ANDROID_TEST_CAVER_DIR/
cp -r $CORE_TEST_CAVER_DIR/common $ANDROID_TEST_CAVER_DIR/

# 2. To make instrumented test works, we need to replace some contents.
find $ANDROID_TEST_CAVER_DIR -type f -name '*.java' | xargs sed -i 's/package com.klaytn.caver/package com.klaytn.caver.android_instrumented_test/g'
find $ANDROID_TEST_CAVER_DIR -type f -name '*.java' | xargs sed -i 's/import static com.klaytn.caver/import static com.klaytn.caver.android_instrumented_test/g'
find $ANDROID_TEST_CAVER_DIR -type f -name '*.java' | xargs sed -i 's/import com.klaytn.caver.base/import com.klaytn.caver.android_instrumented_test.base/g'
find $ANDROID_TEST_CAVER_DIR -type f -name '*.java' | xargs sed -i 's/Caver.DEFAULT_URL/"http:\/\/10.0.2.2:8551"/g'
find $ANDROID_TEST_CAVER_DIR -type f -name '*.java' | xargs sed -i 's/localhost/10.0.2.2/g'
##
22 changes: 22 additions & 0 deletions .circleci/setup_android_instrumented_test_mac.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/sh

PROJECT_DIR=/Users/denver.lee/klaytn/sdk-cell/github-aeharvlee/caver-java

CORE_DIR=$PROJECT_DIR/core
CORE_TEST_DIR=$PROJECT_DIR/core/src/test
CORE_TEST_CAVER_DIR=$CORE_TEST_DIR/java/com/klaytn/caver

ANDROID_DIR=$PROJECT_DIR/android_instrumented_test
ANDROID_TEST_DIR=$ANDROID_DIR/src/androidTest
ANDROID_TEST_CAVER_DIR=$ANDROID_TEST_DIR/java/com/klaytn/caver/android_instrumented_test

mkdir -p $ANDROID_TEST_CAVER_DIR

cp -r $CORE_TEST_CAVER_DIR/base $ANDROID_TEST_CAVER_DIR/
cp -r $CORE_TEST_CAVER_DIR/common $ANDROID_TEST_CAVER_DIR/

find $ANDROID_TEST_CAVER_DIR -type f -name '*.java' | xargs sed -i '' 's/package com.klaytn.caver/package com.klaytn.caver.android_instrumented_test/g'
find $ANDROID_TEST_CAVER_DIR -type f -name '*.java' | xargs sed -i '' 's/import static com.klaytn.caver/import static com.klaytn.caver.android_instrumented_test/g'
find $ANDROID_TEST_CAVER_DIR -type f -name '*.java' | xargs sed -i '' 's/import com.klaytn.caver.base/import com.klaytn.caver.android_instrumented_test.base/g'
find $ANDROID_TEST_CAVER_DIR -type f -name '*.java' | xargs sed -i '' 's/Caver.DEFAULT_URL/"http:\/\/10.0.2.2:8551"/g'
find $ANDROID_TEST_CAVER_DIR -type f -name '*.java' | xargs sed -i '' 's/localhost/10.0.2.2/g'
80 changes: 79 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
.idea/**/gradle.xml
.idea/**/libraries
.gradle/
gradle.properties

# Output
build
Expand Down Expand Up @@ -71,3 +70,82 @@ fabric.properties

# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser

## Gitignores for Android Project
# Built application files
*.apk
*.aar
*.ap_
*.aab

# Files for the ART/Dalvik VM
*.dex

# Java class files
*.class

# Generated files
bin/
gen/
out/
# Uncomment the following line in case you need and you don't have the release build type files in your app
# release/

# Local configuration file (sdk path, etc)
local.properties

# Proguard folder generated by Eclipse
proguard/

# Log Files
*.log

# Android Studio Navigation editor temp files
.navigation/

# Android Studio captures folder
captures/

# IntelliJ
.idea/assetWizardSettings.xml
# Android Studio 3 in .gitignore file.
# Comment next line if keeping position of elements in Navigation Editor is relevant for you
.idea/navEditor.xml

# Keystore files
# Uncomment the following lines if you do not want to check your keystore files in.
#*.jks
#*.keystore

# External native build folder generated in Android Studio 2.2 and later
.externalNativeBuild
.cxx/

# Google Services (e.g. APIs or Firebase)
# google-services.json

# Freeline
freeline.py
freeline/
freeline_project_description.json

# fastlane
fastlane/report.xml
fastlane/Preview.html
fastlane/screenshots
fastlane/test_output
fastlane/readme.md

# Version control
vcs.xml

# lint
lint/intermediates/
lint/generated/
lint/outputs/
lint/tmp/
# lint/reports/

# Android Profiling
*.hprof
##
46 changes: 46 additions & 0 deletions android_instrumented_test/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.3'
}

}

repositories {
google()
mavenCentral()
maven { url 'https://jitpack.io' }
}

apply plugin: 'com.android.application'

android {
compileSdkVersion 30
buildToolsVersion "30.0.3"

defaultConfig {
applicationId "com.klaytn.caver.android_instrumented_test"
minSdkVersion 24
targetSdkVersion 30
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

compileOptions {
sourceCompatibility 1.8
targetCompatibility 1.8
}
}

dependencies {
implementation project(':core')
androidTestImplementation "androidx.test.ext:junit:1.1.2"
androidTestImplementation "junit:junit:$project.junitVersion"
androidTestImplementation "androidx.test:runner:1.3.0"
androidTestImplementation "androidx.test:rules:1.3.0"
}
7 changes: 7 additions & 0 deletions android_instrumented_test/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.klaytn.caver.android_instrumented_test">
<uses-permission android:name="android.permission.INTERNET" />
<application android:usesCleartextTraffic="true">
</application>
</manifest>
8 changes: 5 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
buildscript {
ext.javapoetVersion = '1.7.0'
ext.web3jVersion = '4.6.0'
ext.web3jVersion = '4.6.0-android'
ext.picocliVersion = '3.0.0'
ext.junitVersion = '4.12'
ext.slf4jVersion = '1.7.25'
Expand All @@ -22,13 +22,15 @@ apply plugin: 'io.codearte.nexus-staging'


allprojects {
version '1.6.1'
version '1.6.1-android'
group 'com.klaytn.caver'
description 'caver-java project'

sourceCompatibility = 1.8
targetCompatibility = 1.8
}

configure(allprojects.findAll {it.name != 'android_instrumented_test'}) {
apply plugin: 'java'

repositories {
Expand All @@ -47,7 +49,7 @@ allprojects {
}
}

configure(subprojects.findAll {it.name != 'integration-test' }) {
configure(subprojects.findAll {it.name != 'integration-test' && it.name != 'android_instrumented_test' }) {
apply plugin: 'signing'
apply plugin: 'maven-publish'

Expand Down
4 changes: 4 additions & 0 deletions core/src/test/java/com/klaytn/caver/common/abi/ABITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import com.klaytn.caver.contract.Contract;
import com.klaytn.caver.contract.ContractIOType;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
Expand All @@ -19,6 +21,8 @@

import static org.junit.Assert.assertEquals;

@RunWith(Suite.class)
@Suite.SuiteClasses({ABITest.encodeFunctionSig.class, ABITest.encodeEventSig.class, ABITest.encodeFunctionCall.class, ABITest.decodeParameter.class, ABITest.decodeLog.class, ABITest.buildFunctionEventString.class})
public class ABITest {
static Caver caver = new Caver(Caver.DEFAULT_URL);
public static class encodeFunctionSig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import static org.junit.Assert.*;

@RunWith(Suite.class)
@Suite.SuiteClasses({AccountKeyTest.AccountKeyFailTests.class, AccountKeyTest.AccountKeyLegacyTest.class, AccountKeyTest.AccountKeyPublicTest.class, AccountKeyTest.AccountKeyWeightedMultiSigTest.class, AccountKeyTest.AccountKeyRoleBasedTest.class})
public class AccountKeyTest {

public static class AccountKeyFailTests {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.web3j.protocol.exceptions.TransactionException;

import java.io.IOException;
Expand All @@ -43,6 +45,8 @@
import static com.klaytn.caver.base.Accounts.LUMAN;
import static org.junit.Assert.*;

@RunWith(Suite.class)
@Suite.SuiteClasses({FeeDelegationWithContractTest.deployAndSendBasicTransaction.class, FeeDelegationWithContractTest.deployAndSendFeeDelegatedTransaction.class, FeeDelegationWithContractTest.deployAndSendFeeDelegatedWithRatioTransaction.class, FeeDelegationWithContractTest.signBasicTransaction.class, FeeDelegationWithContractTest.signFeeDelegatedTransaction.class, FeeDelegationWithContractTest.signFeeDelegatedTransactionWitRatio.class, FeeDelegationWithContractTest.signAsFeePayerFeeDelegatedTransaction.class, FeeDelegationWithContractTest.FeeDelegationInDefaultSendOptions.class, FeeDelegationWithContractTest.FeePayerInDefaultSendOptions.class, FeeDelegationWithContractTest.FeeRatioInDefaultSendOptions.class})
public class FeeDelegationWithContractTest {
public static String byteCodeWithoutConstructor = "608060405234801561001057600080fd5b5061051f806100206000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c8063693ec85e1461003b578063e942b5161461016f575b600080fd5b6100f46004803603602081101561005157600080fd5b810190808035906020019064010000000081111561006e57600080fd5b82018360208201111561008057600080fd5b803590602001918460018302840111640100000000831117156100a257600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f8201169050808301925050505050505091929192905050506102c1565b6040518080602001828103825283818151815260200191508051906020019080838360005b83811015610134578082015181840152602081019050610119565b50505050905090810190601f1680156101615780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6102bf6004803603604081101561018557600080fd5b81019080803590602001906401000000008111156101a257600080fd5b8201836020820111156101b457600080fd5b803590602001918460018302840111640100000000831117156101d657600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f8201169050808301925050505050505091929192908035906020019064010000000081111561023957600080fd5b82018360208201111561024b57600080fd5b8035906020019184600183028401116401000000008311171561026d57600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f8201169050808301925050505050505091929192905050506103cc565b005b60606000826040518082805190602001908083835b602083106102f957805182526020820191506020810190506020830392506102d6565b6001836020036101000a03801982511681845116808217855250505050505090500191505090815260200160405180910390208054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156103c05780601f10610395576101008083540402835291602001916103c0565b820191906000526020600020905b8154815290600101906020018083116103a357829003601f168201915b50505050509050919050565b806000836040518082805190602001908083835b6020831061040357805182526020820191506020810190506020830392506103e0565b6001836020036101000a0380198251168184511680821785525050505050509050019150509081526020016040518091039020908051906020019061044992919061044e565b505050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061048f57805160ff19168380011785556104bd565b828001600101855582156104bd579182015b828111156104bc5782518255916020019190600101906104a1565b5b5090506104ca91906104ce565b5090565b6104f091905b808211156104ec5760008160009055506001016104d4565b5090565b9056fea165627a7a723058203ffebc792829e0434ecc495da1b53d24399cd7fff506a4fd03589861843e14990029";
public static String abiWithoutConstructor = "[\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.web3j.protocol.exceptions.TransactionException;

import java.io.IOException;
Expand All @@ -30,6 +32,8 @@
import static com.klaytn.caver.base.Accounts.LUMAN;
import static org.junit.Assert.*;

@RunWith(Suite.class)
@Suite.SuiteClasses({KIP17Test.BurnableTest.class, KIP17Test.CommonTest.class, KIP17Test.DetectInterfaceTest.class, KIP17Test.EnumerableTest.class, KIP17Test.MintableTest.class, KIP17Test.PausableTest.class})
public class KIP17Test {
public static KIP17 kip17Contract;
public static final String CONTRACT_NAME = "NFT";
Expand Down
Loading

0 comments on commit 735c84b

Please sign in to comment.