George Mount | 932dc5d | 2019-10-28 08:47:10 -0700 | [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 |
George Mount | 932dc5d | 2019-10-28 08:47:10 -0700 | [diff] [blame] | 18 | |
| 19 | import android.os.Build |
| 20 | import androidx.compose.Composable |
| 21 | import androidx.test.filters.MediumTest |
| 22 | import androidx.test.filters.SdkSuppress |
Andrey Kulikov | 47f0e1a | 2020-01-28 13:52:46 +0000 | [diff] [blame] | 23 | import androidx.ui.core.DensityAmbient |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame] | 24 | import androidx.ui.core.Modifier |
Filip Pavlis | 6de64f5 | 2020-05-27 18:11:55 +0100 | [diff] [blame] | 25 | import androidx.ui.core.testTag |
Louis Pullen-Freilich | ddda7be | 2020-07-17 18:28:12 +0100 | [diff] [blame] | 26 | import androidx.compose.foundation.shape.corner.CircleShape |
George Mount | 932dc5d | 2019-10-28 08:47:10 -0700 | [diff] [blame] | 27 | import androidx.ui.graphics.Color |
Andrey Kulikov | 267b950 | 2020-04-20 18:55:09 +0100 | [diff] [blame] | 28 | import androidx.ui.graphics.RectangleShape |
George Mount | 932dc5d | 2019-10-28 08:47:10 -0700 | [diff] [blame] | 29 | import androidx.ui.graphics.SolidColor |
Louis Pullen-Freilich | 623e405 | 2020-07-19 20:24:03 +0100 | [diff] [blame] | 30 | import androidx.compose.foundation.layout.Stack |
| 31 | import androidx.compose.foundation.layout.preferredSize |
George Mount | 932dc5d | 2019-10-28 08:47:10 -0700 | [diff] [blame] | 32 | import androidx.ui.test.assertShape |
| 33 | import androidx.ui.test.captureToBitmap |
| 34 | import androidx.ui.test.createComposeRule |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 35 | import androidx.ui.test.onNodeWithTag |
Andrey Kulikov | 47f0e1a | 2020-01-28 13:52:46 +0000 | [diff] [blame] | 36 | import androidx.ui.unit.Density |
George Mount | 932dc5d | 2019-10-28 08:47:10 -0700 | [diff] [blame] | 37 | import org.junit.Rule |
| 38 | import org.junit.Test |
| 39 | import org.junit.runner.RunWith |
| 40 | import org.junit.runners.JUnit4 |
| 41 | |
| 42 | @MediumTest |
| 43 | @SdkSuppress(minSdkVersion = Build.VERSION_CODES.O) |
| 44 | @RunWith(JUnit4::class) |
Matvei Malkov | bf014c1 | 2020-07-09 17:40:10 +0100 | [diff] [blame] | 45 | class BackgroundTest { |
Matvei Malkov | 6e5b8f2 | 2020-01-07 17:24:10 +0000 | [diff] [blame] | 46 | |
George Mount | 932dc5d | 2019-10-28 08:47:10 -0700 | [diff] [blame] | 47 | @get:Rule |
| 48 | val composeTestRule = createComposeRule() |
| 49 | |
| 50 | private val contentTag = "Content" |
| 51 | |
| 52 | @Test |
| 53 | fun background_colorRect() { |
| 54 | composeTestRule.setContent { |
Matvei Malkov | a17b563 | 2020-03-13 12:30:53 +0000 | [diff] [blame] | 55 | SemanticParent { |
| 56 | Box( |
Matvei Malkov | bf014c1 | 2020-07-09 17:40:10 +0100 | [diff] [blame] | 57 | Modifier.preferredSize(40f.toDp()).background(Color.Magenta), |
Matvei Malkov | a17b563 | 2020-03-13 12:30:53 +0000 | [diff] [blame] | 58 | gravity = ContentGravity.Center |
George Mount | 932dc5d | 2019-10-28 08:47:10 -0700 | [diff] [blame] | 59 | ) { |
Matvei Malkov | bf014c1 | 2020-07-09 17:40:10 +0100 | [diff] [blame] | 60 | Box(Modifier.preferredSize(20f.toDp()).background(Color.White)) |
George Mount | 932dc5d | 2019-10-28 08:47:10 -0700 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | } |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 64 | val bitmap = onNodeWithTag(contentTag).captureToBitmap() |
George Mount | 932dc5d | 2019-10-28 08:47:10 -0700 | [diff] [blame] | 65 | bitmap.assertShape( |
Matvei Malkov | 6e5b8f2 | 2020-01-07 17:24:10 +0000 | [diff] [blame] | 66 | density = composeTestRule.density, |
George Mount | 932dc5d | 2019-10-28 08:47:10 -0700 | [diff] [blame] | 67 | backgroundColor = Color.Magenta, |
| 68 | shape = RectangleShape, |
Nader Jawad | 22f5978 | 2020-05-14 17:07:11 -0700 | [diff] [blame] | 69 | shapeSizeX = 20.0f, |
| 70 | shapeSizeY = 20.0f, |
Matvei Malkov | 83c41e0 | 2020-02-03 01:37:15 +0000 | [diff] [blame] | 71 | shapeColor = Color.White |
| 72 | ) |
George Mount | 932dc5d | 2019-10-28 08:47:10 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | @Test |
| 76 | fun background_brushRect() { |
| 77 | composeTestRule.setContent { |
Matvei Malkov | a17b563 | 2020-03-13 12:30:53 +0000 | [diff] [blame] | 78 | SemanticParent { |
| 79 | Box( |
Matvei Malkov | bf014c1 | 2020-07-09 17:40:10 +0100 | [diff] [blame] | 80 | Modifier.preferredSize(40f.toDp()).background(Color.Magenta), |
Matvei Malkov | a17b563 | 2020-03-13 12:30:53 +0000 | [diff] [blame] | 81 | gravity = ContentGravity.Center |
George Mount | 932dc5d | 2019-10-28 08:47:10 -0700 | [diff] [blame] | 82 | ) { |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame] | 83 | Box( |
Nader Jawad | 16e330a | 2020-05-21 21:21:01 -0700 | [diff] [blame] | 84 | Modifier.preferredSize(20f.toDp()) |
Matvei Malkov | bf014c1 | 2020-07-09 17:40:10 +0100 | [diff] [blame] | 85 | .background(SolidColor(Color.White)) |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame] | 86 | ) |
George Mount | 932dc5d | 2019-10-28 08:47:10 -0700 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | } |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 90 | val bitmap = onNodeWithTag(contentTag).captureToBitmap() |
George Mount | 932dc5d | 2019-10-28 08:47:10 -0700 | [diff] [blame] | 91 | bitmap.assertShape( |
Matvei Malkov | 6e5b8f2 | 2020-01-07 17:24:10 +0000 | [diff] [blame] | 92 | density = composeTestRule.density, |
George Mount | 932dc5d | 2019-10-28 08:47:10 -0700 | [diff] [blame] | 93 | backgroundColor = Color.Magenta, |
| 94 | shape = RectangleShape, |
Nader Jawad | 22f5978 | 2020-05-14 17:07:11 -0700 | [diff] [blame] | 95 | shapeSizeX = 20.0f, |
| 96 | shapeSizeY = 20.0f, |
Matvei Malkov | 83c41e0 | 2020-02-03 01:37:15 +0000 | [diff] [blame] | 97 | shapeColor = Color.White |
| 98 | ) |
George Mount | 932dc5d | 2019-10-28 08:47:10 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | @Test |
| 102 | fun background_colorCircle() { |
| 103 | composeTestRule.setContent { |
Matvei Malkov | a17b563 | 2020-03-13 12:30:53 +0000 | [diff] [blame] | 104 | SemanticParent { |
| 105 | Box( |
Nader Jawad | 16e330a | 2020-05-21 21:21:01 -0700 | [diff] [blame] | 106 | Modifier.preferredSize(40f.toDp()) |
Matvei Malkov | bf014c1 | 2020-07-09 17:40:10 +0100 | [diff] [blame] | 107 | .background(Color.Magenta) |
| 108 | .background(color = Color.White, shape = CircleShape) |
Matvei Malkov | a17b563 | 2020-03-13 12:30:53 +0000 | [diff] [blame] | 109 | ) |
George Mount | 932dc5d | 2019-10-28 08:47:10 -0700 | [diff] [blame] | 110 | } |
| 111 | } |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 112 | val bitmap = onNodeWithTag(contentTag).captureToBitmap() |
George Mount | 932dc5d | 2019-10-28 08:47:10 -0700 | [diff] [blame] | 113 | bitmap.assertShape( |
Matvei Malkov | 6e5b8f2 | 2020-01-07 17:24:10 +0000 | [diff] [blame] | 114 | density = composeTestRule.density, |
George Mount | 932dc5d | 2019-10-28 08:47:10 -0700 | [diff] [blame] | 115 | backgroundColor = Color.Magenta, |
| 116 | shape = CircleShape, |
Nader Jawad | afe0666 | 2020-02-10 13:12:15 -0800 | [diff] [blame] | 117 | shapeColor = Color.White, |
Nader Jawad | 22f5978 | 2020-05-14 17:07:11 -0700 | [diff] [blame] | 118 | shapeOverlapPixelCount = 2.0f |
Matvei Malkov | a17b563 | 2020-03-13 12:30:53 +0000 | [diff] [blame] | 119 | ) |
George Mount | 932dc5d | 2019-10-28 08:47:10 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | @Test |
| 123 | fun background_brushCircle() { |
| 124 | composeTestRule.setContent { |
Matvei Malkov | a17b563 | 2020-03-13 12:30:53 +0000 | [diff] [blame] | 125 | SemanticParent { |
| 126 | Box( |
Nader Jawad | 16e330a | 2020-05-21 21:21:01 -0700 | [diff] [blame] | 127 | Modifier.preferredSize(40f.toDp()) |
Matvei Malkov | bf014c1 | 2020-07-09 17:40:10 +0100 | [diff] [blame] | 128 | .background(Color.Magenta) |
| 129 | .background( |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame] | 130 | brush = SolidColor(Color.White), |
| 131 | shape = CircleShape |
| 132 | ) |
Matvei Malkov | a17b563 | 2020-03-13 12:30:53 +0000 | [diff] [blame] | 133 | ) |
George Mount | 932dc5d | 2019-10-28 08:47:10 -0700 | [diff] [blame] | 134 | } |
| 135 | } |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 136 | val bitmap = onNodeWithTag(contentTag).captureToBitmap() |
George Mount | 932dc5d | 2019-10-28 08:47:10 -0700 | [diff] [blame] | 137 | bitmap.assertShape( |
Matvei Malkov | 6e5b8f2 | 2020-01-07 17:24:10 +0000 | [diff] [blame] | 138 | density = composeTestRule.density, |
George Mount | 932dc5d | 2019-10-28 08:47:10 -0700 | [diff] [blame] | 139 | backgroundColor = Color.Magenta, |
| 140 | shape = CircleShape, |
Nader Jawad | afe0666 | 2020-02-10 13:12:15 -0800 | [diff] [blame] | 141 | shapeColor = Color.White, |
Nader Jawad | 22f5978 | 2020-05-14 17:07:11 -0700 | [diff] [blame] | 142 | shapeOverlapPixelCount = 2.0f |
Matvei Malkov | a17b563 | 2020-03-13 12:30:53 +0000 | [diff] [blame] | 143 | ) |
George Mount | 932dc5d | 2019-10-28 08:47:10 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | @Composable |
Matvei Malkov | a17b563 | 2020-03-13 12:30:53 +0000 | [diff] [blame] | 147 | private fun SemanticParent(children: @Composable Density.() -> Unit) { |
Alexandre Elias | 96c9ccbd | 2020-05-19 19:12:54 -0700 | [diff] [blame] | 148 | Stack(Modifier.testTag(contentTag)) { |
Filip Pavlis | 6de64f5 | 2020-05-27 18:11:55 +0100 | [diff] [blame] | 149 | DensityAmbient.current.children() |
George Mount | 932dc5d | 2019-10-28 08:47:10 -0700 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | } |