1package com.xtremelabs.robolectric.shadows;
2
3import static com.xtremelabs.robolectric.Robolectric.shadowOf;
4import static org.hamcrest.CoreMatchers.equalTo;
5import static org.hamcrest.CoreMatchers.instanceOf;
6import static org.hamcrest.CoreMatchers.is;
7import static org.hamcrest.CoreMatchers.notNullValue;
8import static org.junit.Assert.assertThat;
9
10import org.junit.Before;
11import org.junit.Test;
12import org.junit.runner.RunWith;
13
14import android.app.Activity;
15import android.content.res.Configuration;
16import android.content.res.Resources;
17import android.graphics.drawable.AnimationDrawable;
18import android.graphics.drawable.BitmapDrawable;
19import android.graphics.drawable.ColorDrawable;
20import android.graphics.drawable.NinePatchDrawable;
21
22import com.xtremelabs.robolectric.R;
23import com.xtremelabs.robolectric.Robolectric;
24import com.xtremelabs.robolectric.WithTestDefaultsRunner;
25import com.xtremelabs.robolectric.RobolectricTestRunnerTest.RunnerForTesting;
26import com.xtremelabs.robolectric.annotation.Values;
27import com.xtremelabs.robolectric.util.TestR;
28
29
30@RunWith(WithTestDefaultsRunner.class)
31public class ResourcesTest {
32
33	private Resources resources;
34	private ShadowContextWrapper shadowApp;
35
36	@Before
37	public void setup() {
38		resources = new Activity().getResources();
39		shadowApp = shadowOf( Robolectric.application );
40	}
41
42    @Test(expected = Resources.NotFoundException.class)
43    public void getStringArray_shouldThrowExceptionIfNotFound() throws Exception {
44        resources.getStringArray(-1);
45    }
46
47    @Test
48    public void testConfiguration() {
49        Configuration configuration = resources.getConfiguration();
50        assertThat(configuration, notNullValue());
51        assertThat(configuration.locale, notNullValue());
52    }
53
54    @Test
55    public void testConfigurationReturnsTheSameInstance() {
56        assertThat(resources.getConfiguration(), is(resources.getConfiguration()));
57    }
58
59    @Test
60    public void testNewTheme() {
61        assertThat(resources.newTheme(), notNullValue());
62    }
63
64    /**
65     * a missing R.class will result in an BitmapDrawable getting returned
66     * by default
67     */
68    @Test
69    public void testGetDrawableNullRClass() {
70    	shadowApp.getResourceLoader().setLocalRClass( null );
71    	assertThat( resources.getDrawable( TestR.anim.test_anim_1 ), instanceOf( BitmapDrawable.class ) );
72    }
73
74    /**
75     * given an R.anim.id value, will return an AnimationDrawable
76     */
77    @Test
78    public void testGetAnimationDrawable() {
79    	shadowApp.getResourceLoader().setLocalRClass( TestR.class );
80    	assertThat( resources.getDrawable( TestR.anim.test_anim_1 ), instanceOf( AnimationDrawable.class ) );
81    }
82
83    @Test
84    @Values( locale="fr" )
85    public void testGetResourceFromSpecificLocale(){
86    	String hello=resources.getString( R.string.hello );
87    	assertThat( hello, equalTo( "Bonjour" ) );
88    }
89
90    /**
91     * given an R.color.id value, will return a ColorDrawable
92     */
93    @Test
94    public void testGetColorDrawable() {
95    	shadowApp.getResourceLoader().setLocalRClass( TestR.class );
96    	assertThat( resources.getDrawable( TestR.color.test_color_1 ), instanceOf( ColorDrawable.class ) );
97    }
98
99    /**
100     * given an R.drawable.id value, will return a BitmapDrawable
101     */
102    @Test
103    public void testGetBitmapDrawable() {
104    	shadowApp.getResourceLoader().setLocalRClass( TestR.class );
105    	assertThat( resources.getDrawable( TestR.drawable.test_drawable_1 ), instanceOf( BitmapDrawable.class ) );
106    }
107
108    /**
109     * given an R.drawable.id value, will return a NinePatchDrawable for .9.png file
110     */
111    @Test
112    public void testGetNinePatchDrawable() {
113    	assertThat( resources.getDrawable( R.drawable.nine_patch_drawable ), instanceOf( NinePatchDrawable.class ) );
114    }
115
116    /**
117     * given a value that doesn't in one of R's inner classes, will return a BitmapDrawable
118     */
119    @Test
120    public void testGetBitmapDrawableForUnknownId() {
121    	shadowApp.getResourceLoader().setLocalRClass( TestR.class );
122    	assertThat( resources.getDrawable( Integer.MAX_VALUE ), instanceOf( BitmapDrawable.class ) );
123    }
124    @Test
125    public void testDensity() {
126        Activity activity = new Activity();
127        assertThat(activity.getResources().getDisplayMetrics().density, equalTo(1f));
128
129        shadowOf(activity.getResources()).setDensity(1.5f);
130        assertThat(activity.getResources().getDisplayMetrics().density, equalTo(1.5f));
131
132        Activity anotherActivity = new Activity();
133        assertThat(anotherActivity.getResources().getDisplayMetrics().density, equalTo(1.5f));
134    }
135
136    @Test
137    public void displayMetricsShouldNotHaveLotsOfZeros() throws Exception {
138        Activity activity = new Activity();
139        assertThat(activity.getResources().getDisplayMetrics().heightPixels, equalTo(800));
140        assertThat(activity.getResources().getDisplayMetrics().widthPixels, equalTo(480));
141    }
142
143
144}
145