1package com.xtremelabs.robolectric;
2
3import static com.xtremelabs.robolectric.Robolectric.shadowOf;
4import static org.junit.Assert.*;
5
6import org.junit.Test;
7import org.junit.runner.RunWith;
8import org.junit.runners.model.InitializationError;
9
10import android.app.Activity;
11import android.app.Application;
12import android.widget.TextView;
13
14import com.xtremelabs.robolectric.annotation.DisableStrictI18n;
15import com.xtremelabs.robolectric.annotation.EnableStrictI18n;
16import com.xtremelabs.robolectric.annotation.Values;
17import com.xtremelabs.robolectric.res.ResourceLoader;
18
19@RunWith(RobolectricTestRunnerTest.RunnerForTesting.class)
20public class RobolectricTestRunnerTest {
21
22    @Test
23    public void shouldInitializeAndBindApplicationButNotCallOnCreate() throws Exception {
24        assertNotNull(Robolectric.application);
25        assertEquals(MyTestApplication.class, Robolectric.application.getClass());
26        assertFalse(((MyTestApplication) Robolectric.application).onCreateWasCalled);
27        assertNotNull(shadowOf(Robolectric.application).getResourceLoader());
28    }
29
30    @Test
31    public void setStaticValue_shouldIgnoreFinalModifier() {
32        RobolectricTestRunner.setStaticValue(android.os.Build.class, "MODEL", "expected value");
33
34        assertEquals("expected value", android.os.Build.MODEL);
35    }
36
37    @Test
38    @EnableStrictI18n
39    public void internalBeforeTest_setsI18nStrictModeFromProperty() {
40    	assertTrue(RunnerForTesting.instance.robolectricConfig.getStrictI18n());
41    }
42
43    @Test
44    @DisableStrictI18n
45    public void internalBeforeTest_clearsI18nStrictModeFromProperty() {
46    	assertFalse(RunnerForTesting.instance.robolectricConfig.getStrictI18n());
47    }
48
49    @Test
50    @Values( locale="fr")
51    public void internalBeforeTest_setLocale(){
52    	assertEquals( RunnerForTesting.instance.robolectricConfig.getLocale(), "fr" );
53    }
54
55    @Test
56    public void internalBeforeTest_doesNotsetI18nStrictModeFromSystemIfPropertyAbsent() {
57    	assertFalse(RunnerForTesting.instance.robolectricConfig.getStrictI18n());
58    }
59
60    @Test
61    @EnableStrictI18n
62    public void methodBlock_setsI18nStrictModeForClassHandler() {
63    	TextView tv = new TextView(Robolectric.application);
64    	try {
65    		tv.setText("Foo");
66    		fail("TextView#setText(String) should produce an i18nException");
67    	} catch (Exception e) {
68    		// Compare exception name because it was loaded in the instrumented classloader
69    		assertEquals("com.xtremelabs.robolectric.util.I18nException", e.getClass().getName());
70    	}
71    }
72
73    @Test
74    @EnableStrictI18n
75    public void createResourceLoader_setsI18nStrictModeForResourceLoader() {
76    	ResourceLoader loader = Robolectric.shadowOf(Robolectric.application).getResourceLoader();
77    	assertTrue(RunnerForTesting.instance.robolectricConfig.getStrictI18n());
78		assertTrue(loader.getStrictI18n());
79    	try {
80    		loader.inflateView(Robolectric.application, R.layout.text_views, null);
81    		fail("ResourceLoader#inflateView should produce an i18nException");
82    	} catch (Exception e) {
83    		assertEquals("com.xtremelabs.robolectric.util.I18nException", e.getClass().getName());
84    	}
85    }
86
87    public static class RunnerForTesting extends WithTestDefaultsRunner {
88    	public static RunnerForTesting instance;
89
90        public RunnerForTesting(Class<?> testClass) throws InitializationError {
91            super(testClass);
92        	instance = this;
93        }
94
95        @Override protected Application createApplication() {
96            return new MyTestApplication();
97        }
98    }
99
100    public static class MyTestApplication extends Application {
101        private boolean onCreateWasCalled;
102
103        @Override public void onCreate() {
104            this.onCreateWasCalled = true;
105        }
106    }
107
108    public static class MyTestActivity extends Activity {
109
110    }
111
112}
113