[go: nahoru, domu]

blob: c4b7fc27aacccedfc43075bb46808940fd45cc07 [file] [log] [blame]
George Mount932dc5d2019-10-28 08:47:10 -07001/*
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
George Mount932dc5d2019-10-28 08:47:10 -070018
19import android.os.Build
20import androidx.compose.Composable
21import androidx.test.filters.MediumTest
22import androidx.test.filters.SdkSuppress
Andrey Kulikov47f0e1a2020-01-28 13:52:46 +000023import androidx.ui.core.DensityAmbient
Adam Powell999a89b2020-03-11 09:08:07 -070024import androidx.ui.core.Modifier
Filip Pavlis6de64f52020-05-27 18:11:55 +010025import androidx.ui.core.testTag
Louis Pullen-Freilichddda7be2020-07-17 18:28:12 +010026import androidx.compose.foundation.shape.corner.CircleShape
George Mount932dc5d2019-10-28 08:47:10 -070027import androidx.ui.graphics.Color
Andrey Kulikov267b9502020-04-20 18:55:09 +010028import androidx.ui.graphics.RectangleShape
George Mount932dc5d2019-10-28 08:47:10 -070029import androidx.ui.graphics.SolidColor
Louis Pullen-Freilich623e4052020-07-19 20:24:03 +010030import androidx.compose.foundation.layout.Stack
31import androidx.compose.foundation.layout.preferredSize
George Mount932dc5d2019-10-28 08:47:10 -070032import androidx.ui.test.assertShape
33import androidx.ui.test.captureToBitmap
34import androidx.ui.test.createComposeRule
Filip Pavlis659ea722020-07-13 14:14:32 +010035import androidx.ui.test.onNodeWithTag
Andrey Kulikov47f0e1a2020-01-28 13:52:46 +000036import androidx.ui.unit.Density
George Mount932dc5d2019-10-28 08:47:10 -070037import org.junit.Rule
38import org.junit.Test
39import org.junit.runner.RunWith
40import org.junit.runners.JUnit4
41
42@MediumTest
43@SdkSuppress(minSdkVersion = Build.VERSION_CODES.O)
44@RunWith(JUnit4::class)
Matvei Malkovbf014c12020-07-09 17:40:10 +010045class BackgroundTest {
Matvei Malkov6e5b8f22020-01-07 17:24:10 +000046
George Mount932dc5d2019-10-28 08:47:10 -070047 @get:Rule
48 val composeTestRule = createComposeRule()
49
50 private val contentTag = "Content"
51
52 @Test
53 fun background_colorRect() {
54 composeTestRule.setContent {
Matvei Malkova17b5632020-03-13 12:30:53 +000055 SemanticParent {
56 Box(
Matvei Malkovbf014c12020-07-09 17:40:10 +010057 Modifier.preferredSize(40f.toDp()).background(Color.Magenta),
Matvei Malkova17b5632020-03-13 12:30:53 +000058 gravity = ContentGravity.Center
George Mount932dc5d2019-10-28 08:47:10 -070059 ) {
Matvei Malkovbf014c12020-07-09 17:40:10 +010060 Box(Modifier.preferredSize(20f.toDp()).background(Color.White))
George Mount932dc5d2019-10-28 08:47:10 -070061 }
62 }
63 }
Filip Pavlis659ea722020-07-13 14:14:32 +010064 val bitmap = onNodeWithTag(contentTag).captureToBitmap()
George Mount932dc5d2019-10-28 08:47:10 -070065 bitmap.assertShape(
Matvei Malkov6e5b8f22020-01-07 17:24:10 +000066 density = composeTestRule.density,
George Mount932dc5d2019-10-28 08:47:10 -070067 backgroundColor = Color.Magenta,
68 shape = RectangleShape,
Nader Jawad22f59782020-05-14 17:07:11 -070069 shapeSizeX = 20.0f,
70 shapeSizeY = 20.0f,
Matvei Malkov83c41e02020-02-03 01:37:15 +000071 shapeColor = Color.White
72 )
George Mount932dc5d2019-10-28 08:47:10 -070073 }
74
75 @Test
76 fun background_brushRect() {
77 composeTestRule.setContent {
Matvei Malkova17b5632020-03-13 12:30:53 +000078 SemanticParent {
79 Box(
Matvei Malkovbf014c12020-07-09 17:40:10 +010080 Modifier.preferredSize(40f.toDp()).background(Color.Magenta),
Matvei Malkova17b5632020-03-13 12:30:53 +000081 gravity = ContentGravity.Center
George Mount932dc5d2019-10-28 08:47:10 -070082 ) {
Adam Powell999a89b2020-03-11 09:08:07 -070083 Box(
Nader Jawad16e330a2020-05-21 21:21:01 -070084 Modifier.preferredSize(20f.toDp())
Matvei Malkovbf014c12020-07-09 17:40:10 +010085 .background(SolidColor(Color.White))
Adam Powell999a89b2020-03-11 09:08:07 -070086 )
George Mount932dc5d2019-10-28 08:47:10 -070087 }
88 }
89 }
Filip Pavlis659ea722020-07-13 14:14:32 +010090 val bitmap = onNodeWithTag(contentTag).captureToBitmap()
George Mount932dc5d2019-10-28 08:47:10 -070091 bitmap.assertShape(
Matvei Malkov6e5b8f22020-01-07 17:24:10 +000092 density = composeTestRule.density,
George Mount932dc5d2019-10-28 08:47:10 -070093 backgroundColor = Color.Magenta,
94 shape = RectangleShape,
Nader Jawad22f59782020-05-14 17:07:11 -070095 shapeSizeX = 20.0f,
96 shapeSizeY = 20.0f,
Matvei Malkov83c41e02020-02-03 01:37:15 +000097 shapeColor = Color.White
98 )
George Mount932dc5d2019-10-28 08:47:10 -070099 }
100
101 @Test
102 fun background_colorCircle() {
103 composeTestRule.setContent {
Matvei Malkova17b5632020-03-13 12:30:53 +0000104 SemanticParent {
105 Box(
Nader Jawad16e330a2020-05-21 21:21:01 -0700106 Modifier.preferredSize(40f.toDp())
Matvei Malkovbf014c12020-07-09 17:40:10 +0100107 .background(Color.Magenta)
108 .background(color = Color.White, shape = CircleShape)
Matvei Malkova17b5632020-03-13 12:30:53 +0000109 )
George Mount932dc5d2019-10-28 08:47:10 -0700110 }
111 }
Filip Pavlis659ea722020-07-13 14:14:32 +0100112 val bitmap = onNodeWithTag(contentTag).captureToBitmap()
George Mount932dc5d2019-10-28 08:47:10 -0700113 bitmap.assertShape(
Matvei Malkov6e5b8f22020-01-07 17:24:10 +0000114 density = composeTestRule.density,
George Mount932dc5d2019-10-28 08:47:10 -0700115 backgroundColor = Color.Magenta,
116 shape = CircleShape,
Nader Jawadafe06662020-02-10 13:12:15 -0800117 shapeColor = Color.White,
Nader Jawad22f59782020-05-14 17:07:11 -0700118 shapeOverlapPixelCount = 2.0f
Matvei Malkova17b5632020-03-13 12:30:53 +0000119 )
George Mount932dc5d2019-10-28 08:47:10 -0700120 }
121
122 @Test
123 fun background_brushCircle() {
124 composeTestRule.setContent {
Matvei Malkova17b5632020-03-13 12:30:53 +0000125 SemanticParent {
126 Box(
Nader Jawad16e330a2020-05-21 21:21:01 -0700127 Modifier.preferredSize(40f.toDp())
Matvei Malkovbf014c12020-07-09 17:40:10 +0100128 .background(Color.Magenta)
129 .background(
Adam Powell999a89b2020-03-11 09:08:07 -0700130 brush = SolidColor(Color.White),
131 shape = CircleShape
132 )
Matvei Malkova17b5632020-03-13 12:30:53 +0000133 )
George Mount932dc5d2019-10-28 08:47:10 -0700134 }
135 }
Filip Pavlis659ea722020-07-13 14:14:32 +0100136 val bitmap = onNodeWithTag(contentTag).captureToBitmap()
George Mount932dc5d2019-10-28 08:47:10 -0700137 bitmap.assertShape(
Matvei Malkov6e5b8f22020-01-07 17:24:10 +0000138 density = composeTestRule.density,
George Mount932dc5d2019-10-28 08:47:10 -0700139 backgroundColor = Color.Magenta,
140 shape = CircleShape,
Nader Jawadafe06662020-02-10 13:12:15 -0800141 shapeColor = Color.White,
Nader Jawad22f59782020-05-14 17:07:11 -0700142 shapeOverlapPixelCount = 2.0f
Matvei Malkova17b5632020-03-13 12:30:53 +0000143 )
George Mount932dc5d2019-10-28 08:47:10 -0700144 }
145
146 @Composable
Matvei Malkova17b5632020-03-13 12:30:53 +0000147 private fun SemanticParent(children: @Composable Density.() -> Unit) {
Alexandre Elias96c9ccbd2020-05-19 19:12:54 -0700148 Stack(Modifier.testTag(contentTag)) {
Filip Pavlis6de64f52020-05-27 18:11:55 +0100149 DensityAmbient.current.children()
George Mount932dc5d2019-10-28 08:47:10 -0700150 }
151 }
152}