Skip to content
This repository was archived by the owner on Aug 7, 2024. It is now read-only.

Commit 43cf3e1

Browse files
committed
Test both light and dark themes
1 parent 5f208c3 commit 43cf3e1

File tree

5 files changed

+98
-11
lines changed

5 files changed

+98
-11
lines changed

lib/src/androidTest/AndroidManifest.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@
2222
<application>
2323

2424
<activity
25-
android:name=".MdcActivity"
25+
android:name=".LightMdcActivity"
26+
android:theme="@style/Theme.MdcThemeTest" />
27+
28+
<activity
29+
android:name=".DarkMdcActivity"
2630
android:theme="@style/Theme.MdcThemeTest" />
2731

2832
<activity

lib/src/androidTest/java/com/google/android/material/composethemeadapter/MdcActivity.kt renamed to lib/src/androidTest/java/com/google/android/material/composethemeadapter/DarkMdcActivity.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@
1616

1717
package com.google.android.material.composethemeadapter
1818

19+
import android.content.Context
1920
import androidx.appcompat.app.AppCompatActivity
21+
import androidx.appcompat.app.AppCompatDelegate
2022

21-
class MdcActivity : AppCompatActivity()
23+
/**
24+
* An [AppCompatActivity] which forces the night mode to 'dark theme'.
25+
*/
26+
class DarkMdcActivity : AppCompatActivity() {
27+
override fun attachBaseContext(newBase: Context) {
28+
delegate.localNightMode = AppCompatDelegate.MODE_NIGHT_YES
29+
super.attachBaseContext(newBase)
30+
}
31+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2020 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.android.material.composethemeadapter
18+
19+
import android.content.Context
20+
import android.content.res.Configuration
21+
22+
/**
23+
* This allows us to check whether this [Context]s resource configuration is in 'night mode',
24+
* which is also known as dark theme.
25+
*/
26+
fun Context.isInDarkTheme(): Boolean {
27+
return resources.configuration.uiMode and
28+
Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES
29+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2020 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.android.material.composethemeadapter
18+
19+
import android.content.Context
20+
import androidx.appcompat.app.AppCompatActivity
21+
import androidx.appcompat.app.AppCompatDelegate
22+
23+
/**
24+
* An [AppCompatActivity] which forces the night mode to 'light theme'.
25+
*/
26+
class LightMdcActivity : AppCompatActivity() {
27+
override fun attachBaseContext(newBase: Context) {
28+
delegate.localNightMode = AppCompatDelegate.MODE_NIGHT_NO
29+
super.attachBaseContext(newBase)
30+
}
31+
}

lib/src/androidTest/java/com/google/android/material/composethemeadapter/MdcThemeTest.kt

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616

1717
package com.google.android.material.composethemeadapter
1818

19-
import androidx.compose.foundation.isSystemInDarkTheme
19+
import androidx.appcompat.app.AppCompatActivity
2020
import androidx.compose.foundation.shape.CornerSize
2121
import androidx.compose.foundation.shape.CutCornerShape
2222
import androidx.compose.foundation.shape.RoundedCornerShape
2323
import androidx.compose.material.MaterialTheme
2424
import androidx.compose.ui.geometry.Size
25+
import androidx.compose.ui.platform.ContextAmbient
2526
import androidx.compose.ui.platform.DensityAmbient
2627
import androidx.compose.ui.res.colorResource
2728
import androidx.compose.ui.text.font.asFontFamily
@@ -32,8 +33,9 @@ import androidx.compose.ui.unit.TextUnit
3233
import androidx.compose.ui.unit.dp
3334
import androidx.compose.ui.unit.em
3435
import androidx.compose.ui.unit.sp
36+
import androidx.test.ext.junit.rules.ActivityScenarioRule
3537
import androidx.test.filters.MediumTest
36-
import androidx.ui.test.android.createAndroidComposeRule
38+
import androidx.ui.test.android.AndroidComposeTestRule
3739
import com.google.android.material.composethemeadapter.test.R
3840
import org.junit.Assert.assertEquals
3941
import org.junit.Assert.assertNotNull
@@ -42,13 +44,21 @@ import org.junit.Assert.assertTrue
4244
import org.junit.Rule
4345
import org.junit.Test
4446
import org.junit.runner.RunWith
45-
import org.junit.runners.JUnit4
47+
import org.junit.runners.Parameterized
4648

4749
@MediumTest
48-
@RunWith(JUnit4::class)
49-
class MdcThemeTest {
50+
@RunWith(Parameterized::class)
51+
class MdcThemeTest<T : AppCompatActivity>(activityClass: Class<T>) {
52+
companion object {
53+
@JvmStatic
54+
@Parameterized.Parameters
55+
fun activities() = listOf(DarkMdcActivity::class.java, LightMdcActivity::class.java)
56+
}
57+
58+
private val activityRule = ActivityScenarioRule<T>(activityClass)
59+
5060
@get:Rule
51-
val composeTestRule = createAndroidComposeRule<MdcActivity>()
61+
val composeTestRule = AndroidComposeTestRule(activityRule)
5262

5363
@Test
5464
fun colors() = composeTestRule.setContent {
@@ -61,11 +71,14 @@ class MdcThemeTest {
6171

6272
assertEquals(colorResource(R.color.dark_golden_rod), color.secondary)
6373
assertEquals(colorResource(R.color.slate_gray), color.onSecondary)
64-
if (!isSystemInDarkTheme()) {
65-
assertEquals(colorResource(R.color.blue_violet), color.secondaryVariant)
66-
} else {
74+
75+
// We don't check isSystemInDarkTheme() here since that only checks the system
76+
// dark theme setting: https://issuetracker.google.com/163103826
77+
if (ContextAmbient.current.isInDarkTheme()) {
6778
// In dark theme secondaryVariant is ignored and always return secondary
6879
assertEquals(colorResource(R.color.dark_golden_rod), color.secondaryVariant)
80+
} else {
81+
assertEquals(colorResource(R.color.blue_violet), color.secondaryVariant)
6982
}
7083

7184
assertEquals(colorResource(R.color.spring_green), color.surface)

0 commit comments

Comments
 (0)