-
-
Notifications
You must be signed in to change notification settings - Fork 389
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve language list prioritization #3293
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package com.keylesspalace.tusky.util | ||
|
||
import androidx.appcompat.app.AppCompatDelegate | ||
import androidx.core.os.LocaleListCompat | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import com.keylesspalace.tusky.db.AccountEntity | ||
import org.junit.Assert | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.mockito.Mockito | ||
import org.robolectric.annotation.Config | ||
|
||
@Config(sdk = [28]) | ||
@RunWith(AndroidJUnit4::class) | ||
class LocaleUtilsTest { | ||
@Test | ||
fun initialLanguagesContainReplySelectedAppAndSystem() { | ||
val expectedLanguages = arrayOf<String?>("yi", "tok", "da", "fr", "sv", "kab") | ||
val languages = getMockedInitialLanguages(expectedLanguages) | ||
Assert.assertArrayEquals(expectedLanguages, languages.subList(0, expectedLanguages.size).toTypedArray()) | ||
} | ||
|
||
@Test | ||
fun whenReplyLanguageIsNull_DefaultLanguageIsFirst() { | ||
val defaultLanguage = "tok" | ||
val languages = getMockedInitialLanguages(arrayOf(null, defaultLanguage, "da", "fr", "sv", "kab")) | ||
Assert.assertEquals(defaultLanguage, languages[0]) | ||
} | ||
|
||
@Test | ||
fun initialLanguagesAreDistinct() { | ||
val defaultLanguage = "da" | ||
val languages = getMockedInitialLanguages(arrayOf(defaultLanguage, defaultLanguage, "fr", defaultLanguage, "kab", defaultLanguage)) | ||
Assert.assertEquals(1, languages.count { it == defaultLanguage }) | ||
} | ||
|
||
@Test | ||
fun initialLanguageDeduplicationDoesNotReorder() { | ||
val defaultLanguage = "da" | ||
|
||
Assert.assertEquals( | ||
defaultLanguage, | ||
getMockedInitialLanguages(arrayOf(defaultLanguage, defaultLanguage, "fr", defaultLanguage, "kab", defaultLanguage))[0] | ||
) | ||
Assert.assertEquals( | ||
defaultLanguage, | ||
getMockedInitialLanguages(arrayOf(null, defaultLanguage, "fr", defaultLanguage, "kab", defaultLanguage))[0] | ||
) | ||
} | ||
|
||
@Test | ||
fun emptyInitialLanguagesAreDropped() { | ||
val languages = getMockedInitialLanguages(arrayOf("", "", "fr", "", "kab", "")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (curiosity) Why are there empty languages? Or I have seen null checks for locales in lists? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The default post language and reply language can both be empty in normal usage |
||
Assert.assertFalse(languages.any { it.isEmpty() }) | ||
} | ||
|
||
private fun getMockedInitialLanguages(configuredLanguages: Array<String?>): List<String> { | ||
val appLanguages = LocaleListCompat.forLanguageTags(configuredLanguages.slice(2 until 4).joinToString(",")) | ||
val systemLanguages = LocaleListCompat.forLanguageTags(configuredLanguages.slice(4 until configuredLanguages.size).joinToString(",")) | ||
|
||
Mockito.mockStatic(AppCompatDelegate::class.java).use { appCompatDelegate -> | ||
appCompatDelegate.`when`<LocaleListCompat> { AppCompatDelegate.getApplicationLocales() }.thenReturn(appLanguages) | ||
|
||
Mockito.mockStatic(LocaleListCompat::class.java).use { localeListCompat -> | ||
localeListCompat.`when`<LocaleListCompat> { LocaleListCompat.getDefault() }.thenReturn(systemLanguages) | ||
|
||
return getInitialLanguages( | ||
configuredLanguages[0], | ||
AccountEntity( | ||
id = 0, | ||
domain = "foo.bar", | ||
accessToken = "", | ||
clientId = null, | ||
clientSecret = null, | ||
isActive = true, | ||
defaultPostLanguage = configuredLanguages[1] ?: "", | ||
) | ||
) | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't a test simply call the normal method with some test data?
(One wonders what is tested about a mocked list.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function is a wrapper that mocks the response for querying the configured app and system languages, so that: