Andrey Kulikov | 48bd6fa | 2019-06-05 14:52:07 +0100 | [diff] [blame] | 1 | /* |
| 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-Freilich | ddda7be | 2020-07-17 18:28:12 +0100 | [diff] [blame] | 17 | package androidx.compose.foundation.shape.corner |
Andrey Kulikov | 48bd6fa | 2019-06-05 14:52:07 +0100 | [diff] [blame] | 18 | |
| 19 | import androidx.test.filters.SmallTest |
Nader Jawad | feb99f8 | 2020-05-21 13:07:36 -0700 | [diff] [blame] | 20 | import androidx.ui.geometry.Size |
| 21 | import androidx.ui.geometry.toRect |
George Mount | 842c8c1 | 2020-01-08 16:03:42 -0800 | [diff] [blame] | 22 | import androidx.ui.graphics.Outline |
Nader Jawad | ad98353 | 2019-08-29 16:27:37 -0700 | [diff] [blame] | 23 | import androidx.ui.graphics.Path |
| 24 | import androidx.ui.graphics.PathOperation |
George Mount | 842c8c1 | 2020-01-08 16:03:42 -0800 | [diff] [blame] | 25 | import androidx.ui.graphics.Shape |
| 26 | import androidx.ui.unit.Density |
Andrey Kulikov | 6e28e97 | 2020-03-25 12:31:24 +0000 | [diff] [blame] | 27 | import androidx.ui.unit.dp |
Andrey Kulikov | 9a43374 | 2019-06-28 14:24:03 +0100 | [diff] [blame] | 28 | import com.google.common.truth.Truth.assertThat |
Andrey Kulikov | 48bd6fa | 2019-06-05 14:52:07 +0100 | [diff] [blame] | 29 | import org.junit.Assert |
| 30 | import org.junit.Test |
| 31 | import org.junit.runner.RunWith |
| 32 | import org.junit.runners.JUnit4 |
| 33 | |
| 34 | @SmallTest |
| 35 | @RunWith(JUnit4::class) |
| 36 | class CutCornerShapeTest { |
| 37 | |
| 38 | private val density = Density(2f) |
Nader Jawad | feb99f8 | 2020-05-21 13:07:36 -0700 | [diff] [blame] | 39 | private val size = Size(100.0f, 150.0f) |
Andrey Kulikov | 48bd6fa | 2019-06-05 14:52:07 +0100 | [diff] [blame] | 40 | |
| 41 | @Test |
| 42 | fun cutCornersUniformCorners() { |
Nader Jawad | 22f5978 | 2020-05-14 17:07:11 -0700 | [diff] [blame] | 43 | val cut = CutCornerShape(10.0f) |
Andrey Kulikov | 48bd6fa | 2019-06-05 14:52:07 +0100 | [diff] [blame] | 44 | |
| 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 Jawad | 22f5978 | 2020-05-14 17:07:11 -0700 | [diff] [blame] | 65 | val cut = CutCornerShape(size1, size2, size3, size4) |
Andrey Kulikov | 48bd6fa | 2019-06-05 14:52:07 +0100 | [diff] [blame] | 66 | |
| 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 Kulikov | 9a43374 | 2019-06-28 14:24:03 +0100 | [diff] [blame] | 81 | @Test |
Andrey Kulikov | 6e28e97 | 2020-03-25 12:31:24 +0000 | [diff] [blame] | 82 | fun createsRectangleOutlineForZeroSizedCorners() { |
Nader Jawad | 22f5978 | 2020-05-14 17:07:11 -0700 | [diff] [blame] | 83 | val rounded = CutCornerShape(0.0f, 0.0f, 0.0f, 0.0f) |
Andrey Kulikov | 6e28e97 | 2020-03-25 12:31:24 +0000 | [diff] [blame] | 84 | |
| 85 | assertThat(rounded.toOutline()) |
| 86 | .isEqualTo(Outline.Rectangle(size.toRect())) |
| 87 | } |
| 88 | |
| 89 | @Test |
Andrey Kulikov | 9a43374 | 2019-06-28 14:24:03 +0100 | [diff] [blame] | 90 | fun cutCornerShapesAreEquals() { |
Nader Jawad | 22f5978 | 2020-05-14 17:07:11 -0700 | [diff] [blame] | 91 | assertThat(CutCornerShape(10.0f)) |
| 92 | .isEqualTo(CutCornerShape(10.0f)) |
Andrey Kulikov | 48bd6fa | 2019-06-05 14:52:07 +0100 | [diff] [blame] | 93 | } |
Andrey Kulikov | 9a43374 | 2019-06-28 14:24:03 +0100 | [diff] [blame] | 94 | |
Andrey Kulikov | 6e28e97 | 2020-03-25 12:31:24 +0000 | [diff] [blame] | 95 | @Test |
| 96 | fun cutCornerUpdateAllCornerSize() { |
Nader Jawad | 22f5978 | 2020-05-14 17:07:11 -0700 | [diff] [blame] | 97 | assertThat(CutCornerShape(10.0f).copy(CornerSize(5.0f))) |
| 98 | .isEqualTo(CutCornerShape(5.0f)) |
Andrey Kulikov | 6e28e97 | 2020-03-25 12:31:24 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | @Test |
| 102 | fun cutCornerUpdateTwoCornerSizes() { |
Nader Jawad | 22f5978 | 2020-05-14 17:07:11 -0700 | [diff] [blame] | 103 | assertThat(CutCornerShape(10.0f).copy( |
Andrey Kulikov | 6e28e97 | 2020-03-25 12:31:24 +0000 | [diff] [blame] | 104 | topRight = CornerSize(3.dp), |
| 105 | bottomLeft = CornerSize(50) |
| 106 | )).isEqualTo(CutCornerShape( |
Nader Jawad | 22f5978 | 2020-05-14 17:07:11 -0700 | [diff] [blame] | 107 | topLeft = CornerSize(10.0f), |
Andrey Kulikov | 6e28e97 | 2020-03-25 12:31:24 +0000 | [diff] [blame] | 108 | topRight = CornerSize(3.dp), |
Nader Jawad | 22f5978 | 2020-05-14 17:07:11 -0700 | [diff] [blame] | 109 | bottomRight = CornerSize(10.0f), |
Andrey Kulikov | 6e28e97 | 2020-03-25 12:31:24 +0000 | [diff] [blame] | 110 | bottomLeft = CornerSize(50) |
| 111 | )) |
| 112 | } |
| 113 | |
Andrey Kulikov | 9a43374 | 2019-06-28 14:24:03 +0100 | [diff] [blame] | 114 | private fun Shape.toOutline() = createOutline(size, density) |
Andrey Kulikov | 48bd6fa | 2019-06-05 14:52:07 +0100 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | fun 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 | } |