Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for correct alpha and color values #507

Merged
merged 4 commits into from
Jan 29, 2023
Merged
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
2 changes: 1 addition & 1 deletion photoeditor/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ dependencies {

// Mockito
testImplementation 'org.mockito:mockito-core:5.0.0'
// androidTestImplementation 'org.mockito:mockito-android:5.0.0'
androidTestImplementation 'org.mockito:mockito-android:5.0.0'
}

ext {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package ja.burhanrashid52.photoeditor

import android.content.Context
import android.view.MotionEvent
import androidx.test.core.app.ApplicationProvider
import ja.burhanrashid52.photoeditor.shape.ShapeBuilder
import org.robolectric.RuntimeEnvironment

internal open class BaseDrawingViewTest {
protected var mContext = RuntimeEnvironment.systemContext

protected var context: Context = ApplicationProvider.getApplicationContext()

protected fun setupDrawingView(): DrawingView {
// create view and ShapeBuilder
val drawingView = DrawingView(mContext)
// Create view and ShapeBuilder
val drawingView = DrawingView(context)
drawingView.enableDrawing(true)
val shapeBuilder = ShapeBuilder()
drawingView.currentShapeBuilder = shapeBuilder
Expand Down Expand Up @@ -45,7 +48,9 @@ internal open class BaseDrawingViewTest {
100.0f,
0
)

drawingView.dispatchTouchEvent(actionDown)

for (i in 0..99) {
val actionMove = MotionEvent.obtain(
200,
Expand All @@ -57,6 +62,7 @@ internal open class BaseDrawingViewTest {
)
drawingView.dispatchTouchEvent(actionMove)
}

val actionUp = MotionEvent.obtain(
200,
300,
Expand All @@ -65,6 +71,7 @@ internal open class BaseDrawingViewTest {
100.0f,
0
)

drawingView.dispatchTouchEvent(actionUp)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
package ja.burhanrashid52.photoeditor

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import android.graphics.PorterDuffXfermode
import android.view.MotionEvent
import android.view.View
import androidx.test.ext.junit.runners.AndroidJUnit4
import ja.burhanrashid52.photoeditor.shape.ShapeBuilder
import junit.framework.TestCase.assertEquals
import junit.framework.TestCase.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers.any
import org.mockito.Mockito

@RunWith(AndroidJUnit4::class)
internal class DrawingViewApiTest : BaseDrawingViewTest() {
@Test
fun testDefaultPaintAttributes() {
val drawingView = setupDrawingView()
touchView(drawingView, MotionEvent.ACTION_DOWN)
val paint = drawingView.currentShape!!.paint

assertEquals(paint.color, ShapeBuilder.DEFAULT_SHAPE_COLOR)
assertEquals(paint.style, Paint.Style.STROKE)
assertEquals(paint.strokeJoin, Paint.Join.ROUND)
assertEquals(paint.strokeCap, Paint.Cap.ROUND)
assertEquals(paint.strokeWidth, ShapeBuilder.DEFAULT_SHAPE_SIZE)
assertEquals(paint.alpha, ShapeBuilder.DEFAULT_SHAPE_OPACITY ?: DEFAULT_COLOR_ALPHA)
assertTrue(paint.xfermode is PorterDuffXfermode)

// Spy is not working properly
/*Paint spyPaint = Mockito.spy(drawingPaint);
verify(spyPaint, times(1)).setColor(Color.BLACK);*/
}

@Test
fun testPaintAttributesAfterDrawingModeIsEnabled() {
val drawingView = setupDrawingView()
touchView(drawingView, MotionEvent.ACTION_DOWN)
val paint = drawingView.currentShape!!.paint

assertEquals(paint.style, Paint.Style.STROKE)
assertEquals(paint.strokeJoin, Paint.Join.ROUND)
assertEquals(paint.strokeCap, Paint.Cap.ROUND)
assertEquals(paint.strokeWidth, ShapeBuilder.DEFAULT_SHAPE_SIZE)
assertEquals(paint.alpha, ShapeBuilder.DEFAULT_SHAPE_OPACITY ?: DEFAULT_COLOR_ALPHA)
assertTrue(paint.xfermode is PorterDuffXfermode)

val spyPaint = Mockito.spy(paint)
Mockito.verify(spyPaint, Mockito.times(0)).color = ShapeBuilder.DEFAULT_SHAPE_COLOR
}

@Test
fun testByDefaultDrawingModeIsDisabled() {
val drawingView = DrawingView(context)
assertFalse(drawingView.isDrawingEnabled)
assertEquals(drawingView.visibility, View.GONE)
}

@Test
fun testWhenDrawingModeIsSetEnabled() {
val drawingView = setupDrawingView()
drawingView.enableDrawing(true)
assertTrue(drawingView.isDrawingEnabled)
assertEquals(drawingView.visibility, View.VISIBLE)
}

@Test
fun testWhenDrawingModeIsSetDisabled() {
val drawingView = setupDrawingView()
drawingView.enableDrawing(false)
assertFalse(drawingView.isDrawingEnabled)
}

@Test
fun testDefaultShapeSize() {
val drawingView = setupDrawingView()
touchView(drawingView, MotionEvent.ACTION_DOWN)
val paint = drawingView.currentShape!!.paint
assertEquals(drawingView.currentShapeBuilder.shapeSize, ShapeBuilder.DEFAULT_SHAPE_SIZE)
assertEquals(paint.strokeWidth, ShapeBuilder.DEFAULT_SHAPE_SIZE)
}

@Test
fun testCorrectShapeSizeIsSet() {
val drawingView = setupDrawingView()
val shapeSize = 75f
drawingView.currentShapeBuilder.withShapeSize(shapeSize)
touchView(drawingView, MotionEvent.ACTION_DOWN)

assertEquals(drawingView.currentShapeBuilder.shapeSize, shapeSize)
assertEquals(drawingView.currentShape!!.paint.strokeWidth, shapeSize)
assertTrue(drawingView.isDrawingEnabled)
}

@Test
fun testDefaultOpacityValue() {
val drawingView = setupDrawingView()
touchView(drawingView, MotionEvent.ACTION_DOWN)
val paint = drawingView.currentShape!!.paint
val currentShapeBuilder = drawingView.currentShapeBuilder

assertEquals(currentShapeBuilder.shapeOpacity, ShapeBuilder.DEFAULT_SHAPE_OPACITY)
assertEquals(paint.alpha, ShapeBuilder.DEFAULT_SHAPE_OPACITY ?: DEFAULT_COLOR_ALPHA)
}

@Test
fun testCorrectOpacityValueIsSet() {
val drawingView = setupDrawingView()
val shapeOpacity = 240
drawingView.currentShapeBuilder.withShapeOpacity(shapeOpacity)
touchView(drawingView, MotionEvent.ACTION_DOWN)

assertEquals(drawingView.currentShapeBuilder.shapeOpacity, shapeOpacity)
assertEquals(drawingView.currentShape!!.paint.alpha, shapeOpacity)
assertTrue(drawingView.isDrawingEnabled)
}

@Test
fun testCorrectAlphaAndColorValues() {
val drawingView = setupDrawingView()
val currentShapeBuilder = drawingView.currentShapeBuilder

val shapeOpacity = 120
val colorAlpha1 = 11
val colorAlpha2 = 22

currentShapeBuilder.withShapeColor(Color.argb(colorAlpha1, 33, 44, 55))
touchView(drawingView, MotionEvent.ACTION_DOWN)

drawingView.currentShape!!.paint.also { paint ->
assertEquals(colorAlpha1, paint.alpha)
assertEquals(Color.argb(colorAlpha1, 33, 44, 55), paint.color)
}

currentShapeBuilder.withShapeOpacity(shapeOpacity)
touchView(drawingView, MotionEvent.ACTION_DOWN)

drawingView.currentShape!!.paint.also { paint ->
assertEquals(shapeOpacity, paint.alpha)
assertEquals(Color.argb(shapeOpacity, 33, 44, 55), paint.color)
}

currentShapeBuilder.withShapeColor(Color.argb(colorAlpha2, 44, 55, 66))
touchView(drawingView, MotionEvent.ACTION_DOWN)

drawingView.currentShape!!.paint.also { paint ->
assertEquals(shapeOpacity, paint.alpha)
assertEquals(Color.argb(shapeOpacity, 44, 55, 66), paint.color)
}

currentShapeBuilder.withShapeOpacity(null)
touchView(drawingView, MotionEvent.ACTION_DOWN)

drawingView.currentShape!!.paint.also { paint ->
assertEquals(colorAlpha2, paint.alpha)
assertEquals(Color.argb(colorAlpha2, 44, 55, 66), paint.color)
}
}

@Test
fun testDefaultBrushColorValue() {
val drawingView = setupDrawingView()
touchView(drawingView, MotionEvent.ACTION_DOWN)
val paint = drawingView.currentShape!!.paint

assertEquals(drawingView.currentShapeBuilder.shapeColor, ShapeBuilder.DEFAULT_SHAPE_COLOR)
assertEquals(paint.color, ShapeBuilder.DEFAULT_SHAPE_COLOR)
}

@Test
fun testCorrectBrushColorIsSet() {
val drawingView = setupDrawingView()
touchView(drawingView, MotionEvent.ACTION_DOWN)
val shapeColor = Color.RED
drawingView.currentShapeBuilder.withShapeColor(shapeColor)
touchView(drawingView, MotionEvent.ACTION_DOWN)

assertEquals(drawingView.currentShapeBuilder.shapeColor, shapeColor)
assertEquals(drawingView.currentShape!!.paint.color, shapeColor)
assertTrue(drawingView.isDrawingEnabled)
}

@Test
fun testCanvasIsDrawingCorrectlyOnDraw() {
val brushViewChangeListener = Mockito.mock(BrushViewChangeListener::class.java)
val drawingView = setupDrawingViewWithChangeListener(brushViewChangeListener)

val numberOfTouch = 4
for (i in 0 until numberOfTouch) {
touchView(drawingView, MotionEvent.ACTION_DOWN)
}

val canvas = Mockito.mock(Canvas::class.java)
drawingView.onDraw(canvas)

Mockito.verify(canvas, Mockito.times(numberOfTouch))
.drawPath(any(Path::class.java), any(Paint::class.java))
}

private companion object {
val DEFAULT_COLOR_ALPHA = Color.alpha(ShapeBuilder.DEFAULT_SHAPE_COLOR)
}

}
Original file line number Diff line number Diff line change
@@ -1,94 +1,96 @@
package ja.burhanrashid52.photoeditor

import android.view.MotionEvent
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.mockito.Mockito
import junit.framework.TestCase
import androidx.test.ext.junit.runners.AndroidJUnit4
import junit.framework.TestCase.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito

@RunWith(RobolectricTestRunner::class)
@RunWith(AndroidJUnit4::class)
internal class DrawingViewTouchEventTest : BaseDrawingViewTest() {

@Test
fun testDrawingShouldNotWorkWhenDisabled() {
val brushViewChangeListener = Mockito.mock(
BrushViewChangeListener::class.java
)
val brushViewChangeListener = Mockito.mock(BrushViewChangeListener::class.java)
val drawingView = setupDrawingViewWithChangeListener(brushViewChangeListener)
drawingView.enableDrawing(false)
touchView(drawingView, MotionEvent.ACTION_DOWN)
Mockito.verify(brushViewChangeListener, Mockito.times(0)).onStartDrawing()
}

@Test
fun testDrawingChangeListenerAndPathWhenShapeisCreated() {
val brushViewChangeListener = Mockito.mock(
BrushViewChangeListener::class.java
)
fun testDrawingChangeListenerAndPathWhenShapeIsCreated() {
val brushViewChangeListener = Mockito.mock(BrushViewChangeListener::class.java)
val drawingView = setupDrawingViewWithChangeListener(brushViewChangeListener)

swipeView(drawingView)
Mockito.verify(brushViewChangeListener, Mockito.times(1)).onStartDrawing()

val drawingPath = drawingView.drawingPath
val drawnPath = drawingPath.first
val redoPaths = drawingPath.second
assertFalse(drawnPath.empty())
TestCase.assertTrue(redoPaths.empty())
assertTrue(redoPaths.empty())
}

@Test
fun testDrawingChangeListenerAndPathWhenTouchIsMove() {
val brushViewChangeListener = Mockito.mock(
BrushViewChangeListener::class.java
)
val brushViewChangeListener = Mockito.mock(BrushViewChangeListener::class.java)
val drawingView = setupDrawingViewWithChangeListener(brushViewChangeListener)
touchView(drawingView, MotionEvent.ACTION_MOVE)

Mockito.verify(brushViewChangeListener, Mockito.times(0)).onStartDrawing()
Mockito.verify(brushViewChangeListener, Mockito.times(0)).onStopDrawing()
Mockito.verify(brushViewChangeListener, Mockito.times(0)).onViewAdd(drawingView)
Mockito.verify(brushViewChangeListener, Mockito.times(0)).onViewRemoved(drawingView)

val drawingPath = drawingView.drawingPath
val drawnPath = drawingPath.first
val redoPaths = drawingPath.second
TestCase.assertTrue(drawnPath.empty())
TestCase.assertTrue(redoPaths.empty())
assertTrue(drawnPath.empty())
assertTrue(redoPaths.empty())
}

@Test
fun testBrushDrawingChangeListenerAndPathWhenTouchIsUp() {
val brushViewChangeListener = Mockito.mock(
BrushViewChangeListener::class.java
)
val brushViewChangeListener = Mockito.mock(BrushViewChangeListener::class.java)
val drawingView = setupDrawingViewWithChangeListener(brushViewChangeListener)

val touchEventUp = MotionEvent.obtain(200, 300, MotionEvent.ACTION_UP, 150.0f, 100.0f, 0)
drawingView.dispatchTouchEvent(touchEventUp)

Mockito.verify(brushViewChangeListener, Mockito.times(0)).onStartDrawing()
Mockito.verify(brushViewChangeListener, Mockito.times(0)).onStopDrawing()
Mockito.verify(brushViewChangeListener, Mockito.times(0)).onViewAdd(drawingView)
Mockito.verify(brushViewChangeListener, Mockito.times(0)).onViewRemoved(drawingView)

val drawingPath = drawingView.drawingPath
val drawnPath = drawingPath.first
val redoPaths = drawingPath.second
TestCase.assertTrue(drawnPath.empty())
TestCase.assertTrue(redoPaths.empty())
assertTrue(drawnPath.empty())
assertTrue(redoPaths.empty())
}

@Test
fun testPathDrawnOnTouchEvents() {
val brushViewChangeListener = Mockito.mock(
BrushViewChangeListener::class.java
)
val brushViewChangeListener = Mockito.mock(BrushViewChangeListener::class.java)
val drawingView = setupDrawingViewWithChangeListener(brushViewChangeListener)

val touchDownX = 150.0f
val touchDownY = 100.0f
val touchEventDown =
MotionEvent.obtain(200, 300, MotionEvent.ACTION_DOWN, touchDownX, touchDownY, 0)
drawingView.dispatchTouchEvent(touchEventDown)

val touchMoveX = 160.0f
val touchMoveY = 110.0f

val touchEventMove =
MotionEvent.obtain(200, 300, MotionEvent.ACTION_MOVE, touchMoveX, touchMoveY, 0)
drawingView.dispatchTouchEvent(touchEventMove)

val touchEventUp = MotionEvent.obtain(200, 300, MotionEvent.ACTION_UP, 150.0f, 100.0f, 0)
drawingView.dispatchTouchEvent(touchEventUp)

Expand Down
Loading