Skip to content

Commit a7a84f0

Browse files
#149 : Robolectric Test for BrushDrawingView
1 parent b15bfcf commit a7a84f0

File tree

7 files changed

+198
-27
lines changed

7 files changed

+198
-27
lines changed

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ dependencies {
2323
implementation 'com.android.support:appcompat-v7:27.1.1'
2424
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
2525
implementation 'com.android.support:design:27.1.1'
26-
implementation 'ja.burhanrashid52:photoeditor:0.3.3'
27-
//implementation project(':photoeditor')
26+
//implementation 'ja.burhanrashid52:photoeditor:0.3.3'
27+
implementation project(':photoeditor')
2828
implementation 'com.android.support:cardview-v7:27.1.1'
2929

3030
testImplementation 'junit:junit:4.12'

gradlew

100644100755
File mode changed.

photoeditor/build.gradle

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ android {
4646
}
4747
}
4848

49+
testOptions {
50+
unitTests {
51+
includeAndroidResources = true
52+
}
53+
}
54+
4955
tasks.withType(Javadoc) {
5056
options.addStringOption('Xdoclint:none', '-quiet')
5157
options.addStringOption('encoding', 'UTF-8')
@@ -55,13 +61,17 @@ android {
5561

5662
dependencies {
5763
implementation fileTree(dir: 'libs', include: ['*.jar'])
64+
65+
implementation 'com.android.support:appcompat-v7:27.1.1'
66+
implementation "com.android.support:support-v13:27.1.1"
67+
5868
androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
5969
exclude group: 'com.android.support', module: 'support-annotations'
6070
})
61-
implementation 'com.android.support:appcompat-v7:27.1.1'
62-
implementation "com.android.support:support-v13:27.1.1"
63-
androidTestImplementation 'junit:junit:4.12'
71+
6472
testImplementation 'junit:junit:4.12'
73+
testImplementation 'org.mockito:mockito-core:2.23.0'
74+
testImplementation 'org.robolectric:robolectric:4.2'
6575
}
6676

6777
// Place it at the end of the file

photoeditor/src/main/java/ja/burhanrashid52/photoeditor/BrushDrawingView.java

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import android.support.annotation.ColorInt;
1313
import android.support.annotation.IntRange;
1414
import android.support.annotation.NonNull;
15+
import android.support.annotation.VisibleForTesting;
1516
import android.util.AttributeSet;
1617
import android.view.MotionEvent;
1718
import android.view.View;
@@ -30,13 +31,18 @@
3031
*/
3132
public class BrushDrawingView extends View {
3233

33-
private float mBrushSize = 25;
34-
private float mBrushEraserSize = 50;
35-
private int mOpacity = 255;
34+
static final float DEFAULT_BRUSH_SIZE = 25f;
35+
static final float DEFAULT_ERASER_SIZE = 50f;
36+
static final int DEFAULT_OPACITY = 255;
37+
38+
private float mBrushSize = DEFAULT_BRUSH_SIZE;
39+
private float mBrushEraserSize = DEFAULT_ERASER_SIZE;
40+
private int mOpacity = DEFAULT_OPACITY;
3641

3742
private Stack<LinePath> mDrawnPaths = new Stack<>();
3843
private Stack<LinePath> mRedoPaths = new Stack<>();
39-
private Paint mDrawPaint;
44+
private Paint mDrawPaint = new Paint();
45+
;
4046

4147
private Canvas mDrawCanvas;
4248
private boolean mBrushDrawMode;
@@ -64,11 +70,15 @@ public BrushDrawingView(Context context, AttributeSet attrs, int defStyle) {
6470
private void setupBrushDrawing() {
6571
//Caution: This line is to disable hardware acceleration to make eraser feature work properly
6672
setLayerType(LAYER_TYPE_HARDWARE, null);
67-
mDrawPaint = new Paint();
73+
mDrawPaint.setColor(Color.BLACK);
74+
setupPathAndPaint();
75+
setVisibility(View.GONE);
76+
}
77+
78+
private void setupPathAndPaint() {
6879
mPath = new Path();
6980
mDrawPaint.setAntiAlias(true);
7081
mDrawPaint.setDither(true);
71-
mDrawPaint.setColor(Color.BLACK);
7282
mDrawPaint.setStyle(Paint.Style.STROKE);
7383
mDrawPaint.setStrokeJoin(Paint.Join.ROUND);
7484
mDrawPaint.setStrokeCap(Paint.Cap.ROUND);
@@ -77,22 +87,11 @@ private void setupBrushDrawing() {
7787
//Resolve Brush color changes after saving image #52
7888
//Resolve Brush bug using PorterDuff.Mode.SRC_OVER #80 and PR #83
7989
mDrawPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER));
80-
this.setVisibility(View.GONE);
8190
}
8291

8392
private void refreshBrushDrawing() {
8493
mBrushDrawMode = true;
85-
mPath = new Path();
86-
mDrawPaint.setAntiAlias(true);
87-
mDrawPaint.setDither(true);
88-
mDrawPaint.setStyle(Paint.Style.STROKE);
89-
mDrawPaint.setStrokeJoin(Paint.Join.ROUND);
90-
mDrawPaint.setStrokeCap(Paint.Cap.ROUND);
91-
mDrawPaint.setStrokeWidth(mBrushSize);
92-
mDrawPaint.setAlpha(mOpacity);
93-
//Resolve Brush color changes after saving image #52
94-
//Resolve Brush bug using PorterDuff.Mode.SRC_OVER #80 and PR #83
95-
mDrawPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER));
94+
setupPathAndPaint();
9695
}
9796

