1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *       http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package androidx.core.graphics
18
19import android.graphics.Canvas
20import android.graphics.Matrix
21import com.google.common.truth.Truth.assertThat
22import org.junit.Assert.assertEquals
23import org.junit.Test
24
25class CanvasTest {
26    private val values = FloatArray(9)
27    private val canvas = Canvas(createBitmap(1, 1))
28
29    @Suppress("DEPRECATION")
30    @Test fun withSave() {
31        val beforeCount = canvas.saveCount
32
33        canvas.matrix.getValues(values)
34        val x = values[Matrix.MTRANS_X]
35        val y = values[Matrix.MTRANS_Y]
36
37        canvas.withSave {
38            assertThat(beforeCount).isLessThan(saveCount)
39            translate(10.0f, 10.0f)
40        }
41
42        canvas.matrix.getValues(values)
43        assertEquals(x, values[Matrix.MTRANS_X])
44        assertEquals(y, values[Matrix.MTRANS_Y])
45
46        assertEquals(beforeCount, canvas.saveCount)
47    }
48
49    @Test fun withTranslation() {
50        val beforeCount = canvas.saveCount
51        canvas.withTranslation(x = 16.0f, y = 32.0f) {
52            assertThat(beforeCount).isLessThan(saveCount)
53
54            @Suppress("DEPRECATION")
55            matrix.getValues(values) // will work for a software canvas
56
57            assertEquals(16.0f, values[Matrix.MTRANS_X])
58            assertEquals(32.0f, values[Matrix.MTRANS_Y])
59        }
60        assertEquals(beforeCount, canvas.saveCount)
61    }
62
63    @Test fun withRotation() {
64        val beforeCount = canvas.saveCount
65        canvas.withRotation(degrees = 90.0f, pivotX = 16.0f, pivotY = 32.0f) {
66            assertThat(beforeCount).isLessThan(saveCount)
67
68            @Suppress("DEPRECATION")
69            matrix.getValues(values) // will work for a software canvas
70
71            assertEquals(48.0f, values[Matrix.MTRANS_X])
72            assertEquals(16.0f, values[Matrix.MTRANS_Y])
73            assertEquals(-1.0f, values[Matrix.MSKEW_X])
74            assertEquals(1.0f, values[Matrix.MSKEW_Y])
75        }
76        assertEquals(beforeCount, canvas.saveCount)
77    }
78
79    @Test fun withScale() {
80        val beforeCount = canvas.saveCount
81        canvas.withScale(x = 2.0f, y = 4.0f, pivotX = 16.0f, pivotY = 32.0f) {
82            assertThat(beforeCount).isLessThan(saveCount)
83
84            @Suppress("DEPRECATION")
85            matrix.getValues(values) // will work for a software canvas
86
87            assertEquals(-16.0f, values[Matrix.MTRANS_X])
88            assertEquals(-96.0f, values[Matrix.MTRANS_Y])
89            assertEquals(2.0f, values[Matrix.MSCALE_X])
90            assertEquals(4.0f, values[Matrix.MSCALE_Y])
91        }
92        assertEquals(beforeCount, canvas.saveCount)
93    }
94
95    @Test fun withSkew() {
96        val beforeCount = canvas.saveCount
97        canvas.withSkew(x = 2.0f, y = 4.0f) {
98            assertThat(beforeCount).isLessThan(saveCount)
99
100            @Suppress("DEPRECATION")
101            matrix.getValues(values) // will work for a software canvas
102
103            assertEquals(2.0f, values[Matrix.MSKEW_X])
104            assertEquals(4.0f, values[Matrix.MSKEW_Y])
105        }
106        assertEquals(beforeCount, canvas.saveCount)
107    }
108
109    @Suppress("DEPRECATION")
110    @Test fun withMatrix() {
111        val originMatrix = canvas.matrix
112
113        val inputMatrix = Matrix()
114        inputMatrix.postTranslate(16.0f, 32.0f)
115        inputMatrix.postRotate(90.0f, 16.0f, 32.0f)
116        inputMatrix.postScale(2.0f, 4.0f, 16.0f, 32.0f)
117
118        val beforeCount = canvas.saveCount
119        canvas.withMatrix(inputMatrix) {
120            assertThat(beforeCount).isLessThan(saveCount)
121            assertEquals(inputMatrix, matrix)
122        }
123
124        assertEquals(originMatrix, canvas.matrix)
125        assertEquals(beforeCount, canvas.saveCount)
126    }
127}
128