1 package com.xtremelabs.robolectric.shadows; 2 3 import android.content.Context; 4 import android.view.View; 5 import android.view.ViewStub; 6 import android.widget.LinearLayout; 7 import com.xtremelabs.robolectric.R; 8 import com.xtremelabs.robolectric.Robolectric; 9 import com.xtremelabs.robolectric.WithTestDefaultsRunner; 10 import com.xtremelabs.robolectric.tester.android.util.TestAttributeSet; 11 import org.junit.Before; 12 import org.junit.Test; 13 import org.junit.runner.RunWith; 14 15 import static org.junit.Assert.*; 16 17 @RunWith(WithTestDefaultsRunner.class) 18 public class ViewStubTest { 19 private Context ctxt; 20 21 @Before public void setUp() throws Exception { 22 ctxt = Robolectric.application; 23 } 24 25 @Test 26 public void inflate_shouldReplaceOriginalWithLayout() throws Exception { 27 ViewStub viewStub = new ViewStub(ctxt); 28 int stubId = 12345; 29 int inflatedId = 12346; 30 31 viewStub.setId(stubId); 32 viewStub.setInflatedId(inflatedId); 33 viewStub.setLayoutResource(R.layout.media); 34 35 LinearLayout root = new LinearLayout(ctxt); 36 root.addView(new View(ctxt)); 37 root.addView(viewStub); 38 root.addView(new View(ctxt)); 39 40 View inflatedView = viewStub.inflate(); 41 assertNotNull(inflatedView); 42 assertSame(inflatedView, root.findViewById(inflatedId)); 43 44 assertNull(root.findViewById(stubId)); 45 46 assertEquals(1, root.indexOfChild(inflatedView)); 47 assertEquals(3, root.getChildCount()); 48 } 49 50 @Test 51 public void shouldApplyAttributes() throws Exception { 52 ViewStub viewStub = new ViewStub(ctxt, 53 new TestAttributeSet() 54 .put("android:inflatedId", "@+id/include_id") 55 .put("android:layout", "@layout/media") 56 ); 57 58 assertEquals(R.id.include_id, viewStub.getInflatedId()); 59 assertEquals(R.layout.media, viewStub.getLayoutResource()); 60 } 61 } 62