Skip to content

Commit c99f32d

Browse files
committed
add test app
1 parent 46c04e1 commit c99f32d

File tree

11 files changed

+302
-8
lines changed

11 files changed

+302
-8
lines changed

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
* text eol=lf
33
*.bat eol=crlf
44
*.jar binary
5+
*.png binary

.travis.yml

+27-2
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,39 @@ before_install:
99
- echo yes | sdkmanager "platform-tools" &>/dev/null
1010
- echo yes | sdkmanager "build-tools;27.0.1" &>/dev/null
1111
- echo yes | sdkmanager "platforms;android-27" &>/dev/null
12+
# Download emulator
13+
- echo yes | sdkmanager "platforms;android-19" &>/dev/null
14+
- echo yes | sdkmanager "system-images;android-19;default;armeabi-v7a" &>/dev/null
1215
# Update remaining dependencies and accept licenses
1316
- echo yes | sdkmanager --update &>/dev/null
1417
- echo yes | sdkmanager --licenses &>/dev/null
18+
# Setup emulator
19+
- echo no | avdmanager create avd --force -n test -k "system-images;android-19;default;armeabi-v7a" &>/dev/null
20+
- $ANDROID_HOME/emulator/emulator -avd test -no-audio -no-window & &>/dev/null
1521

16-
install: true
22+
install:
23+
- ./gradlew clean build jacocoTestReport coveralls --scan --stacktrace
24+
- rm -rf ~/.m2/repository/com/jaredsburrows/gradle-spoon-plugin/ && ./gradlew publishMavenPublicationToMavenLocal -x test
25+
26+
before_script:
27+
# Make sure the emulator is available
28+
- android-wait-for-emulator
29+
30+
# Turn off animations
31+
- adb shell settings put global window_animation_scale 0 &
32+
- adb shell settings put global transition_animation_scale 0 &
33+
- adb shell settings put global animator_duration_scale 0 &
34+
35+
# For Multidex issue for devices API < 19
36+
- adb shell setprop dalvik.vm.dexopt-flags v=n,o=v
37+
- adb shell stop installd
38+
- adb shell start installd
39+
40+
# Wake up
41+
- adb shell input keyevent 82 &
1742

1843
script:
19-
- ./gradlew clean build jacocoTestReport coveralls --scan --stacktrace
44+
- cd test-app && ../gradlew clean spoonDebugAndroidTest checkReports
2045

2146
after_success:
2247
- .buildscript/deploy-snapshot.sh

gradle/dependencies.gradle

+28-6
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,39 @@ ext {
33
javaVersion = "1.8"
44
agpVersion = rootProject.hasProperty("agpVersion") ? rootProject.property("agpVersion") : "3.0.0"
55
kotlinVersion = "1.2.0"
6+
minSdkVersion = 19
7+
targetSdkVersion = 27
8+
compileSdkVersion = 27
9+
buildToolsVersion = "27.0.1"
10+
spoonVersion = "2.0.0-SNAPSHOT"
11+
jacocoVersion = "0.7.4.201502262128"
612
}
713

814
// Dependencies
915
ext.deps = [
16+
// plugin
17+
"gradle" : "com.android.tools.build:gradle:$agpVersion",
18+
"kotlinGradlePlugin": "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion",
19+
1020
// implementation
11-
"kotlinStdlib" : "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlinVersion",
12-
"kotlinReflect": "org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion",
13-
"gradle" : "com.android.tools.build:gradle:$agpVersion",
14-
"spoonRunner" : "com.squareup.spoon:spoon-runner:latest.integration",
15-
"ddmlib" : "com.android.tools.ddms:ddmlib:26.0.0",
21+
"kotlinStdlib" : "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlinVersion",
22+
"kotlinReflect" : "org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion",
23+
"spoonRunner" : "com.squareup.spoon:spoon-runner:$spoonVersion",
24+
"ddmlib" : "com.android.tools.ddms:ddmlib:26.0.0",
1625

1726
// testImplementation
18-
"spockCore" : "org.spockframework:spock-core:1.1-groovy-2.4"
27+
"junit" : "junit:junit:4.12",
28+
"truth" : "com.google.truth:truth:0.36",
29+
"spockCore" : "org.spockframework:spock-core:1.1-groovy-2.4",
30+
31+
// androidTestImplementation
32+
"spoonClient" : "com.squareup.spoon:spoon-client:$spoonVersion",
33+
"runner" : "com.android.support.test:runner:0.5",
34+
"rules" : "com.android.support.test:rules:0.5",
35+
36+
// jacocoAgent/androidJacocoAgent
37+
"orgJacocoAgent" : "org.jacoco:org.jacoco.agent:$jacocoVersion",
38+
39+
// jacocoAnt/androidJacocoAnt
40+
"orgJacocoAnt" : "org.jacoco:org.jacoco.ant:$jacocoVersion"
1941
]

