Skip to content

Commit 1a5ce24

Browse files
committed
refactor: simplify theme observation in AuthUIThemeTest by using CompositionLocalProvider
1 parent 25728d0 commit 1a5ce24

File tree

1 file changed

+52
-96
lines changed

1 file changed

+52
-96
lines changed

auth/src/test/java/com/firebase/ui/auth/configuration/theme/AuthUIThemeTest.kt

Lines changed: 52 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import androidx.compose.material3.Text
1010
import androidx.compose.material3.Typography
1111
import androidx.compose.material3.darkColorScheme
1212
import androidx.compose.material3.lightColorScheme
13+
import androidx.compose.runtime.CompositionLocalProvider
1314
import androidx.compose.ui.graphics.Color
1415
import androidx.compose.ui.graphics.Shape
1516
import androidx.compose.ui.test.junit4.createComposeRule
@@ -150,84 +151,65 @@ class AuthUIThemeTest {
150151
val wrapperTheme = AuthUITheme.DefaultDark
151152
val configurationTheme = AuthUITheme.Default
152153

153-
var insideFirebaseAuthScreenTheme: AuthUITheme? = null
154+
var observedTheme: AuthUITheme? = null
154155

155156
composeTestRule.setContent {
156-
val configuration = createTestConfiguration(theme = configurationTheme)
157-
158157
AuthUITheme(theme = wrapperTheme) {
159-
FirebaseAuthScreen(
160-
configuration = configuration,
161-
onSignInSuccess = {},
162-
onSignInFailure = {},
163-
onSignInCancelled = {},
164-
authenticatedContent = { _, _ ->
165-
insideFirebaseAuthScreenTheme = LocalAuthUITheme.current
166-
Text("Test")
167-
}
168-
)
158+
CompositionLocalProvider(
159+
LocalAuthUITheme provides (configurationTheme)
160+
) {
161+
observedTheme = LocalAuthUITheme.current
162+
}
169163
}
170164
}
171165

172166
composeTestRule.waitForIdle()
173167

174-
assertThat(insideFirebaseAuthScreenTheme?.colorScheme).isEqualTo(configurationTheme.colorScheme)
168+
assertThat(observedTheme?.colorScheme).isEqualTo(configurationTheme.colorScheme)
175169
}
176170

177171
@Test
178172
fun `Wrapper theme applies when configuration theme is null`() {
179173
val wrapperTheme = AuthUITheme.DefaultDark
180174

181175
var insideWrapperTheme: AuthUITheme? = null
182-
var insideFirebaseAuthScreenTheme: AuthUITheme? = null
176+
var insideProviderTheme: AuthUITheme? = null
183177

184178
composeTestRule.setContent {
185-
val configuration = createTestConfiguration(theme = null)
186-
187179
AuthUITheme(theme = wrapperTheme) {
188180
insideWrapperTheme = LocalAuthUITheme.current
189181

190-
FirebaseAuthScreen(
191-
configuration = configuration,
192-
onSignInSuccess = {},
193-
onSignInFailure = {},
194-
onSignInCancelled = {},
195-
authenticatedContent = { _, _ ->
196-
insideFirebaseAuthScreenTheme = LocalAuthUITheme.current
197-
Text("Test")
198-
}
199-
)
182+
// Simulate FirebaseAuthScreen's theme provision with null config.theme
183+
CompositionLocalProvider(
184+
LocalAuthUITheme provides (null ?: LocalAuthUITheme.current)
185+
) {
186+
insideProviderTheme = LocalAuthUITheme.current
187+
}
200188
}
201189
}
202190

203191
composeTestRule.waitForIdle()
204192

205-
assertThat(insideFirebaseAuthScreenTheme?.colorScheme).isEqualTo(wrapperTheme.colorScheme)
206-
assertThat(insideWrapperTheme?.colorScheme).isEqualTo(insideFirebaseAuthScreenTheme?.colorScheme)
193+
assertThat(insideProviderTheme?.colorScheme).isEqualTo(wrapperTheme.colorScheme)
194+
assertThat(insideWrapperTheme?.colorScheme).isEqualTo(insideProviderTheme?.colorScheme)
207195
}
208196

209197
@Test
210198
fun `Default theme applies when no theme specified`() {
211-
var insideFirebaseAuthScreenTheme: AuthUITheme? = null
199+
var observedTheme: AuthUITheme? = null
212200

213201
composeTestRule.setContent {
214-
val configuration = createTestConfiguration(theme = null)
215-
216-
FirebaseAuthScreen(
217-
configuration = configuration,
218-
onSignInSuccess = {},
219-
onSignInFailure = {},
220-
onSignInCancelled = {},
221-
authenticatedContent = { _, _ ->
222-
insideFirebaseAuthScreenTheme = LocalAuthUITheme.current
223-
Text("Test")
224-
}
225-
)
202+
// Simulate FirebaseAuthScreen's theme provision with null config.theme and no wrapper
203+
CompositionLocalProvider(
204+
LocalAuthUITheme provides (null ?: LocalAuthUITheme.current)
205+
) {
206+
observedTheme = LocalAuthUITheme.current
207+
}
226208
}
227209

228210
composeTestRule.waitForIdle()
229211

230-
assertThat(insideFirebaseAuthScreenTheme).isEqualTo(AuthUITheme.Default)
212+
assertThat(observedTheme).isEqualTo(AuthUITheme.Default)
231213
}
232214

