1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package social.grain
import androidx.compose.foundation.text.input.TextFieldState
import androidx.compose.foundation.text.input.setTextAndPlaceCursorAtEnd
import androidx.compose.material3.Text
import androidx.compose.ui.test.junit4.v2.createComposeRule
import androidx.compose.ui.test.onNodeWithContentDescription
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.test.ext.junit.runners.AndroidJUnit4
import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.put
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import social.grain.data.location.PlaceResult
import social.grain.data.model.Facet
import social.grain.feature.gallery.GalleryCommentSheet
import social.grain.feature.gallery.GalleryCommentsViewModel
import social.grain.support.Fixtures
import social.grain.support.Harness
import social.grain.support.MainDispatcherRule
import social.grain.support.awaitText
import social.grain.support.json
import social.grain.ui.components.LocationPicker
import social.grain.ui.components.RichText
import social.grain.ui.theme.GrainTheme
/**
* The shared pieces screens are built out of.
*
* Each is used from several places, so a regression here is a regression
* everywhere at once — and none of them owns any state a view model test could
* reach.
*/
@RunWith(AndroidJUnit4::class)
class ComponentsTest {
@get:Rule
val mainDispatcher = MainDispatcherRule()
@get:Rule
val composeRule = createComposeRule()
private lateinit var harness: Harness
@Before
fun setUp() {
harness = Harness()
}
@After
fun tearDown() = harness.tearDown()
// MARK: - RichText
private fun facet(start: Int, end: Int, type: String, key: String, value: String) = Facet(
index = Facet.ByteSlice(start, end),
features = listOf(
buildJsonObject {
put("\$type", type)
put(key, value)
},
),
)
@Test
fun plainTextWithNoFacetsIsJustText() {
composeRule.setContent { GrainTheme { RichText(text = "Six frames from a grey morning") } }
composeRule.awaitText("Six frames from a grey morning")
}
@Test
fun aHashtagFacetIsTappableAndReportsItsTag() {
var tapped: String? = null
composeRule.setContent {
GrainTheme {
RichText(
text = "Shot in #fog",
facets = listOf(
facet(9, 13, "app.bsky.richtext.facet#tag", "tag", "fog"),
),
onHashtagTap = { tapped = it },
)
}
}
composeRule.awaitText("Shot in #fog")
composeRule.onNodeWithText("Shot in #fog").performClick()
}
@Test
fun aMentionFacetRendersAlongsideTheTextItAnnotates() {
composeRule.setContent {
GrainTheme {
RichText(
text = "With @alice.test",
facets = listOf(
facet(5, 16, "app.bsky.richtext.facet#mention", "did", "did:plc:alice"),
),
onMentionTap = {},
)
}
}
composeRule.awaitText("With @alice.test")
}
@Test
fun aLinkFacetRendersWithoutNeedingAHandler() {
composeRule.setContent {
GrainTheme {
RichText(
text = "See https://grain.social",
facets = listOf(
facet(4, 24, "app.bsky.richtext.facet#link", "uri", "https://grain.social"),
),
)
}
}
composeRule.awaitText("See https://grain.social")
}
@Test
fun aFacetTypeThisBuildDoesNotKnowIsDrawnAsPlainText() {
composeRule.setContent {
GrainTheme {
RichText(
text = "Something new",
facets = listOf(
facet(0, 9, "app.bsky.richtext.facet#somethingElse", "x", "y"),
),
)
}
}
composeRule.awaitText("Something new")
}
// MARK: - LocationPicker
private fun place(name: String, context: String? = "San Francisco") = PlaceResult(
name = name,
context = context,
latitude = 37.76,
longitude = -122.41,
address = null,
)
@Test
fun thePickerInvitesASearchWhenNothingIsChosen() {
composeRule.setContent {
GrainTheme {
LocationPicker(
queryState = TextFieldState(),
results = emptyList(),
isSearching = false,
selected = null,
suggestion = null,
onSelect = {},
onClear = {},
)
}
}
composeRule.awaitText("Location")
composeRule.onNodeWithText("Search for a place").assertExists()
}
@Test
fun resultsAreListedAndReportTheOneTapped() {
var chosen: PlaceResult? = null
composeRule.setContent {
GrainTheme {
LocationPicker(
queryState = TextFieldState().apply { setTextAndPlaceCursorAtEnd("mission") },
results = listOf(place("Mission District"), place("Mission Bay")),
isSearching = false,
selected = null,
suggestion = null,
onSelect = { chosen = it },
onClear = {},
)
}
}
composeRule.awaitText("Mission District")
composeRule.onNodeWithText("Mission Bay").assertExists()
composeRule.onNodeWithText("Mission District").performClick()
assertEquals("Mission District", chosen?.name)
}
@Test
fun aPlaceReadOffThePhotoIsOfferedRatherThanApplied() {
var chosen: PlaceResult? = null
composeRule.setContent {
GrainTheme {
LocationPicker(
queryState = TextFieldState(),
results = emptyList(),
isSearching = false,
selected = null,
suggestion = place("Ocean Beach"),
onSelect = { chosen = it },
onClear = {},
)
}
}
composeRule.awaitText("From this photo")
composeRule.onNodeWithText("Ocean Beach").performClick()
assertEquals("Ocean Beach", chosen?.name)
}
@Test
fun aChosenPlaceIsShownWithAWayToRemoveIt() {
var cleared = 0
composeRule.setContent {
GrainTheme {
LocationPicker(
queryState = TextFieldState(),
results = emptyList(),
isSearching = false,
selected = place("Mission District"),
suggestion = null,
onSelect = {},
onClear = { cleared += 1 },
)
}
}
composeRule.awaitText("Mission District")
composeRule.onNodeWithContentDescription("Remove location").performClick()
assertEquals(1, cleared)
}
// MARK: - The gallery comment sheet
@Test
fun theGallerySheetOpensOnItsGalleryAndReportsTheCountBack() {
harness.server.on(
"social.grain.comment.getThread",
"""{"comments":[${json(Fixtures.comment(text = "Lovely light"))}],"totalCount":4}""",
)
var count = -1
val model = GalleryCommentsViewModel(harness.client, harness.auth)
composeRule.setContent {
GrainTheme {
GalleryCommentSheet(
galleryUri = "at://did:plc:alice/social.grain.gallery/gallery1",
onDismiss = {},
onProfileTap = {},
onCommentCountChanged = { count = it },
viewModel = model,
)
}
}
composeRule.awaitText("Lovely light")
composeRule.waitUntil(10_000) { count == 4 }
}
@Test
fun tappingAnAuthorInTheGallerySheetClosesItFirst() {
harness.server.on(
"social.grain.comment.getThread",
"""{"comments":[${json(Fixtures.comment())}],"totalCount":1}""",
)
var dismissed = 0
var opened: String? = null
val model = GalleryCommentsViewModel(harness.client, harness.auth)
composeRule.setContent {
GrainTheme {
GalleryCommentSheet(
galleryUri = "at://did:plc:alice/social.grain.gallery/gallery1",
onDismiss = { dismissed += 1 },
onProfileTap = { opened = it },
viewModel = model,
)
}
}
composeRule.awaitText("Lovely light")
composeRule.onNodeWithContentDescription("alice.grain.social's avatar").performClick()
composeRule.waitUntil(10_000) { opened == "did:plc:alice" && dismissed == 1 }
}
}