forked from r0adkll/Slidr
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request r0adkll#32 from DevFactory/release-UTS-330
Add unit tests for library
- Loading branch information
Showing
8 changed files
with
238 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
/build | ||
/build | ||
*.iml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
/build | ||
*.iml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
library/src/test/java/com/r0adkll/slidr/model/PojoTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
119
library/src/test/java/com/r0adkll/slidr/widget/SliderPanelTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |