[go: nahoru, domu]

blob: 313c5995722fc82a4b82285cf63bfe6b6fb782c8 [file] [log] [blame]
Jelle Fresen283afed2020-06-29 16:37:52 +01001/*
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
17package androidx.ui.material
18
19import android.os.Build
20import androidx.test.filters.LargeTest
21import androidx.test.filters.SdkSuppress
22import androidx.test.screenshot.AndroidXScreenshotTestRule
23import androidx.test.screenshot.assertAgainstGolden
24import androidx.ui.core.Modifier
25import androidx.ui.core.testTag
Louis Pullen-Freilichddda7be2020-07-17 18:28:12 +010026import androidx.compose.foundation.Box
Louis Pullen-Freilich4dc4dac2020-07-22 14:39:14 +010027import androidx.compose.ui.graphics.Color
Louis Pullen-Freilich623e4052020-07-19 20:24:03 +010028import androidx.compose.foundation.layout.fillMaxSize
29import androidx.compose.foundation.layout.size
Jelle Fresen283afed2020-06-29 16:37:52 +010030import androidx.ui.test.ComposeTestRule
31import androidx.ui.test.captureToBitmap
32import androidx.ui.test.createComposeRule
Filip Pavlis659ea722020-07-13 14:14:32 +010033import androidx.ui.test.onNodeWithTag
Louis Pullen-Freilicha7eeb102020-07-22 17:54:24 +010034import androidx.compose.ui.unit.dp
Jelle Fresen283afed2020-06-29 16:37:52 +010035import org.junit.Rule
36import org.junit.Test
37import org.junit.runner.RunWith
38import org.junit.runners.JUnit4
39
40@LargeTest
41@RunWith(JUnit4::class)
42@SdkSuppress(minSdkVersion = Build.VERSION_CODES.O)
43class DrawerScreenshotTest {
44
45 @get:Rule
46 val composeTestRule = createComposeRule()
47
48 @get:Rule
49 val screenshotRule = AndroidXScreenshotTestRule(GOLDEN_MATERIAL)
50
Calin Tatarudcde3402020-07-08 11:44:22 +010051 private fun ComposeTestRule.setBottomDrawer(drawerState: BottomDrawerState) {
Jelle Fresen283afed2020-06-29 16:37:52 +010052 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 Tatarudcde3402020-07-08 11:44:22 +010079 composeTestRule.setBottomDrawer(BottomDrawerState.Closed)
Jelle Fresen283afed2020-06-29 16:37:52 +010080 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 Tatarudcde3402020-07-08 11:44:22 +010091 composeTestRule.setBottomDrawer(BottomDrawerState.Opened)
Jelle Fresen283afed2020-06-29 16:37:52 +010092 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 Pavlis659ea722020-07-13 14:14:32 +0100102 onNodeWithTag("container")
Jelle Fresen283afed2020-06-29 16:37:52 +0100103 .captureToBitmap()
104 .assertAgainstGolden(screenshotRule, goldenName)
105 }
106}