Skip to content

Commit

Permalink
Merge pull request r0adkll#32 from DevFactory/release-UTS-330
Browse files Browse the repository at this point in the history
Add unit tests for library
  • Loading branch information
r0adkll committed Feb 5, 2016
2 parents 358ae8a + e344ca9 commit 9c54705
Show file tree
Hide file tree
Showing 8 changed files with 238 additions and 3 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

local.properties
*.iml
.idea/libraries
Expand All @@ -7,4 +6,6 @@ local.properties
.idea/modules.xml
.idea/misc.xml
.idea/vcs.xml
build
build
.idea
.gradle
17 changes: 17 additions & 0 deletions CODE_COVERAGE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Code Coverage Report generation

To generate the code coverage report, execute the following command:

For windows:

> gradlew cleanTest testDebugUnitTestCoverage
For linux and OSx

> ./gradlew cleanTest testDebugUnitTestCoverage
This will generate code coverage report in each of the projects. In order to view the same, open the following file in your browser.
> ./library/build/reports/jacoco/testDebugUnitTestCoverage/html/index.html


3 changes: 2 additions & 1 deletion example/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/build
/build
*.iml
30 changes: 30 additions & 0 deletions jacoco.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
apply plugin: 'jacoco'

jacoco {
toolVersion = "0.7.5.201505241946"
}

task testDebugUnitTestCoverage (type:JacocoReport, dependsOn: "testDebugUnitTest") {
group = "Reporting"
description = "Generate Jacoco coverage reports on the DEBUG build."

classDirectories = fileTree(
dir: "${project.buildDir}/intermediates/classes/debug",
excludes: ['**/R.class',
'**/R$*.class',
'**/*$ViewInjector*.*',
'**/*$ViewBinder*.*',
'**/BuildConfig.*',
'**/Manifest*.*']
)

def coverageSourceDirs = ["src/main/java"]
additionalSourceDirs = files(coverageSourceDirs)
sourceDirectories = files(coverageSourceDirs)
executionData = files("${project.buildDir}/jacoco/testDebugUnitTest.exec")

reports {
xml.enabled = true
html.enabled = true
}
}
1 change: 1 addition & 0 deletions library/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/build
*.iml
16 changes: 16 additions & 0 deletions library/build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
apply from: '../jacoco.gradle'
apply plugin: 'com.android.library'
apply plugin: 'maven'

Expand All @@ -11,10 +12,18 @@ android {
versionName libraryVersion
}

testOptions {
unitTests.returnDefaultValues = true
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
testCoverageEnabled true
}
debug {
testCoverageEnabled true
}
}

