mirror of
https://github.com/MinimalBible/MinimalBible
synced 2024-11-04 23:28:19 -05:00
Refactor the Core of LocaleManager, test it
This commit is contained in:
parent
7bc161214d
commit
6a45d872f8
@ -0,0 +1,43 @@
|
||||
package org.bspeice.minimalbible.test.activity.downloader.manager;
|
||||
|
||||
import org.bspeice.minimalbible.MBTestCase;
|
||||
import org.bspeice.minimalbible.activity.downloader.manager.LocaleManager;
|
||||
import org.crosswire.common.util.Language;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import rx.Observable;
|
||||
|
||||
/**
|
||||
* Test cases for the Locale Manager
|
||||
*/
|
||||
public class LocaleManagerTest extends MBTestCase {
|
||||
|
||||
public void testSortedLanguagesList() {
|
||||
Language english = new Language("en");
|
||||
Language russian = new Language("ru");
|
||||
Language french = new Language("fr");
|
||||
Language german = new Language("de");
|
||||
Language hebrew = new Language("he");
|
||||
Language afrikaans = new Language("af");
|
||||
|
||||
Observable<Language> languages = Observable.just(english, russian, french,
|
||||
german, hebrew, afrikaans);
|
||||
|
||||
LocaleManager.Core core = LocaleManager.Core.INSTANCE$;
|
||||
|
||||
//noinspection ConstantConditions
|
||||
List<Language> sortedLanguages = core.sortedLanguagesList(languages, english)
|
||||
.toBlocking().first();
|
||||
|
||||
// First language should be the 'current' (note this is an identity compare)
|
||||
assertTrue(sortedLanguages.get(0) == english);
|
||||
// Second language should be 'less than' third
|
||||
assertTrue(sortedLanguages.toString(),
|
||||
sortedLanguages.get(1).toString().compareTo(
|
||||
sortedLanguages.get(2).toString()) < 0);
|
||||
// Fifth language should be greater than the fourth
|
||||
assertTrue(sortedLanguages.toString(), sortedLanguages.get(4).toString().compareTo(
|
||||
sortedLanguages.get(3).toString()) > 0);
|
||||
}
|
||||
}
|
@ -12,12 +12,18 @@ class LocaleManager(val rM: RefreshManager) {
|
||||
// Language doesn't have hashCode(), so we actually group by its String
|
||||
.groupBy { FixedLanguage(it.getLanguage()) }
|
||||
|
||||
// I would suppress the warning here if I could figure out how...
|
||||
val modulesByLanguage = languageModuleMap
|
||||
.map { GroupedObservable.from(it.getKey(): Language, it) }
|
||||
|
||||
// Cast back to the original Language implementation
|
||||
val availableLanguages: Observable<Language> = languageModuleMap.map { it.getKey() }
|
||||
val sortedLanguagesList =
|
||||
Core.sortedLanguagesList(availableLanguages, currentLanguage).toBlocking().first()
|
||||
|
||||
object Core {
|
||||
fun sortedLanguagesList(availableLanguages: Observable<Language>,
|
||||
currentLanguage: Language) =
|
||||
availableLanguages.toSortedList {(left, right) ->
|
||||
// Prioritize our current language first
|
||||
if (left.getName() == currentLanguage.getName())
|
||||
@ -25,8 +31,9 @@ class LocaleManager(val rM: RefreshManager) {
|
||||
else if (right.getName() == currentLanguage.getName())
|
||||
1
|
||||
else
|
||||
left.getName().compareTo(right.getName())
|
||||
}.toBlocking().first()
|
||||
left.getName() compareTo right.getName()
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Fix the actual Language implementation - Pull Request?
|
||||
private class FixedLanguage(language: Language?) :
|
||||
|
Loading…
Reference in New Issue
Block a user