Skip to content

Commit 2b8d9fe

Browse files
authored
Merge pull request #15 from lcdsmao/ui-test
Add some UI tests
2 parents 357de76 + fcace93 commit 2b8d9fe

File tree

13 files changed

+414
-89
lines changed

13 files changed

+414
-89
lines changed

build.gradle

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,15 @@ buildscript {
1414
constraintlayout_version = '2.0.0-beta3'
1515
dynamicanimation_version = '1.0.0'
1616
navigation_version = '2.1.0'
17+
espresso_version = '3.2.0'
18+
robolectric_version = '4.3.1'
19+
androidxtest_version = '1.2.0'
20+
androidxtest_junit_version = '1.1.1'
21+
mockk_version = '1.9.3'
1722
}
1823
repositories {
1924
google()
2025
jcenter()
21-
2226
}
2327
dependencies {
2428
classpath 'com.android.tools.build:gradle:3.5.2'
@@ -34,6 +38,11 @@ allprojects {
3438
google()
3539
jcenter()
3640
}
41+
configurations.all {
42+
resolutionStrategy {
43+
force("org.objenesis:objenesis:2.6")
44+
}
45+
}
3746
}
3847

3948
task clean(type: Delete) {

library/build.gradle

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ android {
1414
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1515

1616
archivesBaseName = "$archivesBaseName-$versionName"
17+
18+
multiDexEnabled true
1719
}
1820

1921
buildTypes {
@@ -22,6 +24,23 @@ android {
2224
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
2325
}
2426
}
27+
28+
testOptions {
29+
unitTests {
30+
includeAndroidResources true
31+
}
32+
}
33+
34+
sourceSets {
35+
androidTest {
36+
manifest.srcFile 'src/uiTest/AndroidManifest.xml'
37+
java.srcDirs += "src/uiTest/java"
38+
}
39+
test {
40+
manifest.srcFile 'src/uiTest/AndroidManifest.xml'
41+
java.srcDirs += "src/uiTest/java"
42+
}
43+
}
2544
}
2645

2746
dependencies {
@@ -32,9 +51,18 @@ dependencies {
3251
implementation "androidx.core:core-ktx:$corektx_version"
3352
api "androidx.dynamicanimation:dynamicanimation:$dynamicanimation_version"
3453

35-
testImplementation 'junit:junit:4.12'
36-
androidTestImplementation 'androidx.test:runner:1.2.0'
37-
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
54+
implementation "androidx.multidex:multidex:2.0.1"
55+
56+
testImplementation "androidx.test.ext:truth:$androidxtest_version"
57+
testImplementation "androidx.test.ext:junit:$androidxtest_junit_version"
58+
testImplementation "org.robolectric:robolectric:$robolectric_version"
59+
testImplementation "io.mockk:mockk:$mockk_version"
60+
61+
androidTestImplementation "androidx.test.ext:truth:$androidxtest_version"
62+
androidTestImplementation "androidx.test.ext:junit:$androidxtest_junit_version"
63+
androidTestImplementation "androidx.test:runner:$androidxtest_version"
64+
androidTestImplementation "org.robolectric:annotations:$robolectric_version"
65+
androidTestImplementation "io.mockk:mockk-android:$mockk_version"
3866
}
3967

4068
repositories {

library/src/androidTest/java/com/github/lcdsmao/springx/ExampleInstrumentedTest.java

Lines changed: 0 additions & 27 deletions
This file was deleted.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.github.lcdsmao.springx
2+
3+
import androidx.test.platform.app.InstrumentationRegistry
4+
5+
@Suppress("unused")
6+
class InstrumentationUiTestScope : UiTestScope {
7+
8+
override fun runOnMainSync(block: () -> Unit) {
9+
InstrumentationRegistry.getInstrumentation().runOnMainSync {
10+
block.invoke()
11+
}
12+
}
13+
14+
override fun waitForIdleSync() {
15+
InstrumentationRegistry.getInstrumentation().waitForIdleSync()
16+
}
17+
18+
class Runner : UiTestScope.Runner {
19+
20+
override fun runUiTest(block: () -> Unit) {
21+
block.invoke()
22+
}
23+
}
24+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent">
5+
6+
<View
7+
android:id="@+id/anim_view"
8+
android:background="#D81B60"
9+
android:layout_width="24dp"
10+
android:layout_height="24dp"
11+
android:layout_gravity="center" />
12+
</FrameLayout>

library/src/test/java/com/github/lcdsmao/springx/ExampleUnitTest.java

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.github.lcdsmao.springx
2+
3+
import android.os.Handler
4+
import android.os.Looper
5+
import org.robolectric.shadows.ShadowLooper
6+
import java.util.concurrent.CountDownLatch
7+
import kotlin.concurrent.thread
8+
9+
@Suppress("unused")
10+
class UnitUiTestScope : UiTestScope {
11+
12+
private val mainHandler = Handler(Looper.getMainLooper())
13+
14+
override fun runOnMainSync(block: () -> Unit) {
15+
val latch = CountDownLatch(1)
16+
mainHandler.post {
17+
try {
18+
block.invoke()
19+
} finally {
20+
latch.countDown()
21+
}
22+
}
23+
latch.await()
24+
}
25+
26+
override fun waitForIdleSync() {
27+
val latch = CountDownLatch(1)
28+
mainHandler.post {
29+
ShadowLooper.idleMainLooper()
30+
latch.countDown()
31+
}
32+
latch.await()
33+
}
34+
35+
class Runner : UiTestScope.Runner {
36+
37+
override fun runUiTest(block: () -> Unit) {
38+
val testThread = thread(name = "Unit UI Test Thread") {
39+
block.invoke()
40+
}
41+
while (testThread.isAlive) {
42+
ShadowLooper.idleMainLooper()
43+
}
44+
}
45+
}
46+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2+
package="com.github.lcdsmao.springx">
3+
4+
<application android:theme="@style/Theme.AppCompat">
5+
<activity android:name=".AnimationActivity" />
6+
</application>
7+
</manifest>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.github.lcdsmao.springx
2+
3+
import androidx.appcompat.app.AppCompatActivity
4+
5+
class AnimationActivity : AppCompatActivity(R.layout.activity_animation)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.github.lcdsmao.springx
2+
3+
interface UiTestScope {
4+
5+
interface Runner {
6+
fun runUiTest(block: () -> Unit)
7+
}
8+
9+
fun runOnMainSync(block: () -> Unit)
10+
fun waitForIdleSync()
11+
}
12+
13+
fun runUiTest(uiTestScope: UiTestScope.() -> Unit) {
14+
val (scope, runner) = getUiTestDelegate()
15+
runner.runUiTest { uiTestScope(scope) }
16+
}
17+
18+
private fun getUiTestDelegate(): Pair<UiTestScope, UiTestScope.Runner> {
19+
val testScopeName =
20+
if (System.getProperty("java.runtime.name")!!.toLowerCase().contains("android")) {
21+
"com.github.lcdsmao.springx.InstrumentationUiTestScope"
22+
} else {
23+
"com.github.lcdsmao.springx.UnitUiTestScope"
24+
}
25+
val runnerName = "$testScopeName\$Runner"
26+
return Class.forName(testScopeName).newInstance() as UiTestScope to
27+
Class.forName(runnerName).newInstance() as UiTestScope.Runner
28+
}

0 commit comments

Comments
 (0)