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
package social.grain
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.onRoot
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performTouchInput
import androidx.compose.ui.test.swipeDown
import androidx.compose.ui.test.swipeLeft
import androidx.lifecycle.SavedStateHandle
import androidx.test.ext.junit.runners.AndroidJUnit4
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.model.GrainProfile
import social.grain.feature.stories.StoryCommentsViewModel
import social.grain.feature.stories.StoryViewerScreen
import social.grain.feature.stories.StoryViewerViewModel
import social.grain.support.Fixtures
import social.grain.support.Harness
import social.grain.support.MainDispatcherRule
import social.grain.support.awaitContentDescription
import social.grain.support.awaitText
import social.grain.support.json
import social.grain.ui.theme.GrainTheme
/**
* The story viewer as it appears: a full-bleed photo with controls over it.
*
* Almost everything here is drawn in white over an unknown photograph, and the
* whole screen has no chrome to fall back on — a control that fails to compose
* leaves a reader tapping a black rectangle with no way out.
*/
@RunWith(AndroidJUnit4::class)
class StoryViewerScreenTest {
@get:Rule
val mainDispatcher = MainDispatcherRule()
@get:Rule
val composeRule = createComposeRule()
private lateinit var harness: Harness
private val alice = Fixtures.profile(did = "did:plc:alice", handle = "alice.test")
private fun stories(creator: GrainProfile, vararg rkeys: String) =
"""{"stories":[${rkeys.joinToString(",") {
json(Fixtures.story(rkey = it, creator = creator))
}}]}"""
@Before
fun setUp() {
harness = Harness()
harness.server
.on("social.grain.story.getStories", stories(alice, "a1", "a2"))
.on("social.grain.comment.getThread", """{"comments":[],"totalCount":0}""")
}
@After
fun tearDown() = harness.tearDown()
private lateinit var viewer: StoryViewerViewModel
private fun show(
did: String = alice.did,
story: String = "",
solo: Boolean = true,
onClose: () -> Unit = {},
onOpenProfile: (String) -> Unit = {},
) {
viewer = StoryViewerViewModel(
client = harness.client,
auth = harness.auth,
viewedStore = harness.viewedStories,
statusCache = harness.storyStatus,
labelDefsCache = harness.labelDefinitions,
savedStateHandle = SavedStateHandle(
mapOf("did" to did, "story" to story, "solo" to solo),
),
)
val comments = StoryCommentsViewModel(harness.client, harness.auth)
composeRule.setContent {
GrainTheme {
StoryViewerScreen(
onClose = onClose,
onOpenProfile = onOpenProfile,
viewModel = viewer,
commentsViewModel = comments,
)
}
}
composeRule.waitForIdle()
}
@Test
fun theViewerNamesTheAuthorAndOffersAWayOut() {
show()
composeRule.awaitText("Alice")
composeRule.awaitContentDescription("Close")
}
@Test
fun closingReportsItsTap() {
var closed = 0
show(onClose = { closed += 1 })
composeRule.awaitContentDescription("Close")
composeRule.onNodeWithContentDescription("Close").performClick()
// Leaving plays a short animation before it hands back.
composeRule.waitUntil(10_000) { closed == 1 }
}
@Test
fun aStoryOffersToBeFavoritedAndCommentedOn() {
show()
composeRule.awaitContentDescription("Favorite")
composeRule.awaitContentDescription("Comments")
}
@Test
fun favoritingWritesTheRecord() {
harness.server.on("com.atproto.repo.createRecord", """{"uri":"at://did:plc:me/fav/1"}""")
show()
composeRule.awaitContentDescription("Favorite")
composeRule.onNodeWithContentDescription("Favorite").performClick()
composeRule.waitUntil(10_000) { harness.server.countOf("com.atproto.repo.createRecord") >= 1 }
}
@Test
fun anAlreadyFavoritedStoryOffersTheUndo() {
harness.server.on(
"social.grain.story.getStories",
"""{"stories":[${json(
Fixtures.story(rkey = "a1", creator = alice, favUri = "at://fav/1"),
)}]}""",
)
show()
composeRule.awaitContentDescription("Unfavorite")
}
@Test
fun somebodyElsesStoryOffersReportingRatherThanDeleting() {
show()
composeRule.awaitContentDescription("Report story")
composeRule.onNodeWithContentDescription("Delete story").assertDoesNotExist()
}
@Test
fun theViewersOwnStoryOffersDeletingBehindAConfirmation() {
val me = Fixtures.profile(did = harness.did, handle = harness.handle)
harness.server.on("social.grain.story.getStories", stories(me, "m1"))
show(did = harness.did)
composeRule.awaitContentDescription("Delete story")
composeRule.onNodeWithContentDescription("Delete story").performClick()
composeRule.awaitText("Delete story?")
composeRule.onNodeWithText("This will permanently delete this story.").assertExists()
composeRule.onNodeWithText("Cancel").performClick()
composeRule.waitForIdle()
assertEquals(0, harness.server.countOf("com.atproto.repo.deleteRecord"))
}
@Test
fun confirmingTheDeleteWritesIt() {
val me = Fixtures.profile(did = harness.did, handle = harness.handle)
harness.server.on("social.grain.story.getStories", stories(me, "m1"))
show(did = harness.did)
composeRule.awaitContentDescription("Delete story")
composeRule.onNodeWithContentDescription("Delete story").performClick()
composeRule.awaitText("Delete story?")
composeRule.onNodeWithText("Delete").performClick()
composeRule.waitUntil(10_000) { harness.server.countOf("com.atproto.repo.deleteRecord") >= 1 }
}
@Test
fun tappingTheAuthorOpensTheirProfile() {
var opened: String? = null
show(onOpenProfile = { opened = it })
composeRule.awaitText("Alice")
composeRule.onNodeWithText("Alice").performClick()
composeRule.waitUntil(10_000) { opened == "did:plc:alice" }
}
// MARK: - Gestures
private val bob =
Fixtures.profile(did = "did:plc:bob", handle = "bob.test", displayName = "Bob")
/** Two authors in the strip, alice first — the strip is newest-first. */
private fun seedStrip() {
harness.storyStatus.update(
listOf(
Fixtures.storyAuthor(
profile = alice,
storyCount = 2,
latestAt = Fixtures.recentIso(minutesAgo = 1),
),
Fixtures.storyAuthor(
profile = bob,
storyCount = 1,
latestAt = Fixtures.recentIso(minutesAgo = 30),
),
),
)
harness.server.on("social.grain.story.getStories") { call ->
when (call.params["actor"]) {
bob.did -> stories(bob, "b1")
else -> stories(alice, "a1", "a2")
}
}
}
@Test
fun draggingSidewaysHandsOverToTheNextAuthor() {
seedStrip()
show(solo = false)
composeRule.awaitText("Alice")
composeRule.onRoot().performTouchInput {
swipeLeft(startX = right * 0.9f, endX = left + 1f, durationMillis = 200)
}
composeRule.waitUntil(10_000) { viewer.state.value.author?.did == bob.did }
}
@Test
fun draggingDownThrowsTheViewerAway() {
seedStrip()
var closed = 0
show(solo = false, onClose = { closed += 1 })
composeRule.awaitText("Alice")
composeRule.onRoot().performTouchInput {
swipeDown(startY = top + 1f, endY = bottom - 1f, durationMillis = 200)
}
composeRule.waitUntil(10_000) { closed == 1 || viewer.state.value.isFinished }
}
@Test
fun aDragThatGoesNowhereLeavesTheStoryWhereItWas() {
seedStrip()
show(solo = false)
composeRule.awaitText("Alice")
composeRule.onRoot().performTouchInput {
down(center)
moveTo(center + androidx.compose.ui.geometry.Offset(20f, 0f))
up()
}
composeRule.waitForIdle()
assertEquals(alice.did, viewer.state.value.author?.did)
}
@Test
fun anArchivedStoryOpensOnItsOwn() {
harness.server.on(
"social.grain.story.getStory",
"""{"story":${json(Fixtures.story(rkey = "old", creator = alice, expired = true))}}""",
)
show(story = "at://did:plc:alice/social.grain.story/old", solo = false)
composeRule.awaitText("Alice")
assertEquals(1, viewer.state.value.stories.size)
}
}