From 200efb8a597e1745ea755e4e08df98e6d46e4a70 Mon Sep 17 00:00:00 2001 From: Tony Rankin Date: Thu, 2 Apr 2015 12:44:32 -0700 Subject: [PATCH 1/4] Adding Robolectric test framework and JaCoCo code coverage reports. --- README.md | 4 + WordPressEditor/build.gradle | 54 +++++++++++++ .../editor/EditorFragmentAbstractTest.java | 77 +++++++++++++++++++ 3 files changed, 135 insertions(+) create mode 100644 WordPressEditor/src/androidTest/java/org/wordpress/android/editor/EditorFragmentAbstractTest.java diff --git a/README.md b/README.md index 80a0172530d0..ed5dc2bcdbc0 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,10 @@ The WordPress-Android-Editor is the text editor used in the [WordPress Android app](https://github.com/wordpress-mobile/WordPress-Android) to create and edit pages & posts. In short it's a simple, straightforward way to visually edit HTML. +## Testing + +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`. + ## LICENSE This library is licensed under[MIT](LICENSE-MIT) diff --git a/WordPressEditor/build.gradle b/WordPressEditor/build.gradle index d29339355225..f3308796f468 100644 --- a/WordPressEditor/build.gradle +++ b/WordPressEditor/build.gradle @@ -4,10 +4,13 @@ buildscript { } dependencies { classpath 'com.android.tools.build:gradle:1.0.0' + classpath 'org.robolectric:robolectric-gradle-plugin:0.14.1' } } apply plugin: 'com.android.library' +apply plugin: 'robolectric' +apply plugin: 'jacoco' apply plugin: 'maven' apply plugin: 'signing' @@ -41,6 +44,17 @@ dependencies { compile 'org.wordpress:analytics:1.0.0' releaseCompile project(path:':libs:utils:WordPressUtils', configuration: 'release') debugCompile project(path:':libs:utils:WordPressUtils', configuration: 'debug') + + // Test libraries + testCompile 'junit:junit:4.10' + testCompile 'org.mockito:mockito-core:1.10.19' + testCompile 'org.robolectric:robolectric:2.4' + + // Workaround for IDE bug + // http://stackoverflow.com/questions/22246183/android-studio-doesnt-recognize-espresso-classes + provided 'junit:junit:4.10' + provided 'org.mockito:mockito-core:1.10.19' + provided 'org.robolectric:robolectric:2.4' } configurations.all { @@ -104,3 +118,43 @@ uploadArchives { } } } + +// +// Testing and code coverage +// + +robolectric { + include '**/*Test.class' + exclude '**/ApplicationTest.class' +} + +jacoco { + toolVersion = "0.7.1.201405082137" +} + +// Use these to define which classes to include and exclude from code coverage analysis +def coverageSourceDirs = [ 'src/main/java' ] +def coverageExclusions = [ '**/R.class', + '**/R$*.class', + '**/*$ViewInjector*.*', + '**/BuildConfig.*', + '**/Manifest*.*' ] + +task jacocoTestReport(type: JacocoReport, dependsOn: "testDebug") { + group = "Reporting" + description = "Generate Jacoco coverage reports" + + classDirectories = fileTree( + dir: 'build/intermediates/classes/debug', + excludes: coverageExclusions + ) + + additionalSourceDirs = files(coverageSourceDirs) + sourceDirectories = files(coverageSourceDirs) + executionData = files('build/jacoco/testDebug.exec') + + reports { + xml.enabled = true + html.enabled = true + } +} diff --git a/WordPressEditor/src/androidTest/java/org/wordpress/android/editor/EditorFragmentAbstractTest.java b/WordPressEditor/src/androidTest/java/org/wordpress/android/editor/EditorFragmentAbstractTest.java new file mode 100644 index 000000000000..53e7a31868cb --- /dev/null +++ b/WordPressEditor/src/androidTest/java/org/wordpress/android/editor/EditorFragmentAbstractTest.java @@ -0,0 +1,77 @@ +package org.wordpress.android.editor; + +import android.app.Activity; +import android.text.Spanned; + +import com.android.volley.toolbox.ImageLoader; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.Robolectric; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; +import org.wordpress.android.util.helpers.MediaFile; +import org.wordpress.android.util.helpers.MediaGallery; + +@Config(emulateSdk = 18) +@RunWith(RobolectricTestRunner.class) +public class EditorFragmentAbstractTest { + @Test + public void testActivityMustImplementEditorFragmentListener() { + // Host Activity must implement EditorFragmentListener, exception expected if not + boolean didPassTest = false; + Activity hostActivity = Robolectric.buildActivity(Activity.class).create().get(); + EditorFragmentAbstract testFragment = new DefaultEditorFragment(); + + try { + testFragment.onAttach(hostActivity); + } catch (ClassCastException classCastException) { + didPassTest = true; + } + + Assert.assertTrue(didPassTest); + } + + @Test + public void testOnBackPressReturnsFalseByDefault() { + // The default behavior of onBackPressed should return false + Assert.assertFalse(new DefaultEditorFragment().onBackPressed()); + } + + /** + * Used to test default behavior of non-abstract methods. + */ + public static class DefaultEditorFragment extends EditorFragmentAbstract { + @Override + public void setTitle(CharSequence text) { + } + + @Override + public void setContent(CharSequence text) { + } + + @Override + public CharSequence getTitle() { + return null; + } + + @Override + public CharSequence getContent() { + return null; + } + + @Override + public void appendMediaFile(MediaFile mediaFile, String imageUrl, ImageLoader imageLoader) { + } + + @Override + public void appendGallery(MediaGallery mediaGallery) { + } + + @Override + public Spanned getSpannedContent() { + return null; + } + } +} From 17ec76cc6204158aba9d163047ca6445ca72d1d4 Mon Sep 17 00:00:00 2001 From: Tony Rankin Date: Thu, 9 Apr 2015 15:14:38 -0700 Subject: [PATCH 2/4] Updating robolectric-gradle-plugin and the Android gradle build plugin. --- WordPressEditor/build.gradle | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/WordPressEditor/build.gradle b/WordPressEditor/build.gradle index 3b2e5bd90b1a..a3c00fc4e147 100644 --- a/WordPressEditor/build.gradle +++ b/WordPressEditor/build.gradle @@ -3,13 +3,13 @@ buildscript { mavenCentral() } dependencies { - classpath 'com.android.tools.build:gradle:1.0.0' - classpath 'org.robolectric:robolectric-gradle-plugin:0.14.1' + classpath 'com.android.tools.build:gradle:1.1.0' + classpath 'org.robolectric:robolectric-gradle-plugin:1.0.0' } } apply plugin: 'com.android.library' -apply plugin: 'robolectric' +apply plugin: 'org.robolectric' apply plugin: 'jacoco' apply plugin: 'maven' apply plugin: 'signing' From 4c2f2d1d9cff500cf621d62f69e4946c13dcd09c Mon Sep 17 00:00:00 2001 From: Tony Rankin Date: Thu, 9 Apr 2015 15:37:08 -0700 Subject: [PATCH 3/4] Downgrading plugin, tests aren't working with the new tools. --- WordPressEditor/build.gradle | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/WordPressEditor/build.gradle b/WordPressEditor/build.gradle index a3c00fc4e147..3b2e5bd90b1a 100644 --- a/WordPressEditor/build.gradle +++ b/WordPressEditor/build.gradle @@ -3,13 +3,13 @@ buildscript { mavenCentral() } dependencies { - classpath 'com.android.tools.build:gradle:1.1.0' - classpath 'org.robolectric:robolectric-gradle-plugin:1.0.0' + classpath 'com.android.tools.build:gradle:1.0.0' + classpath 'org.robolectric:robolectric-gradle-plugin:0.14.1' } } apply plugin: 'com.android.library' -apply plugin: 'org.robolectric' +apply plugin: 'robolectric' apply plugin: 'jacoco' apply plugin: 'maven' apply plugin: 'signing' From 5346958aa84f672d94111d8e32b097df0795e073 Mon Sep 17 00:00:00 2001 From: Alex Forcier Date: Fri, 10 Apr 2015 10:21:06 -0400 Subject: [PATCH 4/4] Added a lint rule ignoring the 'InvalidPackage' check for Robolectric - Fixes lint error caused by the IDE bug workaround in build.gradle --- README.md | 10 ++++------ WordPressEditor/lint.xml | 6 ++++++ 2 files changed, 10 insertions(+), 6 deletions(-) create mode 100644 WordPressEditor/lint.xml diff --git a/README.md b/README.md index d446a9db61b3..7e8e16baa09b 100644 --- a/README.md +++ b/README.md @@ -4,12 +4,6 @@ ## Introduction ## -## Testing - -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`. - -## LICENSE - WordPress-Editor-Android is the text editor used in the [WordPress Android app](https://github.com/wordpress-mobile/WordPress-Android) to create and edit pages & posts. In short it's a simple, straightforward way to visually edit HTML. ## Build Instructions ## @@ -30,6 +24,10 @@ Finally, update `[PROJECT_ROOT]\.git\info\exclude` to ignore the symlink locally # assets symlink WordPressEditor/src/main/assets +## Testing ## + +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`. + ## LICENSE ## WordPress-Editor-Android is an Open Source project covered by the [GNU General Public License version 2](LICENSE.md). diff --git a/WordPressEditor/lint.xml b/WordPressEditor/lint.xml new file mode 100644 index 000000000000..b7b4876d98b8 --- /dev/null +++ b/WordPressEditor/lint.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file