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
package social.grain
import androidx.compose.ui.test.junit4.v2.createComposeRule
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performScrollTo
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.assertTrue
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import social.grain.data.auth.SecureStore
import social.grain.data.local.Appearance
import social.grain.data.local.AppearanceStore
import social.grain.data.push.PushManager
import social.grain.feature.settings.SettingsScreen
import social.grain.feature.settings.SettingsViewModel
import social.grain.support.Harness
import social.grain.support.awaitContentDescription
import social.grain.support.awaitText
import social.grain.support.MainDispatcherRule
import social.grain.support.appContext
import social.grain.ui.theme.GrainTheme
/**
* Settings, including the two dialogs that guard the irreversible rows.
*
* Sign-out and account deletion both go through a confirmation, and both of
* those are pure composable state — nothing below this can tell whether the
* dialog appeared, or whether cancelling it left the account alone.
*/
@RunWith(AndroidJUnit4::class)
class SettingsScreenTest {
@get:Rule
val mainDispatcher = MainDispatcherRule()
@get:Rule
val composeRule = createComposeRule()
private lateinit var harness: Harness
private lateinit var appearance: AppearanceStore
@Before
fun setUp() {
harness = Harness()
appearance = AppearanceStore(appContext)
appearance.set(Appearance.AUTOMATIC)
harness.server.on(
"social.grain.actor.getPreferences",
"""{"preferences":[{"${'$'}type":"social.grain.actor.defs#photoMetadataPref",""" +
""""includeExif":true,"includeLocation":false}]}""",
)
}
@After
fun tearDown() {
appearance.set(Appearance.AUTOMATIC)
harness.tearDown()
}
private fun show(
onBack: () -> Unit = {},
onEditProfile: () -> Unit = {},
onOpenBlocked: () -> Unit = {},
onOpenMuted: () -> Unit = {},
) {
val model = SettingsViewModel(
harness.client,
harness.auth,
PushManager(appContext, harness.client, harness.auth, SecureStore(appContext), harness.scope),
appearance,
)
composeRule.setContent {
GrainTheme {
SettingsScreen(
onBack = onBack,
onEditProfile = onEditProfile,
onOpenBlocked = onOpenBlocked,
onOpenMuted = onOpenMuted,
viewModel = model,
)
}
}
composeRule.waitForIdle()
}
@Test
fun theScreenNamesTheSignedInAccountAndItsSections() {
show()
composeRule.awaitText("Settings")
composeRule.onNodeWithText(harness.handle).assertExists()
composeRule.awaitText("Edit profile")
composeRule.awaitText("Appearance")
composeRule.awaitText("Defaults for new uploads")
}
@Test
fun theUploadSwitchesShowWhatTheServerHolds() {
show()
composeRule.awaitText("Include camera data")
composeRule.awaitText("Publish EXIF alongside each photo.")
composeRule.awaitText("Include location")
}
@Test
fun theBackButtonAndTheProfileRowReportTheirTaps() {
var back = 0
var edit = 0
show(onBack = { back += 1 }, onEditProfile = { edit += 1 })
composeRule.onNodeWithText("Edit profile").performClick()
composeRule.onNodeWithContentDescriptionCompat("Back").performClick()
assertEquals(1, edit)
assertEquals(1, back)
}
@Test
fun choosingAnAppearanceStoresIt() {
show()
composeRule.onNodeWithText("Dark").performScrollTo().performClick()
assertEquals(Appearance.DARK, appearance.appearance.value)
}
@Test
fun signingOutGoesThroughAConfirmationThatCanBeCancelled() {
show()
composeRule.onNodeWithText("Sign out").performScrollTo().performClick()
composeRule.awaitText("Sign out?")
composeRule.awaitText("You'll need to sign in again to use Grain.")
composeRule.onNodeWithText("Cancel").performClick()
assertTrue("Cancelling leaves the session alone", harness.auth.state.value.isAuthenticated)
}
@Test
fun confirmingTheSignOutEndsTheSession() {
show()
composeRule.onNodeWithText("Sign out").performScrollTo().performClick()
composeRule.onAllNodesWithTextCompat("Sign out")[1].performClick()
composeRule.waitForIdle()
assertFalse(harness.auth.state.value.isAuthenticated)
}
@Test
fun deletingTheAccountGoesThroughItsOwnConfirmation() {
show()
composeRule.onNodeWithText("Delete account").performScrollTo().performClick()
composeRule.awaitText("Delete account?")
composeRule.onNodeWithText("Cancel").performClick()
composeRule.waitForIdle()
assertEquals(0, harness.server.countOf("social.grain.server.deleteAccount"))
}
@Test
fun theModerationRowsLeadToTheirLists() {
var blocked = 0
var muted = 0
show(onOpenBlocked = { blocked += 1 }, onOpenMuted = { muted += 1 })
composeRule.onNodeWithText("Blocked users").performScrollTo().performClick()
composeRule.onNodeWithText("Muted users").performScrollTo().performClick()
assertEquals(1, blocked)
assertEquals(1, muted)
}
}
private fun androidx.compose.ui.test.junit4.ComposeTestRule.onNodeWithContentDescriptionCompat(
value: String,
) = onNode(androidx.compose.ui.test.hasContentDescription(value))
private fun androidx.compose.ui.test.junit4.ComposeTestRule.onAllNodesWithTextCompat(value: String) =
onAllNodes(androidx.compose.ui.test.hasText(value))