Skip to content

Commit

Permalink
Added Robolectric tests
Browse files Browse the repository at this point in the history
  • Loading branch information
VladimirWrites committed Feb 9, 2018
1 parent f0a44de commit 177e984
Show file tree
Hide file tree
Showing 30 changed files with 545 additions and 29 deletions.
2 changes: 1 addition & 1 deletion build.gradle
@@ -1,5 +1,5 @@
buildscript {
ext.kotlin_version = '1.2.20'
ext.kotlin_version = '1.2.21'
repositories {
jcenter()
google()
Expand Down
15 changes: 7 additions & 8 deletions lemniscate/build.gradle
Expand Up @@ -35,6 +35,9 @@ android {
includeNoLocationClasses = true
}
}
unitTests {
includeAndroidResources = true
}
}
}

Expand All @@ -60,16 +63,12 @@ task jacocoDebugReport(type: JacocoReport, dependsOn: ['testDebugUnitTest', 'cre
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})

implementation fileTree(include: ['*.jar'], dir: 'libs')
testImplementation 'junit:junit:4.12'
testImplementation 'org.mockito:mockito-core:2.11.0'
testImplementation "com.nhaarman:mockito-kotlin:1.5.0"
testImplementation "com.google.truth:truth:0.39"

testImplementation 'com.nhaarman:mockito-kotlin:1.5.0'
testImplementation 'com.google.truth:truth:0.39'
testImplementation 'org.robolectric:robolectric:3.6.1'
implementation 'com.android.support:appcompat-v7:27.0.2'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
Expand Down
Expand Up @@ -45,8 +45,8 @@ interface IBaseCurveView {
return i * 2f * PI.toFloat() / precision
}

fun invalidateView()
fun invalidateProgressView()

fun requestViewLayout()
fun requestProgressViewLayout()
}

