From b3ea7fe7c2cdfc1fec9661d386b3b3de64affb9e Mon Sep 17 00:00:00 2001 From: Michal Wolski Date: Thu, 19 Jan 2023 11:56:55 +0100 Subject: [PATCH 1/4] Move some tests to androidTest --- .../java/ja/burhanrashid52/photoeditor/BaseDrawingViewTest.kt | 0 .../java/ja/burhanrashid52/photoeditor/DrawingViewApiTest.kt | 0 .../ja/burhanrashid52/photoeditor/DrawingViewTouchEventTest.kt | 0 .../java/ja/burhanrashid52/photoeditor/DrawingViewUndoRedoTest.kt | 0 4 files changed, 0 insertions(+), 0 deletions(-) rename photoeditor/src/{test => androidTest}/java/ja/burhanrashid52/photoeditor/BaseDrawingViewTest.kt (100%) rename photoeditor/src/{test => androidTest}/java/ja/burhanrashid52/photoeditor/DrawingViewApiTest.kt (100%) rename photoeditor/src/{test => androidTest}/java/ja/burhanrashid52/photoeditor/DrawingViewTouchEventTest.kt (100%) rename photoeditor/src/{test => androidTest}/java/ja/burhanrashid52/photoeditor/DrawingViewUndoRedoTest.kt (100%) diff --git a/photoeditor/src/test/java/ja/burhanrashid52/photoeditor/BaseDrawingViewTest.kt b/photoeditor/src/androidTest/java/ja/burhanrashid52/photoeditor/BaseDrawingViewTest.kt similarity index 100% rename from photoeditor/src/test/java/ja/burhanrashid52/photoeditor/BaseDrawingViewTest.kt rename to photoeditor/src/androidTest/java/ja/burhanrashid52/photoeditor/BaseDrawingViewTest.kt diff --git a/photoeditor/src/test/java/ja/burhanrashid52/photoeditor/DrawingViewApiTest.kt b/photoeditor/src/androidTest/java/ja/burhanrashid52/photoeditor/DrawingViewApiTest.kt similarity index 100% rename from photoeditor/src/test/java/ja/burhanrashid52/photoeditor/DrawingViewApiTest.kt rename to photoeditor/src/androidTest/java/ja/burhanrashid52/photoeditor/DrawingViewApiTest.kt diff --git a/photoeditor/src/test/java/ja/burhanrashid52/photoeditor/DrawingViewTouchEventTest.kt b/photoeditor/src/androidTest/java/ja/burhanrashid52/photoeditor/DrawingViewTouchEventTest.kt similarity index 100% rename from photoeditor/src/test/java/ja/burhanrashid52/photoeditor/DrawingViewTouchEventTest.kt rename to photoeditor/src/androidTest/java/ja/burhanrashid52/photoeditor/DrawingViewTouchEventTest.kt diff --git a/photoeditor/src/test/java/ja/burhanrashid52/photoeditor/DrawingViewUndoRedoTest.kt b/photoeditor/src/androidTest/java/ja/burhanrashid52/photoeditor/DrawingViewUndoRedoTest.kt similarity index 100% rename from photoeditor/src/test/java/ja/burhanrashid52/photoeditor/DrawingViewUndoRedoTest.kt rename to photoeditor/src/androidTest/java/ja/burhanrashid52/photoeditor/DrawingViewUndoRedoTest.kt From ad9691e3beb3f94458307dc269706b751393174a Mon Sep 17 00:00:00 2001 From: Michal Wolski Date: Thu, 19 Jan 2023 12:21:48 +0100 Subject: [PATCH 2/4] Fix failing tests after migration --- photoeditor/build.gradle | 2 +- .../photoeditor/BaseDrawingViewTest.kt | 8 ++++++-- .../photoeditor/DrawingViewApiTest.kt | 15 ++++++++++----- .../photoeditor/DrawingViewTouchEventTest.kt | 4 ++-- .../photoeditor/DrawingViewUndoRedoTest.kt | 4 ++-- 5 files changed, 21 insertions(+), 12 deletions(-) diff --git a/photoeditor/build.gradle b/photoeditor/build.gradle index 3f268935..a836e802 100644 --- a/photoeditor/build.gradle +++ b/photoeditor/build.gradle @@ -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 { diff --git a/photoeditor/src/androidTest/java/ja/burhanrashid52/photoeditor/BaseDrawingViewTest.kt b/photoeditor/src/androidTest/java/ja/burhanrashid52/photoeditor/BaseDrawingViewTest.kt index a6752481..0bb9eed9 100644 --- a/photoeditor/src/androidTest/java/ja/burhanrashid52/photoeditor/BaseDrawingViewTest.kt +++ b/photoeditor/src/androidTest/java/ja/burhanrashid52/photoeditor/BaseDrawingViewTest.kt @@ -1,11 +1,15 @@ package ja.burhanrashid52.photoeditor +import android.content.Context import android.view.MotionEvent +import androidx.test.core.app.ApplicationProvider +import androidx.test.platform.app.InstrumentationRegistry import ja.burhanrashid52.photoeditor.shape.ShapeBuilder -import org.robolectric.RuntimeEnvironment internal open class BaseDrawingViewTest { - protected var mContext = RuntimeEnvironment.systemContext + + protected var mContext: Context = ApplicationProvider.getApplicationContext() + protected fun setupDrawingView(): DrawingView { // create view and ShapeBuilder val drawingView = DrawingView(mContext) diff --git a/photoeditor/src/androidTest/java/ja/burhanrashid52/photoeditor/DrawingViewApiTest.kt b/photoeditor/src/androidTest/java/ja/burhanrashid52/photoeditor/DrawingViewApiTest.kt index 943a2579..c0bf7234 100644 --- a/photoeditor/src/androidTest/java/ja/burhanrashid52/photoeditor/DrawingViewApiTest.kt +++ b/photoeditor/src/androidTest/java/ja/burhanrashid52/photoeditor/DrawingViewApiTest.kt @@ -2,10 +2,10 @@ package ja.burhanrashid52.photoeditor import android.graphics.* import org.junit.runner.RunWith -import org.robolectric.RobolectricTestRunner import junit.framework.TestCase import android.view.MotionEvent import android.view.View +import androidx.test.ext.junit.runners.AndroidJUnit4 import org.mockito.Mockito import ja.burhanrashid52.photoeditor.shape.ShapeBuilder import junit.framework.TestCase.assertEquals @@ -14,7 +14,7 @@ import org.junit.Test import org.mockito.ArgumentMatchers import java.lang.AssertionError -@RunWith(RobolectricTestRunner::class) +@RunWith(AndroidJUnit4::class) internal class DrawingViewApiTest : BaseDrawingViewTest() { @Test fun testDefaultPaintAttributes() { @@ -26,7 +26,7 @@ internal class DrawingViewApiTest : BaseDrawingViewTest() { assertEquals(strokeJoin, Paint.Join.ROUND) assertEquals(strokeCap, Paint.Cap.ROUND) assertEquals(strokeWidth, ShapeBuilder.DEFAULT_SHAPE_SIZE) - assertEquals(alpha, ShapeBuilder.DEFAULT_SHAPE_OPACITY ?:0) + assertEquals(alpha, ShapeBuilder.DEFAULT_SHAPE_OPACITY ?: DEFAULT_COLOR_ALPHA) TestCase.assertTrue(xfermode is PorterDuffXfermode) } ?: AssertionError("The paint is null") @@ -45,7 +45,7 @@ internal class DrawingViewApiTest : BaseDrawingViewTest() { assertEquals(strokeJoin, Paint.Join.ROUND) assertEquals(strokeCap, Paint.Cap.ROUND) assertEquals(strokeWidth, ShapeBuilder.DEFAULT_SHAPE_SIZE) - assertEquals(alpha, ShapeBuilder.DEFAULT_SHAPE_OPACITY ?:0) + assertEquals(alpha, ShapeBuilder.DEFAULT_SHAPE_OPACITY ?: DEFAULT_COLOR_ALPHA) TestCase.assertTrue(xfermode is PorterDuffXfermode) val spyPaint = Mockito.spy(this) Mockito.verify(spyPaint, Mockito.times(0)).color = @@ -111,7 +111,7 @@ internal class DrawingViewApiTest : BaseDrawingViewTest() { drawingView.currentShapeBuilder?.shapeOpacity, ShapeBuilder.DEFAULT_SHAPE_OPACITY ) - assertEquals(alpha, ShapeBuilder.DEFAULT_SHAPE_OPACITY ?:0) + assertEquals(alpha, ShapeBuilder.DEFAULT_SHAPE_OPACITY ?: DEFAULT_COLOR_ALPHA) } ?: AssertionError("The paint is null") } @@ -171,4 +171,9 @@ internal class DrawingViewApiTest : BaseDrawingViewTest() { ) ) } + + private companion object { + val DEFAULT_COLOR_ALPHA = Color.alpha(ShapeBuilder.DEFAULT_SHAPE_COLOR) + } + } \ No newline at end of file diff --git a/photoeditor/src/androidTest/java/ja/burhanrashid52/photoeditor/DrawingViewTouchEventTest.kt b/photoeditor/src/androidTest/java/ja/burhanrashid52/photoeditor/DrawingViewTouchEventTest.kt index 8bea6ffc..e181809a 100644 --- a/photoeditor/src/androidTest/java/ja/burhanrashid52/photoeditor/DrawingViewTouchEventTest.kt +++ b/photoeditor/src/androidTest/java/ja/burhanrashid52/photoeditor/DrawingViewTouchEventTest.kt @@ -1,14 +1,14 @@ package ja.burhanrashid52.photoeditor import android.view.MotionEvent +import androidx.test.ext.junit.runners.AndroidJUnit4 import org.junit.runner.RunWith -import org.robolectric.RobolectricTestRunner import org.mockito.Mockito import junit.framework.TestCase import junit.framework.TestCase.assertFalse import org.junit.Test -@RunWith(RobolectricTestRunner::class) +@RunWith(AndroidJUnit4::class) internal class DrawingViewTouchEventTest : BaseDrawingViewTest() { @Test fun testDrawingShouldNotWorkWhenDisabled() { diff --git a/photoeditor/src/androidTest/java/ja/burhanrashid52/photoeditor/DrawingViewUndoRedoTest.kt b/photoeditor/src/androidTest/java/ja/burhanrashid52/photoeditor/DrawingViewUndoRedoTest.kt index c290e83f..3ece44ff 100644 --- a/photoeditor/src/androidTest/java/ja/burhanrashid52/photoeditor/DrawingViewUndoRedoTest.kt +++ b/photoeditor/src/androidTest/java/ja/burhanrashid52/photoeditor/DrawingViewUndoRedoTest.kt @@ -1,14 +1,14 @@ package ja.burhanrashid52.photoeditor +import androidx.test.ext.junit.runners.AndroidJUnit4 import org.junit.runner.RunWith -import org.robolectric.RobolectricTestRunner import org.mockito.Mockito import ja.burhanrashid52.photoeditor.shape.ShapeAndPaint import junit.framework.TestCase.assertEquals import junit.framework.TestCase.assertFalse import org.junit.Test -@RunWith(RobolectricTestRunner::class) +@RunWith(AndroidJUnit4::class) internal class DrawingViewUndoRedoTest : BaseDrawingViewTest() { @Test fun testUndoReturnFalseWhenThereNothingToUndo() { From 78d1a7b52ec94f33a56362c958c1d8a8c3c1dbf8 Mon Sep 17 00:00:00 2001 From: Michal Wolski Date: Thu, 19 Jan 2023 12:58:26 +0100 Subject: [PATCH 3/4] Refactor tests --- .../photoeditor/BaseDrawingViewTest.kt | 11 +- .../photoeditor/DrawingViewApiTest.kt | 131 ++++++++---------- .../photoeditor/DrawingViewTouchEventTest.kt | 50 +++---- .../photoeditor/DrawingViewUndoRedoTest.kt | 29 ++-- 4 files changed, 112 insertions(+), 109 deletions(-) diff --git a/photoeditor/src/androidTest/java/ja/burhanrashid52/photoeditor/BaseDrawingViewTest.kt b/photoeditor/src/androidTest/java/ja/burhanrashid52/photoeditor/BaseDrawingViewTest.kt index 0bb9eed9..e6b6c6f9 100644 --- a/photoeditor/src/androidTest/java/ja/burhanrashid52/photoeditor/BaseDrawingViewTest.kt +++ b/photoeditor/src/androidTest/java/ja/burhanrashid52/photoeditor/BaseDrawingViewTest.kt @@ -3,16 +3,15 @@ package ja.burhanrashid52.photoeditor import android.content.Context import android.view.MotionEvent import androidx.test.core.app.ApplicationProvider -import androidx.test.platform.app.InstrumentationRegistry import ja.burhanrashid52.photoeditor.shape.ShapeBuilder internal open class BaseDrawingViewTest { - protected var mContext: Context = ApplicationProvider.getApplicationContext() + 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 @@ -49,7 +48,9 @@ internal open class BaseDrawingViewTest { 100.0f, 0 ) + drawingView.dispatchTouchEvent(actionDown) + for (i in 0..99) { val actionMove = MotionEvent.obtain( 200, @@ -61,6 +62,7 @@ internal open class BaseDrawingViewTest { ) drawingView.dispatchTouchEvent(actionMove) } + val actionUp = MotionEvent.obtain( 200, 300, @@ -69,6 +71,7 @@ internal open class BaseDrawingViewTest { 100.0f, 0 ) + drawingView.dispatchTouchEvent(actionUp) } } \ No newline at end of file diff --git a/photoeditor/src/androidTest/java/ja/burhanrashid52/photoeditor/DrawingViewApiTest.kt b/photoeditor/src/androidTest/java/ja/burhanrashid52/photoeditor/DrawingViewApiTest.kt index c0bf7234..ff1f5bfa 100644 --- a/photoeditor/src/androidTest/java/ja/burhanrashid52/photoeditor/DrawingViewApiTest.kt +++ b/photoeditor/src/androidTest/java/ja/burhanrashid52/photoeditor/DrawingViewApiTest.kt @@ -1,18 +1,21 @@ package ja.burhanrashid52.photoeditor -import android.graphics.* -import org.junit.runner.RunWith -import junit.framework.TestCase +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 org.mockito.Mockito 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.mockito.ArgumentMatchers -import java.lang.AssertionError +import org.junit.runner.RunWith +import org.mockito.ArgumentMatchers.any +import org.mockito.Mockito @RunWith(AndroidJUnit4::class) internal class DrawingViewApiTest : BaseDrawingViewTest() { @@ -20,16 +23,15 @@ internal class DrawingViewApiTest : BaseDrawingViewTest() { fun testDefaultPaintAttributes() { val drawingView = setupDrawingView() touchView(drawingView, MotionEvent.ACTION_DOWN) - drawingView.currentShape?.paint?.apply { - assertEquals(color, ShapeBuilder.DEFAULT_SHAPE_COLOR) - assertEquals(style, Paint.Style.STROKE) - assertEquals(strokeJoin, Paint.Join.ROUND) - assertEquals(strokeCap, Paint.Cap.ROUND) - assertEquals(strokeWidth, ShapeBuilder.DEFAULT_SHAPE_SIZE) - assertEquals(alpha, ShapeBuilder.DEFAULT_SHAPE_OPACITY ?: DEFAULT_COLOR_ALPHA) - TestCase.assertTrue(xfermode is PorterDuffXfermode) - } ?: AssertionError("The paint is null") + 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); @@ -40,22 +42,22 @@ internal class DrawingViewApiTest : BaseDrawingViewTest() { fun testPaintAttributesAfterDrawingModeIsEnabled() { val drawingView = setupDrawingView() touchView(drawingView, MotionEvent.ACTION_DOWN) - drawingView.currentShape?.paint?.apply { - assertEquals(style, Paint.Style.STROKE) - assertEquals(strokeJoin, Paint.Join.ROUND) - assertEquals(strokeCap, Paint.Cap.ROUND) - assertEquals(strokeWidth, ShapeBuilder.DEFAULT_SHAPE_SIZE) - assertEquals(alpha, ShapeBuilder.DEFAULT_SHAPE_OPACITY ?: DEFAULT_COLOR_ALPHA) - TestCase.assertTrue(xfermode is PorterDuffXfermode) - val spyPaint = Mockito.spy(this) - Mockito.verify(spyPaint, Mockito.times(0)).color = - ShapeBuilder.DEFAULT_SHAPE_COLOR - } ?: AssertionError("The paint is null") + 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(mContext) + val drawingView = DrawingView(context) assertFalse(drawingView.isDrawingEnabled) assertEquals(drawingView.visibility, View.GONE) } @@ -64,7 +66,7 @@ internal class DrawingViewApiTest : BaseDrawingViewTest() { fun testWhenDrawingModeIsSetEnabled() { val drawingView = setupDrawingView() drawingView.enableDrawing(true) - TestCase.assertTrue(drawingView.isDrawingEnabled) + assertTrue(drawingView.isDrawingEnabled) assertEquals(drawingView.visibility, View.VISIBLE) } @@ -79,65 +81,54 @@ internal class DrawingViewApiTest : BaseDrawingViewTest() { fun testDefaultShapeSize() { val drawingView = setupDrawingView() touchView(drawingView, MotionEvent.ACTION_DOWN) - - - drawingView.currentShape?.paint?.apply { - assertEquals( - drawingView.currentShapeBuilder?.shapeSize, - ShapeBuilder.DEFAULT_SHAPE_SIZE - ) - assertEquals(strokeWidth, ShapeBuilder.DEFAULT_SHAPE_SIZE) - } ?: AssertionError("The paint is null") + 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) + drawingView.currentShapeBuilder.withShapeSize(shapeSize) touchView(drawingView, MotionEvent.ACTION_DOWN) - assertEquals(drawingView.currentShapeBuilder?.shapeSize, shapeSize) - assertEquals(drawingView.currentShape?.paint?.strokeWidth, shapeSize) - TestCase.assertTrue(drawingView.isDrawingEnabled) + + 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 - drawingView.currentShape?.paint?.apply { - assertEquals( - drawingView.currentShapeBuilder?.shapeOpacity, - ShapeBuilder.DEFAULT_SHAPE_OPACITY - ) - assertEquals(alpha, ShapeBuilder.DEFAULT_SHAPE_OPACITY ?: DEFAULT_COLOR_ALPHA) - } ?: AssertionError("The paint is null") + 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) + drawingView.currentShapeBuilder.withShapeOpacity(shapeOpacity) touchView(drawingView, MotionEvent.ACTION_DOWN) - assertEquals(drawingView.currentShapeBuilder?.shapeOpacity, shapeOpacity) - assertEquals(drawingView.currentShape?.paint?.alpha, shapeOpacity) - TestCase.assertTrue(drawingView.isDrawingEnabled) + + assertEquals(drawingView.currentShapeBuilder.shapeOpacity, shapeOpacity) + assertEquals(drawingView.currentShape!!.paint.alpha, shapeOpacity) + assertTrue(drawingView.isDrawingEnabled) } @Test fun testDefaultBrushColorValue() { val drawingView = setupDrawingView() touchView(drawingView, MotionEvent.ACTION_DOWN) - drawingView.currentShape?.paint?.apply { - assertEquals( - drawingView.currentShapeBuilder?.shapeColor, - ShapeBuilder.DEFAULT_SHAPE_COLOR - ) - assertEquals(color, ShapeBuilder.DEFAULT_SHAPE_COLOR) - } ?: AssertionError("The paint is null") + val paint = drawingView.currentShape!!.paint + assertEquals(drawingView.currentShapeBuilder.shapeColor, ShapeBuilder.DEFAULT_SHAPE_COLOR) + assertEquals(paint.color, ShapeBuilder.DEFAULT_SHAPE_COLOR) } @Test @@ -145,31 +136,29 @@ internal class DrawingViewApiTest : BaseDrawingViewTest() { val drawingView = setupDrawingView() touchView(drawingView, MotionEvent.ACTION_DOWN) val shapeColor = Color.RED - drawingView.currentShapeBuilder?.withShapeColor(shapeColor) + drawingView.currentShapeBuilder.withShapeColor(shapeColor) touchView(drawingView, MotionEvent.ACTION_DOWN) - assertEquals(drawingView.currentShapeBuilder?.shapeColor, shapeColor) - assertEquals(drawingView.currentShape?.paint?.color, shapeColor) - TestCase.assertTrue(drawingView.isDrawingEnabled) + + 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 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( - ArgumentMatchers.any(Path::class.java), ArgumentMatchers.any( - Paint::class.java - ) - ) + .drawPath(any(Path::class.java), any(Paint::class.java)) } private companion object { diff --git a/photoeditor/src/androidTest/java/ja/burhanrashid52/photoeditor/DrawingViewTouchEventTest.kt b/photoeditor/src/androidTest/java/ja/burhanrashid52/photoeditor/DrawingViewTouchEventTest.kt index e181809a..3724f8ab 100644 --- a/photoeditor/src/androidTest/java/ja/burhanrashid52/photoeditor/DrawingViewTouchEventTest.kt +++ b/photoeditor/src/androidTest/java/ja/burhanrashid52/photoeditor/DrawingViewTouchEventTest.kt @@ -2,19 +2,18 @@ package ja.burhanrashid52.photoeditor import android.view.MotionEvent import androidx.test.ext.junit.runners.AndroidJUnit4 -import org.junit.runner.RunWith -import org.mockito.Mockito -import junit.framework.TestCase import junit.framework.TestCase.assertFalse +import org.junit.Assert.assertTrue import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.Mockito @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) @@ -22,73 +21,76 @@ internal class DrawingViewTouchEventTest : BaseDrawingViewTest() { } @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) diff --git a/photoeditor/src/androidTest/java/ja/burhanrashid52/photoeditor/DrawingViewUndoRedoTest.kt b/photoeditor/src/androidTest/java/ja/burhanrashid52/photoeditor/DrawingViewUndoRedoTest.kt index 3ece44ff..aa32b98a 100644 --- a/photoeditor/src/androidTest/java/ja/burhanrashid52/photoeditor/DrawingViewUndoRedoTest.kt +++ b/photoeditor/src/androidTest/java/ja/burhanrashid52/photoeditor/DrawingViewUndoRedoTest.kt @@ -1,38 +1,38 @@ package ja.burhanrashid52.photoeditor import androidx.test.ext.junit.runners.AndroidJUnit4 -import org.junit.runner.RunWith -import org.mockito.Mockito import ja.burhanrashid52.photoeditor.shape.ShapeAndPaint import junit.framework.TestCase.assertEquals import junit.framework.TestCase.assertFalse import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.Mockito @RunWith(AndroidJUnit4::class) internal class DrawingViewUndoRedoTest : BaseDrawingViewTest() { + @Test fun testUndoReturnFalseWhenThereNothingToUndo() { - val drawingView = DrawingView(mContext) + val drawingView = DrawingView(context) assertFalse(drawingView.undo()) } @Test fun testRedoReturnFalseWhenThereNothingToRedo() { - val drawingView = DrawingView(mContext) + val drawingView = DrawingView(context) assertFalse(drawingView.redo()) } @Test fun testUndoAndRedoWithListenerWhenAnythingIsDrawnOnCanvas() { - val brushViewChangeListener = Mockito.mock( - BrushViewChangeListener::class.java - ) + val brushViewChangeListener = Mockito.mock(BrushViewChangeListener::class.java) val drawingView = setupDrawingViewWithChangeListener(brushViewChangeListener) // Add 3 Shapes swipeView(drawingView) swipeView(drawingView) swipeView(drawingView) + Mockito.verify(brushViewChangeListener, Mockito.times(3)).onViewAdd(drawingView) verifyUndo(drawingView, brushViewChangeListener) verifyRedo(drawingView, brushViewChangeListener) @@ -45,16 +45,20 @@ internal class DrawingViewUndoRedoTest : BaseDrawingViewTest() { val drawingPaths = drawingView.drawingPath val undoPaths = drawingPaths.first val redoPaths = drawingPaths.second + assertEquals(3, undoPaths.size) assertEquals(0, redoPaths.size) + drawingView.undo() assertEquals(2, undoPaths.size) assertEquals(1, redoPaths.size) Mockito.verify(brushViewChangeListener, Mockito.times(1)).onViewRemoved(drawingView) + drawingView.undo() assertEquals(1, undoPaths.size) assertEquals(2, redoPaths.size) Mockito.verify(brushViewChangeListener, Mockito.times(2)).onViewRemoved(drawingView) + drawingView.undo() assertEquals(0, undoPaths.size) assertEquals(3, redoPaths.size) @@ -68,16 +72,20 @@ internal class DrawingViewUndoRedoTest : BaseDrawingViewTest() { val drawingPaths = drawingView.drawingPath val undoPaths = drawingPaths.first val redoPaths = drawingPaths.second + assertEquals(undoPaths.size, 0) assertEquals(redoPaths.size, 3) + drawingView.redo() assertEquals(undoPaths.size, 1) assertEquals(redoPaths.size, 2) Mockito.verify(brushViewChangeListener, Mockito.times(4)).onViewAdd(drawingView) + drawingView.redo() assertEquals(undoPaths.size, 2) assertEquals(redoPaths.size, 1) Mockito.verify(brushViewChangeListener, Mockito.times(5)).onViewAdd(drawingView) + drawingView.redo() assertEquals(undoPaths.size, 3) assertEquals(redoPaths.size, 0) @@ -86,17 +94,18 @@ internal class DrawingViewUndoRedoTest : BaseDrawingViewTest() { @Test fun testUndoRedoAndPaintColorWhenEverythingIsCleared() { - val brushViewChangeListener = Mockito.mock( - BrushViewChangeListener::class.java - ) + val brushViewChangeListener = Mockito.mock(BrushViewChangeListener::class.java) val drawingView = setupDrawingViewWithChangeListener(brushViewChangeListener) val paths = drawingView.drawingPath val undoPaths = paths.first val redoPaths = paths.second + val linePath = Mockito.mock(ShapeAndPaint::class.java) + undoPaths.add(linePath) undoPaths.add(linePath) redoPaths.add(linePath) + drawingView.clearAll() assertEquals(undoPaths.size, 0) assertEquals(redoPaths.size, 0) From e177684c46bffa501c457e8836a51b18b3ef0948 Mon Sep 17 00:00:00 2001 From: Michal Wolski Date: Thu, 19 Jan 2023 13:40:46 +0100 Subject: [PATCH 4/4] Add test for correct alpha and color values --- .../photoeditor/DrawingViewApiTest.kt | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/photoeditor/src/androidTest/java/ja/burhanrashid52/photoeditor/DrawingViewApiTest.kt b/photoeditor/src/androidTest/java/ja/burhanrashid52/photoeditor/DrawingViewApiTest.kt index ff1f5bfa..bef43283 100644 --- a/photoeditor/src/androidTest/java/ja/burhanrashid52/photoeditor/DrawingViewApiTest.kt +++ b/photoeditor/src/androidTest/java/ja/burhanrashid52/photoeditor/DrawingViewApiTest.kt @@ -121,6 +121,48 @@ internal class DrawingViewApiTest : BaseDrawingViewTest() { 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()