1package com.xtremelabs.robolectric.shadows; 2 3import android.app.Activity; 4import android.content.Context; 5import android.view.View; 6import android.widget.LinearLayout; 7import android.widget.TextView; 8import com.xtremelabs.robolectric.WithTestDefaultsRunner; 9import org.junit.Before; 10import org.junit.Test; 11import org.junit.runner.RunWith; 12 13import static com.xtremelabs.robolectric.Robolectric.shadowOf; 14import static org.junit.Assert.assertEquals; 15 16@RunWith(WithTestDefaultsRunner.class) 17public class ViewInnerTextTest { 18 private Context activity; 19 20 @Before 21 public void setUp() throws Exception { 22 activity = new Activity(); 23 } 24 25 @Test 26 public void testInnerText() throws Exception { 27 LinearLayout top = new LinearLayout(activity); 28 top.addView(textView("blah")); 29 top.addView(new View(activity)); 30 top.addView(textView("a b c")); 31 32 LinearLayout innerLayout = new LinearLayout(activity); 33 top.addView(innerLayout); 34 35 innerLayout.addView(textView("d e f")); 36 innerLayout.addView(textView("g h i")); 37 innerLayout.addView(textView("")); 38 innerLayout.addView(textView(null)); 39 innerLayout.addView(textView("jkl!")); 40 41 top.addView(textView("mnop")); 42 43 assertEquals("blah a b c d e f g h i jkl! mnop", shadowOf(top).innerText()); 44 } 45 46 @Test 47 public void shouldOnlyIncludeViewTextViewsText() throws Exception { 48 LinearLayout top = new LinearLayout(activity); 49 top.addView(textView("blah", View.VISIBLE)); 50 top.addView(textView("blarg", View.GONE)); 51 top.addView(textView("arrg", View.INVISIBLE)); 52 53 assertEquals("blah", shadowOf(top).innerText()); 54 } 55 56 @Test 57 public void shouldNotPrefixBogusSpaces() throws Exception { 58 LinearLayout top = new LinearLayout(activity); 59 top.addView(textView("blarg", View.GONE)); 60 top.addView(textView("arrg", View.INVISIBLE)); 61 top.addView(textView("blah", View.VISIBLE)); 62 63 assertEquals("blah", shadowOf(top).innerText()); 64 } 65 66 private TextView textView(String text) { 67 return textView(text, View.VISIBLE); 68 } 69 70 private TextView textView(String text, int visibility) { 71 TextView textView = new TextView(activity); 72 textView.setText(text); 73 textView.setVisibility(visibility); 74 return textView; 75 } 76} 77