1 /*
2  * Copyright (C) 2023 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.intentresolver.contentpreview
18 
19 import android.view.LayoutInflater
20 import android.view.View
21 import android.view.ViewGroup
22 import android.widget.TextView
23 import androidx.test.ext.junit.runners.AndroidJUnit4
24 import androidx.test.platform.app.InstrumentationRegistry
25 import com.android.intentresolver.ContentTypeHint
26 import com.android.intentresolver.R
27 import com.android.intentresolver.widget.ActionRow
28 import com.google.common.truth.Truth.assertThat
29 import java.util.function.Consumer
30 import kotlin.coroutines.EmptyCoroutineContext
31 import kotlinx.coroutines.test.TestScope
32 import kotlinx.coroutines.test.UnconfinedTestDispatcher
33 import org.junit.Test
34 import org.junit.runner.RunWith
35 import org.mockito.kotlin.doReturn
36 import org.mockito.kotlin.mock
37 
38 @RunWith(AndroidJUnit4::class)
39 class TextContentPreviewUiTest {
40     private val text = "Shared Text"
41     private val title = "Preview Title"
42     private val albumHeadline = "Album headline"
43     private val testScope = TestScope(EmptyCoroutineContext + UnconfinedTestDispatcher())
44     private val actionFactory =
45         object : ChooserContentPreviewUi.ActionFactory {
getEditButtonRunnablenull46             override fun getEditButtonRunnable(): Runnable? = null
47 
48             override fun getCopyButtonRunnable(): Runnable? = null
49 
50             override fun createCustomActions(): List<ActionRow.Action> = emptyList()
51 
52             override fun getModifyShareAction(): ActionRow.Action? = null
53 
54             override fun getExcludeSharedTextAction(): Consumer<Boolean> = Consumer<Boolean> {}
55         }
56     private val imageLoader = mock<ImageLoader>()
57     private val headlineGenerator =
<lambda>null58         mock<HeadlineGenerator> {
59             on { getTextHeadline(text) } doReturn text
60             on { getAlbumHeadline() } doReturn albumHeadline
61         }
62     private val testMetadataText: CharSequence = "Test metadata text"
63 
64     private val context
65         get() = InstrumentationRegistry.getInstrumentation().context
66 
67     private val testSubject =
68         TextContentPreviewUi(
69             testScope,
70             text,
71             title,
72             testMetadataText,
73             /*previewThumbnail=*/ null,
74             actionFactory,
75             imageLoader,
76             headlineGenerator,
77             ContentTypeHint.NONE,
78         )
79 
80     @Test
test_display_headlineIsDisplayednull81     fun test_display_headlineIsDisplayed() {
82         val layoutInflater = LayoutInflater.from(context)
83         val gridLayout =
84             layoutInflater.inflate(R.layout.chooser_grid_scrollable_preview, null, false)
85                 as ViewGroup
86         val headlineRow = gridLayout.requireViewById<View>(R.id.chooser_headline_row_container)
87 
88         val previewView =
89             testSubject.display(
90                 context.resources,
91                 layoutInflater,
92                 gridLayout,
93                 headlineRow,
94             )
95 
96         assertThat(previewView).isNotNull()
97         val headlineView = headlineRow.findViewById<TextView>(R.id.headline)
98         assertThat(headlineView).isNotNull()
99         assertThat(headlineView?.text).isEqualTo(text)
100         val metadataView = headlineRow.findViewById<TextView>(R.id.metadata)
101         assertThat(metadataView).isNotNull()
102         assertThat(metadataView?.text).isEqualTo(testMetadataText)
103     }
104 
105     @Test
test_display_albumHeadlineOverridenull106     fun test_display_albumHeadlineOverride() {
107         val layoutInflater = LayoutInflater.from(context)
108         val gridLayout =
109             layoutInflater.inflate(R.layout.chooser_grid_scrollable_preview, null, false)
110                 as ViewGroup
111         val headlineRow = gridLayout.requireViewById<View>(R.id.chooser_headline_row_container)
112 
113         val albumSubject =
114             TextContentPreviewUi(
115                 testScope,
116                 text,
117                 title,
118                 testMetadataText,
119                 /*previewThumbnail=*/ null,
120                 actionFactory,
121                 imageLoader,
122                 headlineGenerator,
123                 ContentTypeHint.ALBUM,
124             )
125 
126         val previewView =
127             albumSubject.display(
128                 context.resources,
129                 layoutInflater,
130                 gridLayout,
131                 headlineRow,
132             )
133 
134         assertThat(previewView).isNotNull()
135         val headlineView = headlineRow.findViewById<TextView>(R.id.headline)
136         assertThat(headlineView).isNotNull()
137         assertThat(headlineView?.text).isEqualTo(albumHeadline)
138         val metadataView = headlineRow.findViewById<TextView>(R.id.metadata)
139         assertThat(metadataView).isNotNull()
140         assertThat(metadataView?.text).isEqualTo(testMetadataText)
141     }
142 }
143