Refactor the Core of LocaleManager, test it

This commit is contained in:
Bradlee Speice 2014-11-10 23:45:21 -05:00
parent 7bc161214d
commit 6a45d872f8
2 changed files with 52 additions and 2 deletions

View File

@ -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);
}
}

View File

@ -12,12 +12,18 @@ class LocaleManager(val rM: RefreshManager) {
// Language doesn't have hashCode(), so we actually group by its String // Language doesn't have hashCode(), so we actually group by its String
.groupBy { FixedLanguage(it.getLanguage()) } .groupBy { FixedLanguage(it.getLanguage()) }
// I would suppress the warning here if I could figure out how...
val modulesByLanguage = languageModuleMap val modulesByLanguage = languageModuleMap
.map { GroupedObservable.from(it.getKey(): Language, it) } .map { GroupedObservable.from(it.getKey(): Language, it) }
// Cast back to the original Language implementation // Cast back to the original Language implementation
val availableLanguages: Observable<Language> = languageModuleMap.map { it.getKey() } val availableLanguages: Observable<Language> = languageModuleMap.map { it.getKey() }
val sortedLanguagesList = val sortedLanguagesList =
Core.sortedLanguagesList(availableLanguages, currentLanguage).toBlocking().first()
object Core {
fun sortedLanguagesList(availableLanguages: Observable<Language>,
currentLanguage: Language) =
availableLanguages.toSortedList {(left, right) -> availableLanguages.toSortedList {(left, right) ->
// Prioritize our current language first // Prioritize our current language first
if (left.getName() == currentLanguage.getName()) if (left.getName() == currentLanguage.getName())
@ -25,8 +31,9 @@ class LocaleManager(val rM: RefreshManager) {
else if (right.getName() == currentLanguage.getName()) else if (right.getName() == currentLanguage.getName())
1 1
else else
left.getName().compareTo(right.getName()) left.getName() compareTo right.getName()
}.toBlocking().first() }
}
// TODO: Fix the actual Language implementation - Pull Request? // TODO: Fix the actual Language implementation - Pull Request?
private class FixedLanguage(language: Language?) : private class FixedLanguage(language: Language?) :