[go: nahoru, domu]

blob: be8fcee8e58ca016b6737d9e72670ea64cc540dc [file] [log] [blame]
Filip Pavlise86f9bd2019-12-02 13:11:21 +00001/*
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
17package androidx.ui.test
18
Filip Pavlis0f18f8a2020-06-11 16:58:01 +010019import android.os.Build
Filip Pavlis26c32d62020-05-28 18:55:45 +010020import androidx.activity.ComponentActivity
Filip Pavlis607cc412020-06-11 21:56:49 +010021import androidx.test.ext.junit.rules.ActivityScenarioRule
Filip Pavlise86f9bd2019-12-02 13:11:21 +000022import androidx.test.filters.MediumTest
Filip Pavlis0f18f8a2020-06-11 16:58:01 +010023import androidx.test.filters.SdkSuppress
Matvei Malkov75c42d02020-03-30 16:03:11 +010024import androidx.ui.core.Modifier
Alexandre Elias5c8252f2020-05-27 15:52:40 -070025import androidx.ui.core.testTag
Louis Pullen-Freilichddda7be2020-07-17 18:28:12 +010026import androidx.compose.foundation.Box
27import androidx.compose.foundation.background
Louis Pullen-Freilich4dc4dac2020-07-22 14:39:14 +010028import androidx.compose.ui.graphics.Color
Louis Pullen-Freilich623e4052020-07-19 20:24:03 +010029import androidx.compose.foundation.layout.Column
30import androidx.compose.foundation.layout.Row
31import androidx.compose.foundation.layout.fillMaxSize
32import androidx.compose.foundation.layout.padding
33import androidx.compose.foundation.layout.preferredSize
Filip Pavlis26c32d62020-05-28 18:55:45 +010034import androidx.ui.test.android.AndroidComposeTestRule
George Mount8f237572020-04-30 12:08:30 -070035import androidx.ui.unit.IntOffset
36import androidx.ui.unit.IntSize
Filip Pavlise86f9bd2019-12-02 13:11:21 +000037import com.google.common.truth.Truth.assertThat
38import org.junit.Rule
39import org.junit.Test
40import org.junit.runner.RunWith
Filip Pavlis26c32d62020-05-28 18:55:45 +010041import org.junit.runners.Parameterized
Filip Pavlise86f9bd2019-12-02 13:11:21 +000042
43@MediumTest
Filip Pavlis26c32d62020-05-28 18:55:45 +010044@RunWith(Parameterized::class)
Filip Pavlis0f18f8a2020-06-11 16:58:01 +010045@SdkSuppress(minSdkVersion = Build.VERSION_CODES.O)
Filip Pavlis26c32d62020-05-28 18:55:45 +010046class BitmapCapturingTest(val config: TestConfig) {
47 data class TestConfig(
48 val activityClass: Class<out ComponentActivity>
49 )
50
51 companion object {
52 @JvmStatic
53 @Parameterized.Parameters(name = "{0}")
54 fun createTestSet(): List<TestConfig> = listOf(
55 TestConfig(ComponentActivity::class.java),
56 TestConfig(ActivityWithActionBar::class.java)
57 )
58 }
Filip Pavlise86f9bd2019-12-02 13:11:21 +000059
60 @get:Rule
Aurimas Liutikas506ab64d2020-06-02 14:37:26 -070061 val composeTestRule = AndroidComposeTestRule(
Filip Pavlis607cc412020-06-11 21:56:49 +010062 ActivityScenarioRule(config.activityClass)
Aurimas Liutikas506ab64d2020-06-02 14:37:26 -070063 )
Filip Pavlise86f9bd2019-12-02 13:11:21 +000064
65 private val rootTag = "Root"
66 private val tag11 = "Rect11"
67 private val tag12 = "Rect12"
68 private val tag21 = "Rect21"
69 private val tag22 = "Rect22"
70
71 private val color11 = Color.Red
72 private val color12 = Color.Blue
73 private val color21 = Color.Green
74 private val color22 = Color.Yellow
75 private val colorBg = Color.Black
76
77 @Test
78 fun captureIndividualRects_checkSizeAndColors() {
79 composeCheckerboard()
80
81 var calledCount = 0
Filip Pavlis659ea722020-07-13 14:14:32 +010082 onNodeWithTag(tag11)
Filip Pavlise86f9bd2019-12-02 13:11:21 +000083 .captureToBitmap()
George Mount8f237572020-04-30 12:08:30 -070084 .assertPixels(expectedSize = IntSize(100, 50)) {
Filip Pavlise86f9bd2019-12-02 13:11:21 +000085 calledCount++
86 color11
87 }
88 assertThat(calledCount).isEqualTo(100 * 50)
89
Filip Pavlis659ea722020-07-13 14:14:32 +010090 onNodeWithTag(tag12)
Filip Pavlise86f9bd2019-12-02 13:11:21 +000091 .captureToBitmap()
George Mount8f237572020-04-30 12:08:30 -070092 .assertPixels(expectedSize = IntSize(100, 50)) {
Filip Pavlise86f9bd2019-12-02 13:11:21 +000093 color12
94 }
Filip Pavlis659ea722020-07-13 14:14:32 +010095 onNodeWithTag(tag21)
Filip Pavlise86f9bd2019-12-02 13:11:21 +000096 .captureToBitmap()
George Mount8f237572020-04-30 12:08:30 -070097 .assertPixels(expectedSize = IntSize(100, 50)) {
Filip Pavlise86f9bd2019-12-02 13:11:21 +000098 color21
99 }
Filip Pavlis659ea722020-07-13 14:14:32 +0100100 onNodeWithTag(tag22)
Filip Pavlise86f9bd2019-12-02 13:11:21 +0000101 .captureToBitmap()
George Mount8f237572020-04-30 12:08:30 -0700102 .assertPixels(expectedSize = IntSize(100, 50)) {
Filip Pavlise86f9bd2019-12-02 13:11:21 +0000103 color22
104 }
105 }
106
107 @Test
108 fun captureRootContainer_checkSizeAndColors() {
109 composeCheckerboard()
110
Filip Pavlis659ea722020-07-13 14:14:32 +0100111 onNodeWithTag(rootTag)
Filip Pavlise86f9bd2019-12-02 13:11:21 +0000112 .captureToBitmap()
George Mount8f237572020-04-30 12:08:30 -0700113 .assertPixels(expectedSize = IntSize(200, 100)) {
114 if (it.y >= 100 || it.x >= 200) {
Filip Pavlise86f9bd2019-12-02 13:11:21 +0000115 throw AssertionError("$it is out of range!")
116 }
117 expectedColorProvider(it)
118 }
119 }
120
Filip Pavlise86f9bd2019-12-02 13:11:21 +0000121 @Test(expected = AssertionError::class)
122 fun assertWrongColor_expectException() {
123 composeCheckerboard()
124
Filip Pavlis659ea722020-07-13 14:14:32 +0100125 onNodeWithTag(tag11)
Filip Pavlise86f9bd2019-12-02 13:11:21 +0000126 .captureToBitmap()
George Mount8f237572020-04-30 12:08:30 -0700127 .assertPixels(expectedSize = IntSize(100, 50)) {
Filip Pavlise86f9bd2019-12-02 13:11:21 +0000128 color22 // Assuming wrong color
129 }
130 }
131
132 @Test(expected = AssertionError::class)
133 fun assertWrongSize_expectException() {
134 composeCheckerboard()
135
Filip Pavlis659ea722020-07-13 14:14:32 +0100136 onNodeWithTag(tag11)
Filip Pavlise86f9bd2019-12-02 13:11:21 +0000137 .captureToBitmap()
George Mount8f237572020-04-30 12:08:30 -0700138 .assertPixels(expectedSize = IntSize(10, 10)) {
Filip Pavlise86f9bd2019-12-02 13:11:21 +0000139 color21
140 }
141 }
142
George Mount8f237572020-04-30 12:08:30 -0700143 private fun expectedColorProvider(pos: IntOffset): Color {
144 if (pos.y < 50) {
145 if (pos.x < 100) {
Filip Pavlise86f9bd2019-12-02 13:11:21 +0000146 return color11
George Mount8f237572020-04-30 12:08:30 -0700147 } else if (pos.x < 200) {
Filip Pavlise86f9bd2019-12-02 13:11:21 +0000148 return color12
149 }
George Mount8f237572020-04-30 12:08:30 -0700150 } else if (pos.y < 100) {
151 if (pos.x < 100) {
Filip Pavlise86f9bd2019-12-02 13:11:21 +0000152 return color21
George Mount8f237572020-04-30 12:08:30 -0700153 } else if (pos.x < 200) {
Filip Pavlise86f9bd2019-12-02 13:11:21 +0000154 return color22
155 }
156 }
157 return colorBg
158 }
159
160 private fun composeCheckerboard() {
Andrey Kulikov47f0e1a2020-01-28 13:52:46 +0000161 with(composeTestRule.density) {
Filip Pavlise86f9bd2019-12-02 13:11:21 +0000162 composeTestRule.setContent {
Andrey Kulikov1526ea52020-04-07 18:43:27 +0100163 Box(Modifier.fillMaxSize(), backgroundColor = colorBg) {
George Mount8f237572020-04-30 12:08:30 -0700164 Box(Modifier.padding(top = 20.toDp()), backgroundColor = colorBg) {
Filip Pavlis26c32d62020-05-28 18:55:45 +0100165 Column(Modifier.testTag(rootTag)) {
166 Row {
Matvei Malkovbf014c12020-07-09 17:40:10 +0100167 Box(
168 Modifier
169 .testTag(tag11)
170 .preferredSize(100.toDp(), 50.toDp())
171 .background(color = color11)
Filip Pavlis26c32d62020-05-28 18:55:45 +0100172 )
173 Box(Modifier
174 .testTag(tag12)
George Mount8f237572020-04-30 12:08:30 -0700175 .preferredSize(100.toDp(), 50.toDp())
Matvei Malkovbf014c12020-07-09 17:40:10 +0100176 .background(color12)
Filip Pavlis26c32d62020-05-28 18:55:45 +0100177 )
178 }
179 Row {
180 Box(Modifier
181 .testTag(tag21)
George Mount8f237572020-04-30 12:08:30 -0700182 .preferredSize(100.toDp(), 50.toDp())
Matvei Malkovbf014c12020-07-09 17:40:10 +0100183 .background(color21)
Filip Pavlis26c32d62020-05-28 18:55:45 +0100184 )
185 Box(Modifier
186 .testTag(tag22)
George Mount8f237572020-04-30 12:08:30 -0700187 .preferredSize(100.toDp(), 50.toDp())
Matvei Malkovbf014c12020-07-09 17:40:10 +0100188 .background(color22)
Filip Pavlis26c32d62020-05-28 18:55:45 +0100189 )
190 }
Filip Pavlise86f9bd2019-12-02 13:11:21 +0000191 }
192 }
193 }
194 }
195 }
196 }
197}