Filip Pavlis | e86f9bd | 2019-12-02 13:11:21 +0000 | [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 | |
| 17 | package androidx.ui.test |
| 18 | |
Filip Pavlis | 0f18f8a | 2020-06-11 16:58:01 +0100 | [diff] [blame] | 19 | import android.os.Build |
Filip Pavlis | 26c32d6 | 2020-05-28 18:55:45 +0100 | [diff] [blame] | 20 | import androidx.activity.ComponentActivity |
Filip Pavlis | 607cc41 | 2020-06-11 21:56:49 +0100 | [diff] [blame] | 21 | import androidx.test.ext.junit.rules.ActivityScenarioRule |
Filip Pavlis | e86f9bd | 2019-12-02 13:11:21 +0000 | [diff] [blame] | 22 | import androidx.test.filters.MediumTest |
Filip Pavlis | 0f18f8a | 2020-06-11 16:58:01 +0100 | [diff] [blame] | 23 | import androidx.test.filters.SdkSuppress |
Matvei Malkov | 75c42d0 | 2020-03-30 16:03:11 +0100 | [diff] [blame] | 24 | import androidx.ui.core.Modifier |
Alexandre Elias | 5c8252f | 2020-05-27 15:52:40 -0700 | [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.Box |
| 27 | import androidx.compose.foundation.background |
Louis Pullen-Freilich | 4dc4dac | 2020-07-22 14:39:14 +0100 | [diff] [blame^] | 28 | import androidx.compose.ui.graphics.Color |
Louis Pullen-Freilich | 623e405 | 2020-07-19 20:24:03 +0100 | [diff] [blame] | 29 | import androidx.compose.foundation.layout.Column |
| 30 | import androidx.compose.foundation.layout.Row |
| 31 | import androidx.compose.foundation.layout.fillMaxSize |
| 32 | import androidx.compose.foundation.layout.padding |
| 33 | import androidx.compose.foundation.layout.preferredSize |
Filip Pavlis | 26c32d6 | 2020-05-28 18:55:45 +0100 | [diff] [blame] | 34 | import androidx.ui.test.android.AndroidComposeTestRule |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 35 | import androidx.ui.unit.IntOffset |
| 36 | import androidx.ui.unit.IntSize |
Filip Pavlis | e86f9bd | 2019-12-02 13:11:21 +0000 | [diff] [blame] | 37 | import com.google.common.truth.Truth.assertThat |
| 38 | import org.junit.Rule |
| 39 | import org.junit.Test |
| 40 | import org.junit.runner.RunWith |
Filip Pavlis | 26c32d6 | 2020-05-28 18:55:45 +0100 | [diff] [blame] | 41 | import org.junit.runners.Parameterized |
Filip Pavlis | e86f9bd | 2019-12-02 13:11:21 +0000 | [diff] [blame] | 42 | |
| 43 | @MediumTest |
Filip Pavlis | 26c32d6 | 2020-05-28 18:55:45 +0100 | [diff] [blame] | 44 | @RunWith(Parameterized::class) |
Filip Pavlis | 0f18f8a | 2020-06-11 16:58:01 +0100 | [diff] [blame] | 45 | @SdkSuppress(minSdkVersion = Build.VERSION_CODES.O) |
Filip Pavlis | 26c32d6 | 2020-05-28 18:55:45 +0100 | [diff] [blame] | 46 | class 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 Pavlis | e86f9bd | 2019-12-02 13:11:21 +0000 | [diff] [blame] | 59 | |
| 60 | @get:Rule |
Aurimas Liutikas | 506ab64d | 2020-06-02 14:37:26 -0700 | [diff] [blame] | 61 | val composeTestRule = AndroidComposeTestRule( |
Filip Pavlis | 607cc41 | 2020-06-11 21:56:49 +0100 | [diff] [blame] | 62 | ActivityScenarioRule(config.activityClass) |
Aurimas Liutikas | 506ab64d | 2020-06-02 14:37:26 -0700 | [diff] [blame] | 63 | ) |
Filip Pavlis | e86f9bd | 2019-12-02 13:11:21 +0000 | [diff] [blame] | 64 | |
| 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 Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 82 | onNodeWithTag(tag11) |
Filip Pavlis | e86f9bd | 2019-12-02 13:11:21 +0000 | [diff] [blame] | 83 | .captureToBitmap() |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 84 | .assertPixels(expectedSize = IntSize(100, 50)) { |
Filip Pavlis | e86f9bd | 2019-12-02 13:11:21 +0000 | [diff] [blame] | 85 | calledCount++ |
| 86 | color11 |
| 87 | } |
| 88 | assertThat(calledCount).isEqualTo(100 * 50) |
| 89 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 90 | onNodeWithTag(tag12) |
Filip Pavlis | e86f9bd | 2019-12-02 13:11:21 +0000 | [diff] [blame] | 91 | .captureToBitmap() |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 92 | .assertPixels(expectedSize = IntSize(100, 50)) { |
Filip Pavlis | e86f9bd | 2019-12-02 13:11:21 +0000 | [diff] [blame] | 93 | color12 |
| 94 | } |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 95 | onNodeWithTag(tag21) |
Filip Pavlis | e86f9bd | 2019-12-02 13:11:21 +0000 | [diff] [blame] | 96 | .captureToBitmap() |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 97 | .assertPixels(expectedSize = IntSize(100, 50)) { |
Filip Pavlis | e86f9bd | 2019-12-02 13:11:21 +0000 | [diff] [blame] | 98 | color21 |
| 99 | } |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 100 | onNodeWithTag(tag22) |
Filip Pavlis | e86f9bd | 2019-12-02 13:11:21 +0000 | [diff] [blame] | 101 | .captureToBitmap() |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 102 | .assertPixels(expectedSize = IntSize(100, 50)) { |
Filip Pavlis | e86f9bd | 2019-12-02 13:11:21 +0000 | [diff] [blame] | 103 | color22 |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | @Test |
| 108 | fun captureRootContainer_checkSizeAndColors() { |
| 109 | composeCheckerboard() |
| 110 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 111 | onNodeWithTag(rootTag) |
Filip Pavlis | e86f9bd | 2019-12-02 13:11:21 +0000 | [diff] [blame] | 112 | .captureToBitmap() |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 113 | .assertPixels(expectedSize = IntSize(200, 100)) { |
| 114 | if (it.y >= 100 || it.x >= 200) { |
Filip Pavlis | e86f9bd | 2019-12-02 13:11:21 +0000 | [diff] [blame] | 115 | throw AssertionError("$it is out of range!") |
| 116 | } |
| 117 | expectedColorProvider(it) |
| 118 | } |
| 119 | } |
| 120 | |
Filip Pavlis | e86f9bd | 2019-12-02 13:11:21 +0000 | [diff] [blame] | 121 | @Test(expected = AssertionError::class) |
| 122 | fun assertWrongColor_expectException() { |
| 123 | composeCheckerboard() |
| 124 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 125 | onNodeWithTag(tag11) |
Filip Pavlis | e86f9bd | 2019-12-02 13:11:21 +0000 | [diff] [blame] | 126 | .captureToBitmap() |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 127 | .assertPixels(expectedSize = IntSize(100, 50)) { |
Filip Pavlis | e86f9bd | 2019-12-02 13:11:21 +0000 | [diff] [blame] | 128 | color22 // Assuming wrong color |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | @Test(expected = AssertionError::class) |
| 133 | fun assertWrongSize_expectException() { |
| 134 | composeCheckerboard() |
| 135 | |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 136 | onNodeWithTag(tag11) |
Filip Pavlis | e86f9bd | 2019-12-02 13:11:21 +0000 | [diff] [blame] | 137 | .captureToBitmap() |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 138 | .assertPixels(expectedSize = IntSize(10, 10)) { |
Filip Pavlis | e86f9bd | 2019-12-02 13:11:21 +0000 | [diff] [blame] | 139 | color21 |
| 140 | } |
| 141 | } |
| 142 | |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 143 | private fun expectedColorProvider(pos: IntOffset): Color { |
| 144 | if (pos.y < 50) { |
| 145 | if (pos.x < 100) { |
Filip Pavlis | e86f9bd | 2019-12-02 13:11:21 +0000 | [diff] [blame] | 146 | return color11 |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 147 | } else if (pos.x < 200) { |
Filip Pavlis | e86f9bd | 2019-12-02 13:11:21 +0000 | [diff] [blame] | 148 | return color12 |
| 149 | } |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 150 | } else if (pos.y < 100) { |
| 151 | if (pos.x < 100) { |
Filip Pavlis | e86f9bd | 2019-12-02 13:11:21 +0000 | [diff] [blame] | 152 | return color21 |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 153 | } else if (pos.x < 200) { |
Filip Pavlis | e86f9bd | 2019-12-02 13:11:21 +0000 | [diff] [blame] | 154 | return color22 |
| 155 | } |
| 156 | } |
| 157 | return colorBg |
| 158 | } |
| 159 | |
| 160 | private fun composeCheckerboard() { |
Andrey Kulikov | 47f0e1a | 2020-01-28 13:52:46 +0000 | [diff] [blame] | 161 | with(composeTestRule.density) { |
Filip Pavlis | e86f9bd | 2019-12-02 13:11:21 +0000 | [diff] [blame] | 162 | composeTestRule.setContent { |
Andrey Kulikov | 1526ea5 | 2020-04-07 18:43:27 +0100 | [diff] [blame] | 163 | Box(Modifier.fillMaxSize(), backgroundColor = colorBg) { |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 164 | Box(Modifier.padding(top = 20.toDp()), backgroundColor = colorBg) { |
Filip Pavlis | 26c32d6 | 2020-05-28 18:55:45 +0100 | [diff] [blame] | 165 | Column(Modifier.testTag(rootTag)) { |
| 166 | Row { |
Matvei Malkov | bf014c1 | 2020-07-09 17:40:10 +0100 | [diff] [blame] | 167 | Box( |
| 168 | Modifier |
| 169 | .testTag(tag11) |
| 170 | .preferredSize(100.toDp(), 50.toDp()) |
| 171 | .background(color = color11) |
Filip Pavlis | 26c32d6 | 2020-05-28 18:55:45 +0100 | [diff] [blame] | 172 | ) |
| 173 | Box(Modifier |
| 174 | .testTag(tag12) |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 175 | .preferredSize(100.toDp(), 50.toDp()) |
Matvei Malkov | bf014c1 | 2020-07-09 17:40:10 +0100 | [diff] [blame] | 176 | .background(color12) |
Filip Pavlis | 26c32d6 | 2020-05-28 18:55:45 +0100 | [diff] [blame] | 177 | ) |
| 178 | } |
| 179 | Row { |
| 180 | Box(Modifier |
| 181 | .testTag(tag21) |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 182 | .preferredSize(100.toDp(), 50.toDp()) |
Matvei Malkov | bf014c1 | 2020-07-09 17:40:10 +0100 | [diff] [blame] | 183 | .background(color21) |
Filip Pavlis | 26c32d6 | 2020-05-28 18:55:45 +0100 | [diff] [blame] | 184 | ) |
| 185 | Box(Modifier |
| 186 | .testTag(tag22) |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 187 | .preferredSize(100.toDp(), 50.toDp()) |
Matvei Malkov | bf014c1 | 2020-07-09 17:40:10 +0100 | [diff] [blame] | 188 | .background(color22) |
Filip Pavlis | 26c32d6 | 2020-05-28 18:55:45 +0100 | [diff] [blame] | 189 | ) |
| 190 | } |
Filip Pavlis | e86f9bd | 2019-12-02 13:11:21 +0000 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | } |