Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ buildscript {
constraintlayout_version = '2.0.0-beta3'
dynamicanimation_version = '1.0.0'
navigation_version = '2.1.0'
espresso_version = '3.2.0'
robolectric_version = '4.3.1'
androidxtest_version = '1.2.0'
androidxtest_junit_version = '1.1.1'
mockk_version = '1.9.3'
}
repositories {
google()
jcenter()

}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.2'
Expand All @@ -34,6 +38,11 @@ allprojects {
google()
jcenter()
}
configurations.all {
resolutionStrategy {
force("org.objenesis:objenesis:2.6")
}
}
}

task clean(type: Delete) {
Expand Down
34 changes: 31 additions & 3 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ android {
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

archivesBaseName = "$archivesBaseName-$versionName"

multiDexEnabled true
}

buildTypes {
Expand All @@ -22,6 +24,23 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

testOptions {
unitTests {
includeAndroidResources true
}
}

sourceSets {
androidTest {
manifest.srcFile 'src/uiTest/AndroidManifest.xml'
java.srcDirs += "src/uiTest/java"
}
test {
manifest.srcFile 'src/uiTest/AndroidManifest.xml'
java.srcDirs += "src/uiTest/java"
}
}
}

dependencies {
Expand All @@ -32,9 +51,18 @@ dependencies {
implementation "androidx.core:core-ktx:$corektx_version"
api "androidx.dynamicanimation:dynamicanimation:$dynamicanimation_version"

testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation "androidx.multidex:multidex:2.0.1"

testImplementation "androidx.test.ext:truth:$androidxtest_version"
testImplementation "androidx.test.ext:junit:$androidxtest_junit_version"
testImplementation "org.robolectric:robolectric:$robolectric_version"
testImplementation "io.mockk:mockk:$mockk_version"

androidTestImplementation "androidx.test.ext:truth:$androidxtest_version"
androidTestImplementation "androidx.test.ext:junit:$androidxtest_junit_version"
androidTestImplementation "androidx.test:runner:$androidxtest_version"
androidTestImplementation "org.robolectric:annotations:$robolectric_version"
androidTestImplementation "io.mockk:mockk-android:$mockk_version"
}

repositories {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.github.lcdsmao.springx

import androidx.test.platform.app.InstrumentationRegistry

@Suppress("unused")
class InstrumentationUiTestScope : UiTestScope {

override fun runOnMainSync(block: () -> Unit) {
InstrumentationRegistry.getInstrumentation().runOnMainSync {
block.invoke()
}
}

override fun waitForIdleSync() {
InstrumentationRegistry.getInstrumentation().waitForIdleSync()
}

class Runner : UiTestScope.Runner {

override fun runUiTest(block: () -> Unit) {
block.invoke()
}
}
}
12 changes: 12 additions & 0 deletions library/src/main/res/layout/activity_animation.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<View
android:id="@+id/anim_view"
android:background="#D81B60"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_gravity="center" />
</FrameLayout>

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.github.lcdsmao.springx

import android.os.Handler
import android.os.Looper
import org.robolectric.shadows.ShadowLooper
import java.util.concurrent.CountDownLatch
import kotlin.concurrent.thread

@Suppress("unused")
class UnitUiTestScope : UiTestScope {

private val mainHandler = Handler(Looper.getMainLooper())

override fun runOnMainSync(block: () -> Unit) {
val latch = CountDownLatch(1)
mainHandler.post {
try {
block.invoke()
} finally {
latch.countDown()
}
}
latch.await()
}

override fun waitForIdleSync() {
val latch = CountDownLatch(1)
mainHandler.post {
ShadowLooper.idleMainLooper()
latch.countDown()
}
latch.await()
}

class Runner : UiTestScope.Runner {

override fun runUiTest(block: () -> Unit) {
val testThread = thread(name = "Unit UI Test Thread") {
block.invoke()
}
while (testThread.isAlive) {
ShadowLooper.idleMainLooper()
}
}
}
}
7 changes: 7 additions & 0 deletions library/src/uiTest/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.github.lcdsmao.springx">

<application android:theme="@style/Theme.AppCompat">
<activity android:name=".AnimationActivity" />
</application>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.github.lcdsmao.springx

import androidx.appcompat.app.AppCompatActivity

class AnimationActivity : AppCompatActivity(R.layout.activity_animation)
28 changes: 28 additions & 0 deletions library/src/uiTest/java/com/github/lcdsmao/springx/UiTestScope.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.github.lcdsmao.springx

interface UiTestScope {

interface Runner {
fun runUiTest(block: () -> Unit)
}

fun runOnMainSync(block: () -> Unit)
fun waitForIdleSync()
}

fun runUiTest(uiTestScope: UiTestScope.() -> Unit) {
val (scope, runner) = getUiTestDelegate()
runner.runUiTest { uiTestScope(scope) }
}

private fun getUiTestDelegate(): Pair<UiTestScope, UiTestScope.Runner> {
val testScopeName =
if (System.getProperty("java.runtime.name")!!.toLowerCase().contains("android")) {
"com.github.lcdsmao.springx.InstrumentationUiTestScope"
} else {
"com.github.lcdsmao.springx.UnitUiTestScope"
}
val runnerName = "$testScopeName\$Runner"
return Class.forName(testScopeName).newInstance() as UiTestScope to
Class.forName(runnerName).newInstance() as UiTestScope.Runner
}
Loading