test-app/build.gradle

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
buildscript {
2+
apply from: rootProject.file("../gradle/dependencies.gradle")
3+
4+
repositories {
5+
google()
6+
jcenter()
7+
maven { url "https://oss.sonatype.org/content/repositories/snapshots" } // For Spoon snapshot, until 2.0.0 is released
8+
mavenLocal()
9+
}
10+
11+
dependencies {
12+
classpath deps.gradle
13+
classpath deps.kotlinGradlePlugin
14+
classpath "com.jaredsburrows:gradle-spoon-plugin:latest.integration"
15+
}
16+
}
17+
18+
repositories {
19+
jcenter()
20+
google()
21+
maven { url "https://oss.sonatype.org/content/repositories/snapshots" } // For Spoon snapshot, until 2.0.0 is released
22+
}
23+
24+
apply plugin: "com.android.application"
25+
apply plugin: "kotlin-android"
26+
apply plugin: "com.jaredsburrows.spoon"
27+
28+
android {
29+
compileSdkVersion rootProject.ext.minSdkVersion
30+
buildToolsVersion rootProject.ext.buildToolsVersion
31+
32+
defaultConfig {
33+
applicationId "com.jaredsburrows.spoon.example"
34+
minSdkVersion rootProject.ext.minSdkVersion
35+
targetSdkVersion rootProject.ext.targetSdkVersion
36+
versionCode 1
37+
versionName "1.0"
38+
39+
testApplicationId "com.jaredsburrows.spoon.example.test"
40+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
41+
}
42+
43+
buildTypes {
44+
debug {
45+
testCoverageEnabled = true
46+
}
47+
}
48+
}
49+
50+
spoon {
51+
debug = true
52+
53+
output = file("$buildDir/custom-report-dir")
54+
55+
if (project.hasProperty("spoonClassName")) {
56+
className = project.spoonClassName
57+
58+
if (project.hasProperty("spoonMethodName")) {
59+
methodName = project.spoonMethodName
60+
}
61+
}
62+
63+
instrumentationArgs = ["foo:bar"]
64+
65+
adbTimeout = 30
66+
67+
// codeCoverage = true
68+
69+
grantAllPermissions = true
70+
}
71+
72+
configurations.all {
73+
resolutionStrategy {
74+
force deps.orgJacocoAgent
75+
force deps.orgJacocoAnt
76+
}
77+
}
78+
79+
dependencies {
80+
implementation deps.kotlinStdlib
81+
82+
androidTestImplementation deps.truth
83+
androidTestImplementation deps.junit
84+
androidTestImplementation deps.spoonClient
85+
androidTestImplementation deps.runner
86+
androidTestImplementation deps.rules
87+
}
88+
89+
task checkReports() {
90+
doLast {
91+
assert project.tasks.spoonDebugAndroidTest != null
92+
assert project.file("$buildDir/custom-report-dir").exists()
93+
assert project.file("$buildDir/custom-report-dir/debug").exists()
94+
// assert project.file("$buildDir/custom-report-dir/debug/coverage").exists()
95+
}
96+
}

