1616
1717package com .example .android .pictureinpicture ;
1818
19- import static androidx .test .espresso .Espresso .onView ;
20- import static androidx .test .espresso .action .ViewActions .click ;
21- import static androidx .test .espresso .assertion .ViewAssertions .matches ;
22- import static androidx .test .espresso .matcher .ViewMatchers .isAssignableFrom ;
23- import static androidx .test .espresso .matcher .ViewMatchers .isDisplayed ;
24- import static androidx .test .espresso .matcher .ViewMatchers .withId ;
25-
26- import static org .hamcrest .Matchers .not ;
27- import static org .hamcrest .core .AllOf .allOf ;
28- import static org .junit .Assert .assertNotNull ;
29- import static org .junit .Assert .assertThat ;
30- import static org .junit .Assert .assertTrue ;
31-
3219import android .content .pm .ActivityInfo ;
33- import androidx .test .platform .app .InstrumentationRegistry ;
20+ import android .view .View ;
21+
22+ import androidx .core .graphics .Insets ;
23+ import androidx .core .view .ViewCompat ;
24+ import androidx .core .view .WindowInsetsCompat ;
3425import androidx .test .espresso .UiController ;
3526import androidx .test .espresso .ViewAction ;
36- import androidx .test .rule . ActivityTestRule ;
27+ import androidx .test .ext . junit . rules . ActivityScenarioRule ;
3728import androidx .test .ext .junit .runners .AndroidJUnit4 ;
38- import android .view .View ;
29+ import androidx .test .filters .LargeTest ;
30+ import androidx .test .platform .app .InstrumentationRegistry ;
3931
4032import com .example .android .pictureinpicture .widget .MovieView ;
4133
4638import org .junit .Test ;
4739import org .junit .runner .RunWith ;
4840
41+ import static androidx .test .espresso .Espresso .onView ;
42+ import static androidx .test .espresso .action .ViewActions .click ;
43+ import static androidx .test .espresso .assertion .ViewAssertions .matches ;
44+ import static androidx .test .espresso .matcher .ViewMatchers .isAssignableFrom ;
45+ import static androidx .test .espresso .matcher .ViewMatchers .isDisplayed ;
46+ import static androidx .test .espresso .matcher .ViewMatchers .withId ;
47+ import static org .hamcrest .Matchers .is ;
48+ import static org .hamcrest .Matchers .not ;
49+ import static org .hamcrest .core .AllOf .allOf ;
50+ import static org .junit .Assert .assertNotNull ;
51+ import static org .junit .Assert .assertThat ;
52+ import static org .junit .Assert .assertTrue ;
4953
5054@ RunWith (AndroidJUnit4 .class )
55+ @ LargeTest
5156public class MainActivityTest {
5257
5358 @ Rule
54- public ActivityTestRule <MainActivity > rule = new ActivityTestRule <>(MainActivity .class );
59+ public ActivityScenarioRule <MainActivity > rule = new ActivityScenarioRule <>(MainActivity .class );
5560
5661 @ Test
57- public void movie_playingOnPip () throws Throwable {
62+ public void movie_playingOnPip () {
5863 // The movie should be playing on start
5964 onView (withId (R .id .movie ))
6065 .check (matches (allOf (isDisplayed (), isPlaying ())))
@@ -63,21 +68,18 @@ public void movie_playingOnPip() throws Throwable {
6368 onView (withId (R .id .minimize )).perform (click ());
6469 InstrumentationRegistry .getInstrumentation ().waitForIdleSync ();
6570 // The Activity is paused. We cannot use Espresso to test paused activities.
66- rule .runOnUiThread (new Runnable () {
67- @ Override
68- public void run () {
69- // We are now in Picture-in-Picture mode
70- assertTrue (rule .getActivity ().isInPictureInPictureMode ());
71- final MovieView view = rule .getActivity ().findViewById (R .id .movie );
72- assertNotNull (view );
73- // The video should still be playing
74- assertTrue (view .isPlaying ());
75- }
71+ rule .getScenario ().onActivity (activity -> {
72+ // We are now in Picture-in-Picture mode
73+ assertTrue (activity .isInPictureInPictureMode ());
74+ final MovieView view = activity .findViewById (R .id .movie );
75+ assertNotNull (view );
76+ // The video should still be playing
77+ assertTrue (view .isPlaying ());
7678 });
7779 }
7880
7981 @ Test
80- public void movie_pauseAndResume () throws Throwable {
82+ public void movie_pauseAndResume () {
8183 // The movie should be playing on start
8284 onView (withId (R .id .movie ))
8385 .check (matches (allOf (isDisplayed (), isPlaying ())))
@@ -91,42 +93,36 @@ public void movie_pauseAndResume() throws Throwable {
9193 }
9294
9395 @ Test
94- public void fullscreen_enabledOnLandscape () throws Throwable {
95- rule .runOnUiThread (new Runnable () {
96- @ Override
97- public void run () {
98- rule .getActivity ()
99- .setRequestedOrientation (ActivityInfo .SCREEN_ORIENTATION_LANDSCAPE );
100- }
101- });
96+ public void fullscreen_enabledOnLandscape () {
97+ rule .getScenario ().onActivity (activity ->
98+ activity .setRequestedOrientation (ActivityInfo .SCREEN_ORIENTATION_LANDSCAPE )
99+ );
102100 InstrumentationRegistry .getInstrumentation ().waitForIdleSync ();
103- rule .runOnUiThread (new Runnable () {
104- @ Override
105- public void run () {
106- final View decorView = rule .getActivity ().getWindow ().getDecorView ();
107- assertThat (decorView .getSystemUiVisibility (),
108- hasFlag (View .SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN ));
109- }
101+ rule .getScenario ().onActivity (activity -> {
102+ final WindowInsetsCompat insets = ViewCompat .getRootWindowInsets (
103+ activity .getWindow ().getDecorView ()
104+ );
105+ final Insets systemBars = insets .getInsets (WindowInsetsCompat .Type .systemBars ());
106+ assertThat (systemBars .left , is (0 ));
107+ assertThat (systemBars .top , is (0 ));
108+ assertThat (systemBars .right , is (0 ));
109+ assertThat (systemBars .bottom , is (0 ));
110110 });
111111 }
112112
113113 @ Test
114- public void fullscreen_disabledOnPortrait () throws Throwable {
115- rule .runOnUiThread (new Runnable () {
116- @ Override
117- public void run () {
118- rule .getActivity ()
119- .setRequestedOrientation (ActivityInfo .SCREEN_ORIENTATION_PORTRAIT );
120- }
121- });
114+ public void fullscreen_disabledOnPortrait () {
115+ rule .getScenario ().onActivity (activity ->
116+ activity .setRequestedOrientation (ActivityInfo .SCREEN_ORIENTATION_PORTRAIT )
117+ );
122118 InstrumentationRegistry .getInstrumentation ().waitForIdleSync ();
123- rule .runOnUiThread ( new Runnable () {
124- @ Override
125- public void run () {
126- final View decorView = rule . getActivity (). getWindow (). getDecorView ( );
127- assertThat ( decorView . getSystemUiVisibility (),
128- not (hasFlag ( View . SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN )));
129- }
119+ rule .getScenario (). onActivity ( activity -> {
120+ final WindowInsetsCompat insets = ViewCompat . getRootWindowInsets (
121+ activity . getWindow (). getDecorView ()
122+ );
123+ final Insets systemBars = insets . getInsets ( WindowInsetsCompat . Type . systemBars ());
124+ assertThat ( systemBars . top , is ( not (0 )));
125+ assertThat ( systemBars . bottom , is ( not ( 0 )));
130126 });
131127 }
132128
@@ -164,19 +160,4 @@ public void perform(UiController uiController, View view) {
164160 }
165161 };
166162 }
167-
168- private static Matcher <? super Integer > hasFlag (final int flag ) {
169- return new TypeSafeMatcher <Integer >() {
170- @ Override
171- protected boolean matchesSafely (Integer i ) {
172- return (i & flag ) == flag ;
173- }
174-
175- @ Override
176- public void describeTo (Description description ) {
177- description .appendText ("Flag integer contains " + flag );
178- }
179- };
180- }
181-
182163}
0 commit comments