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
package social.grain
import androidx.lifecycle.SavedStateHandle
import androidx.test.ext.junit.runners.AndroidJUnit4
import okhttp3.OkHttpClient
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
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.CamerasViewModel
import social.grain.feature.explore.LocationsViewModel
import social.grain.support.Fixtures
import social.grain.support.Harness
import social.grain.support.MainDispatcherRule
import social.grain.support.awaitCall
import social.grain.support.awaitValue
import social.grain.support.json
import social.grain.support.settle
import social.grain.support.testJson
/** The two index screens behind explore's "See all". */
@RunWith(AndroidJUnit4::class)
class BrowseIndexViewModelsTest {
@get:Rule
val mainDispatcher = MainDispatcherRule()
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"]}]}""",
)
.on("social.grain.gallery.getLocations") { call ->
if (call.params["pins"] == "true") {
"""{"locations":[
{"name":"Lisbon, PT","h3Index":"85390cb7fffffff","galleryCount":3},
{"name":"Porto, PT","h3Index":"853913d3fffffff","galleryCount":1}
]}"""
} else {
"""{"locations":[{"name":"Lisbon, PT","h3Index":"85390cb7fffffff","galleryCount":3,"thumbs":["https://cdn.grain.test/thumb/2.jpg"]}]}"""
}
}
}
@After
fun tearDown() = harness.tearDown()
private fun cameras() = CamerasViewModel(harness.client, harness.auth)
private fun locations(
services: LocationServices = LocationServices(OkHttpClient(), testJson),
route: SavedStateHandle = SavedStateHandle(),
) = LocationsViewModel(harness.client, harness.auth, services, harness.labelDefinitions, route)
@Test
fun camerasArriveWithTheirThumbnails() {
val model = cameras()
val state = model.state.awaitValue("the cameras") { !it.isLoading }
assertEquals("Leica M6", state.items.single().camera)
assertEquals(1, state.items.single().thumbs.size)
assertFalse(state.isEmpty)
assertNull(state.errorMessage)
}
@Test
fun anEmptyCamerasIndexIsEmptyOnlyOnceItHasAnswered() {
harness.server.on("social.grain.gallery.getCameras", """{"cameras":[]}""")
val model = cameras()
assertFalse(model.state.value.isEmpty)
val state = model.state.awaitValue("the answer") { !it.isLoading }
assertTrue(state.isEmpty)
}
@Test
fun aFailedCamerasLoadSaysSoAndCanBeDismissed() {
harness.server.on("social.grain.gallery.getCameras", """{"error":"no"}""", code = 500)
val model = cameras()
val state = model.state.awaitValue("the error") { it.errorMessage != null }
assertEquals("HTTP error 500", state.errorMessage)
assertFalse(state.isLoading)
model.dismissError()
assertNull(model.state.value.errorMessage)
}
@Test
fun refreshingCamerasShowsTheIndicatorAndReReads() {
val model = cameras()
model.state.awaitValue("the cameras") { !it.isLoading }
val before = harness.server.countOf("social.grain.gallery.getCameras")
model.refresh()
harness.server.awaitCall("social.grain.gallery.getCameras", before + 1)
model.state.awaitValue("the refresh to finish") { !it.isRefreshing }
}
@Test
fun locationsAskForTilesAndPinsSeparately() {
val model = locations()
val state = model.state.awaitValue("the tiles") { !it.isLoading }
assertEquals("Lisbon, PT", state.items.single().name)
assertEquals(1, state.items.single().thumbs.size)
// The two go out together, so the tiles can answer before the pins
// request has even been recorded.
harness.server.awaitCall("social.grain.gallery.getLocations", 2)
val calls = harness.server.callsTo("social.grain.gallery.getLocations")
assertEquals(2, calls.size)
assertEquals(setOf(null, "true"), calls.map { it.params["pins"] }.toSet())
}
@Test
fun everyPlaceWithACoordinateBecomesAPin() {
val services = LocationServices(OkHttpClient(), testJson)
val model = locations(services)
harness.server.awaitCall("social.grain.gallery.getLocations", 2)
model.state.awaitValue("the tiles") { !it.isLoading }
// h3 is a native library that only ships for arm; where it can't load
// there are no coordinates and so no pins, and that is the contract.
if (services.isAvailable) {
val pins = model.pins.awaitValue("the pins") { it.size == 2 }
assertEquals(listOf("Lisbon, PT", "Porto, PT"), pins.map { it.name })
assertEquals(3, pins.first().galleryCount)
} else {
assertTrue(model.pins.value.isEmpty())
}
}
@Test
fun pickingAPlaceLoadsItsGalleriesWithoutLeavingTheIndex() {
harness.server.on("social.grain.feed.getFeed", """{"items":[${json(Fixtures.gallery())}],"cursor":"c1"}""")
val model = locations()
model.state.awaitValue("the tiles") { !it.isLoading }
model.select("85390cb7fffffff", "Lisbon, PT", 3)
val place = model.selected.awaitValue("the galleries") { it?.galleries?.isNotEmpty() == true }!!
assertEquals("Lisbon, PT", place.name)
assertEquals(3, place.galleryCount)
assertFalse(place.isLoading)
val call = harness.server.lastCall("social.grain.feed.getFeed")
assertEquals("location", call.params["feed"]?.substringAfterLast('/'))
assertEquals("85390cb7fffffff", call.params["location"])
assertEquals("Lisbon, PT", call.params["name"])
// The index is still there underneath.
assertEquals(1, model.state.value.items.size)
}
@Test
fun moreOfAPlacesGalleriesAppendBehindTheCursor() {
harness.server.on("social.grain.feed.getFeed", """{"items":[${json(Fixtures.gallery())}],"cursor":"c1"}""")
val model = locations()
model.select("85390cb7fffffff", "Lisbon, PT", 3)
model.selected.awaitValue("the first page") { it?.galleries?.isNotEmpty() == true }
harness.server.on(
"social.grain.feed.getFeed",
"""{"items":[${json(Fixtures.gallery(rkey = "g2", title = "Second"))}]}""",
)
model.loadMoreGalleries()
val place = model.selected.awaitValue("the second page") { it?.galleries?.size == 2 }!!
assertEquals("Second", place.galleries[1].title)
assertEquals("c1", harness.server.lastCall("social.grain.feed.getFeed").params["cursor"])
assertFalse(place.isLoadingMore)
}
@Test
fun pickingThePlaceAlreadyShownDoesNotReload() {
harness.server.on("social.grain.feed.getFeed", """{"items":[${json(Fixtures.gallery())}]}""")
val model = locations()
model.select("85390cb7fffffff", "Lisbon, PT", 3)
model.selected.awaitValue("the galleries") { it?.galleries?.isNotEmpty() == true }
val before = harness.server.countOf("social.grain.feed.getFeed")
model.select("85390cb7fffffff", "Lisbon, PT", 3)
settle()
assertEquals(before, harness.server.countOf("social.grain.feed.getFeed"))
}
@Test
fun openedFromAChipTheScreenStartsOnThatPlace() {
harness.server.on("social.grain.feed.getFeed", """{"items":[${json(Fixtures.gallery())}]}""")
val model = locations(
route = SavedStateHandle(
mapOf("cell" to "85390cb7fffffff", "name" to "Lisbon, PT", "galleryCount" to 3),
),
)
assertTrue(model.openedOnPlace)
val place = model.selected.awaitValue("the galleries") { it?.galleries?.isNotEmpty() == true }!!
assertEquals("Lisbon, PT", place.name)
assertEquals(3, place.galleryCount)
assertEquals("85390cb7fffffff", harness.server.lastCall("social.grain.feed.getFeed").params["location"])
}
@Test
fun openedFromSeeAllTheScreenStartsOnTheIndex() {
val model = locations()
assertFalse(model.openedOnPlace)
assertNull(model.selected.value)
}
@Test
fun clearingTheSelectionBringsTheIndexBack() {
harness.server.on("social.grain.feed.getFeed", """{"items":[]}""")
val model = locations()
model.select("85390cb7fffffff", "Lisbon, PT", 3)
val place = model.selected.awaitValue("the answer") { it?.isLoading == false }!!
assertTrue(place.isEmpty)
model.clearSelection()
assertNull(model.selected.value)
}
@Test
fun aFailedLocationsLoadSaysSo() {
harness.server.on("social.grain.gallery.getLocations", """{"error":"no"}""", code = 500)
val model = locations()
val state = model.state.awaitValue("the error") { it.errorMessage != null }
assertEquals("HTTP error 500", state.errorMessage)
assertTrue(model.pins.value.isEmpty())
model.dismissError()
assertNull(model.state.value.errorMessage)
}
}