1717package androidx .test .espresso .matcher ;
1818
1919import static androidx .test .core .app .ApplicationProvider .getApplicationContext ;
20+ import static androidx .test .espresso .Espresso .onView ;
21+ import static androidx .test .espresso .assertion .ViewAssertions .matches ;
2022import static androidx .test .espresso .matcher .MatcherTestUtils .getDescription ;
2123import static androidx .test .espresso .matcher .MatcherTestUtils .getMismatchDescription ;
2224import static androidx .test .espresso .matcher .ViewMatchers .assertThat ;
2325import static androidx .test .espresso .matcher .ViewMatchers .isDisplayingAtLeast ;
2426import static androidx .test .espresso .matcher .ViewMatchers .withEffectiveVisibility ;
2527import static org .hamcrest .Matchers .is ;
26- import static org .junit . Assert . assertFalse ;
28+ import static org .hamcrest . Matchers . not ;
2729import static org .junit .Assert .assertThrows ;
28- import static org .junit .Assert .assertTrue ;
29- import static org .mockito .ArgumentMatchers .any ;
30- import static org .mockito .Mockito .mock ;
31- import static org .mockito .Mockito .when ;
32-
33- import android .content .Context ;
34- import android .graphics .Point ;
35- import android .graphics .Rect ;
30+
3631import android .view .View ;
32+ import android .view .ViewGroup ;
33+ import android .widget .FrameLayout ;
34+ import androidx .test .core .app .testing .UiActivity ;
3735import androidx .test .espresso .matcher .ViewMatchers .Visibility ;
36+ import androidx .test .ext .junit .rules .ActivityScenarioRule ;
3837import androidx .test .ext .junit .runners .AndroidJUnit4 ;
3938import androidx .test .filters .LargeTest ;
40- import org .junit .Before ;
39+ import org .junit .Rule ;
4140import org .junit .Test ;
4241import org .junit .runner .RunWith ;
43- import org .mockito .stubbing .Answer ;
4442
4543/** Integration tests for {@link ViewMatchers#isDisplayingAtLeast(int)}. */
46- // TODO: Use real views instead of mocking.
47- @ LargeTest
4844@ RunWith (AndroidJUnit4 .class )
45+ @ LargeTest
4946public class IsDisplayingAtLeastIntegrationTest {
5047
51- private Context context ;
52-
53- @ Before
54- public void setUp () throws Exception {
55- context = getApplicationContext ();
56- }
48+ @ Rule
49+ public ActivityScenarioRule <UiActivity > activityScenarioRule =
50+ new ActivityScenarioRule <>(UiActivity .class );
5751
5852 @ Test
5953 public void invalidPercentageRange () {
6054 assertThrows (IllegalArgumentException .class , () -> isDisplayingAtLeast (-1 ));
6155 assertThrows (IllegalArgumentException .class , () -> isDisplayingAtLeast (101 ));
6256 }
6357
58+ @ Test
59+ public void fullyDisplayed () {
60+ View [] childHolder = new View [1 ];
61+ activityScenarioRule
62+ .getScenario ()
63+ .onActivity (
64+ activity -> {
65+ View child = new View (activity );
66+ childHolder [0 ] = child ;
67+ activity .setContentView (child , new ViewGroup .LayoutParams (100 , 100 ));
68+ });
69+
70+ onView (is (childHolder [0 ])).check (matches (isDisplayingAtLeast (100 )));
71+ }
72+
73+ @ Test
74+ public void fullyDisplayed_withScale () {
75+ View [] childHolder = new View [1 ];
76+ activityScenarioRule
77+ .getScenario ()
78+ .onActivity (
79+ activity -> {
80+ FrameLayout parent = new FrameLayout (activity );
81+ parent .setScaleX (0.5f );
82+ View child = new View (activity );
83+ childHolder [0 ] = child ;
84+ child .setScaleY (0.5f );
85+ parent .addView (child , new FrameLayout .LayoutParams (100 , 100 ));
86+ activity .setContentView (
87+ parent ,
88+ new ViewGroup .LayoutParams (
89+ ViewGroup .LayoutParams .MATCH_PARENT , ViewGroup .LayoutParams .MATCH_PARENT ));
90+ });
91+
92+ onView (is (childHolder [0 ])).check (matches (isDisplayingAtLeast (100 )));
93+ }
94+
6495 @ Test
6596 public void partiallyDisplayed () {
66- GlobalVisibleRectProvider providerMock = mock (GlobalVisibleRectProvider .class );
67- View view = new GlobalVisibleRectTestView (context , providerMock );
97+ View [] childHolder = new View [1 ];
98+ activityScenarioRule
99+ .getScenario ()
100+ .onActivity (
101+ activity -> {
102+ FrameLayout parent = new FrameLayout (activity );
103+ View child = new View (activity );
104+ childHolder [0 ] = child ;
105+ parent .addView (child , new FrameLayout .LayoutParams (100 , 100 ));
106+ activity .setContentView (parent , new ViewGroup .LayoutParams (50 , 50 ));
107+ });
108+
109+ // Set the view to be 100x100: 10,000 pixels, parent 50x50: 2,500 pixels (25% visible)
110+ onView (is (childHolder [0 ])).check (matches (isDisplayingAtLeast (20 )));
111+ onView (is (childHolder [0 ])).check (matches (not (isDisplayingAtLeast (30 ))));
112+ }
68113
69- view .setVisibility (View .GONE );
70- assertFalse (isDisplayingAtLeast (5 ).matches (view ));
114+ @ Test
115+ public void partiallyDisplayed_withScale () {
116+ View [] childHolder = new View [1 ];
117+ activityScenarioRule
118+ .getScenario ()
119+ .onActivity (
120+ activity -> {
121+ FrameLayout parent = new FrameLayout (activity );
122+ parent .setScaleY (-0.9f );
123+ View child = new View (activity );
124+ childHolder [0 ] = child ;
125+ child .setScaleX (0.6f );
126+ parent .addView (child , new FrameLayout .LayoutParams (100 , 100 ));
127+ activity .setContentView (parent , new ViewGroup .LayoutParams (60 , 60 ));
128+ });
129+
130+ // Scaled child: 60x90 = 5,400 pixels (horizontal range [20, 80]),
131+ // parent 60x60 clips child to 40x54 = 2,160 pixels (40% visible).
132+ onView (is (childHolder [0 ])).check (matches (isDisplayingAtLeast (39 )));
133+ onView (is (childHolder [0 ])).check (matches (not (isDisplayingAtLeast (41 ))));
134+ }
71135
72- // Set the view to be 100x100: 10,000 pixels
73- view .setVisibility (View .VISIBLE );
74- view .layout (0 , 0 , 100 , 100 );
75- when (providerMock .get (any (), any ()))
76- .then (
77- (Answer <Boolean >)
78- invocation -> {
79- // Set the output rectangle to 50x50: 2500 pixels
80- Rect argRect = invocation .getArgument (0 );
81- argRect .set (0 , 0 , 50 , 50 );
82- return true ;
83- });
84-
85- assertFalse (isDisplayingAtLeast (30 ).matches (view ));
86- assertTrue (isDisplayingAtLeast (20 ).matches (view ));
136+ @ Test
137+ public void gone () {
138+ View [] childHolder = new View [1 ];
139+ activityScenarioRule
140+ .getScenario ()
141+ .onActivity (
142+ activity -> {
143+ View child = new View (activity );
144+ childHolder [0 ] = child ;
145+ child .setVisibility (View .GONE );
146+ activity .setContentView (child , new ViewGroup .LayoutParams (100 , 100 ));
147+ });
148+
149+ onView (is (childHolder [0 ])).check (matches (not (isDisplayingAtLeast (5 ))));
87150 }
88151
89152 @ Test
@@ -99,7 +162,7 @@ public void description() {
99162
100163 @ Test
101164 public void mismatchDescription_wrongVisibility () {
102- View view = new View (context );
165+ View view = new View (getApplicationContext () );
103166 view .setVisibility (View .GONE );
104167 assertThat (
105168 getMismatchDescription (isDisplayingAtLeast (15 ), view ),
@@ -108,53 +171,33 @@ public void mismatchDescription_wrongVisibility() {
108171
109172 @ Test
110173 public void mismatchDescription_notVisible () {
111- GlobalVisibleRectProvider providerMock = mock (GlobalVisibleRectProvider .class );
112- View view = new GlobalVisibleRectTestView (context , providerMock );
174+ View view = new View (getApplicationContext ());
113175 view .setVisibility (View .VISIBLE );
114- when (providerMock .get (any (), any ())).thenReturn (false );
115176 assertThat (
116177 getMismatchDescription (isDisplayingAtLeast (15 ), view ),
117178 is ("view was <0> percent visible to the user" ));
118179 }
119180
120181 @ Test
121182 public void mismatchDescription_lowVisibility () {
122- GlobalVisibleRectProvider providerMock = mock (GlobalVisibleRectProvider .class );
123- View view = new GlobalVisibleRectTestView (context , providerMock );
124- view .setVisibility (View .VISIBLE );
125- // Set the area of the view to 100x100 = 10,000
126- view .layout (0 , 0 , 100 , 100 );
127- when (providerMock .get (any (), any ()))
128- .then (
129- (Answer <Boolean >)
130- invocation -> {
131- // Set the output rectangle to 50x50: 2500 pixels
132- Rect argRect = invocation .getArgument (0 );
133- argRect .set (0 , 0 , 50 , 50 );
134- return true ;
135- });
136- assertThat (
137- getMismatchDescription (isDisplayingAtLeast (35 ), view ),
138- is ("view was <25> percent visible to the user" ));
139- }
140-
141- /** This interface is used to mock the {@link View#getGlobalVisibleRect(Rect, Point)} method. */
142- interface GlobalVisibleRectProvider {
143- boolean get (Rect r , Point offset );
144- }
145-
146- private static class GlobalVisibleRectTestView extends View {
147-
148- private final GlobalVisibleRectProvider provider ;
149-
150- GlobalVisibleRectTestView (Context context , GlobalVisibleRectProvider provider ) {
151- super (context );
152- this .provider = provider ;
153- }
154-
155- @ Override
156- public final boolean getGlobalVisibleRect (Rect r , Point globalOffset ) {
157- return provider .get (r , globalOffset );
158- }
183+ View [] childHolder = new View [1 ];
184+ activityScenarioRule
185+ .getScenario ()
186+ .onActivity (
187+ activity -> {
188+ // Set the area of the view to 100x100 = 10,000, parent to 50x50 = 2,500: 25% visible
189+ FrameLayout parent = new FrameLayout (activity );
190+ View child = new View (activity );
191+ childHolder [0 ] = child ;
192+ parent .addView (child , new FrameLayout .LayoutParams (100 , 100 ));
193+ activity .setContentView (parent , new ViewGroup .LayoutParams (50 , 50 ));
194+ });
195+
196+ onView (is (childHolder [0 ]))
197+ .check (
198+ (view , noViewFoundException ) ->
199+ assertThat (
200+ getMismatchDescription (isDisplayingAtLeast (35 ), view ),
201+ is ("view was <25> percent visible to the user" )));
159202 }
160203}
0 commit comments