• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 package org.robolectric.res.android;
3 
4 import static com.google.common.truth.Truth.assertThat;
5 
6 import org.junit.Test;
7 import org.junit.runner.RunWith;
8 import org.junit.runners.JUnit4;
9 
10 @RunWith(JUnit4.class)
11 public class ResTableConfigTest {
12 
13   public static final int MCC_US_CARRIER = 310;
14   public static final int MCC_US_VERIZON = 4;
15   public static final byte[] LANGUAGE_FRENCH = new byte[] {'f', 'r'};
16   private static final byte[] LANGUAGE_SPANISH = new byte[]{'e', 's'};
17 
18   @Test
isBetterThan_emptyConfig()19   public void isBetterThan_emptyConfig() {
20     // When a configuration is not specified the result is always false
21     assertThat(newBuilder().build().isBetterThan(newBuilder().build(), newBuilder().build())).isFalse();
22   }
23 
24   /**
25    * https://developer.android.com/guide/topics/resources/providing-resources.html#MccQualifier
26    * @see <a href="http://mcc-mnc.com/">http://mcc-mnc.com/</a>
27    */
28   @Test
isBetterThan_mcc()29   public void isBetterThan_mcc() {
30     // When requested is less of a match
31     assertThat(newBuilder().setMcc(MCC_US_CARRIER).build()
32         .isBetterThan(newBuilder().setMcc(MCC_US_CARRIER).build(), newBuilder().build()))
33         .isFalse();
34 
35     // When requested is a better match
36     assertThat(newBuilder().setMcc(MCC_US_CARRIER).build()
37         .isBetterThan(newBuilder().build(), newBuilder().setMcc(MCC_US_CARRIER).build()))
38         .isTrue();
39   }
40 
41   /**
42    * https://developer.android.com/guide/topics/resources/providing-resources.html#MccQualifier
43    * @see <a href="http://mcc-mnc.com/">http://mcc-mnc.com/</a>
44    */
45   @Test
isBetterThan_mnc()46   public void isBetterThan_mnc() {
47     // When a configuration is not specified the result is always false
48     assertThat(newBuilder().build().isBetterThan(newBuilder().build(), newBuilder().build())).isFalse();
49 
50     // When requested is less of a match
51     assertThat(newBuilder().setMcc(MCC_US_CARRIER).setMnc(MCC_US_VERIZON).build()
52         .isBetterThan(newBuilder().setMcc(MCC_US_CARRIER).build(), newBuilder().build()))
53         .isFalse();
54 
55     // When requested is a better match - any US Carrier is a better match to US + Verizon
56     assertThat(newBuilder().setMcc(MCC_US_CARRIER).setMnc(MCC_US_VERIZON).build()
57         .isBetterThan(newBuilder().build(), newBuilder().setMcc(MCC_US_CARRIER).build()))
58         .isTrue();
59 
60     // When requested is a better match - any US Carrier is a better match to US + Verizon
61     assertThat(newBuilder().setMcc(MCC_US_CARRIER).setMnc(MCC_US_VERIZON).build()
62         .isBetterThan(newBuilder().setMcc(MCC_US_CARRIER).build(), newBuilder().setMcc(MCC_US_CARRIER).setMnc(MCC_US_VERIZON).build()))
63         .isTrue();
64 
65     // When requested is a better match - any US Carrier is not a better match to US + Verizon
66     assertThat(newBuilder().setMcc(MCC_US_CARRIER).setMnc(MCC_US_VERIZON).build()
67         .isBetterThan(newBuilder().setMcc(MCC_US_CARRIER).setMnc(MCC_US_VERIZON).build(), newBuilder().setMcc(MCC_US_CARRIER).build()))
68         .isFalse();
69   }
70 
71   @Test
isBetterThan_language()72   public void isBetterThan_language() {
73     // When requested has no language, is not a better match
74     assertThat(newBuilder().setLanguage(LANGUAGE_FRENCH).build()
75         .isBetterThan(newBuilder().setLanguage(LANGUAGE_FRENCH).build(), newBuilder().build()))
76         .isFalse();
77   }
78 
79   @Test
isBetterThan_language_comparedNotSame_requestedEnglish()80   public void isBetterThan_language_comparedNotSame_requestedEnglish() {
81     // When requested has no language, is not a better match
82     assertThat(newBuilder().setLanguage(LANGUAGE_FRENCH).build()
83         .isBetterThan(newBuilder().setLanguage(LANGUAGE_SPANISH).build(), newBuilder().setLanguage(
84             ResTable_config.kEnglish).build()))
85         .isTrue();
86   }
87 
88   @Test
isBetterThan_language_comparedNotSame_requestedEnglishUS()89   public void isBetterThan_language_comparedNotSame_requestedEnglishUS() {
90     // When requested has no language, is not a better match
91     assertThat(newBuilder().setLanguage(LANGUAGE_FRENCH).build()
92         .isBetterThan(newBuilder().setLanguage(LANGUAGE_SPANISH).build(), newBuilder().setLanguage(
93             ResTable_config.kEnglish).build()))
94         .isTrue();
95   }
96 
97   @Test
isBetterThan_layoutDirection_()98   public void isBetterThan_layoutDirection_() {
99     // Requested matches this configuration
100     assertThat(newBuilder().setLayoutDirection(ResTable_config.SCREENLAYOUT_LAYOUTDIR_RTL).build()
101         .isBetterThan(newBuilder().setLayoutDirection(ResTable_config.SCREENLAYOUT_LAYOUTDIR_LTR).build(),
102             newBuilder().setLayoutDirection(ResTable_config.SCREENLAYOUT_LAYOUTDIR_RTL).build()))
103         .isTrue();
104 
105     // Requested matches this configuration
106     assertThat(newBuilder().setLayoutDirection(ResTable_config.SCREENLAYOUT_LAYOUTDIR_LTR).build()
107         .isBetterThan(newBuilder().setLayoutDirection(ResTable_config.SCREENLAYOUT_LAYOUTDIR_RTL).build(),
108             newBuilder().setLayoutDirection(ResTable_config.SCREENLAYOUT_LAYOUTDIR_RTL).build()))
109         .isFalse();
110   }
111 
newBuilder()112   public static ResTableConfigBuilder newBuilder() {
113     return new ResTableConfigBuilder();
114   }
115 
116   private static class ResTableConfigBuilder {
117         int mcc;
118         int mnc;
119         byte[] language = new byte[2];
120         byte[] region = new byte[2];
121         int orientation;
122         int touchscreen;
123         int density;
124         int keyboard;
125         int navigation;
126         int inputFlags;
127         int screenWidth;
128         int screenHeight;
129         int sdkVersion;
130         int minorVersion;
131         int screenLayout;
132         int uiMode;
133         int smallestScreenWidthDp;
134         int screenWidthDp;
135         int screenHeightDp;
136         byte[] localeScript = new byte[4];
137         byte[] localeVariant = new byte[8];
138         byte screenLayout2;
139         byte screenConfigPad1;
140         short screenConfigPad2;
141 
build()142     ResTable_config build() {
143       return new ResTable_config(0, mcc, mnc, language, region, orientation, touchscreen, density, keyboard, navigation, inputFlags, screenWidth,
144           screenHeight, sdkVersion, minorVersion, screenLayout, uiMode, smallestScreenWidthDp, screenWidthDp, screenHeightDp, localeScript, localeVariant, screenLayout2,
145           screenConfigPad1, screenConfigPad2, null
146       );
147     }
148 
setMcc(int mcc)149     public ResTableConfigBuilder setMcc(int mcc) {
150       this.mcc = mcc;
151       return this;
152     }
153 
setMnc(int mnc)154     public ResTableConfigBuilder setMnc(int mnc) {
155       this.mnc = mnc;
156       return this;
157     }
158 
setLanguage(byte[] language)159     public ResTableConfigBuilder setLanguage(byte[] language) {
160       this.language = language;
161       return this;
162     }
163 
setLayoutDirection(int layoutDirection)164     public ResTableConfigBuilder setLayoutDirection(int layoutDirection) {
165       screenLayout = layoutDirection;
166       return this;
167     }
168   }
169 }
170