aboutsummaryrefslogtreecommitdiff
path: root/robolectric/src/test/java/org/robolectric/QualifiersTest.java
blob: 1d5e4a77f1534b93a333de61b34dcd093eb61fa7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package org.robolectric;

import static android.os.Build.VERSION_CODES.O;
import static androidx.test.core.app.ApplicationProvider.getApplicationContext;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;

import android.app.Activity;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.view.View;
import android.widget.TextView;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import java.util.Locale;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;

@RunWith(AndroidJUnit4.class)
public class QualifiersTest {

  private Resources resources;

  @Before
  public void setUp() throws Exception {
    resources = getApplicationContext().getResources();
  }

  @Test
  @Config(sdk = 26)
  public void testDefaultQualifiers() throws Exception {
    assertThat(RuntimeEnvironment.getQualifiers())
        .isEqualTo(
            "en-rUS-ldltr-sw320dp-w320dp-h470dp-normal-notlong-notround-nowidecg-lowdr-port-notnight-mdpi-finger-keyssoft-nokeys-navhidden-nonav");
  }

  @Test
  @Config(qualifiers = "en", sdk = 26)
  public void testDefaultQualifiers_withoutRegion() throws Exception {
    assertThat(RuntimeEnvironment.getQualifiers())
        .isEqualTo(
            "en-ldltr-sw320dp-w320dp-h470dp-normal-notlong-notround-nowidecg-lowdr-port-notnight-mdpi-finger-keyssoft-nokeys-navhidden-nonav");
  }

  @Test
  @Config(qualifiers = "land")
  public void orientation() throws Exception {
    assertThat(resources.getConfiguration().orientation).isEqualTo(Configuration.ORIENTATION_LANDSCAPE);
  }

  @Config(qualifiers = "en")
  @Test public void shouldBeEnglish() {
    Locale locale = resources.getConfiguration().locale;
    assertThat(locale.getLanguage()).isEqualTo("en");
  }

  @Config(qualifiers = "ja")
  @Test public void shouldBeJapanese() {
    Locale locale = resources.getConfiguration().locale;
    assertThat(locale.getLanguage()).isEqualTo("ja");
  }

  @Config(qualifiers = "fr")
  @Test public void shouldBeFrench() {
    Locale locale = resources.getConfiguration().locale;
    assertThat(locale.getLanguage()).isEqualTo("fr");
  }

  @Test @Config(qualifiers = "fr")
  public void shouldGetFromMethod() throws Exception {
    assertThat(RuntimeEnvironment.getQualifiers()).contains("fr");
  }

  @Test @Config(qualifiers = "de")
  public void getQuantityString() throws Exception {
    assertThat(resources.getQuantityString(R.plurals.minute, 2)).isEqualTo(
        resources.getString(R.string.minute_plural));
  }

  @Test
  public void inflateLayout_defaultsTo_sw320dp() throws Exception {
    View view = Robolectric.setupActivity(Activity.class).getLayoutInflater().inflate(R.layout.layout_smallest_width, null);
    TextView textView = view.findViewById(R.id.text1);
    assertThat(textView.getText().toString()).isEqualTo("320");

    assertThat(resources.getConfiguration().smallestScreenWidthDp).isEqualTo(320);
  }

  @Test @Config(qualifiers = "sw720dp")
  public void inflateLayout_overridesTo_sw720dp() throws Exception {
    View view = Robolectric.setupActivity(Activity.class).getLayoutInflater().inflate(R.layout.layout_smallest_width, null);
    TextView textView = view.findViewById(R.id.text1);
    assertThat(textView.getText().toString()).isEqualTo("720");

    assertThat(resources.getConfiguration().smallestScreenWidthDp).isEqualTo(720);
  }

  @Test
  @Config(qualifiers = "b+sr+Latn")
  public void supportsBcp47() throws Exception {
    assertThat(resources.getString(R.string.hello)).isEqualTo("Zdravo");
  }

  @Test
  public void defaultScreenWidth() {
    assertThat(resources.getBoolean(R.bool.value_only_present_in_w320dp)).isTrue();
    assertThat(resources.getConfiguration().screenWidthDp).isEqualTo(320);
  }

  @Test @Config(qualifiers = "land")
  public void setQualifiers_updatesSystemAndAppResources() throws Exception {
    Resources systemResources = Resources.getSystem();
    Resources appResources = getApplicationContext().getResources();

    assertThat(systemResources.getConfiguration().orientation).isEqualTo(
        Configuration.ORIENTATION_LANDSCAPE);
    assertThat(appResources.getConfiguration().orientation).isEqualTo(
        Configuration.ORIENTATION_LANDSCAPE);

    RuntimeEnvironment.setQualifiers("port");
    assertThat(systemResources.getConfiguration().orientation).isEqualTo(
        Configuration.ORIENTATION_PORTRAIT);
    assertThat(appResources.getConfiguration().orientation).isEqualTo(
        Configuration.ORIENTATION_PORTRAIT);
  }

  @Test
  public void setQualifiers_allowsSameSdkVersion() throws Exception {
    RuntimeEnvironment.setQualifiers("v" + RuntimeEnvironment.getApiLevel());
  }

  @Test
  public void setQualifiers_disallowsOtherSdkVersions() throws Exception {
    try {
      RuntimeEnvironment.setQualifiers("v13");
      fail();
    } catch (IllegalArgumentException e) {
      assertThat(e.getMessage())
          .contains("Cannot specify conflicting platform version in qualifiers");
    }
  }

  @Test
  @Config(minSdk = O, qualifiers = "widecg-highdr-vrheadset")
  public void testQualifiersNewIn26() throws Exception {
    assertThat(RuntimeEnvironment.getQualifiers()).contains("-widecg-highdr-");
    assertThat(RuntimeEnvironment.getQualifiers()).contains("-vrheadset-");
  }
}