Skip to content

Commit

Permalink
Merge pull request android#69 from android/yaraki-PictureInPicture
Browse files Browse the repository at this point in the history
[PictureInPicture] Update the PiP samples
  • Loading branch information
yaraki committed Mar 11, 2021
2 parents 7923125 + e3c62cc commit 596d081
Show file tree
Hide file tree
Showing 17 changed files with 738 additions and 688 deletions.
23 changes: 14 additions & 9 deletions PictureInPicture/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 29
compileSdkVersion 30
defaultConfig {
applicationId "com.example.android.pictureinpicture"
minSdkVersion 26
targetSdkVersion 29
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand All @@ -32,15 +32,20 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.media:media:1.1.0'
implementation 'androidx.core:core:1.5.0-beta03'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.media:media:1.2.1'

testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test:rules:1.2.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test:runner:1.3.0'
androidTestImplementation 'androidx.test:rules:1.3.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,18 @@

package com.example.android.pictureinpicture;

import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.isAssignableFrom;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withId;

import static org.hamcrest.Matchers.not;
import static org.hamcrest.core.AllOf.allOf;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import android.content.pm.ActivityInfo;
import androidx.test.platform.app.InstrumentationRegistry;
import android.view.View;

import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.test.espresso.UiController;
import androidx.test.espresso.ViewAction;
import androidx.test.rule.ActivityTestRule;
import androidx.test.ext.junit.rules.ActivityScenarioRule;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import android.view.View;
import androidx.test.filters.LargeTest;
import androidx.test.platform.app.InstrumentationRegistry;

import com.example.android.pictureinpicture.widget.MovieView;

Expand All @@ -46,15 +38,28 @@
import org.junit.Test;
import org.junit.runner.RunWith;

import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.isAssignableFrom;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.core.AllOf.allOf;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

@RunWith(AndroidJUnit4.class)
@LargeTest
public class MainActivityTest {

@Rule
public ActivityTestRule<MainActivity> rule = new ActivityTestRule<>(MainActivity.class);
public ActivityScenarioRule<MainActivity> rule = new ActivityScenarioRule<>(MainActivity.class);

@Test
public void movie_playingOnPip() throws Throwable {
public void movie_playingOnPip() {
// The movie should be playing on start
onView(withId(R.id.movie))
.check(matches(allOf(isDisplayed(), isPlaying())))
Expand All @@ -63,21 +68,18 @@ public void movie_playingOnPip() throws Throwable {
onView(withId(R.id.minimize)).perform(click());
InstrumentationRegistry.getInstrumentation().waitForIdleSync();
// The Activity is paused. We cannot use Espresso to test paused activities.
rule.runOnUiThread(new Runnable() {
@Override
public void run() {
// We are now in Picture-in-Picture mode
assertTrue(rule.getActivity().isInPictureInPictureMode());
final MovieView view = rule.getActivity().findViewById(R.id.movie);
assertNotNull(view);
// The video should still be playing
assertTrue(view.isPlaying());
}
rule.getScenario().onActivity(activity -> {
// We are now in Picture-in-Picture mode
assertTrue(activity.isInPictureInPictureMode());
final MovieView view = activity.findViewById(R.id.movie);
assertNotNull(view);
// The video should still be playing
assertTrue(view.isPlaying());
});
}

@Test
public void movie_pauseAndResume() throws Throwable {
public void movie_pauseAndResume() {
// The movie should be playing on start
onView(withId(R.id.movie))
.check(matches(allOf(isDisplayed(), isPlaying())))
Expand All @@ -91,42 +93,36 @@ public void movie_pauseAndResume() throws Throwable {
}

@Test
public void fullscreen_enabledOnLandscape() throws Throwable {
rule.runOnUiThread(new Runnable() {
@Override
public void run() {
rule.getActivity()
.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
});
public void fullscreen_enabledOnLandscape() {
rule.getScenario().onActivity(activity ->
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
);
InstrumentationRegistry.getInstrumentation().waitForIdleSync();
rule.runOnUiThread(new Runnable() {
@Override
public void run() {
final View decorView = rule.getActivity().getWindow().getDecorView();
assertThat(decorView.getSystemUiVisibility(),
hasFlag(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN));
}
rule.getScenario().onActivity(activity -> {
final WindowInsetsCompat insets = ViewCompat.getRootWindowInsets(
activity.getWindow().getDecorView()
);
final Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
assertThat(systemBars.left, is(0));
assertThat(systemBars.top, is(0));
assertThat(systemBars.right, is(0));
assertThat(systemBars.bottom, is(0));
});
}

@Test
public void fullscreen_disabledOnPortrait() throws Throwable {
rule.runOnUiThread(new Runnable() {
@Override
public void run() {
rule.getActivity()
.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
});
public void fullscreen_disabledOnPortrait() {
rule.getScenario().onActivity(activity ->
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
);
InstrumentationRegistry.getInstrumentation().waitForIdleSync();
rule.runOnUiThread(new Runnable() {
@Override
public void run() {
final View decorView = rule.getActivity().getWindow().getDecorView();
assertThat(decorView.getSystemUiVisibility(),
not(hasFlag(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)));
}
rule.getScenario().onActivity(activity -> {
final WindowInsetsCompat insets = ViewCompat.getRootWindowInsets(
activity.getWindow().getDecorView()
);
final Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
assertThat(systemBars.top, is(not(0)));
assertThat(systemBars.bottom, is(not(0)));
});
}

Expand Down Expand Up @@ -164,19 +160,4 @@ public void perform(UiController uiController, View view) {
}
};
}

private static Matcher<? super Integer> hasFlag(final int flag) {
return new TypeSafeMatcher<Integer>() {
@Override
protected boolean matchesSafely(Integer i) {
return (i & flag) == flag;
}

@Override
public void describeTo(Description description) {
description.appendText("Flag integer contains " + flag);
}
};
}

}
Loading

0 comments on commit 596d081

Please sign in to comment.