@@ -99,7 +99,7 @@ class CanvasTest {
9999 fun drawString () = runTest {
100100 val surface = Surface .makeRasterN32Premul(100 , 100 )
101101
102- val bytes = Bitmap .makeFromImage(surface.makeImageSnapshot()).readPixels()!!
102+ val bytes = Bitmap .makeFromImage(surface.makeImageSnapshot()).readPixels()!!
103103 assertTrue {
104104 bytes.isNotEmpty() && bytes.all { it == 0 .toByte() }
105105 }
@@ -114,7 +114,7 @@ class CanvasTest {
114114 }
115115 )
116116
117- val bytes2 = Bitmap .makeFromImage(surface.makeImageSnapshot()).readPixels()!!
117+ val bytes2 = Bitmap .makeFromImage(surface.makeImageSnapshot()).readPixels()!!
118118 assertTrue {
119119 bytes2.isNotEmpty() && bytes2.any { it != 0 .toByte() }
120120 }
@@ -210,7 +210,7 @@ class CanvasTest {
210210 surface.canvas.drawBlackPixel(1 , 1 )
211211
212212 surface.assertPixelsMatch(
213- IntArray (16 ){ index ->
213+ IntArray (16 ) { index ->
214214 when (index) {
215215 10 , 11 , 14 , 15 -> 0xff000000 .toInt()
216216 else -> 0xffffffff .toInt()
@@ -233,7 +233,7 @@ class CanvasTest {
233233 @Test
234234 fun testRotateXY () {
235235 val surface = whiteSurface(4 , 4 )
236- surface.canvas.rotate(deg = 90f , x = 2f , y= 2f )
236+ surface.canvas.rotate(deg = 90f , x = 2f , y = 2f )
237237 surface.canvas.drawBlackPixel(0 , 0 )
238238
239239 surface.assertSingleBlackPixelAt(3 , 0 )
@@ -247,7 +247,7 @@ class CanvasTest {
247247 surface.canvas.drawBlackPixel(0 , 2 )
248248
249249 surface.assertPixelsMatch(
250- IntArray (16 ){ index ->
250+ IntArray (16 ) { index ->
251251 when (index) {
252252 // Skewing skews the shape of the pixel itself, so it becomes a parallelogram
253253 9 -> 0xff3f3f3f .toInt()
@@ -258,6 +258,56 @@ class CanvasTest {
258258 )
259259 }
260260
261+ @Test
262+ fun testSaveLayerRecRect () {
263+ val surface = whiteSurface(5 , 5 )
264+
265+ surface.canvas.saveLayer(
266+ Canvas .SaveLayerRec (
267+ bounds = Rect (1f , 1f , 4f , 4f ),
268+ saveLayerFlags = Canvas .SaveLayerFlags (Canvas .SaveLayerFlagsSet .InitWithPrevious )
269+ )
270+ )
271+
272+ val black = Paint ().also { it.setARGB(255 , 0 , 0 , 0 ) }
273+ surface.canvas.drawRect(Rect (1f , 1f , 4f , 4f ), black)
274+
275+ surface.canvas.restore()
276+
277+ surface.assertPixelsMatch(
278+ intArrayOf(
279+ Color .WHITE , Color .WHITE , Color .WHITE , Color .WHITE , Color .WHITE ,
280+ Color .WHITE , Color .BLACK , Color .BLACK , Color .BLACK , Color .WHITE ,
281+ Color .WHITE , Color .BLACK , Color .BLACK , Color .BLACK , Color .WHITE ,
282+ Color .WHITE , Color .BLACK , Color .BLACK , Color .BLACK , Color .WHITE ,
283+ Color .WHITE , Color .WHITE , Color .WHITE , Color .WHITE , Color .WHITE ,
284+ )
285+ )
286+ }
287+
288+
289+ @Test
290+ fun testSaveLayerRec () {
291+ val surface = whiteSurface(5 , 5 )
292+
293+ surface.canvas.saveLayer(Canvas .SaveLayerRec (saveLayerFlags = Canvas .SaveLayerFlags (Canvas .SaveLayerFlagsSet .InitWithPrevious )))
294+
295+ val black = Paint ().also { it.setARGB(255 , 0 , 0 , 0 ) }
296+ surface.canvas.drawRect(Rect (1f , 1f , 4f , 4f ), black)
297+
298+ surface.canvas.restore()
299+
300+ surface.assertPixelsMatch(
301+ intArrayOf(
302+ Color .WHITE , Color .WHITE , Color .WHITE , Color .WHITE , Color .WHITE ,
303+ Color .WHITE , Color .BLACK , Color .BLACK , Color .BLACK , Color .WHITE ,
304+ Color .WHITE , Color .BLACK , Color .BLACK , Color .BLACK , Color .WHITE ,
305+ Color .WHITE , Color .BLACK , Color .BLACK , Color .BLACK , Color .WHITE ,
306+ Color .WHITE , Color .WHITE , Color .WHITE , Color .WHITE , Color .WHITE ,
307+ )
308+ )
309+ }
310+
261311
262312 private fun whiteSurface (width : Int , height : Int ): Surface {
263313 val surface = Surface .makeRasterN32Premul(width, height)
@@ -286,8 +336,8 @@ class CanvasTest {
286336
287337
288338 private fun Surface.assertSingleBlackPixelAt (x : Int , y : Int ) {
289- val pixArray = IntArray (width * height){ 0xffffffff .toInt() }
290- pixArray[y* width + x] = 0xff000000 .toInt()
339+ val pixArray = IntArray (width * height) { 0xffffffff .toInt() }
340+ pixArray[y * width + x] = 0xff000000 .toInt()
291341
292342 assertPixelsMatch(pixArray)
293343 }
0 commit comments