1package com.xtremelabs.robolectric.shadows;
2
3import android.support.v4.app.Fragment;
4import android.support.v4.app.FragmentActivity;
5import com.xtremelabs.robolectric.WithTestDefaultsRunner;
6import org.junit.Before;
7import org.junit.Test;
8import org.junit.runner.RunWith;
9
10import static org.junit.Assert.assertEquals;
11import static org.junit.Assert.assertTrue;
12
13@RunWith(WithTestDefaultsRunner.class)
14public class FragmentTest {
15    private Fragment fragment;
16    private FragmentActivity fragmentActivity;
17
18    @Before
19    public void setUp() throws Exception {
20        fragmentActivity = new FragmentActivity();
21        fragment = new TestFragment();
22        fragmentActivity.getSupportFragmentManager().beginTransaction().add(fragment, null).commit();
23    }
24
25    @Test
26    public void retrieveIdOfResource() {
27        int id = fragment.getResources().getIdentifier("hello", "string", "com.xtremelabs.robolectric");
28        assertTrue(id > 0);
29
30        String hello = fragment.getResources().getString(id);
31        assertEquals("Hello", hello);
32
33        hello = fragment.getString(id);
34        assertEquals("Hello", hello);
35    }
36
37    @Test(expected = IllegalStateException.class)
38    public void unattachedFragmentsCannotGetResources() throws Exception {
39        new TestFragment().getResources();
40    }
41}
42