diff options
author | Maurice Lam <yukl@google.com> | 2018-12-13 12:03:13 -0800 |
---|---|---|
committer | Maurice Lam <yukl@google.com> | 2019-03-19 14:20:45 -0700 |
commit | 4cafc9e99b6ae30ed7ca43f5e612b6f2d867bbcd (patch) | |
tree | 808b29bb3e3b9271c9ab248644940f506a4c637e /tests | |
parent | e9667b239be11175c1af25374e9bf5f6f3bcbd05 (diff) | |
download | localepicker-4cafc9e99b6ae30ed7ca43f5e612b6f2d867bbcd.tar.gz |
Separate locale suggestion code to static library
Test: atest RunLcoalePickerRoboTests
Bug: 114040620
Change-Id: Ic47278955e837302aa3c7e68705d5cca86e30de6
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Android.mk | 66 | ||||
-rw-r--r-- | tests/AndroidManifest.xml | 21 | ||||
-rw-r--r-- | tests/config/robolectric.properties | 16 | ||||
-rw-r--r-- | tests/src/com/android/localepicker/LocaleHelperTest.java | 82 | ||||
-rw-r--r-- | tests/src/com/android/localepicker/LocaleStoreTest.java | 89 | ||||
-rw-r--r-- | tests/src/com/android/localepicker/SuggestedLocaleAdapterTest.java | 140 |
6 files changed, 414 insertions, 0 deletions
diff --git a/tests/Android.mk b/tests/Android.mk new file mode 100644 index 0000000..194ab27 --- /dev/null +++ b/tests/Android.mk @@ -0,0 +1,66 @@ +############################################################# +# Build test package for locale picker lib. # +############################################################# + +LOCAL_PATH := $(call my-dir) +include $(CLEAR_VARS) + +LOCAL_PACKAGE_NAME := LocalePickerTest + +LOCAL_PRIVATE_PLATFORM_APIS := true +LOCAL_PROGUARD_ENABLED := disabled + +LOCAL_STATIC_ANDROID_LIBRARIES += localepicker + +LOCAL_USE_AAPT2 := true + +LOCAL_MODULE_TAGS := optional + +include $(BUILD_PACKAGE) + +############################################################# +# LocalePicker Robolectric test target. # +############################################################# +include $(CLEAR_VARS) + +LOCAL_MODULE := LocalePickerRoboTests +LOCAL_MODULE_CLASS := JAVA_LIBRARIES + +LOCAL_SRC_FILES := $(call all-java-files-under, src) +LOCAL_RESOURCE_DIR := $(LOCAL_PATH)/res +LOCAL_JAVA_RESOURCE_DIRS := config + +LOCAL_JAVA_LIBRARIES := \ + robolectric_android-all-stub \ + Robolectric_all-target \ + mockito-robolectric-prebuilt \ + truth-prebuilt + +LOCAL_INSTRUMENTATION_FOR := LocalePickerTest + +LOCAL_MODULE_TAGS := optional + +# Generate test_config.properties +include external/robolectric-shadows/gen_test_config.mk + +include $(BUILD_STATIC_JAVA_LIBRARY) + +############################################################# +# LocalePicker runner target to run the previous target. # +############################################################# +include $(CLEAR_VARS) + +LOCAL_MODULE := RunLocalePickerRoboTests + +LOCAL_JAVA_LIBRARIES := \ + LocalePickerRoboTests \ + robolectric_android-all-stub \ + Robolectric_all-target \ + mockito-robolectric-prebuilt \ + truth-prebuilt + +LOCAL_TEST_PACKAGE := LocalePickerTest + +LOCAL_INSTRUMENT_SOURCE_DIRS := $(LOCAL_PATH)/../src + +include external/robolectric-shadows/run_robotests.mk diff --git a/tests/AndroidManifest.xml b/tests/AndroidManifest.xml new file mode 100644 index 0000000..bc6f0b6 --- /dev/null +++ b/tests/AndroidManifest.xml @@ -0,0 +1,21 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + Copyright (C) 2018 The Android Open Source Project + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> + +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="com.android.localepicker.test"> + +</manifest>
\ No newline at end of file diff --git a/tests/config/robolectric.properties b/tests/config/robolectric.properties new file mode 100644 index 0000000..4c863dc --- /dev/null +++ b/tests/config/robolectric.properties @@ -0,0 +1,16 @@ +# +# Copyright (C) 2018 Google Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +sdk=NEWEST_SDK diff --git a/tests/src/com/android/localepicker/LocaleHelperTest.java b/tests/src/com/android/localepicker/LocaleHelperTest.java new file mode 100644 index 0000000..e4ddb42 --- /dev/null +++ b/tests/src/com/android/localepicker/LocaleHelperTest.java @@ -0,0 +1,82 @@ +/* + * Copyright 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +package com.android.localepicker; + +import static com.google.common.truth.Truth.assertThat; + +import com.android.localepicker.LocaleHelper.LocaleInfoComparator; +import com.android.localepicker.LocaleStore.LocaleInfo; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Locale; + +@RunWith(RobolectricTestRunner.class) +public class LocaleHelperTest { + + @Test + public void toSentenceCase_shouldUpperCaseFirstLetter() { + String sentenceCase = LocaleHelper.toSentenceCase("hello Google", Locale.US); + assertThat(sentenceCase).isEqualTo("Hello Google"); + } + + @Test + public void normalizeForSearch_sameStrings_shouldBeEqualAfterNormalized() { + String lowerCase = LocaleHelper.normalizeForSearch("english", Locale.US); + String upperCase = LocaleHelper.normalizeForSearch("ENGLISH", Locale.US); + assertThat(lowerCase).isEqualTo(upperCase); + } + + @Test + public void getDisplayName_shouldReturnLocaleDisplayName() { + String displayName = + LocaleHelper.getDisplayName(Locale.US, Locale.US, /* sentenceCase */ true); + assertThat(displayName).isEqualTo("English (United States)"); + } + + @Test + public void getDisplayName_withDifferentLocale_shouldReturnLocalizedDisplayName() { + String displayName = + LocaleHelper.getDisplayName( + Locale.CANADA_FRENCH, + Locale.US, + /* sentenceCase */ true); + assertThat(displayName).isEqualTo("French (Canada)"); + } + + @Test + public void getDisplayCountry_shouldReturnLocalizedCountryName() { + String displayCountry = LocaleHelper.getDisplayCountry(Locale.GERMANY, Locale.GERMANY); + assertThat(displayCountry).isEqualTo("Deutschland"); + } + + @Test + public void localeInfoComparator_shouldSortLocales() { + LocaleInfo germany = LocaleStore.getLocaleInfo(Locale.GERMANY); + LocaleInfo unitedStates = LocaleStore.getLocaleInfo(Locale.US); + LocaleInfo japan = LocaleStore.getLocaleInfo(Locale.JAPAN); + + ArrayList<LocaleInfo> list = + new ArrayList<>(Arrays.asList(germany, unitedStates, japan)); + list.sort(new LocaleInfoComparator(Locale.US, /* countryMode */ false)); + assertThat(list).containsExactly(germany, unitedStates, japan).inOrder(); + } +} diff --git a/tests/src/com/android/localepicker/LocaleStoreTest.java b/tests/src/com/android/localepicker/LocaleStoreTest.java new file mode 100644 index 0000000..ae406e7 --- /dev/null +++ b/tests/src/com/android/localepicker/LocaleStoreTest.java @@ -0,0 +1,89 @@ +/* + * Copyright 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +package com.android.localepicker; + +import static com.google.common.truth.Truth.assertThat; + +import static org.robolectric.RuntimeEnvironment.application; +import static org.robolectric.Shadows.shadowOf; + +import android.icu.util.ULocale; +import android.telephony.TelephonyManager; + +import com.android.internal.app.LocalePicker; +import com.android.localepicker.LocaleStore.LocaleInfo; +import com.android.localepicker.LocaleStoreTest.ShadowICU; +import com.android.localepicker.LocaleStoreTest.ShadowLocalePicker; + +import libcore.icu.ICU; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; +import org.robolectric.annotation.Implementation; +import org.robolectric.annotation.Implements; + +import java.util.Locale; + +@RunWith(RobolectricTestRunner.class) +@Config( + shadows = { + ShadowLocalePicker.class, + ShadowICU.class, + }) +public class LocaleStoreTest { + + @Before + public void setupTelephonyManager() { + TelephonyManager telephonyManager = application.getSystemService(TelephonyManager.class); + shadowOf(telephonyManager).setNetworkCountryIso("us"); + shadowOf(telephonyManager).setSimCountryIso("us"); + } + + @Before + public void fillCache() { + LocaleStore.fillCache(application); + } + + @Test + public void getLevel() { + LocaleInfo localeInfo = LocaleStore.getLocaleInfo(Locale.forLanguageTag("zh-Hant-HK")); + assertThat(localeInfo.getParent().toLanguageTag()).isEqualTo("zh-Hant"); + assertThat(localeInfo.isTranslated()).named("is translated").isTrue(); + } + + @Implements(LocalePicker.class) + public static class ShadowLocalePicker { + + @Implementation + public static String[] getSystemAssetLocales() { + return new String[] { "en-US", "zh-HK", "ja-JP", "zh-TW" }; + } + } + + @Implements(ICU.class) + public static class ShadowICU { + + @Implementation + public static Locale addLikelySubtags(Locale locale) { + ULocale uLocale = ULocale.addLikelySubtags(ULocale.forLocale(locale)); + return uLocale.toLocale(); + } + } +}
\ No newline at end of file diff --git a/tests/src/com/android/localepicker/SuggestedLocaleAdapterTest.java b/tests/src/com/android/localepicker/SuggestedLocaleAdapterTest.java new file mode 100644 index 0000000..2c735dd --- /dev/null +++ b/tests/src/com/android/localepicker/SuggestedLocaleAdapterTest.java @@ -0,0 +1,140 @@ +/* + * Copyright 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +package com.android.localepicker; + +import static com.google.common.truth.Truth.assertThat; + +import com.android.localepicker.LocaleHelper.LocaleInfoComparator; +import com.android.localepicker.LocaleStore.LocaleInfo; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; + +import java.util.HashSet; +import java.util.List; +import java.util.Locale; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +@RunWith(RobolectricTestRunner.class) +public class SuggestedLocaleAdapterTest { + + private HashSet<LocaleInfo> mLocaleOptions; + private HashSet<LocaleInfo> mEnglishCountryOptions; + + @Before + public void setUp() { + mLocaleOptions = new HashSet<>(); + mLocaleOptions.add(LocaleStore.getLocaleInfo(Locale.US)); + mLocaleOptions.add(LocaleStore.getLocaleInfo(Locale.GERMANY)); + mLocaleOptions.add(LocaleStore.getLocaleInfo(Locale.JAPAN)); + LocaleInfo korea = LocaleStore.getLocaleInfo(Locale.KOREA); + korea.setTranslated(true); + korea.mSuggestionFlags = LocaleInfo.SUGGESTION_TYPE_SIM; + mLocaleOptions.add(korea); + + mEnglishCountryOptions = new HashSet<>(); + mEnglishCountryOptions.add(LocaleStore.getLocaleInfo(Locale.US)); + mEnglishCountryOptions.add(LocaleStore.getLocaleInfo(Locale.UK)); + mEnglishCountryOptions.add(LocaleStore.getLocaleInfo(Locale.CANADA)); + mEnglishCountryOptions.add(LocaleStore.getLocaleInfo(Locale.forLanguageTag("en-IN"))); + mEnglishCountryOptions.add(LocaleStore.getLocaleInfo(Locale.forLanguageTag("en-HK"))); + mEnglishCountryOptions.add(LocaleStore.getLocaleInfo(Locale.forLanguageTag("en-SG"))); + LocaleInfo australianEnglish = LocaleStore.getLocaleInfo(Locale.forLanguageTag("en-AU")); + australianEnglish.setTranslated(true); + australianEnglish.mSuggestionFlags = LocaleInfo.SUGGESTION_TYPE_SIM; + mEnglishCountryOptions.add(australianEnglish); + } + + @Test + public void suggestedLocaleAdapter_notCountryMode_shouldDisplayLocalesSorted() { + SuggestedLocaleAdapter suggestedLocaleAdapter = + new SuggestedLocaleAdapter(mLocaleOptions, /* countryMode */ false); + suggestedLocaleAdapter.sort(new LocaleInfoComparator(Locale.US, /* countryMode */ false)); + + assertThat(getItemTypeList(suggestedLocaleAdapter)) + .containsExactly( + SuggestedLocaleAdapter.TYPE_HEADER_SUGGESTED, + SuggestedLocaleAdapter.TYPE_LOCALE, + SuggestedLocaleAdapter.TYPE_HEADER_ALL_OTHERS, + SuggestedLocaleAdapter.TYPE_LOCALE, + SuggestedLocaleAdapter.TYPE_LOCALE, + SuggestedLocaleAdapter.TYPE_LOCALE) + .inOrder(); + assertThat(getLocaleTagList(suggestedLocaleAdapter)) + .containsExactly( + null, + "ko-KR", + null, + "de-DE", + "en-US", + "ja-JP") + .inOrder(); + } + + @Test + public void suggestedLocaleAdapter_countryMode_shouldDisplayCountriesSorted() { + SuggestedLocaleAdapter suggestedLocaleAdapter = + new SuggestedLocaleAdapter(mEnglishCountryOptions, /* countryMode */ true); + suggestedLocaleAdapter.sort(new LocaleInfoComparator(Locale.US, /* countryMode */ true)); + + assertThat(getItemTypeList(suggestedLocaleAdapter)) + .containsExactly( + SuggestedLocaleAdapter.TYPE_HEADER_SUGGESTED, + SuggestedLocaleAdapter.TYPE_LOCALE, + SuggestedLocaleAdapter.TYPE_HEADER_ALL_OTHERS, + SuggestedLocaleAdapter.TYPE_LOCALE, + SuggestedLocaleAdapter.TYPE_LOCALE, + SuggestedLocaleAdapter.TYPE_LOCALE, + SuggestedLocaleAdapter.TYPE_LOCALE, + SuggestedLocaleAdapter.TYPE_LOCALE, + SuggestedLocaleAdapter.TYPE_LOCALE) + .inOrder(); + assertThat(getLocaleTagList(suggestedLocaleAdapter)) + .containsExactly( + null, + "en-AU", + null, + "en-CA", + "en-HK", + "en-IN", + "en-SG", + "en-GB", + "en-US") + .inOrder(); + } + + private static List<Integer> getItemTypeList(final SuggestedLocaleAdapter adapter) { + return IntStream.range(0, adapter.getCount()) + .mapToObj(adapter::getItemViewType) + .collect(Collectors.toList()); + } + + private static List<String> getLocaleTagList(final SuggestedLocaleAdapter adapter) { + return IntStream.range(0, adapter.getCount()) + .mapToObj(position -> { + LocaleInfo localeInfo = adapter.getItem(position); + if (localeInfo == null) { + return null; + } + return localeInfo.getLocale().toLanguageTag(); + }) + .collect(Collectors.toList()); + } +} |