Expand Up @@ -18,7 +18,7 @@ class BaseCurvePresenter(override val view: IBaseCurveView,
override fun updateStartingPointOnCurve(point: Int) {
animationSettings.startingPointOnCurve = point
drawState.recalculateLineLength(curveSettings.lineLength)
view.invalidateView()
view.invalidateProgressView()
}

internal val lineLengthToDraw: Int
Expand Down
Expand Up @@ -50,7 +50,7 @@ abstract class BaseCurveProgressView : View, IBaseCurveView {

constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {

val curveAttributes = context.theme.obtainStyledAttributes(
val curveAttributes = context.obtainStyledAttributes(
attrs,
R.styleable.BaseCurveProgressView,
0, 0)
Expand Down Expand Up @@ -107,11 +107,11 @@ abstract class BaseCurveProgressView : View, IBaseCurveView {
setMeasuredDimension(round(presenter.viewSize.size + xPadding).toInt(), round(presenter.viewSize.size + yPadding).toInt())
}

private fun getMaxViewSquareSize(height: Int, width: Int, xPadding: Int, yPadding: Int): Int {
internal fun getMaxViewSquareSize(height: Int, width: Int, xPadding: Int, yPadding: Int): Int {
return min(height - yPadding, width - xPadding)
}

private fun getViewDimension(mode: Int, viewSize: Float, defaultSize: Float): Float {
internal fun getViewDimension(mode: Int, viewSize: Float, defaultSize: Float): Float {
return when {
viewSize == 0.0f -> defaultSize
mode == View.MeasureSpec.EXACTLY -> viewSize
Expand Down Expand Up @@ -248,11 +248,11 @@ abstract class BaseCurveProgressView : View, IBaseCurveView {
}
}

override fun invalidateView() {
override fun invalidateProgressView() {
invalidate()
}

override fun requestViewLayout() {
override fun requestProgressViewLayout() {
requestLayout()
}
}
Expand Up @@ -40,7 +40,7 @@ class LineLength : Parcelable {

constructor()

protected constructor(`in`: Parcel) {
internal constructor(`in`: Parcel) {
this.lineMinLength = `in`.readFloat()
this.lineMaxLength = `in`.readFloat()
}
Expand Down
Expand Up @@ -20,7 +20,7 @@ import android.os.Parcelable

class AnimationSettings(var startingPointOnCurve:Int = 0, var duration: Int = 1000) : Parcelable {

protected constructor(`in`: Parcel) : this() {
internal constructor(`in`: Parcel) : this() {
this.startingPointOnCurve = `in`.readInt()
this.duration = `in`.readInt()
}
Expand Down
Expand Up @@ -45,7 +45,7 @@ open class CurveSettings (val paint: Paint = Paint(Paint.ANTI_ALIAS_FLAG), var
}
var hasHole = false

protected constructor(`in`: Parcel) : this() {
internal constructor(`in`: Parcel) : this() {
this.precision = `in`.readInt()
this.strokeWidth = `in`.readFloat()
this.color = `in`.readInt()
Expand Down
Expand Up @@ -46,4 +46,9 @@ class CannabisProgressView : BaseCurveProgressView {
* (9 / 10f * cos(8 * t) + 1)
* (1 / 10f * cos(24 * t) + 1)
* (1 / 10f * cos(200 * t) + 9 / 10f)) + size / 4

override var hasHole: Boolean = false
set(hasHole) {
super.hasHole = false
}
}
Expand Up @@ -40,4 +40,9 @@ class HeartProgressView : BaseCurveProgressView {
- 5 * cos(2 * t)
- 2 * cos(3 * t)
- cos(4 * t))

override var hasHole: Boolean = false
set(hasHole) {
super.hasHole = false
}
}
Expand Up @@ -32,14 +32,14 @@ class XProgressView : BaseCurveProgressView {

constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

override var hasHole: Boolean = false
set(hasHole) {
super.hasHole = false
}

override fun getGraphX(t: Float): Float =
size * abs(sin(t)) * cos(t)

override fun getGraphY(t: Float): Float =
size * sin(t) * cos(t)

override var hasHole: Boolean = false
set(hasHole) {
super.hasHole = false
}
}
Expand Up @@ -61,7 +61,7 @@ abstract class BaseRouletteProgressView : BaseCurveProgressView {
constructor(context: Context) : super(context)

constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
val rouletteCurveAttributes = context.theme.obtainStyledAttributes(
val rouletteCurveAttributes = context.obtainStyledAttributes(
attrs,
R.styleable.RouletteCurveProgressView,
0, 0)
Expand All @@ -78,7 +78,7 @@ abstract class BaseRouletteProgressView : BaseCurveProgressView {

constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

protected open fun recalculateConstants() {}
internal open fun recalculateConstants() {}

override var hasHole: Boolean = false
set(hasHole) {
Expand Down
Expand Up @@ -24,7 +24,7 @@ class RouletteCurveSettings : Parcelable {

constructor()

protected constructor(`in`: Parcel) {
internal constructor(`in`: Parcel) {
this.radiusFixed = `in`.readFloat()
this.radiusMoving = `in`.readFloat()
this.distanceFromCenter = `in`.readFloat()
Expand Down
Expand Up @@ -24,7 +24,7 @@ class BaseCurvePresenterTest {
presenter.updateStartingPointOnCurve(1)
verify(animationSettings).startingPointOnCurve = 1
verify(drawState).recalculateLineLength(curveSettings.lineLength)
verify(view).invalidateView()
verify(view).invalidateProgressView()
}

@Test
Expand Down
@@ -0,0 +1,78 @@
package com.vlad1m1r.lemniscate.base

import android.view.View
import com.google.common.truth.Truth.assertThat
import com.nhaarman.mockito_kotlin.any
import com.nhaarman.mockito_kotlin.doCallRealMethod
import com.nhaarman.mockito_kotlin.mock
import com.nhaarman.mockito_kotlin.whenever
import org.junit.Before
import org.junit.Test


class BaseCurveProgressViewTest {

val baseCurve: BaseCurve = BaseCurve()

val baseCurveProgressView = mock<BaseCurveProgressView>()

@Before
fun setUp() {
doCallRealMethod().whenever(baseCurveProgressView).getMaxViewSquareSize(any(), any(), any(), any())
doCallRealMethod().whenever(baseCurveProgressView).getViewDimension(any(), any(), any())
}

@Test
fun getMaxViewSquareSize() {
assertThat(baseCurveProgressView.getMaxViewSquareSize(100, 200, 30, 50)).isEqualTo(100-50)
assertThat(baseCurveProgressView.getMaxViewSquareSize(220, 150, 30, 50)).isEqualTo(150-30)
}

@Test
fun getViewDimensionWhenViewSizeIsZero() {
val defaultSize = 10f
assertThat(baseCurveProgressView.getViewDimension(View.MeasureSpec.AT_MOST, 0f, defaultSize)).isEqualTo(defaultSize)
}

@Test
fun getViewDimensionWhenExactly() {
val viewSize = 10f
assertThat(baseCurveProgressView.getViewDimension(View.MeasureSpec.EXACTLY, viewSize, 10f)).isEqualTo(viewSize)
}

@Test
fun getViewDimensionWhenAtMost() {
assertThat(baseCurveProgressView.getViewDimension(View.MeasureSpec.AT_MOST, 10f, 20f)).isEqualTo(10f)
assertThat(baseCurveProgressView.getViewDimension(View.MeasureSpec.AT_MOST, 30f, 20f)).isEqualTo(20f)
}

@Test
fun getViewDimensionWhenUnspecified() {
val defaultSize = 10f
assertThat(baseCurveProgressView.getViewDimension(View.MeasureSpec.UNSPECIFIED, 20f, defaultSize)).isEqualTo(defaultSize)
}

@Test
fun getT() {
assertThat(baseCurve.getT(0, 10)).isEqualTo(0.0f)
assertThat(baseCurve.getT(1, 10)).isEqualTo(0.62831855f)
assertThat(baseCurve.getT(10, 10)).isEqualTo(6.2831855f)
assertThat(baseCurve.getT(99, 8)).isEqualTo(77.75442f)
assertThat(baseCurve.getT(-1, 10)).isEqualTo(-0.62831855f)
assertThat(baseCurve.getT(-10, 10)).isEqualTo(-6.2831855f)
}

inner class BaseCurve : IBaseCurveView {
override fun getGraphX(t: Float): Float {
return 0f
}

override fun getGraphY(t: Float): Float {
return 0f
}

override fun invalidateProgressView() {}

override fun requestProgressViewLayout() {}
}
}
@@ -0,0 +1,59 @@
package com.vlad1m1r.lemniscate.base

import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
import com.google.common.truth.Truth.assertThat
import com.nhaarman.mockito_kotlin.*
import com.vlad1m1r.lemniscate.BernoullisProgressView
import com.vlad1m1r.lemniscate.sample.lemniscate.R
import com.vlad1m1r.lemniscate.testutils.TestLayoutInflater
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment

@RunWith(RobolectricTestRunner::class)
class BaseProgressViewAttributesTest {

val context = RuntimeEnvironment.application.applicationContext
val typedArray = mock<TypedArray>()
val atributeSet = mock<AttributeSet>()

val contextSpy = spy(context)

@Before
fun setUp() {

val layoutInflater = TestLayoutInflater(contextSpy)
whenever(contextSpy.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
.thenReturn(layoutInflater)

whenever(typedArray.getFloat(eq(R.styleable.BaseCurveProgressView_maxLineLength), any())).thenReturn(0.81f)
whenever(typedArray.getFloat(eq(R.styleable.BaseCurveProgressView_minLineLength), any())).thenReturn(0.23f)

whenever(typedArray.getColor(eq(R.styleable.BaseCurveProgressView_lineColor), any())).thenReturn(123)
whenever(typedArray.getBoolean(eq(R.styleable.BaseCurveProgressView_hasHole), any())).thenReturn(true)
whenever(typedArray.getDimension(eq(R.styleable.BaseCurveProgressView_strokeWidth), any())).thenReturn(32.2f)
whenever(typedArray.getInteger(eq(R.styleable.BaseCurveProgressView_precision), any())).thenReturn(111)

whenever(typedArray.getInteger(eq(R.styleable.BaseCurveProgressView_duration), any())).thenReturn(999)

doReturn(typedArray).whenever(contextSpy).obtainStyledAttributes(atributeSet, R.styleable.BaseCurveProgressView, 0, 0)
}

@Test
fun constructorWithAttributeSet() {
val bernoullisProgressView = BernoullisProgressView(contextSpy, atributeSet)
assertThat(bernoullisProgressView.lineMaxLength).isEqualTo(0.81f)
assertThat(bernoullisProgressView.lineMinLength).isEqualTo(0.23f)

assertThat(bernoullisProgressView.color).isEqualTo(123)
assertThat(bernoullisProgressView.hasHole).isTrue()
assertThat(bernoullisProgressView.strokeWidth).isEqualTo(32.2f)
assertThat(bernoullisProgressView.precision).isEqualTo(111)

assertThat(bernoullisProgressView.duration).isEqualTo(999)
}
}
@@ -0,0 +1,35 @@
package com.vlad1m1r.lemniscate.base.models

import android.os.Parcel
import com.google.common.truth.Truth.assertThat
import com.vlad1m1r.lemniscate.testutils.isEqualTo
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner


@RunWith(RobolectricTestRunner::class)
class LineLengthParcelableTest {

private lateinit var lineLength: LineLength

@Before
fun setUp() {
lineLength = LineLength()
lineLength.lineMinLength = 0.24f
lineLength.lineMaxLength = 0.83f
}

@Test
fun parcelable() {
val parcel = Parcel.obtain()
lineLength.writeToParcel(parcel, 0)
parcel.setDataPosition(0)

val copy = LineLength(parcel)
parcel.recycle()

assertThat(lineLength.isEqualTo(copy)).isTrue()
}
}

0 comments on commit 177e984

Please sign in to comment.