Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions circularfillableloaders/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ android {
compileSdkVersion 23
buildToolsVersion "23.0.2"

testOptions {
unitTests.returnDefaultValues = true
}

defaultConfig {
minSdkVersion 15
targetSdkVersion 23
Expand Down Expand Up @@ -113,4 +117,14 @@ bintray {
}
}
}
}
dependencies {
compile 'junit:junit:4.12'
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'
}
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(imageSize, imageSize);
}

private int measureWidth(int measureSpec) {
protected int measureWidth(int measureSpec) {
int result;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
Expand All @@ -281,7 +281,7 @@ private int measureWidth(int measureSpec) {
return result;
}

private int measureHeight(int measureSpecHeight) {
protected int measureHeight(int measureSpecHeight) {
int result;
int specMode = MeasureSpec.getMode(measureSpecHeight);
int specSize = MeasureSpec.getSize(measureSpecHeight);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package com.mikhaellopez.circularfillableloaders;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.internal.verification.Times;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import static junit.framework.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyFloat;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.internal.util.reflection.Whitebox.getInternalState;

/**
* Created by mohd.farid@devfactory.com on 20/01/16.
*/

@RunWith(PowerMockRunner.class)
@PrepareForTest({CircularFillableLoaders.class, ObjectAnimator.class, AnimatorSet.class, View.class})
public class CircularFillableLoadersTest {

private CircularFillableLoaders loaders;

@Mock
private Context context;

@Mock
private AnimatorSet animatorSet;

@Mock
private Paint paint;

@Mock
private TypedArray typedArray;

@Before
public void setUp() throws Exception {
doNothing().when(paint).setAntiAlias(true);
PowerMockito.whenNew(Paint.class).withNoArguments().thenReturn(paint);

PowerMockito.mockStatic(ObjectAnimator.class);
ObjectAnimator objectAnimator = mock(ObjectAnimator.class);
Mockito.when(ObjectAnimator.ofFloat(any(), eq("waveShiftRatio"), eq(0f), eq(1f))).thenReturn(objectAnimator);

ObjectAnimator objectAnimator2 = mock(ObjectAnimator.class);
Mockito.when(ObjectAnimator.ofFloat(any(), eq("waterLevelRatio"), anyFloat(), anyFloat())).thenReturn(objectAnimator2);

PowerMockito.whenNew(AnimatorSet.class).withNoArguments().thenReturn(animatorSet);

Mockito.when(context.obtainStyledAttributes(any(AttributeSet.class), eq(R.styleable.CircularFillableLoaders), anyInt(), eq(0))).thenReturn(typedArray);

loaders = new CircularFillableLoaders(context);

//pre-test assertions
assertEquals(animatorSet, getInternalState(loaders, "animatorSetWave"));
assertEquals(paint, getInternalState(loaders, "paint"));
}

@Test
public void testOnSizeChanged() throws Exception {
//when
loaders.onSizeChanged(10, 20, 20, 40);

//then
assertEquals(10, getInternalState(loaders, "canvasSize"));

//when
loaders.onSizeChanged(20, 10, 40, 20);

//then
assertEquals(10, getInternalState(loaders, "canvasSize"));
}


@Test
public void testSetColor() throws Exception {
//when
loaders.setColor(10);

//then
assertEquals(10, getInternalState(loaders, "waveColor"));
}

@Test
public void testSetBorderWidth() throws Exception {
//when
loaders.setBorderWidth(100);

//then
verify(paint).setStrokeWidth(100);
}

@Test
public void testOnAttachedToWindow() throws Exception {
//given
verify(animatorSet, new Times(1)).start();

//when
loaders.onAttachedToWindow();

//then
verify(animatorSet, new Times(2)).start();
}

@Test
public void testOnDetachedFromWindow() throws Exception {
//when
loaders.onDetachedFromWindow();

//then
verify(animatorSet).end();
}
}
Empty file modified gradlew
100644 → 100755
Empty file.