9897
void brushEraser() {
@@ -283,4 +282,9 @@ private void touchUp() {
283282
mBrushViewChangeListener.onViewAdd(this);
284283
}
285284
}
285+
286+
@VisibleForTesting
287+
Paint getDrawingPaint() {
288+
return mDrawPaint;
289+
}
286290
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package ja.burhanrashid52.photoeditor;
2+
3+
import android.content.Context;
4+
import android.graphics.Color;
5+
import android.graphics.Paint;
6+
import android.graphics.PorterDuffXfermode;
7+
import android.view.View;
8+
9+
import org.junit.Test;
10+
import org.junit.runner.RunWith;
11+
import org.mockito.Mockito;
12+
import org.robolectric.RobolectricTestRunner;
13+
import org.robolectric.RuntimeEnvironment;
14+
15+
import static junit.framework.Assert.assertEquals;
16+
import static junit.framework.Assert.assertFalse;
17+
import static junit.framework.TestCase.assertTrue;
18+
import static org.mockito.Mockito.times;
19+
import static org.mockito.Mockito.verify;
20+
21+
@RunWith(RobolectricTestRunner.class)
22+
public class BrushDrawingViewTest {
23+
24+
private Context mContext = RuntimeEnvironment.systemContext;
25+
26+
@Test
27+
public void testDefaultPaintAttributes() {
28+
BrushDrawingView brushDrawingView = new BrushDrawingView(mContext);
29+
Paint drawingPaint = brushDrawingView.getDrawingPaint();
30+
31+
assertEquals(drawingPaint.getColor(), Color.BLACK);
32+
assertEquals(drawingPaint.getStyle(), Paint.Style.STROKE);
33+
assertEquals(drawingPaint.getStrokeJoin(), Paint.Join.ROUND);
34+
assertEquals(drawingPaint.getStrokeCap(), Paint.Cap.ROUND);
35+
assertEquals(drawingPaint.getStrokeWidth(), BrushDrawingView.DEFAULT_BRUSH_SIZE);
36+
assertEquals(drawingPaint.getAlpha(), BrushDrawingView.DEFAULT_OPACITY);
37+
assertTrue(drawingPaint.getXfermode() instanceof PorterDuffXfermode);
38+
}
39+
40+
@Test
41+
public void testPaintAttributesAfterBrushModeIsEnabled() {
42+
BrushDrawingView brushDrawingView = new BrushDrawingView(mContext);
43+
brushDrawingView.setBrushDrawingMode(true);
44+
Paint drawingPaint = brushDrawingView.getDrawingPaint();
45+
46+
assertEquals(drawingPaint.getStyle(), Paint.Style.STROKE);
47+
assertEquals(drawingPaint.getStrokeJoin(), Paint.Join.ROUND);
48+
assertEquals(drawingPaint.getStrokeCap(), Paint.Cap.ROUND);
49+
assertEquals(drawingPaint.getStrokeWidth(), BrushDrawingView.DEFAULT_BRUSH_SIZE);
50+
assertEquals(drawingPaint.getAlpha(), BrushDrawingView.DEFAULT_OPACITY);
51+
assertTrue(drawingPaint.getXfermode() instanceof PorterDuffXfermode);
52+
53+
Paint spyPaint = Mockito.spy(drawingPaint);
54+
verify(spyPaint, times(0)).setColor(Color.BLACK);
55+
}
56+
57+
@Test
58+
public void testByDefaultBrushDrawingModeIsDisabled() {
59+
BrushDrawingView brushDrawingView = new BrushDrawingView(mContext);
60+
assertFalse(brushDrawingView.getBrushDrawingMode());
61+
assertEquals(brushDrawingView.getVisibility(), View.GONE);
62+
}
63+
64+
@Test
65+
public void testWhenBrushDrawingModeIsSetEnabled() {
66+
BrushDrawingView brushDrawingView = new BrushDrawingView(mContext);
67+
brushDrawingView.setBrushDrawingMode(true);
68+
assertTrue(brushDrawingView.getBrushDrawingMode());
69+
assertEquals(brushDrawingView.getVisibility(), View.VISIBLE);
70+
}
71+
72+
/*
73+
@Test
74+
public void brushEraser() {
75+
}
76+
77+
@Test
78+
public void setOpacity() {
79+
}
80+
81+
@Test
82+
public void getBrushDrawingMode() {
83+
}
84+
85+
@Test
86+
public void setBrushSize() {
87+
}
88+
89+
@Test
90+
public void setBrushColor() {
91+
}
92+
93+
@Test
94+
public void setBrushEraserSize() {
95+
}
96+
97+
@Test
98+
public void setBrushEraserColor() {
99+
}
100+
101+
@Test
102+
public void getEraserSize() {
103+
}
104+
105+
@Test
106+
public void getBrushSize() {
107+
}
108+
109+
@Test
110+
public void getBrushColor() {
111+
}
112+
113+
@Test
114+
public void clearAll() {
115+
}
116+
117+
@Test
118+
public void setBrushViewChangeListener() {
119+
}
120+
121+
@Test
122+
public void onSizeChanged() {
123+
}
124+
125+
@Test
126+
public void onDraw() {
127+
}
128+
129+
@Test
130+
public void onTouchEvent() {
131+
}
132+
133+
@Test
134+
public void undo() {
135+
}
136+
137+
@Test
138+
public void redo() {
139+
}*/
140+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package ja.burhanrashid52.photoeditor;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertEquals;
6+
7+
public class EnumTest {
8+
9+
@Test
10+
public void testNumberOfViewTypes() {
11+
assertEquals(ViewType.values().length, 4);
12+
}
13+
14+
@Test
15+
public void testNumberOfPhotoFilterTypes() {
16+
assertEquals(PhotoFilter.values().length, 24);
17+
}
18+
19+
}

photoeditor/src/test/java/ja/burhanrashid52/photoeditor/SaveSettingsTest.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package ja.burhanrashid52.photoeditor;
22

3-
import org.junit.After;
4-
import org.junit.Before;
53
import org.junit.Test;
64

75
import static junit.framework.Assert.assertFalse;
@@ -10,13 +8,13 @@
108

119
public class SaveSettingsTest {
1210

13-
@Before
11+
/*@Before
1412
public void setUp() throws Exception {
1513
}
1614
1715
@After
1816
public void tearDown() throws Exception {
19-
}
17+
}*/
2018

2119
@Test
2220
public void testByDefaultTransparentAndClearViewFlagSettingIsEnabled() {

0 commit comments

Comments
 (0)