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
package social.grain
import androidx.test.ext.junit.runners.AndroidJUnit4
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.local.Appearance
import social.grain.data.local.AppearanceStore
import social.grain.data.push.PushManager
import social.grain.feature.settings.SettingsViewModel
import social.grain.support.Harness
import social.grain.support.MainDispatcherRule
import social.grain.support.appContext
import social.grain.support.awaitCall
import social.grain.support.awaitValue
import social.grain.support.settle
/**
* Settings: three switches, an account, and the two ways out of it.
*
* The upload defaults live on the server, so each switch is a write that can
* fail — and a switch that stays flipped after a failed write is a lie about
* what the account actually holds.
*/
@RunWith(AndroidJUnit4::class)
class SettingsViewModelTest {
@get:Rule
val mainDispatcher = MainDispatcherRule()
private lateinit var harness: Harness
private lateinit var appearance: AppearanceStore
private lateinit var push: PushManager
@Before
fun setUp() {
harness = Harness()
appearance = AppearanceStore(appContext)
appearance.set(Appearance.AUTOMATIC)
push = PushManager(
appContext,
harness.client,
harness.auth,
social.grain.data.auth.SecureStore(appContext),
harness.scope,
)
harness.server.on(
"social.grain.actor.getPreferences",
"""{"preferences":[{"${'$'}type":"social.grain.actor.defs#photoMetadataPref",""" +
""""includeExif":false,"includeLocation":true}]}""",
)
}
@After
fun tearDown() {
appearance.set(Appearance.AUTOMATIC)
harness.tearDown()
}
private fun viewModel() =
SettingsViewModel(harness.client, harness.auth, push, appearance)
@Test
fun theSignedInAccountAndItsUploadDefaultsAreShown() {
val model = viewModel()
val state = model.state.awaitValue("the preferences") { it.did != null && !it.includeExif }
assertEquals(harness.handle, state.handle)
assertEquals(harness.did, state.did)
assertFalse(state.includeExif)
assertTrue(state.includeLocation)
assertEquals(Appearance.AUTOMATIC, state.appearance)
}
@Test
fun preferencesTheServerHasNeverStoredDefaultToOn() {
harness.server.on("social.grain.actor.getPreferences", """{"preferences":[]}""")
val model = viewModel()
val state = model.state.awaitValue("the account") { it.did != null }
assertTrue(state.includeExif)
assertTrue(state.includeLocation)
}
@Test
fun aFailedPreferenceReadLeavesTheDefaultsAlone() {
harness.server.on("social.grain.actor.getPreferences", """{"error":"no"}""", code = 500)
val model = viewModel()
val state = model.state.awaitValue("the account") { it.did != null }
assertTrue(state.includeExif)
assertNull(state.errorMessage)
}
@Test
fun theAppearanceChoiceIsRememberedAndObserved() {
val model = viewModel()
model.state.awaitValue("the account") { it.did != null }
model.setAppearance(Appearance.DARK)
assertEquals(
Appearance.DARK,
model.state.awaitValue("the appearance") { it.appearance == Appearance.DARK }.appearance,
)
assertEquals(
"It has to survive the process, not just the screen",
Appearance.DARK,
AppearanceStore(appContext).appearance.value,
)
}
@Test
fun aSwitchFlipsImmediatelyAndWritesThePreference() {
val model = viewModel()
// Not just the account: the stored preferences land after it, and would
// otherwise overwrite the flip under test.
model.state.awaitValue("the preferences") { it.did != null && !it.includeExif }
model.setIncludeExif(true)
assertTrue("The switch moves before the write", model.state.value.includeExif)
val call = harness.server.awaitCall("social.grain.actor.putPreferences")
assertTrue(call.body.contains("\"${'$'}type\":\"social.grain.actor.defs#photoMetadataPref\""))
assertTrue(call.body.contains("\"includeExif\":true"))
assertTrue("The other setting goes back as it was", call.body.contains("\"includeLocation\":true"))
}
@Test
fun aSwitchThatCannotBeSavedFlipsBackAndSaysSo() {
harness.server.on("social.grain.actor.putPreferences", """{"error":"no"}""", code = 500)
val model = viewModel()
model.state.awaitValue("the preferences") { it.did != null && !it.includeExif }
model.setIncludeLocation(false)
val state = model.state.awaitValue("the revert") { it.errorMessage != null }
assertTrue(state.includeLocation)
assertEquals("Couldn't save that setting.", state.errorMessage)
model.dismissError()
assertNull(model.state.value.errorMessage)
}
@Test
fun signingOutClearsTheSessionAndTakesTheDeviceOffPush() {
val model = viewModel()
model.state.awaitValue("the account") { it.did != null }
model.signOut()
model.state.awaitValue("the sign-out") { it.did == null }
assertFalse(harness.auth.state.value.isAuthenticated)
}
@Test
fun deletingTheAccountSignsOutOnceTheServerAgrees() {
harness.server.on("social.grain.server.deleteAccount", """{"deleted":0,"done":true}""")
val model = viewModel()
model.state.awaitValue("the account") { it.did != null }
model.deleteAccount()
val state = model.state.awaitValue("the deletion") { it.isDeleted }
assertFalse(state.isDeleting)
assertFalse(harness.auth.state.value.isAuthenticated)
harness.server.awaitCall("social.grain.server.deleteAccount")
}
@Test
fun aDeletionThatFailsLeavesTheAccountSignedIn() {
harness.server.on(
"social.grain.server.deleteAccount",
"""{"error":"no"}""",
code = 500,
)
val model = viewModel()
model.state.awaitValue("the account") { it.did != null }
model.deleteAccount()
val state = model.state.awaitValue("the error") { it.errorMessage != null }
assertFalse(state.isDeleted)
assertEquals("Couldn't delete the account. Try again.", state.errorMessage)
assertTrue(harness.auth.state.value.isAuthenticated)
}
@Test
fun aSignedOutViewerCannotDeleteAnAccount() {
harness.tearDown()
harness = Harness(signedIn = false)
val model = viewModel()
settle()
model.deleteAccount()
val state = model.state.awaitValue("the error") { it.errorMessage != null }
assertEquals("You're signed out.", state.errorMessage)
assertEquals(0, harness.server.countOf("social.grain.server.deleteAccount"))
}
}