1 /* 2 * Copyright (C) 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.textclassifier.downloader; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import android.util.Pair; 22 import com.android.textclassifier.common.ModelType; 23 import com.android.textclassifier.common.TextClassifierSettings; 24 import com.android.textclassifier.testing.TestingDeviceConfig; 25 import com.google.common.collect.ImmutableList; 26 import java.util.Locale; 27 import org.junit.Before; 28 import org.junit.Test; 29 import org.junit.runner.RunWith; 30 import org.junit.runners.JUnit4; 31 32 @RunWith(JUnit4.class) 33 public final class LocaleUtilsTest { 34 private static final String MODEL_TYPE = ModelType.ANNOTATOR; 35 36 private TestingDeviceConfig deviceConfig; 37 private TextClassifierSettings settings; 38 39 @Before setUp()40 public void setUp() { 41 deviceConfig = new TestingDeviceConfig(); 42 settings = new TextClassifierSettings(deviceConfig); 43 } 44 45 @Test lookupBestLocaleTag_simpleMatch()46 public void lookupBestLocaleTag_simpleMatch() { 47 assertThat( 48 LocaleUtils.lookupBestLocaleTag( 49 Locale.forLanguageTag("en"), ImmutableList.of("en", "zh"))) 50 .isEqualTo("en"); 51 } 52 53 @Test lookupBestLocaleTag_noMatch()54 public void lookupBestLocaleTag_noMatch() { 55 assertThat(LocaleUtils.lookupBestLocaleTag(Locale.forLanguageTag("en"), ImmutableList.of("zh"))) 56 .isNull(); 57 assertThat( 58 LocaleUtils.lookupBestLocaleTag(Locale.forLanguageTag("en"), ImmutableList.of("en-uk"))) 59 .isNull(); 60 assertThat( 61 LocaleUtils.lookupBestLocaleTag( 62 Locale.forLanguageTag("en-US"), ImmutableList.of("en-uk"))) 63 .isNull(); 64 } 65 66 @Test lookupBestLocaleTag_partialMatch()67 public void lookupBestLocaleTag_partialMatch() { 68 assertThat( 69 LocaleUtils.lookupBestLocaleTag( 70 Locale.forLanguageTag("en-US"), ImmutableList.of("en", "zh"))) 71 .isEqualTo("en"); 72 assertThat( 73 LocaleUtils.lookupBestLocaleTag( 74 Locale.forLanguageTag("en-US"), ImmutableList.of("en", "en-us"))) 75 .isEqualTo("en-us"); 76 assertThat( 77 LocaleUtils.lookupBestLocaleTag( 78 Locale.forLanguageTag("en-US"), ImmutableList.of("en", "en-uk"))) 79 .isEqualTo("en"); 80 } 81 82 @Test lookupBestLocaleTag_universalMatch()83 public void lookupBestLocaleTag_universalMatch() { 84 assertThat( 85 LocaleUtils.lookupBestLocaleTag( 86 Locale.forLanguageTag("en"), 87 ImmutableList.of("zh", LocaleUtils.UNIVERSAL_LOCALE_TAG))) 88 .isEqualTo(LocaleUtils.UNIVERSAL_LOCALE_TAG); 89 } 90 91 @Test lookupBestLocaleTagAndManifestUrl_found()92 public void lookupBestLocaleTagAndManifestUrl_found() throws Exception { 93 setUpManifestUrl(MODEL_TYPE, "en", "url_1"); 94 Pair<String, String> pair = 95 LocaleUtils.lookupBestLocaleTagAndManifestUrl( 96 MODEL_TYPE, Locale.forLanguageTag("en"), settings); 97 assertThat(pair.first).isEqualTo("en"); 98 assertThat(pair.second).isEqualTo("url_1"); 99 } 100 101 @Test lookupBestLocaleTagAndManifestUrl_notFound()102 public void lookupBestLocaleTagAndManifestUrl_notFound() throws Exception { 103 Pair<String, String> pair = 104 LocaleUtils.lookupBestLocaleTagAndManifestUrl( 105 MODEL_TYPE, Locale.forLanguageTag("en"), settings); 106 assertThat(pair).isNull(); 107 } 108 setUpManifestUrl( @odelType.ModelTypeDef String modelType, String localeTag, String url)109 private void setUpManifestUrl( 110 @ModelType.ModelTypeDef String modelType, String localeTag, String url) { 111 String deviceConfigFlag = 112 String.format(TextClassifierSettings.MANIFEST_URL_TEMPLATE, modelType, localeTag); 113 deviceConfig.setConfig(deviceConfigFlag, url); 114 } 115 } 116