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
package social.grain
import androidx.compose.ui.test.junit4.v2.createComposeRule
import androidx.lifecycle.SavedStateHandle
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 okhttp3.OkHttpClient
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.LocationServices
import social.grain.feature.explore.CamerasScreen
import social.grain.feature.explore.CamerasViewModel
import social.grain.feature.explore.LocationsScreen
import social.grain.feature.explore.LocationsViewModel
import social.grain.feature.feed.CameraFeedScreen
import social.grain.feature.feed.CameraFeedViewModel
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.support.testJson
import social.grain.ui.theme.GrainTheme
/** The screens behind explore: the two indexes, a tile per entry, and the camera feed. */
@RunWith(AndroidJUnit4::class)
class ExploreScreensTest {
@get:Rule
val mainDispatcher = MainDispatcherRule()
@get:Rule
val composeRule = createComposeRule()
private lateinit var harness: Harness
@Before
fun setUp() {
harness = Harness()
harness.server
.on(
"social.grain.gallery.getCameras",
"""{"cameras":[
{"camera":"Leica M6","photoCount":120,"thumbs":["https://cdn.grain.test/thumb/1.jpg"]},
{"camera":"Ricoh GR III","photoCount":1}
]}""",
)
.on(
"social.grain.gallery.getLocations",
"""{"locations":[{"name":"Lisbon, PT","h3Index":"85390cb7fffffff","galleryCount":3}]}""",
)
}
@After
fun tearDown() = harness.tearDown()
@Test
fun theCamerasIndexDrawsATilePerCameraWithItsCount() {
var opened: String? = null
composeRule.setContent {
GrainTheme {
CamerasScreen(
onBack = {},
onOpenCamera = { opened = it },
viewModel = CamerasViewModel(harness.client, harness.auth),
)
}
}
composeRule.awaitText("Cameras")
composeRule.awaitText("Leica M6")
composeRule.awaitText("120 photos")
composeRule.awaitText("1 photo")
composeRule.onNodeWithText("Ricoh GR III").performClick()
assertEquals("Ricoh GR III", opened)
}
@Test
fun aCameraFeedIsTitledByTheCameraAndDrawsItsCards() {
harness.server
.on("social.grain.feed.getFeed", """{"items":[${json(Fixtures.gallery(title = "Shot on film"))}]}""")
composeRule.setContent {
GrainTheme {
CameraFeedScreen(
onBack = {},
onOpenProfile = {},
onOpenHashtag = {},
viewModel = CameraFeedViewModel(
client = harness.client,
auth = harness.auth,
labelDefsCache = harness.labelDefinitions,
savedStateHandle = SavedStateHandle(mapOf("name" to "Leica M6")),
),
)
}
}
composeRule.awaitText("Leica M6")
composeRule.awaitText("Shot on film")
assertEquals("Leica M6", harness.server.lastCall("social.grain.feed.getFeed").params["camera"])
}
@Test
fun anEmptyCameraFeedSaysSo() {
harness.server
.on("social.grain.feed.getFeed", """{"items":[]}""")
composeRule.setContent {
GrainTheme {
CameraFeedScreen(
onBack = {},
onOpenProfile = {},
onOpenHashtag = {},
viewModel = CameraFeedViewModel(
client = harness.client,
auth = harness.auth,
labelDefsCache = harness.labelDefinitions,
savedStateHandle = SavedStateHandle(mapOf("name" to "Leica M6")),
),
)
}
}
composeRule.awaitText("No galleries from this camera yet")
}
@Test
fun anEmptyCamerasIndexSaysSo() {
harness.server.on("social.grain.gallery.getCameras", """{"cameras":[]}""")
composeRule.setContent {
GrainTheme {
CamerasScreen(
onBack = {},
onOpenCamera = {},
viewModel = CamerasViewModel(harness.client, harness.auth),
)
}
}
composeRule.awaitText("No cameras yet.")
}
@Test
fun pickingAPlaceSwapsTheSheetToItsGalleriesAndBackAgain() {
harness.server
.on("social.grain.feed.getFeed", """{"items":[${json(Fixtures.gallery(title = "By the river"))}]}""")
var opened: String? = null
composeRule.setContent {
GrainTheme {
LocationsScreen(
onBack = {},
onOpenGallery = { opened = it },
viewModel = LocationsViewModel(
harness.client,
harness.auth,
LocationServices(OkHttpClient(), testJson),
harness.labelDefinitions,
SavedStateHandle(),
),
)
}
}
composeRule.awaitText("Locations")
composeRule.awaitText("Lisbon, PT")
composeRule.awaitText("3 galleries")
composeRule.onNodeWithText("Lisbon, PT").performClick()
// The place's galleries arrive in the sheet; the screen was not left.
composeRule.awaitContentDescription("By the river")
composeRule.awaitContentDescription("All locations")
assertEquals("85390cb7fffffff", harness.server.lastCall("social.grain.feed.getFeed").params["location"])
composeRule.onNodeWithContentDescription("By the river").performClick()
assertEquals(Fixtures.gallery().uri, opened)
composeRule.onNodeWithContentDescription("All locations").performClick()
composeRule.awaitText("Locations")
composeRule.onNodeWithContentDescription("By the river").assertDoesNotExist()
}
@Test
fun anEmptyLocationsIndexSaysSo() {
harness.server.on("social.grain.gallery.getLocations", """{"locations":[]}""")
composeRule.setContent {
GrainTheme {
LocationsScreen(
onBack = {},
onOpenGallery = {},
viewModel = LocationsViewModel(
harness.client,
harness.auth,
LocationServices(OkHttpClient(), testJson),
harness.labelDefinitions,
SavedStateHandle(),
),
)
}
}
composeRule.awaitText("No locations yet.")
}
@Test
fun aPlaceWithNoGalleriesSaysSoInItsSheet() {
harness.server
.on("social.grain.feed.getFeed", """{"items":[]}""")
composeRule.setContent {
GrainTheme {
LocationsScreen(
onBack = {},
onOpenGallery = {},
viewModel = LocationsViewModel(
harness.client,
harness.auth,
LocationServices(OkHttpClient(), testJson),
harness.labelDefinitions,
SavedStateHandle(),
),
)
}
}
composeRule.awaitText("Lisbon, PT")
composeRule.onNodeWithText("Lisbon, PT").performClick()
composeRule.awaitText("No galleries here yet.")
}
}