1 package org.robolectric.android; 2 3 import static android.os.Build.VERSION_CODES.O; 4 import static com.google.common.truth.Truth.assertThat; 5 import static org.junit.Assume.assumeTrue; 6 import static org.robolectric.shadows.ShadowAssetManager.useLegacy; 7 8 import android.content.res.Configuration; 9 import android.content.res.Resources; 10 import android.os.Build; 11 import android.os.Build.VERSION_CODES; 12 import android.view.LayoutInflater; 13 import android.view.View; 14 import android.widget.TextView; 15 import androidx.test.core.app.ApplicationProvider; 16 import androidx.test.ext.junit.runners.AndroidJUnit4; 17 import java.util.Locale; 18 import org.junit.Before; 19 import org.junit.Test; 20 import org.junit.runner.RunWith; 21 import org.robolectric.R; 22 import org.robolectric.Robolectric; 23 import org.robolectric.RuntimeEnvironment; 24 import org.robolectric.annotation.Config; 25 import org.robolectric.res.ResName; 26 import org.robolectric.res.ResourceTable; 27 28 @RunWith(AndroidJUnit4.class) 29 public class ResourceLoaderTest { 30 31 private String optsForO; 32 33 @Before setUp()34 public void setUp() { 35 assumeTrue(useLegacy()); 36 37 optsForO = RuntimeEnvironment.getApiLevel() >= O 38 ? "nowidecg-lowdr-" 39 : ""; 40 } 41 42 @Test 43 @Config(qualifiers="w0dp") checkDefaultBooleanValue()44 public void checkDefaultBooleanValue() throws Exception { 45 assertThat( 46 ApplicationProvider.getApplicationContext() 47 .getResources() 48 .getBoolean(R.bool.different_resource_boolean)) 49 .isEqualTo(false); 50 } 51 52 @Test 53 @Config(qualifiers="w820dp") checkQualifiedBooleanValue()54 public void checkQualifiedBooleanValue() throws Exception { 55 assertThat( 56 ApplicationProvider.getApplicationContext() 57 .getResources() 58 .getBoolean(R.bool.different_resource_boolean)) 59 .isEqualTo(true); 60 } 61 62 @Test checkForPollution1()63 public void checkForPollution1() throws Exception { 64 checkForPollutionHelper(); 65 } 66 67 @Test checkForPollution2()68 public void checkForPollution2() throws Exception { 69 checkForPollutionHelper(); 70 } 71 checkForPollutionHelper()72 private void checkForPollutionHelper() { 73 assertThat(RuntimeEnvironment.getQualifiers()) 74 .isEqualTo("en-rUS-ldltr-sw320dp-w320dp-h470dp-normal-notlong-notround-" + optsForO + "port-notnight-mdpi-finger-keyssoft-nokeys-navhidden-nonav-v" + Build.VERSION.RESOURCES_SDK_INT); 75 76 View view = 77 LayoutInflater.from(ApplicationProvider.getApplicationContext()) 78 .inflate(R.layout.different_screen_sizes, null); 79 TextView textView = view.findViewById(android.R.id.text1); 80 assertThat(textView.getText().toString()).isEqualTo("default"); 81 RuntimeEnvironment.setQualifiers("fr-land"); // testing if this pollutes the other test 82 Configuration configuration = Resources.getSystem().getConfiguration(); 83 if (RuntimeEnvironment.getApiLevel() <= VERSION_CODES.JELLY_BEAN) { 84 configuration.locale = new Locale("fr", "FR"); 85 } else { 86 configuration.setLocale(new Locale("fr", "FR")); 87 } 88 configuration.orientation = Configuration.ORIENTATION_LANDSCAPE; 89 Resources.getSystem().updateConfiguration(configuration, null); 90 } 91 92 @Test shouldMakeInternalResourcesAvailable()93 public void shouldMakeInternalResourcesAvailable() throws Exception { 94 ResourceTable resourceProvider = RuntimeEnvironment.getSystemResourceTable(); 95 ResName internalResource = new ResName("android", "string", "badPin"); 96 Integer resId = resourceProvider.getResourceId(internalResource); 97 assertThat(resId).isNotNull(); 98 assertThat(resourceProvider.getResName(resId)).isEqualTo(internalResource); 99 100 Class<?> internalRIdClass = Robolectric.class.getClassLoader().loadClass("com.android.internal.R$" + internalResource.type); 101 int internalResourceId; 102 internalResourceId = (Integer) internalRIdClass.getDeclaredField(internalResource.name).get(null); 103 assertThat(resId).isEqualTo(internalResourceId); 104 105 assertThat(ApplicationProvider.getApplicationContext().getResources().getString(resId)) 106 .isEqualTo("The old PIN you typed isn't correct."); 107 } 108 } 109