1 package com.xtremelabs.robolectric.shadows; 2 3 import android.support.v4.app.Fragment; 4 import android.support.v4.app.FragmentActivity; 5 import com.xtremelabs.robolectric.WithTestDefaultsRunner; 6 import org.junit.Before; 7 import org.junit.Test; 8 import org.junit.runner.RunWith; 9 10 import static org.junit.Assert.assertEquals; 11 import static org.junit.Assert.assertTrue; 12 13 @RunWith(WithTestDefaultsRunner.class) 14 public class FragmentTest { 15 private Fragment fragment; 16 private FragmentActivity fragmentActivity; 17 18 @Before setUp()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 retrieveIdOfResource()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) unattachedFragmentsCannotGetResources()38 public void unattachedFragmentsCannotGetResources() throws Exception { 39 new TestFragment().getResources(); 40 } 41 } 42