1 /*
2  * Copyright 2022 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 package androidx.wear.compose.material.test
17 
18 import android.os.Build
19 import androidx.compose.runtime.Composable
20 import androidx.compose.runtime.CompositionLocalProvider
21 import androidx.compose.testutils.assertAgainstGolden
22 import androidx.compose.ui.Modifier
23 import androidx.compose.ui.graphics.painter.Painter
24 import androidx.compose.ui.platform.LocalLayoutDirection
25 import androidx.compose.ui.platform.testTag
26 import androidx.compose.ui.res.painterResource
27 import androidx.compose.ui.test.captureToImage
28 import androidx.compose.ui.test.junit4.createComposeRule
29 import androidx.compose.ui.test.onNodeWithTag
30 import androidx.compose.ui.unit.LayoutDirection
31 import androidx.test.ext.junit.runners.AndroidJUnit4
32 import androidx.test.filters.MediumTest
33 import androidx.test.filters.SdkSuppress
34 import androidx.test.screenshot.AndroidXScreenshotTestRule
35 import androidx.wear.compose.material.AppCard
36 import androidx.wear.compose.material.CardDefaults
37 import androidx.wear.compose.material.SCREENSHOT_GOLDEN_PATH
38 import androidx.wear.compose.material.TEST_TAG
39 import androidx.wear.compose.material.TestIcon
40 import androidx.wear.compose.material.Text
41 import androidx.wear.compose.material.TitleCard
42 import androidx.wear.compose.material.setContentWithTheme
43 import org.junit.Rule
44 import org.junit.Test
45 import org.junit.rules.TestName
46 import org.junit.runner.RunWith
47 
48 @MediumTest
49 @RunWith(AndroidJUnit4::class)
50 @SdkSuppress(minSdkVersion = Build.VERSION_CODES.O)
51 class CardScreenshotTest {
52 
53     @get:Rule val rule = createComposeRule()
54 
55     @get:Rule val screenshotRule = AndroidXScreenshotTestRule(SCREENSHOT_GOLDEN_PATH)
56 
57     @get:Rule val testName = TestName()
58 
59     @Test
<lambda>null60     fun app_card_ltr() = verifyScreenshot(layoutDirection = LayoutDirection.Ltr) { sampleAppCard() }
61 
62     @Test
app_card_disablednull63     fun app_card_disabled() =
64         verifyScreenshot(layoutDirection = LayoutDirection.Ltr) { sampleAppCard(enabled = false) }
65 
66     @Test
<lambda>null67     fun app_card_rtl() = verifyScreenshot(layoutDirection = LayoutDirection.Rtl) { sampleAppCard() }
68 
69     @Test
title_card_ltrnull70     fun title_card_ltr() =
71         verifyScreenshot(layoutDirection = LayoutDirection.Ltr) { sampleTitleCard() }
72 
73     @Test
title_card_disablednull74     fun title_card_disabled() =
75         verifyScreenshot(layoutDirection = LayoutDirection.Ltr) { sampleTitleCard(enabled = false) }
76 
77     @Test
title_card_rtlnull78     fun title_card_rtl() =
79         verifyScreenshot(layoutDirection = LayoutDirection.Rtl) { sampleTitleCard() }
80 
81     @Test
<lambda>null82     fun title_card_image_background() = verifyScreenshot {
83         sampleTitleCard(
84             backgroundPainter =
85                 CardDefaults.imageWithScrimBackgroundPainter(
86                     backgroundImagePainter = painterResource(id = R.drawable.backgroundimage1)
87                 )
88         )
89     }
90 
91     @Composable
sampleAppCardnull92     private fun sampleAppCard(enabled: Boolean = true) {
93         AppCard(
94             enabled = enabled,
95             onClick = {},
96             appName = { Text("AppName") },
97             appImage = { TestIcon() },
98             title = { Text("AppCard") },
99             time = { Text("now") },
100             modifier = Modifier.testTag(TEST_TAG),
101         ) {
102             Text("Some body content")
103             Text("and some more body content")
104         }
105     }
106 
107     @Composable
sampleTitleCardnull108     private fun sampleTitleCard(
109         enabled: Boolean = true,
110         backgroundPainter: Painter = CardDefaults.cardBackgroundPainter()
111     ) {
112         TitleCard(
113             enabled = enabled,
114             onClick = {},
115             title = { Text("TitleCard") },
116             time = { Text("now") },
117             backgroundPainter = backgroundPainter,
118             modifier = Modifier.testTag(TEST_TAG),
119         ) {
120             Text("Some body content")
121             Text("and some more body content")
122         }
123     }
124 
verifyScreenshotnull125     private fun verifyScreenshot(
126         layoutDirection: LayoutDirection = LayoutDirection.Ltr,
127         content: @Composable () -> Unit
128     ) {
129         rule.setContentWithTheme {
130             CompositionLocalProvider(LocalLayoutDirection provides layoutDirection) { content() }
131         }
132 
133         rule
134             .onNodeWithTag(TEST_TAG)
135             .captureToImage()
136             .assertAgainstGolden(screenshotRule, testName.methodName)
137     }
138 }
139