• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.robolectric.shadows;
2 
3 import static org.junit.Assert.assertEquals;
4 import static org.robolectric.Shadows.shadowOf;
5 
6 import android.content.Context;
7 import android.view.View;
8 import android.widget.LinearLayout;
9 import android.widget.TextView;
10 import androidx.test.core.app.ApplicationProvider;
11 import androidx.test.ext.junit.runners.AndroidJUnit4;
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.junit.runner.RunWith;
15 
16 @RunWith(AndroidJUnit4.class)
17 public class ViewInnerTextTest {
18   private Context context;
19 
20   @Before
setUp()21   public void setUp() throws Exception {
22     context = ApplicationProvider.getApplicationContext();
23   }
24 
25   @Test
testInnerText()26   public void testInnerText() throws Exception {
27     LinearLayout top = new LinearLayout(context);
28     top.addView(textView("blah"));
29     top.addView(new View(context));
30     top.addView(textView("a b c"));
31 
32     LinearLayout innerLayout = new LinearLayout(context);
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
shouldOnlyIncludeViewTextViewsText()47   public void shouldOnlyIncludeViewTextViewsText() throws Exception {
48     LinearLayout top = new LinearLayout(context);
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
shouldNotPrefixBogusSpaces()57   public void shouldNotPrefixBogusSpaces() throws Exception {
58     LinearLayout top = new LinearLayout(context);
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 
textView(String text)66   private TextView textView(String text) {
67     return textView(text, View.VISIBLE);
68   }
69 
textView(String text, int visibility)70   private TextView textView(String text, int visibility) {
71     TextView textView = new TextView(context);
72     textView.setText(text);
73     textView.setVisibility(visibility);
74     return textView;
75   }
76 }
77