1/*
2 * Copyright (C) 2018 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.Path
20import android.graphics.RectF
21import android.support.test.filters.SdkSuppress
22import org.junit.Assert.assertEquals
23import org.junit.Assert.assertNotEquals
24import org.junit.Assert.assertTrue
25import org.junit.Test
26
27class PathTest {
28    @SdkSuppress(minSdkVersion = 26)
29    @Test fun testFlatten() {
30        val p = Path()
31
32        // Start with several moves
33        p.moveTo(5.0f, 5.0f)
34        p.moveTo(10.0f, 10.0f)
35        p.lineTo(20.0f, 20.0f)
36        p.lineTo(30.0f, 10.0f)
37        // Several moves in the middle
38        p.moveTo(40.0f, 10.0f)
39        p.moveTo(50.0f, 10.0f)
40        p.lineTo(60.0f, 20.0f)
41        // End with several moves
42        p.moveTo(10.0f, 10.0f)
43        p.moveTo(30.0f, 30.0f)
44
45        var count = 0
46        p.flatten().forEach {
47            count++
48            assertNotEquals(it.startFraction, it.endFraction)
49        }
50        assertEquals(3, count)
51    }
52
53    @SdkSuppress(minSdkVersion = 19)
54    @Test fun testUnion() {
55        val r1 = Path().apply { addRect(0.0f, 0.0f, 10.0f, 10.0f, Path.Direction.CW) }
56        val r2 = Path().apply { addRect(5.0f, 0.0f, 15.0f, 15.0f, Path.Direction.CW) }
57
58        val p = r1 + r2
59        val r = RectF()
60        p.computeBounds(r, true)
61
62        assertEquals(RectF(0.0f, 0.0f, 15.0f, 15.0f), r)
63    }
64
65    @SdkSuppress(minSdkVersion = 19)
66    @Test fun testAnd() {
67        val r1 = Path().apply { addRect(0.0f, 0.0f, 10.0f, 10.0f, Path.Direction.CW) }
68        val r2 = Path().apply { addRect(5.0f, 0.0f, 15.0f, 15.0f, Path.Direction.CW) }
69
70        val p = r1 and r2
71        val r = RectF()
72        p.computeBounds(r, true)
73
74        assertEquals(RectF(0.0f, 0.0f, 15.0f, 15.0f), r)
75    }
76
77    @SdkSuppress(minSdkVersion = 19)
78    @Test fun testDifference() {
79        val r1 = Path().apply { addRect(0.0f, 0.0f, 10.0f, 10.0f, Path.Direction.CW) }
80        val r2 = Path().apply { addRect(5.0f, 0.0f, 15.0f, 15.0f, Path.Direction.CW) }
81
82        val p = r1 - r2
83        val r = RectF()
84        p.computeBounds(r, true)
85
86        assertEquals(RectF(0.0f, 0.0f, 5.0f, 10.0f), r)
87    }
88
89    @SdkSuppress(minSdkVersion = 19)
90    @Test fun testIntersection() {
91        val r1 = Path().apply { addRect(0.0f, 0.0f, 10.0f, 10.0f, Path.Direction.CW) }
92        val r2 = Path().apply { addRect(5.0f, 0.0f, 15.0f, 15.0f, Path.Direction.CW) }
93
94        val p = r1 or r2
95        val r = RectF()
96        p.computeBounds(r, true)
97
98        assertEquals(RectF(5.0f, 0.0f, 10.0f, 10.0f), r)
99    }
100
101    @SdkSuppress(minSdkVersion = 19)
102    @Test fun testEmptyIntersection() {
103        val r1 = Path().apply { addRect(0.0f, 0.0f, 2.0f, 2.0f, Path.Direction.CW) }
104        val r2 = Path().apply { addRect(5.0f, 5.0f, 7.0f, 7.0f, Path.Direction.CW) }
105
106        val p = r1 or r2
107        assertTrue(p.isEmpty)
108    }
109
110    @SdkSuppress(minSdkVersion = 19)
111    @Test fun testXor() {
112        val r1 = Path().apply { addRect(0.0f, 0.0f, 10.0f, 10.0f, Path.Direction.CW) }
113        val r2 = Path().apply { addRect(5.0f, 5.0f, 15.0f, 15.0f, Path.Direction.CW) }
114
115        val p = r1 xor r2
116        val r = RectF()
117        p.computeBounds(r, true)
118
119        assertEquals(RectF(0.0f, 0.0f, 15.0f, 15.0f), r)
120    }
121}
122