From 6a45d872f8f7d5bf2a6cc82f1c994581589256c7 Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Mon, 10 Nov 2014 23:45:21 -0500 Subject: [PATCH] Refactor the Core of LocaleManager, test it --- .../downloader/manager/LocaleManagerTest.java | 43 +++++++++++++++++++ .../downloader/manager/LocaleManager.kt | 11 ++++- 2 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 app/src/androidTest/java/org/bspeice/minimalbible/test/activity/downloader/manager/LocaleManagerTest.java diff --git a/app/src/androidTest/java/org/bspeice/minimalbible/test/activity/downloader/manager/LocaleManagerTest.java b/app/src/androidTest/java/org/bspeice/minimalbible/test/activity/downloader/manager/LocaleManagerTest.java new file mode 100644 index 0000000..6314cfd --- /dev/null +++ b/app/src/androidTest/java/org/bspeice/minimalbible/test/activity/downloader/manager/LocaleManagerTest.java @@ -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 languages = Observable.just(english, russian, french, + german, hebrew, afrikaans); + + LocaleManager.Core core = LocaleManager.Core.INSTANCE$; + + //noinspection ConstantConditions + List 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); + } +} diff --git a/app/src/main/kotlin/org/bspeice/minimalbible/activity/downloader/manager/LocaleManager.kt b/app/src/main/kotlin/org/bspeice/minimalbible/activity/downloader/manager/LocaleManager.kt index cb0f5f1..f7e0cdc 100644 --- a/app/src/main/kotlin/org/bspeice/minimalbible/activity/downloader/manager/LocaleManager.kt +++ b/app/src/main/kotlin/org/bspeice/minimalbible/activity/downloader/manager/LocaleManager.kt @@ -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 = languageModuleMap.map { it.getKey() } val sortedLanguagesList = + Core.sortedLanguagesList(availableLanguages, currentLanguage).toBlocking().first() + + object Core { + fun sortedLanguagesList(availableLanguages: Observable, + 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?) :