Jelle Fresen | 283afed | 2020-06-29 16:37:52 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 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.material |
| 18 | |
| 19 | import android.os.Build |
| 20 | import androidx.test.filters.LargeTest |
| 21 | import androidx.test.filters.SdkSuppress |
| 22 | import androidx.test.screenshot.AndroidXScreenshotTestRule |
| 23 | import androidx.test.screenshot.assertAgainstGolden |
| 24 | import androidx.ui.core.Modifier |
| 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 |
Louis Pullen-Freilich | 4dc4dac | 2020-07-22 14:39:14 +0100 | [diff] [blame] | 27 | import androidx.compose.ui.graphics.Color |
Louis Pullen-Freilich | 623e405 | 2020-07-19 20:24:03 +0100 | [diff] [blame] | 28 | import androidx.compose.foundation.layout.fillMaxSize |
| 29 | import androidx.compose.foundation.layout.size |
Jelle Fresen | 283afed | 2020-06-29 16:37:52 +0100 | [diff] [blame] | 30 | import androidx.ui.test.ComposeTestRule |
| 31 | import androidx.ui.test.captureToBitmap |
| 32 | import androidx.ui.test.createComposeRule |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 33 | import androidx.ui.test.onNodeWithTag |
Louis Pullen-Freilich | a7eeb10 | 2020-07-22 17:54:24 +0100 | [diff] [blame] | 34 | import androidx.compose.ui.unit.dp |
Jelle Fresen | 283afed | 2020-06-29 16:37:52 +0100 | [diff] [blame] | 35 | import org.junit.Rule |
| 36 | import org.junit.Test |
| 37 | import org.junit.runner.RunWith |
| 38 | import org.junit.runners.JUnit4 |
| 39 | |
| 40 | @LargeTest |
| 41 | @RunWith(JUnit4::class) |
| 42 | @SdkSuppress(minSdkVersion = Build.VERSION_CODES.O) |
| 43 | class DrawerScreenshotTest { |
| 44 | |
| 45 | @get:Rule |
| 46 | val composeTestRule = createComposeRule() |
| 47 | |
| 48 | @get:Rule |
| 49 | val screenshotRule = AndroidXScreenshotTestRule(GOLDEN_MATERIAL) |
| 50 | |
Calin Tataru | dcde340 | 2020-07-08 11:44:22 +0100 | [diff] [blame] | 51 | private fun ComposeTestRule.setBottomDrawer(drawerState: BottomDrawerState) { |
Jelle Fresen | 283afed | 2020-06-29 16:37:52 +0100 | [diff] [blame] | 52 | setMaterialContent { |
| 53 | Box(Modifier.size(10.dp, 100.dp).testTag("container")) { |
| 54 | BottomDrawerLayout( |
| 55 | drawerState = drawerState, |
| 56 | onStateChange = {}, |
| 57 | drawerContent = { Box(Modifier.fillMaxSize(), backgroundColor = Color.Red) }, |
| 58 | bodyContent = { Box(Modifier.fillMaxSize(), backgroundColor = Color.Yellow) } |
| 59 | ) |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | private fun ComposeTestRule.setModalDrawer(drawerState: DrawerState) { |
| 65 | setMaterialContent { |
| 66 | Box(Modifier.size(100.dp, 10.dp).testTag("container")) { |
| 67 | ModalDrawerLayout( |
| 68 | drawerState = drawerState, |
| 69 | onStateChange = {}, |
| 70 | drawerContent = { Box(Modifier.fillMaxSize(), backgroundColor = Color.Red) }, |
| 71 | bodyContent = { Box(Modifier.fillMaxSize(), backgroundColor = Color.Yellow) } |
| 72 | ) |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | @Test |
| 78 | fun bottomDrawer_closed() { |
Calin Tataru | dcde340 | 2020-07-08 11:44:22 +0100 | [diff] [blame] | 79 | composeTestRule.setBottomDrawer(BottomDrawerState.Closed) |
Jelle Fresen | 283afed | 2020-06-29 16:37:52 +0100 | [diff] [blame] | 80 | assertScreenshotAgainstGolden("bottomDrawer_closed") |
| 81 | } |
| 82 | |
| 83 | @Test |
| 84 | fun modalDrawer_closed() { |
| 85 | composeTestRule.setModalDrawer(DrawerState.Closed) |
| 86 | assertScreenshotAgainstGolden("modalDrawer_closed") |
| 87 | } |
| 88 | |
| 89 | @Test |
| 90 | fun bottomDrawer_opened() { |
Calin Tataru | dcde340 | 2020-07-08 11:44:22 +0100 | [diff] [blame] | 91 | composeTestRule.setBottomDrawer(BottomDrawerState.Opened) |
Jelle Fresen | 283afed | 2020-06-29 16:37:52 +0100 | [diff] [blame] | 92 | assertScreenshotAgainstGolden("bottomDrawer_opened") |
| 93 | } |
| 94 | |
| 95 | @Test |
| 96 | fun modalDrawer_opened() { |
| 97 | composeTestRule.setModalDrawer(DrawerState.Opened) |
| 98 | assertScreenshotAgainstGolden("modalDrawer_opened") |
| 99 | } |
| 100 | |
| 101 | private fun assertScreenshotAgainstGolden(goldenName: String) { |
Filip Pavlis | 659ea72 | 2020-07-13 14:14:32 +0100 | [diff] [blame] | 102 | onNodeWithTag("container") |
Jelle Fresen | 283afed | 2020-06-29 16:37:52 +0100 | [diff] [blame] | 103 | .captureToBitmap() |
| 104 | .assertAgainstGolden(screenshotRule, goldenName) |
| 105 | } |
| 106 | } |