1package com.xtremelabs.robolectric.bytecode;
2
3import android.app.Application;
4import com.xtremelabs.robolectric.Robolectric;
5import com.xtremelabs.robolectric.WithTestDefaultsRunner;
6import org.junit.AfterClass;
7import org.junit.Before;
8import org.junit.Test;
9import org.junit.runner.RunWith;
10import org.junit.runners.model.InitializationError;
11
12import java.lang.reflect.Method;
13
14import static org.junit.Assert.assertEquals;
15import static org.junit.Assert.assertNotNull;
16import static org.junit.Assert.assertTrue;
17
18@RunWith(CustomRobolectricTestRunnerTest.CustomRobolectricTestRunner.class)
19public class CustomRobolectricTestRunnerTest {
20    Object preparedTest;
21    static Method testMethod;
22    static int beforeCallCount = 0;
23    static int afterTestCallCount = 0;
24
25    @Before
26    public void setUp() throws Exception {
27        beforeCallCount++;
28    }
29
30    @Test
31    public void shouldInitializeApplication() throws Exception {
32        assertNotNull(Robolectric.application);
33        assertEquals(CustomApplication.class, Robolectric.application.getClass());
34    }
35
36    @Test
37    public void shouldInvokePrepareTestWithAnInstanceOfTheTest() throws Exception {
38        assertEquals(this, preparedTest);
39        assertEquals(RobolectricClassLoader.class.getName(), preparedTest.getClass().getClassLoader().getClass().getName());
40    }
41
42    @Test
43    public void shouldInvokeBeforeTestWithTheCorrectMethod() throws Exception {
44        assertEquals("shouldInvokeBeforeTestWithTheCorrectMethod", testMethod.getName());
45    }
46
47    @AfterClass
48    public static void shouldHaveCalledAfterTest() {
49        assertTrue(beforeCallCount > 0);
50        assertEquals(beforeCallCount, afterTestCallCount);
51    }
52
53    public static class CustomRobolectricTestRunner extends WithTestDefaultsRunner {
54        public CustomRobolectricTestRunner(Class<?> testClass) throws InitializationError {
55            super(testClass);
56        }
57
58        @Override public void prepareTest(Object test) {
59            ((CustomRobolectricTestRunnerTest) test).preparedTest = test;
60        }
61
62        @Override public void beforeTest(Method method) {
63            testMethod = method;
64        }
65
66        @Override public void afterTest(Method method) {
67            afterTestCallCount++;
68        }
69
70        @Override protected Application createApplication() {
71            return new CustomApplication();
72        }
73    }
74
75    public static class CustomApplication extends Application {
76    }
77}
78