Skip to content

Commit

Permalink
fix: [#274] fixing locale on android 14 and setup test using junit & …
Browse files Browse the repository at this point in the history
…mockk (#282)

* chore: [#274] initial setup test using JUnit & MockK

* chore: [#274] add more test case at LocaleHelperTest

* fix: [#274] fix default language from shared preferences

* fix: [#274] disable language split on AAB

* chore: [#274] rename method
  • Loading branch information
andhikayuana authored Nov 25, 2024
1 parent 342ae91 commit cfc463b
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 4 deletions.
9 changes: 9 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ android {
buildConfig true
viewBinding true
}
bundle {
language {
enableSplit = false
}
}
assetPacks = [":install_time_asset_pack",":fast_follow_asset_pack_01"]
signingConfigs {
release {
Expand Down Expand Up @@ -365,6 +370,10 @@ dependencies {

// Get the latest version from https://mvnrepository.com/artifact/com.appsflyer/af-android-sdk
implementation 'com.appsflyer:af-android-sdk:6.15.1'

//test
testImplementation "io.mockk:mockk:1.13.13"
testImplementation 'junit:junit:4.13.2'
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ class IntroLanguageResource {
private val EUR: String = "EUR"
private val RMB: String = "RMB"
private val JPY: String = "JPY"
fun loadResources() : Array<IntroLanguage>{
return arrayOf<IntroLanguage> (
fun loadResources(): Array<IntroLanguage> {
return arrayOf<IntroLanguage>(
IntroLanguage(
Language.ENGLISH.code,
Language.ENGLISH.title,
Expand Down Expand Up @@ -115,4 +115,8 @@ class IntroLanguageResource {
)
)
}

fun findLanguageIndex(language: Language): Int {
return loadResources().map { intro -> intro.lang }.indexOf(language)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.content.Intent;
import android.graphics.Point;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
Expand All @@ -14,13 +15,12 @@
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.appsflyer.AppsFlyerLib;
import com.breadwallet.entities.IntroLanguageResource;
import com.breadwallet.entities.Language;
import com.breadwallet.presenter.activities.SetPinActivity;
import com.breadwallet.presenter.entities.PartnerNames;
import com.breadwallet.tools.adapter.CountryLanguageAdapter;
import com.breadwallet.tools.util.LocaleHelper;
import com.google.android.material.snackbar.Snackbar;
//import com.breadwallet.BuildConfig;
import com.breadwallet.R;
import com.breadwallet.presenter.activities.BreadActivity;
Expand Down Expand Up @@ -78,6 +78,12 @@ protected void onCreate(Bundle savedInstanceState) {
countryLanguageAdapter = new CountryLanguageAdapter(this, introLanguageResource.loadResources());
listLangRecyclerView.setAdapter(countryLanguageAdapter);

Language currentLanguage = LocaleHelper.Companion.getInstance().getCurrentLocale();
int currentIndex = introLanguageResource.findLanguageIndex(currentLanguage);
countryLanguageAdapter.updateCenterPosition(currentIndex);
new Handler().post(() -> listLangRecyclerView.scrollToPosition(currentIndex));


listLangRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/com/breadwallet/tools/util/LocaleHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.breadwallet.tools.util
import android.content.Context
import android.content.SharedPreferences
import android.os.Build
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.os.LocaleListCompat
import androidx.preference.PreferenceManager
import com.breadwallet.entities.Language
import java.util.*
Expand Down Expand Up @@ -34,6 +36,8 @@ class LocaleHelper private constructor() {
Locale.setDefault(locale)
val config = context.resources.configuration
config.setLocale(locale)
val localeList = LocaleListCompat.forLanguageTags(language.code)
AppCompatDelegate.setApplicationLocales(localeList)
return context.createConfigurationContext(config)
}

Expand Down
53 changes: 53 additions & 0 deletions app/src/test/java/com/breadwallet/tools/util/LocaleHelperTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.breadwallet.tools.util

import android.content.Context
import com.breadwallet.entities.Language
import io.mockk.mockk
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test

class LocaleHelperTest {

@Test
fun `given LocaleHelper instance, then should validate default value`() {
val context: Context = mockk(relaxed = true)

LocaleHelper.init(context)

val currentLocale = LocaleHelper.instance.currentLocale

assertTrue(currentLocale == Language.ENGLISH)
assertEquals("en", currentLocale.code)
assertEquals("English", currentLocale.title)
assertEquals("Select language", currentLocale.desc)
}

@Test
fun `getLocale invoked, should return Locale object`() {
val localeIndonesian = LocaleHelper.getLocale(Language.INDONESIAN)

assertEquals("id", localeIndonesian.language)

val localeChineseSimplified = LocaleHelper.getLocale(Language.CHINESE_SIMPLIFIED)

assertEquals("zh", localeChineseSimplified.language)
assertEquals("CN", localeChineseSimplified.country)
}

@Test
fun `setLocaleIfNeeded invoked, should update current locale`() {
val context: Context = mockk(relaxed = true)
LocaleHelper.init(context)

val currentLocale = LocaleHelper.instance.currentLocale
assertTrue(currentLocale == Language.ENGLISH)

var changed = LocaleHelper.instance.setLocaleIfNeeded(Language.INDONESIAN)
assertTrue(changed)

changed = LocaleHelper.instance.setLocaleIfNeeded(Language.INDONESIAN)
assertFalse(changed)
}
}

0 comments on commit cfc463b

Please sign in to comment.