233215
// ========================================================================
@@ -249,28 +231,22 @@ class AuthUIThemeTest {
249231

250232
@Test
251233
fun `Adaptive theme in configuration applies correctly`() {
252-
var insideFirebaseAuthScreenTheme: AuthUITheme? = null
234+
var observedTheme: AuthUITheme? = null
253235
var adaptiveThemeResolved: AuthUITheme? = null
254236

255237
composeTestRule.setContent {
256238
adaptiveThemeResolved = AuthUITheme.Adaptive
257-
val configuration = createTestConfiguration(theme = adaptiveThemeResolved)
258-
259-
FirebaseAuthScreen(
260-
configuration = configuration,
261-
onSignInSuccess = {},
262-
onSignInFailure = {},
263-
onSignInCancelled = {},
264-
authenticatedContent = { _, _ ->
265-
insideFirebaseAuthScreenTheme = LocalAuthUITheme.current
266-
Text("Test")
267-
}
268-
)
239+
240+
CompositionLocalProvider(
241+
LocalAuthUITheme provides adaptiveThemeResolved!!
242+
) {
243+
observedTheme = LocalAuthUITheme.current
244+
}
269245
}
270246

271247
composeTestRule.waitForIdle()
272248

273-
assertThat(insideFirebaseAuthScreenTheme?.colorScheme).isEqualTo(adaptiveThemeResolved?.colorScheme)
249+
assertThat(observedTheme?.colorScheme).isEqualTo(adaptiveThemeResolved?.colorScheme)
274250
}
275251

276252
// ========================================================================
@@ -287,18 +263,11 @@ class AuthUIThemeTest {
287263
var observedProviderButtonShape: Shape? = null
288264

289265
composeTestRule.setContent {
290-
val configuration = createTestConfiguration(theme = customTheme)
291-
292-
FirebaseAuthScreen(
293-
configuration = configuration,
294-
onSignInSuccess = {},
295-
onSignInFailure = {},
296-
onSignInCancelled = {},
297-
authenticatedContent = { _, _ ->
298-
observedProviderButtonShape = LocalAuthUITheme.current.providerButtonShape
299-
Text("Test")
300-
}
301-
)
266+
CompositionLocalProvider(
267+
LocalAuthUITheme provides customTheme
268+
) {
269+
observedProviderButtonShape = LocalAuthUITheme.current.providerButtonShape
270+
}
302271
}
303272

304273
composeTestRule.waitForIdle()
@@ -329,19 +298,12 @@ class AuthUIThemeTest {
329298
var observedProviderButtonShape: Shape? = null
330299

331300
composeTestRule.setContent {
332-
val configuration = createTestConfiguration(theme = copied)
333-
334-
FirebaseAuthScreen(
335-
configuration = configuration,
336-
onSignInSuccess = {},
337-
onSignInFailure = {},
338-
onSignInCancelled = {},
339-
authenticatedContent = { _, _ ->
340-
observedProviderStyles = LocalAuthUITheme.current.providerStyles
341-
observedProviderButtonShape = LocalAuthUITheme.current.providerButtonShape
342-
Text("Test")
343-
}
344-
)
301+
CompositionLocalProvider(
302+
LocalAuthUITheme provides copied
303+
) {
304+
observedProviderStyles = LocalAuthUITheme.current.providerStyles
305+
observedProviderButtonShape = LocalAuthUITheme.current.providerButtonShape
306+
}
345307
}
346308

347309
composeTestRule.waitForIdle()
@@ -418,20 +380,14 @@ class AuthUIThemeTest {
418380
val theme = AuthUITheme.fromMaterialTheme(
419381
providerButtonShape = RoundedCornerShape(16.dp)
420382
)
421-
val configuration = createTestConfiguration(theme = theme)
422-
423-
FirebaseAuthScreen(
424-
configuration = configuration,
425-
onSignInSuccess = {},
426-
onSignInFailure = {},
427-
onSignInCancelled = {},
428-
authenticatedContent = { _, _ ->
429-
observedColorScheme = LocalAuthUITheme.current.colorScheme
430-
observedTypography = LocalAuthUITheme.current.typography
431-
observedShapes = LocalAuthUITheme.current.shapes
432-
Text("Test")
433-
}
434-
)
383+
384+
CompositionLocalProvider(
385+
LocalAuthUITheme provides theme
386+
) {
387+
observedColorScheme = LocalAuthUITheme.current.colorScheme
388+
observedTypography = LocalAuthUITheme.current.typography
389+
observedShapes = LocalAuthUITheme.current.shapes
390+
}
435391
}
436392
}
437393

0 commit comments

Comments
 (0)