[go: nahoru, domu]

blob: ff53d2a66946396b96f7244cdae02b31649d31c5 [file] [log] [blame]
Andrey Kulikov48bd6fa2019-06-05 14:52:07 +01001/*
2 * Copyright 2019 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
Louis Pullen-Freilichddda7be2020-07-17 18:28:12 +010017package androidx.compose.foundation.shape.corner
Andrey Kulikov48bd6fa2019-06-05 14:52:07 +010018
19import androidx.test.filters.SmallTest
Louis Pullen-Freilichf434a132020-07-22 14:19:24 +010020import androidx.compose.ui.geometry.Size
21import androidx.compose.ui.geometry.toRect
Louis Pullen-Freilich4dc4dac2020-07-22 14:39:14 +010022import androidx.compose.ui.graphics.Outline
23import androidx.compose.ui.graphics.Path
24import androidx.compose.ui.graphics.PathOperation
25import androidx.compose.ui.graphics.Shape
George Mount842c8c12020-01-08 16:03:42 -080026import androidx.ui.unit.Density
Andrey Kulikov6e28e972020-03-25 12:31:24 +000027import androidx.ui.unit.dp
Andrey Kulikov9a433742019-06-28 14:24:03 +010028import com.google.common.truth.Truth.assertThat
Andrey Kulikov48bd6fa2019-06-05 14:52:07 +010029import org.junit.Assert
30import org.junit.Test
31import org.junit.runner.RunWith
32import org.junit.runners.JUnit4
33
34@SmallTest
35@RunWith(JUnit4::class)
36class CutCornerShapeTest {
37
38 private val density = Density(2f)
Nader Jawadfeb99f82020-05-21 13:07:36 -070039 private val size = Size(100.0f, 150.0f)
Andrey Kulikov48bd6fa2019-06-05 14:52:07 +010040
41 @Test
42 fun cutCornersUniformCorners() {
Nader Jawad22f59782020-05-14 17:07:11 -070043 val cut = CutCornerShape(10.0f)
Andrey Kulikov48bd6fa2019-06-05 14:52:07 +010044
45 val outline = cut.toOutline() as Outline.Generic
46 assertPathsEquals(outline.path, Path().apply {
47 moveTo(0f, 10f)
48 lineTo(10f, 0f)
49 lineTo(90f, 0f)
50 lineTo(100f, 10f)
51 lineTo(100f, 140f)
52 lineTo(90f, 150f)
53 lineTo(10f, 150f)
54 lineTo(0f, 140f)
55 close()
56 })
57 }
58
59 @Test
60 fun cutCornersDifferentCorners() {
61 val size1 = 12f
62 val size2 = 22f
63 val size3 = 32f
64 val size4 = 42f
Nader Jawad22f59782020-05-14 17:07:11 -070065 val cut = CutCornerShape(size1, size2, size3, size4)
Andrey Kulikov48bd6fa2019-06-05 14:52:07 +010066
67 val outline = cut.toOutline() as Outline.Generic
68 assertPathsEquals(outline.path, Path().apply {
69 moveTo(0f, 12f)
70 lineTo(12f, 0f)
71 lineTo(78f, 0f)
72 lineTo(100f, 22f)
73 lineTo(100f, 118f)
74 lineTo(68f, 150f)
75 lineTo(42f, 150f)
76 lineTo(0f, 108f)
77 close()
78 })
79 }
80
Andrey Kulikov9a433742019-06-28 14:24:03 +010081 @Test
Andrey Kulikov6e28e972020-03-25 12:31:24 +000082 fun createsRectangleOutlineForZeroSizedCorners() {
Nader Jawad22f59782020-05-14 17:07:11 -070083 val rounded = CutCornerShape(0.0f, 0.0f, 0.0f, 0.0f)
Andrey Kulikov6e28e972020-03-25 12:31:24 +000084
85 assertThat(rounded.toOutline())
86 .isEqualTo(Outline.Rectangle(size.toRect()))
87 }
88
89 @Test
Andrey Kulikov9a433742019-06-28 14:24:03 +010090 fun cutCornerShapesAreEquals() {
Nader Jawad22f59782020-05-14 17:07:11 -070091 assertThat(CutCornerShape(10.0f))
92 .isEqualTo(CutCornerShape(10.0f))
Andrey Kulikov48bd6fa2019-06-05 14:52:07 +010093 }
Andrey Kulikov9a433742019-06-28 14:24:03 +010094
Andrey Kulikov6e28e972020-03-25 12:31:24 +000095 @Test
96 fun cutCornerUpdateAllCornerSize() {
Nader Jawad22f59782020-05-14 17:07:11 -070097 assertThat(CutCornerShape(10.0f).copy(CornerSize(5.0f)))
98 .isEqualTo(CutCornerShape(5.0f))
Andrey Kulikov6e28e972020-03-25 12:31:24 +000099 }
100
101 @Test
102 fun cutCornerUpdateTwoCornerSizes() {
Nader Jawad22f59782020-05-14 17:07:11 -0700103 assertThat(CutCornerShape(10.0f).copy(
Andrey Kulikov6e28e972020-03-25 12:31:24 +0000104 topRight = CornerSize(3.dp),
105 bottomLeft = CornerSize(50)
106 )).isEqualTo(CutCornerShape(
Nader Jawad22f59782020-05-14 17:07:11 -0700107 topLeft = CornerSize(10.0f),
Andrey Kulikov6e28e972020-03-25 12:31:24 +0000108 topRight = CornerSize(3.dp),
Nader Jawad22f59782020-05-14 17:07:11 -0700109 bottomRight = CornerSize(10.0f),
Andrey Kulikov6e28e972020-03-25 12:31:24 +0000110 bottomLeft = CornerSize(50)
111 ))
112 }
113
Andrey Kulikov9a433742019-06-28 14:24:03 +0100114 private fun Shape.toOutline() = createOutline(size, density)
Andrey Kulikov48bd6fa2019-06-05 14:52:07 +0100115}
116
117fun assertPathsEquals(path1: Path, path2: Path) {
118 val diff = Path()
119 val reverseDiff = Path()
120 Assert.assertTrue(
121 diff.op(path1, path2, PathOperation.difference) &&
122 reverseDiff.op(path2, path1, PathOperation.difference) &&
123 diff.isEmpty &&
124 reverseDiff.isEmpty
125 )
126}