Expand All @@ -26,6 +35,13 @@ android {

dependencies {
compile supportDependencies.support
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:1.10.19'
testCompile 'org.hamcrest:hamcrest-library:1.1'
testCompile 'org.powermock:powermock-api-mockito:1.6.2'
testCompile 'org.powermock:powermock-module-junit4:1.6.2'
testCompile 'com.openpojo:openpojo:0.8.0'
testCompile 'asm:asm:3.3.1'
}

apply from: '../gradle-mvn-push.gradle'
Expand Down
50 changes: 50 additions & 0 deletions library/src/test/java/com/r0adkll/slidr/model/PojoTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.r0adkll.slidr.model;

import com.openpojo.reflection.PojoClass;
import com.openpojo.reflection.filters.FilterPackageInfo;
import com.openpojo.reflection.impl.PojoClassFactory;
import com.openpojo.validation.Validator;
import com.openpojo.validation.ValidatorBuilder;
import com.openpojo.validation.test.impl.GetterTester;
import com.openpojo.validation.test.impl.SetterTester;

import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
* Created by farid on 17/01/16.
*/
public class PojoTest {

private ArrayList<PojoClass> pojoClasses = new ArrayList<>();

@Before
public void setUp() throws Exception {
String[] packages = {"com.r0adkll.slidr.model"};

for (String pojoPackage : packages) {
List<PojoClass> packagePojoClasses = PojoClassFactory.getPojoClasses(pojoPackage, new FilterPackageInfo());
for (PojoClass clazz : packagePojoClasses) {
if (clazz.getName().contains("$") || clazz.isAbstract() || clazz.isInterface() || clazz.isEnum()
|| clazz.getName().endsWith("Test"))
continue;
pojoClasses.add(clazz);
}
}
}

@Test
public void testPojos() throws Exception {
Validator validator = ValidatorBuilder.create().with(new GetterTester()).with(new SetterTester()).build();
for (PojoClass clazz : pojoClasses) {
try {
validator.validate(clazz);
} catch (AssertionError ex) {
continue;
}
}
}
}
119 changes: 119 additions & 0 deletions library/src/test/java/com/r0adkll/slidr/widget/SliderPanelTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package com.r0adkll.slidr.widget;

import android.content.Context;
import android.support.v4.widget.ViewDragHelper;
import android.view.MotionEvent;
import android.view.View;

import com.r0adkll.slidr.model.SlidrConfig;
import com.r0adkll.slidr.model.SlidrPosition;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Matchers;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.internal.util.reflection.Whitebox;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;
import static org.mockito.internal.util.reflection.Whitebox.setInternalState;

/**
* Created by farid on 18/01/16.
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({View.class})
public class SliderPanelTest {

@Mock
Context context;

@Mock
MotionEvent motionEvent;

@Test
public void testOnInterceptTouchEvent_whenLocked() throws Exception {
//given
SliderPanel sliderPanel = new SliderPanel(context);
setInternalState(sliderPanel, "mIsLocked", true);

//when
boolean result = sliderPanel.onInterceptTouchEvent(motionEvent);

//then
assertFalse("Result must be false when isLocked is true", result);
}


@Test
public void testOnInterceptTouchEvent_whenNotLoacked_edgeOnly() throws Exception {
//given
SliderPanel sliderPanel = Mockito.spy(new SliderPanel(context));
PowerMockito.when(sliderPanel, "getWidth").thenReturn(10);

SlidrConfig slidrConfig = Mockito.mock(SlidrConfig.class);
when(slidrConfig.isEdgeOnly()).thenReturn(true);
when(slidrConfig.getPosition()).thenReturn(SlidrPosition.LEFT);
when(slidrConfig.getEdgeSize(Matchers.anyInt())).thenReturn(10.1f);

setInternalState(sliderPanel, "mIsLocked", false);
setInternalState(sliderPanel, "mConfig", slidrConfig);

//when
boolean result = sliderPanel.onInterceptTouchEvent(motionEvent);

//then
assertFalse("Result must be false", result);
}

@Test
public void testOnTouchEvent_whenLocked() throws Exception {
//given
SliderPanel sliderPanel = Mockito.spy(new SliderPanel(context));
setInternalState(sliderPanel, "mIsLocked", true);

//when
boolean result = sliderPanel.onTouchEvent(motionEvent);

//then
assertFalse("Result must be false when locked", result);
}

@Test
public void testOnTouchEvent_whenNotLocked() throws Exception {
//given
SliderPanel sliderPanel = Mockito.spy(new SliderPanel(context));
setInternalState(sliderPanel, "mIsLocked", false);

ViewDragHelper viewDragHelper = Mockito.mock(ViewDragHelper.class);
setInternalState(sliderPanel, "mDragHelper", viewDragHelper);

//when
boolean result = sliderPanel.onTouchEvent(motionEvent);

//then
assertTrue("Result must be true when not locked", result);
}

@Test
public void testOnTouchEvent_whenNotLocked_butExceptionInProcessTouchEvent() throws Exception {
//given
SliderPanel sliderPanel = Mockito.spy(new SliderPanel(context));
setInternalState(sliderPanel, "mIsLocked", false);

ViewDragHelper viewDragHelper = Mockito.mock(ViewDragHelper.class);
PowerMockito.doThrow(new IllegalArgumentException()).when(viewDragHelper).processTouchEvent(motionEvent);
setInternalState(sliderPanel, "mDragHelper", viewDragHelper);

//when
boolean result = sliderPanel.onTouchEvent(motionEvent);

//then
assertFalse("Result must be false when not locked but exception occured during processTouchEvent()", result);
}
}

0 comments on commit 9c54705

Please sign in to comment.