test-app/settings.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include ":test-app"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.jaredsburrows.spoon.example
2+
3+
import android.support.test.annotation.UiThreadTest
4+
import android.support.test.rule.ActivityTestRule
5+
import android.support.test.runner.AndroidJUnit4
6+
import android.widget.TextView
7+
8+
import com.squareup.spoon.SpoonRule
9+
import com.jaredsburrows.spoon.example.MainActivity
10+
11+
import org.junit.Rule
12+
import org.junit.Test
13+
import org.junit.runner.RunWith
14+
15+
import com.google.common.truth.Truth.assertThat
16+
17+
@RunWith(AndroidJUnit4::class)
18+
class AnotherMainActivityTest {
19+
@get:Rule
20+
val spoon = SpoonRule()
21+
@get:Rule
22+
val activityRule = ActivityTestRule(MainActivity::class.java)
23+
24+
@Test
25+
@UiThreadTest
26+
fun testSetText() {
27+
val act = activityRule.getActivity()
28+
val text = act.findViewById(android.R.id.text1) as TextView
29+
spoon.screenshot(act, "startup")
30+
31+
val steps = 5
32+
for (i in 1..steps) {
33+
val step = i.toString()
34+
act.setText(step)
35+
spoon.screenshot(act, "step-" + i)
36+
assertThat(text.getText().toString()).isEqualTo(step)
37+
}
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.jaredsburrows.spoon.example
2+
3+
import android.support.test.annotation.UiThreadTest
4+
import android.support.test.rule.ActivityTestRule
5+
import android.support.test.runner.AndroidJUnit4
6+
import android.test.suitebuilder.annotation.LargeTest
7+
import android.test.suitebuilder.annotation.MediumTest
8+
import android.test.suitebuilder.annotation.SmallTest
9+
import android.widget.TextView
10+
11+
import com.squareup.spoon.SpoonRule
12+
import com.jaredsburrows.spoon.example.MainActivity
13+
14+
import org.junit.Rule
15+
import org.junit.Test
16+
import org.junit.runner.RunWith
17+
18+
import com.google.common.truth.Truth.assertThat
19+
20+
@RunWith(AndroidJUnit4::class)
21+
class MainActivityTest {
22+
@get:Rule
23+
val spoon = SpoonRule()
24+
@get:Rule
25+
val activityRule = ActivityTestRule(MainActivity::class.java)
26+
27+
@Test
28+
@UiThreadTest
29+
fun testSetText() {
30+
val act = activityRule.getActivity()
31+
val text = act.findViewById(android.R.id.text1) as TextView
32+
spoon.screenshot(act, "startup")
33+
34+
val steps = 5
35+
for (i in 1..steps) {
36+
val step = i.toString()
37+
act.setText(step)
38+
spoon.screenshot(act, "step-" + i)
39+
assertThat(text.getText().toString()).isEqualTo(step)
40+
}
41+
}
42+
43+
@SmallTest
44+
fun testSmallTest() {
45+
val act = activityRule.getActivity()
46+
spoon.screenshot(act, "startup-smallTest")
47+
}
48+
49+
@MediumTest
50+
fun testMediumTest() {
51+
val act = activityRule.getActivity()
52+
spoon.screenshot(act, "startup-mediumTest")
53+
}
54+
55+
@LargeTest
56+
fun testLargeTest() {
57+
val act = activityRule.getActivity()
58+
spoon.screenshot(act, "startup-largeTest")
59+
}
60+
}

test-app/src/main/AndroidManifest.xml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.jaredsburrows.spoon.example">
4+
5+
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
6+
7+
<application
8+
android:allowBackup="false"
9+
android:icon="@mipmap/ic_launcher"
10+
android:label="@string/app_name">
11+
<activity
12+
android:name=".MainActivity">
13+
<intent-filter>
14+
<action android:name="android.intent.action.MAIN"/>
15+
<category android:name="android.intent.category.LAUNCHER"/>
16+
</intent-filter>
17+
</activity>
18+
</application>
19+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.jaredsburrows.spoon.example
2+
3+
import android.app.Activity
4+
import android.os.Bundle
5+
import android.view.Gravity
6+
import android.widget.TextView
7+
8+
class MainActivity : Activity() {
9+
companion object {
10+
private const val TEXT_SIZE = 30F
11+
}
12+
13+
private lateinit var textView: TextView
14+
15+
override fun onCreate(savedInstanceState: Bundle?) {
16+
super.onCreate(savedInstanceState)
17+
textView = TextView(this)
18+
textView.setId(android.R.id.text1)
19+
textView.setGravity(Gravity.CENTER)
20+
textView.setTextSize(TEXT_SIZE)
21+
setContentView(textView)
22+
}
23+
24+
fun setText(text: String) {
25+
textView.setText(text)
26+
}
27+
}
2.74 KB
Loading
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<string name="app_name">Spoon Gradle Example</string>
4+
</resources>

0 commit comments

Comments
 (0)