For #15754: Get playback state using MediaSession in MediaNotificationTests

upstream-sync
Oana Horvath 3 years ago
parent 8ffc619d8d
commit 9fd7fe3df1

@ -9,18 +9,5 @@
<source src="../resources/audioSample.mp3">
</audio>
</div>
<div class="playbackState">
</div>
<script>
const audio = document.querySelector('audio');
audio.addEventListener('playing', (event) => {
document.querySelector('.playbackState').innerText="Media file is playing"
});
audio.addEventListener('pause', (event) => {
document.querySelector('.playbackState').innerText="Media file is paused"
});
</script>
</body>
</html>

@ -9,18 +9,5 @@
<source src="../resources/videoSample.webm">
</video>
</div>
<div class="playbackState">
</div>
<script>
const video = document.querySelector('video');
video.addEventListener('playing', (event) => {
document.querySelector('.playbackState').innerText="Media file is playing";
});
video.addEventListener('pause', (event) => {
document.querySelector('.playbackState').innerHTML="Media file is paused";
});
</script>
</body>
</html>

@ -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")

@ -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) {

Loading…
Cancel
Save