• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 Google LLC
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 package com.google.android.libraries.mobiledatadownload.populator;
17 
18 import static com.google.common.truth.Truth.assertThat;
19 
20 import com.google.common.base.Supplier;
21 import com.google.common.util.concurrent.Futures;
22 import com.google.common.util.concurrent.ListenableFuture;
23 import com.google.common.util.concurrent.MoreExecutors;
24 import com.google.mobiledatadownload.DownloadConfigProto.DataFile;
25 import com.google.mobiledatadownload.DownloadConfigProto.DataFileGroup;
26 import com.google.mobiledatadownload.DownloadConfigProto.ManifestConfig;
27 import java.util.Locale;
28 import java.util.concurrent.ExecutionException;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.robolectric.RobolectricTestRunner;
32 
33 /** Tests for {@link LocaleOverrider}. */
34 @RunWith(RobolectricTestRunner.class)
35 public class LocaleOverriderTest {
36 
37   @Test
override_equalStrategy_hasMatch()38   public void override_equalStrategy_hasMatch() throws InterruptedException, ExecutionException {
39     Supplier<Locale> localeSupplier = () -> Locale.forLanguageTag("en-US");
40     ManifestConfig config =
41         ManifestConfig.newBuilder()
42             .addEntry(createEntryWithLocaleAndIdentifier("en", "en-resource"))
43             .addEntry(createEntryWithLocaleAndIdentifier("en-US", "en-US-resource"))
44             .build();
45     LocaleOverrider overrider =
46         LocaleOverrider.builder()
47             .setLocaleSupplier(localeSupplier)
48             .setMatchStrategy(LocaleOverrider.EQUAL_STRATEGY)
49             .build();
50     DataFileGroup dataFileGroup = overrider.override(config).get().get(0);
51     assertThat(dataFileGroup.getLocale(0)).isEqualTo("en-US");
52     assertThat(dataFileGroup.toString()).contains("en-US-resource");
53   }
54 
55   @Test
override_equalStrategy_hasMatch_localeLF()56   public void override_equalStrategy_hasMatch_localeLF()
57       throws InterruptedException, ExecutionException {
58     Supplier<ListenableFuture<Locale>> localeSupplier =
59         () -> Futures.immediateFuture(Locale.forLanguageTag("en-US"));
60     ManifestConfig config =
61         ManifestConfig.newBuilder()
62             .addEntry(createEntryWithLocaleAndIdentifier("en", "en-resource"))
63             .addEntry(createEntryWithLocaleAndIdentifier("en-US", "en-US-resource"))
64             .build();
65     LocaleOverrider overrider =
66         LocaleOverrider.builder()
67             .setLocaleFutureSupplier(localeSupplier, MoreExecutors.directExecutor())
68             .setMatchStrategy(LocaleOverrider.EQUAL_STRATEGY)
69             .build();
70     DataFileGroup dataFileGroup = overrider.override(config).get().get(0);
71     assertThat(dataFileGroup.getLocale(0)).isEqualTo("en-US");
72     assertThat(dataFileGroup.toString()).contains("en-US-resource");
73   }
74 
75   @Test
override_equalStrategy_noMatch()76   public void override_equalStrategy_noMatch() throws InterruptedException, ExecutionException {
77     Supplier<Locale> localeSupplier = () -> Locale.forLanguageTag("jp-JP");
78     ManifestConfig config =
79         ManifestConfig.newBuilder()
80             .addEntry(createEntryWithLocaleAndIdentifier("en", "en-resource"))
81             .addEntry(createEntryWithLocaleAndIdentifier("en-US", "en-US-resource"))
82             .build();
83     LocaleOverrider overrider =
84         LocaleOverrider.builder()
85             .setLocaleSupplier(localeSupplier)
86             .setMatchStrategy(LocaleOverrider.EQUAL_STRATEGY)
87             .build();
88 
89     assertThat(overrider.override(config).get()).isEmpty();
90   }
91 
92   @Test
override_langFallbackStrategy_exactMatch()93   public void override_langFallbackStrategy_exactMatch()
94       throws InterruptedException, ExecutionException {
95     Supplier<Locale> localeSupplier = () -> Locale.forLanguageTag("en-US");
96     ManifestConfig config =
97         ManifestConfig.newBuilder()
98             .addEntry(createEntryWithLocaleAndIdentifier("en", "en-resource"))
99             .addEntry(createEntryWithLocaleAndIdentifier("en-US", "en-US-resource"))
100             .build();
101     LocaleOverrider overrider =
102         LocaleOverrider.builder()
103             .setLocaleSupplier(localeSupplier)
104             .setMatchStrategy(LocaleOverrider.LANG_FALLBACK_STRATEGY)
105             .build();
106 
107     assertThat(overrider.override(config).get().get(0).toString()).contains("en-US-resource");
108   }
109 
110   @Test
override_langFallbackStrategy_fallbackMatch()111   public void override_langFallbackStrategy_fallbackMatch()
112       throws InterruptedException, ExecutionException {
113     Supplier<Locale> localeSupplier = () -> Locale.forLanguageTag("en-US");
114     ManifestConfig config =
115         ManifestConfig.newBuilder()
116             .addEntry(createEntryWithLocaleAndIdentifier("en", "en-resource", true))
117             .addEntry(createEntryWithLocaleAndIdentifier("en-GB", "en-GB-resource", true))
118             .build();
119     LocaleOverrider overrider =
120         LocaleOverrider.builder()
121             .setLocaleSupplier(localeSupplier)
122             .setMatchStrategy(LocaleOverrider.LANG_FALLBACK_STRATEGY)
123             .build();
124 
125     DataFileGroup dataFileGroup = overrider.override(config).get().get(0);
126     assertThat(dataFileGroup.getLocale(0)).isEqualTo("en");
127     assertThat(dataFileGroup.getLocaleCount()).isEqualTo(1);
128     assertThat(dataFileGroup.toString()).contains("en-resource");
129   }
130 
131   @Test
override_langFallbackStrategy_noMatch()132   public void override_langFallbackStrategy_noMatch()
133       throws InterruptedException, ExecutionException {
134     Supplier<Locale> localeSupplier = () -> Locale.forLanguageTag("en-US");
135     ManifestConfig config =
136         ManifestConfig.newBuilder()
137             .addEntry(createEntryWithLocaleAndIdentifier("jp", "jp-resource"))
138             .addEntry(createEntryWithLocaleAndIdentifier("en-GB", "en-GB-resource"))
139             .build();
140     LocaleOverrider overrider =
141         LocaleOverrider.builder()
142             .setLocaleSupplier(localeSupplier)
143             .setMatchStrategy(LocaleOverrider.EQUAL_STRATEGY)
144             .build();
145 
146     assertThat(overrider.override(config).get()).isEmpty();
147   }
148 
149   /**
150    * Creates a {@link ManifestConfig.Entry} with {@code locale} and some field set to {@code
151    * identifier}
152    */
createEntryWithLocaleAndIdentifier( String locale, String identifier)153   private static ManifestConfig.Entry createEntryWithLocaleAndIdentifier(
154       String locale, String identifier) {
155     return ManifestConfig.Entry.newBuilder()
156         .setModifier(ManifestConfig.Entry.Modifier.newBuilder().addLocale(locale).build())
157         .setDataFileGroup(
158             DataFileGroup.newBuilder().addFile(DataFile.newBuilder().setFileId(identifier)))
159         .build();
160   }
161 
162   /**
163    * Creates a {@link ManifestConfig.Entry} with {@code locale} and some field set to {@code
164    * identifier}
165    *
166    * @param isLocaleSetInDF if true, locale is set to DataFileGroup
167    */
createEntryWithLocaleAndIdentifier( String locale, String identifier, boolean isLocaleSetInDF)168   private static ManifestConfig.Entry createEntryWithLocaleAndIdentifier(
169       String locale, String identifier, boolean isLocaleSetInDF) {
170     if (isLocaleSetInDF) {
171       return ManifestConfig.Entry.newBuilder()
172           .setModifier(ManifestConfig.Entry.Modifier.newBuilder().addLocale(locale).build())
173           .setDataFileGroup(
174               DataFileGroup.newBuilder()
175                   .addFile(DataFile.newBuilder().setFileId(identifier))
176                   .addLocale(locale))
177           .build();
178     }
179     return createEntryWithLocaleAndIdentifier(locale, identifier);
180   }
181 }
182