xref: /aosp_15_r20/external/robolectric/resources/src/test/java/org/robolectric/res/android/ResTableConfigTest.java (revision e6ba16074e6af37d123cb567d575f496bf0a58ee)
1 package org.robolectric.res.android;
2 
3 import static com.google.common.truth.Truth.assertThat;
4 
5 import org.junit.Test;
6 import org.junit.runner.RunWith;
7 import org.junit.runners.JUnit4;
8 
9 @RunWith(JUnit4.class)
10 public class ResTableConfigTest {
11 
12   public static final int MCC_US_CARRIER = 310;
13   public static final int MCC_US_VERIZON = 4;
14   public static final byte[] LANGUAGE_FRENCH = new byte[] {'f', 'r'};
15   private static final byte[] LANGUAGE_SPANISH = new byte[] {'e', 's'};
16 
17   @Test
isBetterThan_emptyConfig()18   public void isBetterThan_emptyConfig() {
19     // When a configuration is not specified the result is always false
20     assertThat(newBuilder().build().isBetterThan(newBuilder().build(), newBuilder().build()))
21         .isFalse();
22   }
23 
24   /**
25    * https://developer.android.com/guide/topics/resources/providing-resources.html#MccQualifier
26    *
27    * @see <a href="http://mcc-mnc.com/">http://mcc-mnc.com/</a>
28    */
29   @Test
isBetterThan_mcc()30   public void isBetterThan_mcc() {
31     // When requested is less of a match
32     assertThat(
33             newBuilder()
34                 .setMcc(MCC_US_CARRIER)
35                 .build()
36                 .isBetterThan(newBuilder().setMcc(MCC_US_CARRIER).build(), newBuilder().build()))
37         .isFalse();
38 
39     // When requested is a better match
40     assertThat(
41             newBuilder()
42                 .setMcc(MCC_US_CARRIER)
43                 .build()
44                 .isBetterThan(newBuilder().build(), newBuilder().setMcc(MCC_US_CARRIER).build()))
45         .isTrue();
46   }
47 
48   /**
49    * https://developer.android.com/guide/topics/resources/providing-resources.html#MccQualifier
50    *
51    * @see <a href="http://mcc-mnc.com/">http://mcc-mnc.com/</a>
52    */
53   @Test
isBetterThan_mnc()54   public void isBetterThan_mnc() {
55     // When a configuration is not specified the result is always false
56     assertThat(newBuilder().build().isBetterThan(newBuilder().build(), newBuilder().build()))
57         .isFalse();
58 
59     // When requested is less of a match
60     assertThat(
61             newBuilder()
62                 .setMcc(MCC_US_CARRIER)
63                 .setMnc(MCC_US_VERIZON)
64                 .build()
65                 .isBetterThan(newBuilder().setMcc(MCC_US_CARRIER).build(), newBuilder().build()))
66         .isFalse();
67 
68     // When requested is a better match - any US Carrier is a better match to US + Verizon
69     assertThat(
70             newBuilder()
71                 .setMcc(MCC_US_CARRIER)
72                 .setMnc(MCC_US_VERIZON)
73                 .build()
74                 .isBetterThan(newBuilder().build(), newBuilder().setMcc(MCC_US_CARRIER).build()))
75         .isTrue();
76 
77     // When requested is a better match - any US Carrier is a better match to US + Verizon
78     assertThat(
79             newBuilder()
80                 .setMcc(MCC_US_CARRIER)
81                 .setMnc(MCC_US_VERIZON)
82                 .build()
83                 .isBetterThan(
84                     newBuilder().setMcc(MCC_US_CARRIER).build(),
85                     newBuilder().setMcc(MCC_US_CARRIER).setMnc(MCC_US_VERIZON).build()))
86         .isTrue();
87 
88     // When requested is a better match - any US Carrier is not a better match to US + Verizon
89     assertThat(
90             newBuilder()
91                 .setMcc(MCC_US_CARRIER)
92                 .setMnc(MCC_US_VERIZON)
93                 .build()
94                 .isBetterThan(
95                     newBuilder().setMcc(MCC_US_CARRIER).setMnc(MCC_US_VERIZON).build(),
96                     newBuilder().setMcc(MCC_US_CARRIER).build()))
97         .isFalse();
98   }
99 
100   @Test
isBetterThan_language()101   public void isBetterThan_language() {
102     // When requested has no language, is not a better match
103     assertThat(
104             newBuilder()
105                 .setLanguage(LANGUAGE_FRENCH)
106                 .build()
107                 .isBetterThan(
108                     newBuilder().setLanguage(LANGUAGE_FRENCH).build(), newBuilder().build()))
109         .isFalse();
110   }
111 
112   @Test
isBetterThan_language_comparedNotSame_requestedEnglish()113   public void isBetterThan_language_comparedNotSame_requestedEnglish() {
114     // When requested has no language, is not a better match
115     assertThat(
116             newBuilder()
117                 .setLanguage(LANGUAGE_FRENCH)
118                 .build()
119                 .isBetterThan(
120                     newBuilder().setLanguage(LANGUAGE_SPANISH).build(),
121                     newBuilder().setLanguage(ResTable_config.kEnglish).build()))
122         .isTrue();
123   }
124 
125   @Test
isBetterThan_language_comparedNotSame_requestedEnglishUS()126   public void isBetterThan_language_comparedNotSame_requestedEnglishUS() {
127     // When requested has no language, is not a better match
128     assertThat(
129             newBuilder()
130                 .setLanguage(LANGUAGE_FRENCH)
131                 .build()
132                 .isBetterThan(
133                     newBuilder().setLanguage(LANGUAGE_SPANISH).build(),
134                     newBuilder().setLanguage(ResTable_config.kEnglish).build()))
135         .isTrue();
136   }
137 
138   @Test
isBetterThan_layoutDirection_()139   public void isBetterThan_layoutDirection_() {
140     // Requested matches this configuration
141     assertThat(
142             newBuilder()
143                 .setLayoutDirection(ResTable_config.SCREENLAYOUT_LAYOUTDIR_RTL)
144                 .build()
145                 .isBetterThan(
146                     newBuilder()
147                         .setLayoutDirection(ResTable_config.SCREENLAYOUT_LAYOUTDIR_LTR)
148                         .build(),
149                     newBuilder()
150                         .setLayoutDirection(ResTable_config.SCREENLAYOUT_LAYOUTDIR_RTL)
151                         .build()))
152         .isTrue();
153 
154     // Requested matches this configuration
155     assertThat(
156             newBuilder()
157                 .setLayoutDirection(ResTable_config.SCREENLAYOUT_LAYOUTDIR_LTR)
158                 .build()
159                 .isBetterThan(
160                     newBuilder()
161                         .setLayoutDirection(ResTable_config.SCREENLAYOUT_LAYOUTDIR_RTL)
162                         .build(),
163                     newBuilder()
164                         .setLayoutDirection(ResTable_config.SCREENLAYOUT_LAYOUTDIR_RTL)
165                         .build()))
166         .isFalse();
167   }
168 
newBuilder()169   public static ResTableConfigBuilder newBuilder() {
170     return new ResTableConfigBuilder();
171   }
172 
173   private static class ResTableConfigBuilder {
174     int mcc;
175     int mnc;
176     byte[] language = new byte[2];
177     byte[] region = new byte[2];
178     int orientation;
179     int touchscreen;
180     int density;
181     int keyboard;
182     int navigation;
183     int inputFlags;
184     int screenWidth;
185     int screenHeight;
186     int sdkVersion;
187     int minorVersion;
188     int screenLayout;
189     int uiMode;
190     int smallestScreenWidthDp;
191     int screenWidthDp;
192     int screenHeightDp;
193     byte[] localeScript = new byte[4];
194     byte[] localeVariant = new byte[8];
195     byte screenLayout2;
196     byte screenConfigPad1;
197     short screenConfigPad2;
198 
build()199     ResTable_config build() {
200       return new ResTable_config(
201           0,
202           mcc,
203           mnc,
204           language,
205           region,
206           orientation,
207           touchscreen,
208           density,
209           keyboard,
210           navigation,
211           inputFlags,
212           screenWidth,
213           screenHeight,
214           sdkVersion,
215           minorVersion,
216           screenLayout,
217           uiMode,
218           smallestScreenWidthDp,
219           screenWidthDp,
220           screenHeightDp,
221           localeScript,
222           localeVariant,
223           screenLayout2,
224           screenConfigPad1,
225           screenConfigPad2,
226           null);
227     }
228 
setMcc(int mcc)229     public ResTableConfigBuilder setMcc(int mcc) {
230       this.mcc = mcc;
231       return this;
232     }
233 
setMnc(int mnc)234     public ResTableConfigBuilder setMnc(int mnc) {
235       this.mnc = mnc;
236       return this;
237     }
238 
setLanguage(byte[] language)239     public ResTableConfigBuilder setLanguage(byte[] language) {
240       this.language = language;
241       return this;
242     }
243 
setLayoutDirection(int layoutDirection)244     public ResTableConfigBuilder setLayoutDirection(int layoutDirection) {
245       screenLayout = layoutDirection;
246       return this;
247     }
248   }
249 }
250