Skip to content

Commit eaa2f17

Browse files
committed
Merge pull request #21 from wordpress-mobile/feature/test-framework
Adding Robolectric test framework and JaCoCo code coverage reports.
2 parents fc5f50d + 5346958 commit eaa2f17

File tree

4 files changed

+144
-1
lines changed

4 files changed

+144
-1
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ Finally, update `[PROJECT_ROOT]\.git\info\exclude` to ignore the symlink locally
2424
# assets symlink
2525
WordPressEditor/src/main/assets
2626

27+
## Testing ##
28+
29+
Testing is done with the [Robolectric framework.](http://robolectric.org/) To run tests simply run `gradlew testDebug`. Code coverage reports can be generated via [JaCoCo.](http://www.eclemma.org/jacoco/) To generate them locally run `gradlew jacocoTestReport`.
30+
2731
## LICENSE ##
2832

29-
WordPress-Editor-Android is an Open Source project covered by the [GNU General Public License version 2](LICENSE.md).
33+
WordPress-Editor-Android is an Open Source project covered by the [GNU General Public License version 2](LICENSE.md).

WordPressEditor/build.gradle

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ buildscript {
44
}
55
dependencies {
66
classpath 'com.android.tools.build:gradle:1.0.0'
7+
classpath 'org.robolectric:robolectric-gradle-plugin:0.14.1'
78
}
89
}
910

1011
apply plugin: 'com.android.library'
12+
apply plugin: 'robolectric'
13+
apply plugin: 'jacoco'
1114
apply plugin: 'maven'
1215
apply plugin: 'signing'
1316

@@ -40,6 +43,17 @@ dependencies {
4043
compile 'com.android.support:support-v4:21.0.+'
4144
compile 'org.wordpress:analytics:1.0.0'
4245
compile 'org.wordpress:utils:1.5.0'
46+
47+
// Test libraries
48+
testCompile 'junit:junit:4.10'
49+
testCompile 'org.mockito:mockito-core:1.10.19'
50+
testCompile 'org.robolectric:robolectric:2.4'
51+
52+
// Workaround for IDE bug
53+
// http://stackoverflow.com/questions/22246183/android-studio-doesnt-recognize-espresso-classes
54+
provided 'junit:junit:4.10'
55+
provided 'org.mockito:mockito-core:1.10.19'
56+
provided 'org.robolectric:robolectric:2.4'
4357
}
4458

4559
signing {
@@ -97,3 +111,45 @@ uploadArchives {
97111
}
98112
}
99113
}
114+
115+
//
116+
// Testing and code coverage
117+
//
118+
119+
robolectric {
120+
include '**/*Test.class'
121+
exclude '**/ApplicationTest.class'
122+
}
123+
124+
jacoco {
125+
toolVersion = "0.7.1.201405082137"
126+
}
127+
128+
// Use these to define which classes to include and exclude from code coverage analysis
129+
def coverageSourceDirs = [ 'src/main/java' ]
130+
def coverageExclusions = [ '**/R.class',
131+
'**/R$*.class',
132+
'**/*$ViewInjector*.*',
133+
'**/BuildConfig.*',
134+
'**/Manifest*.*',
135+
'**/Legacy**.class',
136+
'**/legacy/**/*.class' ]
137+
138+
task jacocoTestReport(type: JacocoReport, dependsOn: "testDebug") {
139+
group = "Reporting"
140+
description = "Generate Jacoco coverage reports"
141+
142+
classDirectories = fileTree(
143+
dir: 'build/intermediates/classes/debug',
144+
excludes: coverageExclusions
145+
)
146+
147+
additionalSourceDirs = files(coverageSourceDirs)
148+
sourceDirectories = files(coverageSourceDirs)
149+
executionData = files('build/jacoco/testDebug.exec')
150+
151+
reports {
152+
xml.enabled = true
153+
html.enabled = true
154+
}
155+
}

WordPressEditor/lint.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<lint>
3+
<issue id="InvalidPackage">
4+
<ignore regexp="robolectric-2.4.jar" />
5+
</issue>
6+
</lint>
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package org.wordpress.android.editor;
2+
3+
import android.app.Activity;
4+
import android.text.Spanned;
5+
6+
import com.android.volley.toolbox.ImageLoader;
7+
8+
import org.junit.Assert;
9+
import org.junit.Test;
10+
import org.junit.runner.RunWith;
11+
import org.robolectric.Robolectric;
12+
import org.robolectric.RobolectricTestRunner;
13+
import org.robolectric.annotation.Config;
14+
import org.wordpress.android.util.helpers.MediaFile;
15+
import org.wordpress.android.util.helpers.MediaGallery;
16+
17+
@Config(emulateSdk = 18)
18+
@RunWith(RobolectricTestRunner.class)
19+
public class EditorFragmentAbstractTest {
20+
@Test
21+
public void testActivityMustImplementEditorFragmentListener() {
22+
// Host Activity must implement EditorFragmentListener, exception expected if not
23+
boolean didPassTest = false;
24+
Activity hostActivity = Robolectric.buildActivity(Activity.class).create().get();
25+
EditorFragmentAbstract testFragment = new DefaultEditorFragment();
26+
27+
try {
28+
testFragment.onAttach(hostActivity);
29+
} catch (ClassCastException classCastException) {
30+
didPassTest = true;
31+
}
32+
33+
Assert.assertTrue(didPassTest);
34+
}
35+
36+
@Test
37+
public void testOnBackPressReturnsFalseByDefault() {
38+
// The default behavior of onBackPressed should return false
39+
Assert.assertFalse(new DefaultEditorFragment().onBackPressed());
40+
}
41+
42+
/**
43+
* Used to test default behavior of non-abstract methods.
44+
*/
45+
public static class DefaultEditorFragment extends EditorFragmentAbstract {
46+
@Override
47+
public void setTitle(CharSequence text) {
48+
}
49+
50+
@Override
51+
public void setContent(CharSequence text) {
52+
}
53+
54+
@Override
55+
public CharSequence getTitle() {
56+
return null;
57+
}
58+
59+
@Override
60+
public CharSequence getContent() {
61+
return null;
62+
}
63+
64+
@Override
65+
public void appendMediaFile(MediaFile mediaFile, String imageUrl, ImageLoader imageLoader) {
66+
}
67+
68+
@Override
69+
public void appendGallery(MediaGallery mediaGallery) {
70+
}
71+
72+
@Override
73+
public Spanned getSpannedContent() {
74+
return null;
75+
}
76+
}
77+
}

0 commit comments

Comments
 (0)