1 /*
2  * Copyright (C) 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 
17 package com.android.settingslib.spa.widget.illustration
18 
19 import androidx.annotation.DrawableRes
20 import androidx.annotation.RawRes
21 import androidx.compose.ui.Modifier
22 import androidx.compose.ui.semantics.SemanticsPropertyKey
23 import androidx.compose.ui.semantics.SemanticsPropertyReceiver
24 import androidx.compose.ui.semantics.semantics
25 import androidx.compose.ui.test.SemanticsMatcher
26 import androidx.compose.ui.test.assertIsDisplayed
27 import androidx.compose.ui.test.assertIsNotDisplayed
28 import androidx.compose.ui.test.filterToOne
29 import androidx.compose.ui.test.hasAnyAncestor
30 import androidx.compose.ui.test.junit4.createComposeRule
31 import androidx.test.ext.junit.runners.AndroidJUnit4
32 import com.android.settingslib.spa.test.R
33 import org.junit.Rule
34 import org.junit.Test
35 import org.junit.runner.RunWith
36 
37 @RunWith(AndroidJUnit4::class)
38 class IllustrationTest {
39     @get:Rule
40     val composeTestRule = createComposeRule()
41 
42     private val drawableId = SemanticsPropertyKey<Int>("DrawableResId")
43     private var SemanticsPropertyReceiver.drawableId by drawableId
44 
45     @Test
46     fun image_displayed() {
47         val resId = R.drawable.accessibility_captioning_banner
48         composeTestRule.setContent {
49             Illustration(
50                 resId = resId,
51                 resourceType = ResourceType.IMAGE,
52                 modifier = Modifier.semantics { drawableId = resId }
53             )
54         }
55 
56         fun hasDrawable(@DrawableRes id: Int): SemanticsMatcher =
57             SemanticsMatcher.expectValue(drawableId, id)
58 
59         val isIllustrationNode = hasAnyAncestor(hasDrawable(resId))
60         composeTestRule.onAllNodes(hasDrawable(resId))
61             .filterToOne(isIllustrationNode)
62             .assertIsDisplayed()
63     }
64 
65     private val rawId = SemanticsPropertyKey<Int>("RawResId")
66     private var SemanticsPropertyReceiver.rawId by rawId
67 
68     @Test
69     fun empty_lottie_not_displayed() {
70         val resId = R.raw.empty
71         composeTestRule.setContent {
72             Illustration(
73                 resId = resId,
74                 resourceType = ResourceType.LOTTIE,
75                 modifier = Modifier.semantics { rawId = resId }
76             )
77         }
78 
79         fun hasRaw(@RawRes id: Int): SemanticsMatcher =
80             SemanticsMatcher.expectValue(rawId, id)
81 
82         val isIllustrationNode = hasAnyAncestor(hasRaw(resId))
83         composeTestRule.onAllNodes(hasRaw(resId))
84             .filterToOne(isIllustrationNode)
85             .assertIsNotDisplayed()
86     }
87 }
88