From 9fd7fe3df117f1592f07e916b50559c11f8b4710 Mon Sep 17 00:00:00 2001 From: Oana Horvath Date: Fri, 21 May 2021 17:20:21 +0300 Subject: [PATCH] For #15754: Get playback state using MediaSession in MediaNotificationTests --- .../assets/pages/audioMediaPage.html | 13 ------- .../assets/pages/videoMediaPage.html | 13 ------- .../mozilla/fenix/ui/MediaNotificationTest.kt | 36 ++++++++--------- .../mozilla/fenix/ui/robots/BrowserRobot.kt | 39 +++++++++++++++---- 4 files changed, 48 insertions(+), 53 deletions(-) diff --git a/app/src/androidTest/assets/pages/audioMediaPage.html b/app/src/androidTest/assets/pages/audioMediaPage.html index 1a8720a85e..8fd6da11d7 100644 --- a/app/src/androidTest/assets/pages/audioMediaPage.html +++ b/app/src/androidTest/assets/pages/audioMediaPage.html @@ -9,18 +9,5 @@ -
-
- \ No newline at end of file diff --git a/app/src/androidTest/assets/pages/videoMediaPage.html b/app/src/androidTest/assets/pages/videoMediaPage.html index 32baa8d785..3de177e2c3 100644 --- a/app/src/androidTest/assets/pages/videoMediaPage.html +++ b/app/src/androidTest/assets/pages/videoMediaPage.html @@ -9,18 +9,5 @@ -
-
- \ No newline at end of file diff --git a/app/src/androidTest/java/org/mozilla/fenix/ui/MediaNotificationTest.kt b/app/src/androidTest/java/org/mozilla/fenix/ui/MediaNotificationTest.kt index 02ce669f2d..9bd17d6080 100644 --- a/app/src/androidTest/java/org/mozilla/fenix/ui/MediaNotificationTest.kt +++ b/app/src/androidTest/java/org/mozilla/fenix/ui/MediaNotificationTest.kt @@ -4,13 +4,15 @@ package org.mozilla.fenix.ui -import androidx.test.uiautomator.UiSelector +import mozilla.components.browser.state.store.BrowserStore +import mozilla.components.concept.engine.mediasession.MediaSession import okhttp3.mockwebserver.MockWebServer import org.junit.After import org.junit.Before import org.junit.Ignore import org.junit.Rule import org.junit.Test +import org.mozilla.fenix.ext.components import org.mozilla.fenix.helpers.AndroidAssetDispatcher import org.mozilla.fenix.helpers.HomeActivityTestRule import org.mozilla.fenix.helpers.TestAssetHelper @@ -33,9 +35,14 @@ class MediaNotificationTest { @get:Rule val activityTestRule = HomeActivityTestRule() + private lateinit var browserStore: BrowserStore @Before fun setUp() { + // Initializing this as part of class construction, below the rule would throw a NPE + // So we are initializing this here instead of in all tests. + browserStore = activityTestRule.activity.components.core.store + mockWebServer = MockWebServer().apply { dispatcher = AndroidAssetDispatcher() start() @@ -45,16 +52,10 @@ class MediaNotificationTest { @After fun tearDown() { mockWebServer.shutdown() - // verify if the notification tray is expanded and should be closed before the next test - val notificationShade = - mDevice.findObject(UiSelector().resourceId("com.android.systemui:id/notification_stack_scroller")) - - if (notificationShade.exists()) - mDevice.pressBack() } + @Ignore("Still failing, due to https://github.com/mozilla-mobile/android-components/issues/9748") @Test - @Ignore("https://github.com/mozilla-mobile/fenix/issues/15754") fun videoPlaybackSystemNotificationTest() { val videoTestPage = TestAssetHelper.getVideoPageAsset(mockWebServer) @@ -62,7 +63,7 @@ class MediaNotificationTest { }.enterURLAndEnterToBrowser(videoTestPage.url) { mDevice.waitForIdle() clickMediaPlayerPlayButton() - waitForPlaybackToStart() + assertPlaybackState(browserStore, MediaSession.PlaybackState.PLAYING) }.openNotificationShade { verifySystemNotificationExists(videoTestPage.title) clickMediaSystemNotificationControlButton("Pause") @@ -72,7 +73,7 @@ class MediaNotificationTest { mDevice.pressBack() browserScreen { - verifyMediaIsPaused() + assertPlaybackState(browserStore, MediaSession.PlaybackState.PAUSED) }.openTabDrawer { closeTab() } @@ -88,7 +89,6 @@ class MediaNotificationTest { } @Test - @Ignore("https://github.com/mozilla-mobile/fenix/issues/15754") fun audioPlaybackSystemNotificationTest() { val audioTestPage = TestAssetHelper.getAudioPageAsset(mockWebServer) @@ -96,7 +96,7 @@ class MediaNotificationTest { }.enterURLAndEnterToBrowser(audioTestPage.url) { mDevice.waitForIdle() clickMediaPlayerPlayButton() - waitForPlaybackToStart() + assertPlaybackState(browserStore, MediaSession.PlaybackState.PLAYING) }.openNotificationShade { verifySystemNotificationExists(audioTestPage.title) clickMediaSystemNotificationControlButton("Pause") @@ -106,7 +106,7 @@ class MediaNotificationTest { mDevice.pressBack() browserScreen { - verifyMediaIsPaused() + assertPlaybackState(browserStore, MediaSession.PlaybackState.PAUSED) }.openTabDrawer { closeTab() } @@ -122,7 +122,6 @@ class MediaNotificationTest { } @Test - @Ignore("https://github.com/mozilla-mobile/fenix/issues/15754") fun tabMediaControlButtonTest() { val audioTestPage = TestAssetHelper.getAudioPageAsset(mockWebServer) @@ -130,18 +129,17 @@ class MediaNotificationTest { }.enterURLAndEnterToBrowser(audioTestPage.url) { mDevice.waitForIdle() clickMediaPlayerPlayButton() - waitForPlaybackToStart() + assertPlaybackState(browserStore, MediaSession.PlaybackState.PLAYING) }.openTabDrawer { verifyTabMediaControlButtonState("Pause") clickTabMediaControlButton() verifyTabMediaControlButtonState("Play") }.openTab(audioTestPage.title) { - verifyMediaIsPaused() + assertPlaybackState(browserStore, MediaSession.PlaybackState.PAUSED) } } @Test - @Ignore("https://github.com/mozilla-mobile/fenix/issues/15754") fun mediaSystemNotificationInPrivateModeTest() { val audioTestPage = TestAssetHelper.getAudioPageAsset(mockWebServer) @@ -151,7 +149,7 @@ class MediaNotificationTest { }.enterURLAndEnterToBrowser(audioTestPage.url) { mDevice.waitForIdle() clickMediaPlayerPlayButton() - waitForPlaybackToStart() + assertPlaybackState(browserStore, MediaSession.PlaybackState.PLAYING) }.openNotificationShade { verifySystemNotificationExists("A site is playing media") clickMediaSystemNotificationControlButton("Pause") @@ -161,7 +159,7 @@ class MediaNotificationTest { mDevice.pressBack() browserScreen { - verifyMediaIsPaused() + assertPlaybackState(browserStore, MediaSession.PlaybackState.PAUSED) }.openTabDrawer { closeTab() verifySnackBarText("Private tab closed") diff --git a/app/src/androidTest/java/org/mozilla/fenix/ui/robots/BrowserRobot.kt b/app/src/androidTest/java/org/mozilla/fenix/ui/robots/BrowserRobot.kt index d10a7df655..30a1691bff 100644 --- a/app/src/androidTest/java/org/mozilla/fenix/ui/robots/BrowserRobot.kt +++ b/app/src/androidTest/java/org/mozilla/fenix/ui/robots/BrowserRobot.kt @@ -9,6 +9,7 @@ package org.mozilla.fenix.ui.robots import android.content.Context import android.content.Intent import android.net.Uri +import android.os.SystemClock import android.widget.EditText import androidx.test.espresso.Espresso.onView import androidx.test.espresso.Espresso.pressBack @@ -33,10 +34,13 @@ import androidx.test.uiautomator.UiDevice import androidx.test.uiautomator.UiSelector import androidx.test.uiautomator.Until import mozilla.components.browser.state.selector.selectedTab +import mozilla.components.browser.state.store.BrowserStore +import mozilla.components.concept.engine.mediasession.MediaSession import org.hamcrest.CoreMatchers.allOf import org.hamcrest.CoreMatchers.containsString import org.hamcrest.Matchers.not import org.junit.Assert.assertTrue +import org.junit.Assert.fail import org.mozilla.fenix.R import org.mozilla.fenix.ext.components import org.mozilla.fenix.helpers.Constants.LONG_CLICK_DURATION @@ -371,14 +375,33 @@ class BrowserRobot { mediaPlayerPlayButton().click() } - fun waitForPlaybackToStart() { - val playStateMessage = mDevice.findObject(UiSelector().text("Media file is playing")) - assertTrue(playStateMessage.waitForExists(waitingTime)) - } - - fun verifyMediaIsPaused() { - val pausedStateMessage = mDevice.findObject(UiSelector().text("Media file is paused")) - assertTrue(pausedStateMessage.waitForExists(waitingTime)) + /** + * Get the current playback state of the currently selected tab. + * The result may be null if there if the currently playing media tab cannot be found in [store] + * + * @param store [BrowserStore] from which to get data about the current tab's state. + * @return nullable [MediaSession.PlaybackState] indicating the media playback state for the current tab. + */ + private fun getCurrentPlaybackState(store: BrowserStore): MediaSession.PlaybackState? { + return store.state.selectedTab?.mediaSessionState?.playbackState + } + + /** + * Asserts that in [waitingTime] the playback state of the current tab will be [expectedState]. + * + * @param store [BrowserStore] from which to get data about the current tab's state. + * @param expectedState [MediaSession.PlaybackState] the playback state that will be asserted + * @param waitingTime maximum time the test will wait for the playback state to become [expectedState] + * before failing the assertion. + */ + fun assertPlaybackState(store: BrowserStore, expectedState: MediaSession.PlaybackState) { + val startMills = SystemClock.uptimeMillis() + var currentMills: Long = 0 + while (currentMills <= waitingTime) { + if (expectedState == getCurrentPlaybackState(store)) return + currentMills = SystemClock.uptimeMillis() - startMills + } + fail("Playback did not moved to state: $expectedState") } fun swipeNavBarRight(tabUrl: String) {