1 package org.robolectric;
2 
3 import static android.os.Build.VERSION_CODES.O;
4 import static androidx.test.core.app.ApplicationProvider.getApplicationContext;
5 import static com.google.common.truth.Truth.assertThat;
6 import static org.junit.Assert.fail;
7 
8 import android.app.Activity;
9 import android.content.res.Configuration;
10 import android.content.res.Resources;
11 import android.os.Build.VERSION_CODES;
12 import android.view.View;
13 import android.widget.TextView;
14 import androidx.test.ext.junit.runners.AndroidJUnit4;
15 import java.util.Locale;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.junit.runner.RunWith;
19 import org.robolectric.annotation.Config;
20 
21 @RunWith(AndroidJUnit4.class)
22 public class QualifiersTest {
23 
24   private Resources resources;
25 
26   @Before
setUp()27   public void setUp() throws Exception {
28     resources = getApplicationContext().getResources();
29   }
30 
31   @Test
32   @Config(sdk = 26)
testDefaultQualifiers()33   public void testDefaultQualifiers() throws Exception {
34     assertThat(RuntimeEnvironment.getQualifiers())
35         .isEqualTo("en-rUS-ldltr-sw320dp-w320dp-h470dp-normal-notlong-notround-nowidecg-lowdr-port-notnight-mdpi-finger-keyssoft-nokeys-navhidden-nonav-v26");
36   }
37 
38   @Test
39   @Config(qualifiers = "en", sdk = 26)
testDefaultQualifiers_withoutRegion()40   public void testDefaultQualifiers_withoutRegion() throws Exception {
41     assertThat(RuntimeEnvironment.getQualifiers())
42         .isEqualTo("en-ldltr-sw320dp-w320dp-h470dp-normal-notlong-notround-nowidecg-lowdr-port-notnight-mdpi-finger-keyssoft-nokeys-navhidden-nonav-v26");
43   }
44 
45   @Test
46   @Config(qualifiers = "land")
orientation()47   public void orientation() throws Exception {
48     assertThat(resources.getConfiguration().orientation).isEqualTo(Configuration.ORIENTATION_LANDSCAPE);
49   }
50 
51   @Config(qualifiers = "en")
shouldBeEnglish()52   @Test public void shouldBeEnglish() {
53     Locale locale = resources.getConfiguration().locale;
54     assertThat(locale.getLanguage()).isEqualTo("en");
55   }
56 
57   @Config(qualifiers = "ja")
shouldBeJapanese()58   @Test public void shouldBeJapanese() {
59     Locale locale = resources.getConfiguration().locale;
60     assertThat(locale.getLanguage()).isEqualTo("ja");
61   }
62 
63   @Config(qualifiers = "fr")
shouldBeFrench()64   @Test public void shouldBeFrench() {
65     Locale locale = resources.getConfiguration().locale;
66     assertThat(locale.getLanguage()).isEqualTo("fr");
67   }
68 
69   @Test @Config(qualifiers = "fr")
shouldGetFromMethod()70   public void shouldGetFromMethod() throws Exception {
71     assertThat(RuntimeEnvironment.getQualifiers()).contains("fr");
72   }
73 
74   @Test @Config(qualifiers = "de")
getQuantityString()75   public void getQuantityString() throws Exception {
76     assertThat(resources.getQuantityString(R.plurals.minute, 2)).isEqualTo(
77         resources.getString(R.string.minute_plural));
78   }
79 
80   @Test
inflateLayout_defaultsTo_sw320dp()81   public void inflateLayout_defaultsTo_sw320dp() throws Exception {
82     View view = Robolectric.setupActivity(Activity.class).getLayoutInflater().inflate(R.layout.layout_smallest_width, null);
83     TextView textView = view.findViewById(R.id.text1);
84     assertThat(textView.getText()).isEqualTo("320");
85 
86     assertThat(resources.getConfiguration().smallestScreenWidthDp).isEqualTo(320);
87   }
88 
89   @Test @Config(qualifiers = "sw720dp")
inflateLayout_overridesTo_sw720dp()90   public void inflateLayout_overridesTo_sw720dp() throws Exception {
91     View view = Robolectric.setupActivity(Activity.class).getLayoutInflater().inflate(R.layout.layout_smallest_width, null);
92     TextView textView = view.findViewById(R.id.text1);
93     assertThat(textView.getText()).isEqualTo("720");
94 
95     assertThat(resources.getConfiguration().smallestScreenWidthDp).isEqualTo(720);
96   }
97 
98   @Test @Config(qualifiers = "b+sr+Latn", minSdk = VERSION_CODES.LOLLIPOP)
supportsBcp47()99   public void supportsBcp47() throws Exception {
100     assertThat(resources.getString(R.string.hello)).isEqualTo("Zdravo");
101   }
102 
103   @Test
defaultScreenWidth()104   public void defaultScreenWidth() {
105     assertThat(resources.getBoolean(R.bool.value_only_present_in_w320dp)).isTrue();
106     assertThat(resources.getConfiguration().screenWidthDp).isEqualTo(320);
107   }
108 
109   @Test @Config(qualifiers = "land")
setQualifiers_updatesSystemAndAppResources()110   public void setQualifiers_updatesSystemAndAppResources() throws Exception {
111     Resources systemResources = Resources.getSystem();
112     Resources appResources = getApplicationContext().getResources();
113 
114     assertThat(systemResources.getConfiguration().orientation).isEqualTo(
115         Configuration.ORIENTATION_LANDSCAPE);
116     assertThat(appResources.getConfiguration().orientation).isEqualTo(
117         Configuration.ORIENTATION_LANDSCAPE);
118 
119     RuntimeEnvironment.setQualifiers("port");
120     assertThat(systemResources.getConfiguration().orientation).isEqualTo(
121         Configuration.ORIENTATION_PORTRAIT);
122     assertThat(appResources.getConfiguration().orientation).isEqualTo(
123         Configuration.ORIENTATION_PORTRAIT);
124   }
125 
126   @Test
setQualifiers_allowsSameSdkVersion()127   public void setQualifiers_allowsSameSdkVersion() throws Exception {
128     RuntimeEnvironment.setQualifiers("v" + RuntimeEnvironment.getApiLevel());
129   }
130 
131   @Test
setQualifiers_disallowsOtherSdkVersions()132   public void setQualifiers_disallowsOtherSdkVersions() throws Exception {
133     try {
134       RuntimeEnvironment.setQualifiers("v13");
135       fail();
136     } catch (IllegalArgumentException e) {
137       assertThat(e.getMessage()).contains("Cannot specify conflicting platform version in qualifiers");
138     }
139   }
140 
141   @Test
142   @Config(minSdk = O, qualifiers = "widecg-highdr-vrheadset")
testQualifiersNewIn26()143   public void testQualifiersNewIn26() throws Exception {
144     assertThat(RuntimeEnvironment.getQualifiers()).contains("-widecg-highdr-");
145     assertThat(RuntimeEnvironment.getQualifiers()).contains("-vrheadset-");
146   }
147 }
148