1 /*
2  * Copyright 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License
15  */
16 
17 package com.android.localepicker;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import com.android.localepicker.LocaleHelper.LocaleInfoComparator;
22 import com.android.localepicker.LocaleStore.LocaleInfo;
23 
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.robolectric.RobolectricTestRunner;
27 
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.Locale;
31 
32 @RunWith(RobolectricTestRunner.class)
33 public class LocaleHelperTest {
34 
35     @Test
toSentenceCase_shouldUpperCaseFirstLetter()36     public void toSentenceCase_shouldUpperCaseFirstLetter() {
37         String sentenceCase = LocaleHelper.toSentenceCase("hello Google", Locale.US);
38         assertThat(sentenceCase).isEqualTo("Hello Google");
39     }
40 
41     @Test
normalizeForSearch_sameStrings_shouldBeEqualAfterNormalized()42     public void normalizeForSearch_sameStrings_shouldBeEqualAfterNormalized() {
43         String lowerCase = LocaleHelper.normalizeForSearch("english", Locale.US);
44         String upperCase = LocaleHelper.normalizeForSearch("ENGLISH", Locale.US);
45         assertThat(lowerCase).isEqualTo(upperCase);
46     }
47 
48     @Test
getDisplayName_shouldReturnLocaleDisplayName()49     public void getDisplayName_shouldReturnLocaleDisplayName() {
50         String displayName =
51                 LocaleHelper.getDisplayName(Locale.US, Locale.US, /* sentenceCase */ true);
52         assertThat(displayName).isEqualTo("English (United States)");
53     }
54 
55     @Test
getDisplayName_withDifferentLocale_shouldReturnLocalizedDisplayName()56     public void getDisplayName_withDifferentLocale_shouldReturnLocalizedDisplayName() {
57         String displayName =
58                 LocaleHelper.getDisplayName(
59                         Locale.CANADA_FRENCH,
60                         Locale.US,
61                         /* sentenceCase */ true);
62         assertThat(displayName).isEqualTo("French (Canada)");
63     }
64 
65     @Test
getDisplayCountry_shouldReturnLocalizedCountryName()66     public void getDisplayCountry_shouldReturnLocalizedCountryName() {
67         String displayCountry = LocaleHelper.getDisplayCountry(Locale.GERMANY, Locale.GERMANY);
68         assertThat(displayCountry).isEqualTo("Deutschland");
69     }
70 
71     @Test
localeInfoComparator_shouldSortLocales()72     public void localeInfoComparator_shouldSortLocales() {
73         LocaleInfo germany = LocaleStore.getLocaleInfo(Locale.GERMANY);
74         LocaleInfo unitedStates = LocaleStore.getLocaleInfo(Locale.US);
75         LocaleInfo japan = LocaleStore.getLocaleInfo(Locale.JAPAN);
76 
77         ArrayList<LocaleInfo> list =
78                 new ArrayList<>(Arrays.asList(germany, unitedStates, japan));
79         list.sort(new LocaleInfoComparator(Locale.US, /* countryMode */ false));
80         assertThat(list).containsExactly(germany, unitedStates, japan).inOrder();
81     }
82 }
83