forked from nextcloud/android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOnboardingResourceTest.kt
More file actions
131 lines (114 loc) · 3.96 KB
/
OnboardingResourceTest.kt
File metadata and controls
131 lines (114 loc) · 3.96 KB
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
/*
* Nextcloud - Android Client
*
* SPDX-FileCopyrightText: 2025 TSI-mc <surinder.kumar@t-systems.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
package com.nmc.android
import android.content.Context
import android.content.res.Configuration
import android.util.DisplayMetrics
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.owncloud.android.R
import junit.framework.TestCase.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
import java.util.Locale
/**
* Test class to verify the strings and dimens customized in this branch PR for NMC
*/
@RunWith(AndroidJUnit4::class)
class OnboardingResourceTest {
private val baseContext = ApplicationProvider.getApplicationContext<Context>()
private val localizedStringMap = mapOf(
R.string.common_login to ExpectedLocalizedString(
translations = mapOf(
Locale.ENGLISH to "Login",
Locale.GERMAN to "Anmelden"
)
),
)
@Test
fun verifyLocalizedStrings() {
localizedStringMap.forEach { (stringRes, expected) ->
expected.translations.forEach { (locale, expectedText) ->
val config = Configuration(baseContext.resources.configuration)
config.setLocale(locale)
val localizedContext = baseContext.createConfigurationContext(config)
val actualText = localizedContext.getString(stringRes)
assertEquals(
"Mismatch for ${baseContext.resources.getResourceEntryName(stringRes)} in $locale",
expectedText,
actualText
)
}
}
}
data class ExpectedLocalizedString(val translations: Map<Locale, String>)
private val expectedDimenMap = mapOf(
R.dimen.login_btn_bottom_margin to ExpectedDimen(
default = 48f,
unit = DimenUnit.DP,
alt = 96f
),
R.dimen.alternate_double_margin to ExpectedDimen(
default = 20f,
unit = DimenUnit.DP
),
)
@Test
fun validateDefaultDimens() {
validateDimens(
configModifier = { it }, // no change → default values
) { it.default to it.unit }
}
@Test
fun validate_sw600dp_Dimens() {
validateDimens(configModifier = { config ->
config.smallestScreenWidthDp = 600
config
}) { it.alt to it.unit }
}
private fun validateDimens(
configModifier: (Configuration) -> Configuration,
selector: (ExpectedDimen) -> Pair<Float?, DimenUnit>
) {
val baseConfig = Configuration(baseContext.resources.configuration)
val testConfig = configModifier(baseConfig)
val testContext = baseContext.createConfigurationContext(testConfig)
val dm = testContext.resources.displayMetrics
val config = testContext.resources.configuration
expectedDimenMap.forEach { (resId, entry) ->
val (value, unit) = selector(entry)
val actualPx = testContext.resources.getDimension(resId)
value?.let {
val expectedPx = convertToPx(value, unit, dm, config)
assertEquals(
"Mismatch for ${testContext.resources.getResourceEntryName(resId)} ($unit)",
expectedPx,
actualPx,
0.01f
)
}
}
}
private fun convertToPx(
value: Float,
unit: DimenUnit,
dm: DisplayMetrics,
config: Configuration
): Float {
return when (unit) {
DimenUnit.DP -> value * dm.density
DimenUnit.SP -> value * dm.density * config.fontScale
DimenUnit.PX -> value
}
}
data class ExpectedDimen(
val default: Float,
val alt: Float? = null,
val unit: DimenUnit,
)
enum class DimenUnit { DP, SP, PX }
}