1 /*
2  * Copyright 2024 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 
17 package androidx.compose.testutils
18 
19 import androidx.compose.foundation.shape.CircleShape
20 import androidx.compose.ui.geometry.Offset
21 import androidx.compose.ui.geometry.Size
22 import androidx.compose.ui.graphics.Path
23 import androidx.compose.ui.graphics.RectangleShape
24 import androidx.compose.ui.graphics.addOutline
25 import androidx.compose.ui.unit.Density
26 import androidx.compose.ui.unit.LayoutDirection
27 import androidx.test.ext.junit.runners.AndroidJUnit4
28 import androidx.test.filters.SmallTest
29 import org.junit.Test
30 import org.junit.runner.RunWith
31 
32 @SmallTest
33 @RunWith(AndroidJUnit4::class)
34 class PathContainsTest {
35     @Test
testRectangleContainsnull36     fun testRectangleContains() {
37         val errors = mutableListOf<String>()
38         val path = Path()
39         path.addOutline(
40             RectangleShape.createOutline(Size(1f, 1f), LayoutDirection.Ltr, Density(1f))
41         )
42         fun assertContains(point: Offset, expected: Boolean) {
43             if (path.contains(point) != expected) {
44                 val toBeOrNotToBe = if (expected) "be" else "not be"
45                 errors.add(
46                     "Point(${point.x}, ${point.y}) should $toBeOrNotToBe within the Rect(0, 0, 1, 1)"
47                 )
48             }
49         }
50         // Verify the corners
51         assertContains(Offset(0f, 0f), true)
52         assertContains(Offset(0f, 1f), true)
53         assertContains(Offset(1f, 0f), true)
54         assertContains(Offset(1f, 1f), true)
55         // Verify just outside the corners
56         assertContains(Offset(-.1f, -.1f), false)
57         assertContains(Offset(0f, -.1f), false)
58         assertContains(Offset(1f, -.1f), false)
59         assertContains(Offset(1.1f, -.1f), false)
60         assertContains(Offset(1.1f, 0f), false)
61         assertContains(Offset(1.1f, 1f), false)
62         assertContains(Offset(1.1f, 1.1f), false)
63         assertContains(Offset(1f, 1.1f), false)
64         assertContains(Offset(0f, 1.1f), false)
65         assertContains(Offset(-.1f, 1.1f), false)
66         assertContains(Offset(-.1f, 1f), false)
67         assertContains(Offset(-.1f, 0f), false)
68 
69         if (errors.isNotEmpty()) {
70             throw AssertionError("Found the following errors:\n${errors.joinToString("\n")}")
71         }
72     }
73 
74     @Test
testCircleContainsnull75     fun testCircleContains() {
76         val path = Path()
77         path.addOutline(CircleShape.createOutline(Size(50f, 50f), LayoutDirection.Ltr, Density(1f)))
78         path.translate(Offset(25f, 25f))
79         val errors = mutableListOf<String>()
80         fun assertContains(point: Offset, expected: Boolean) {
81             if (path.contains(point) != expected) {
82                 val toBeOrNotToBe = if (expected) "be" else "not be"
83                 errors.add(
84                     "Point(${point.x}, ${point.y}) should $toBeOrNotToBe within the Circle(50x50)"
85                 )
86             }
87         }
88         assertContains(Offset(25f, 25f), false)
89         assertContains(Offset(50f, 25f), true)
90         assertContains(Offset(75f, 25f), false)
91         assertContains(Offset(25f, 50f), true)
92         assertContains(Offset(50f, 50f), true)
93         assertContains(Offset(75f, 50f), true)
94         assertContains(Offset(25f, 75f), false)
95         assertContains(Offset(50f, 75f), true)
96         assertContains(Offset(75f, 75f), false)
97 
98         if (errors.isNotEmpty()) {
99             throw AssertionError("Found the following errors:\n${errors.joinToString("\n")}")
100         }
101     }
102 }
103