Bug 1658937 - [2.4] Update tests to work with the WebIDL events. r=geckoview-reviewers,agi,alwu
authorEugen Sawin <esawin@mozilla.com>
Thu, 24 Sep 2020 21:21:09 +0000
changeset 550248 f1c6d2333d5a46881e9dd178a91eaafc72d81dc1
parent 550247 d15245dad149b882414e5657a15cfad1a1270fa4
child 550249 f4662be775dbf2cb8a5717fee0d1c5e80378ee3a
push id127197
push useresawin@mozilla.com
push dateThu, 24 Sep 2020 21:40:56 +0000
treeherderautoland@f4662be775db [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgeckoview-reviewers, agi, alwu
bugs1658937
milestone83.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1658937 - [2.4] Update tests to work with the WebIDL events. r=geckoview-reviewers,agi,alwu Differential Revision: https://phabricator.services.mozilla.com/D89820
mobile/android/geckoview/src/androidTest/assets/www/media_session_dom1.html
mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/MediaSessionTest.kt
--- a/mobile/android/geckoview/src/androidTest/assets/www/media_session_dom1.html
+++ b/mobile/android/geckoview/src/androidTest/assets/www/media_session_dom1.html
@@ -1,99 +1,99 @@
 <html>
   <head><title>MediaSessionDOMTest1</title></head>
   <body>
     <script>
       function updatePositionState(event) {
+        if (event.target != active) {
+          return;
+        }
         navigator.mediaSession.setPositionState({
           duration: parseFloat(event.target.duration),
           position: parseFloat(event.target.currentTime),
           playbackRate: 1,
         });
       }
 
-      function updateMetadata(event) {
-        active.removeEventListener("timeupdate", updatePositionState);
-        active = event.target;
-        updatePositionState(event);
-        active.addEventListener("timeupdate", updatePositionState);
+      function updateMetadata() {
         navigator.mediaSession.metadata = active.metadata;
       }
 
       function getTrack(offset) {
         console.log("" + active.id + " " + offset);
         const nextId = Math.min(tracks.length - 1,
             Math.max(0, parseInt(active.id) + offset));
         return tracks[nextId];
       }
 
-      navigator.mediaSession.setActionHandler("play", () => {
-        active.play();
+      navigator.mediaSession.setActionHandler("play", async () => {
+        await active.play();
+        updateMetadata();
       });
 
       navigator.mediaSession.setActionHandler("pause", () => {
         active.pause();
       });
 
       navigator.mediaSession.setActionHandler("previoustrack", () => {
-        active.pause();
-        getTrack(-1).play();
+        active = getTrack(-1);
       });
 
       navigator.mediaSession.setActionHandler("nexttrack", () => {
-        active.pause();
-        getTrack(1).play();
+        active = getTrack(1);
       });
 
       const audio1 = document.createElement("audio");
       audio1.src = "audio/owl.mp3";
-      audio1.addEventListener("play", updateMetadata);
+      audio1.addEventListener("timeupdate", updatePositionState);
       audio1.metadata = new window.MediaMetadata({
         title: "hoot",
         artist: "owl",
         album: "hoots",
         artwork: [{
           src: "images/test.gif",
           type: "image/gif",
           sizes: "265x199"
         }]
       });
       audio1.id = 0;
 
       const audio2 = document.createElement("audio");
       audio2.src = "audio/owl.mp3";
-      audio2.addEventListener("play", updateMetadata);
+      audio2.addEventListener("timeupdate", updatePositionState);
       audio2.metadata = new window.MediaMetadata({
         title: "hoot2",
         artist: "stillowl",
         album: "dahoots",
         artwork: [{
           src: "images/test.gif",
           type: "image/gif",
           sizes: "265x199"
         }]
       });
       audio2.id = 1;
 
       const audio3 = document.createElement("audio");
       audio3.src = "audio/owl.mp3";
-      audio3.addEventListener("play", updateMetadata);
+      audio3.addEventListener("timeupdate", updatePositionState);
       audio3.metadata = new window.MediaMetadata({
         title: "hoot3",
         artist: "immaowl",
         album: "mahoots",
         artwork: [{
           src: "images/test.gif",
           type: "image/gif",
           sizes: "265x199"
         }]
       });
       audio3.id = 2;
 
       const tracks = [audio1, audio2, audio3];
       let active = audio1;
 
-      window.onload = () => {
-        getTrack(0).play();
+      window.onload = async () => {
+        active = getTrack(0);
+        await active.play();
+        updateMetadata();
       };
     </script>
   </body>
 </html>
--- a/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/MediaSessionTest.kt
+++ b/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/MediaSessionTest.kt
@@ -56,23 +56,16 @@ class MediaSessionTest : BaseSessionTest
                 Metadata(
                         DOM_TEST_TITLE2,
                         DOM_TEST_ARTIST2,
                         DOM_TEST_ALBUM2),
                 Metadata(
                         DOM_TEST_TITLE3,
                         DOM_TEST_ARTIST3,
                         DOM_TEST_ALBUM3))
-
-        val DEFAULT_META = arrayOf(
-                Metadata(
-                        DEFAULT_TEST_TITLE1,
-                        // TODO: enforce null for empty strings?
-                        "",
-                        ""))
     }
 
     @Before
     fun setup() {
         sessionRule.setPrefsUntilTestEnd(mapOf(
             "media.mediacontrol.stopcontrol.aftermediaends" to false,
             "dom.media.mediasession.enabled" to true))
     }
@@ -118,46 +111,49 @@ class MediaSessionTest : BaseSessionTest
         //    a. Ensure onPause (1) is called.
         val completedStep3 = GeckoResult.allOf(
                 onPauseCalled[0])
 
         // 4. Resume playback (1).
         //    a. Ensure onMetadata (1) is called.
         //    b. Ensure onPlay (1) is called.
         val completedStep4 = GeckoResult.allOf(
-                onPlayCalled[1])
+                onPlayCalled[1],
+                onMetadataCalled[1])
 
         // 5. Wait for track 1 end.
         //    a. Ensure onPause (1) is called.
         val completedStep5 = GeckoResult.allOf(
                 onPauseCalled[1])
 
         // 6. Play next track (2).
         //    a. Ensure onMetadata (2) is called.
         //    b. Ensure onPlay (2) is called.
         val completedStep6 = GeckoResult.allOf(
-                onMetadataCalled[1],
+                onMetadataCalled[2],
                 onPlayCalled[2])
 
         // 7. Play next track (3).
         //    a. Ensure onPause (2) is called.
         //    b. Ensure onMetadata (3) is called.
         //    c. Ensure onPlay (3) is called.
         val completedStep7 = GeckoResult.allOf(
                 onPauseCalled[2],
-                onMetadataCalled[2],
+                onMetadataCalled[3],
                 onPlayCalled[3])
 
         // 8. Play previous track (2).
         //    a. Ensure onPause (3) is called.
         //    b. Ensure onMetadata (2) is called.
         //    c. Ensure onPlay (2) is called.
-        val completedStep8 = GeckoResult.allOf(
-                onPauseCalled[3],
-                onMetadataCalled[3],
+        val completedStep8a = GeckoResult.allOf(
+                onPauseCalled[3])
+        // Without the split, this seems to race and we don't get the pause event.
+        val completedStep8b = GeckoResult.allOf(
+                onMetadataCalled[4],
                 onPlayCalled[4])
 
         // 9. Wait for track 2 end.
         //    a. Ensure onPause (2) is called.
         val completedStep9 = GeckoResult.allOf(
                 onPauseCalled[4])
 
         val path = MEDIA_SESSION_DOM1_PATH
@@ -171,16 +167,22 @@ class MediaSessionTest : BaseSessionTest
             @AssertCalled(count = 1)
             override fun onActivated(
                     session: GeckoSession,
                     mediaSession: MediaSession) {
                 onActivatedCalled[0].complete(null)
                 mediaSession1 = mediaSession
             }
 
+            @AssertCalled(false)
+            override fun onDeactivated(
+                    session: GeckoSession,
+                    mediaSession: MediaSession) {
+            }
+
             @AssertCalled
             override fun onFeatures(
                     session: GeckoSession,
                     mediaSession: MediaSession,
                     features: Long) {
 
                 val play = (features and MediaSession.Feature.PLAY) != 0L
                 val pause = (features and MediaSession.Feature.PAUSE) != 0L
@@ -277,37 +279,39 @@ class MediaSessionTest : BaseSessionTest
         sessionRule.waitForResult(completedStep2)
         mediaSession1!!.pause()
 
         sessionRule.waitForResult(completedStep3)
         mediaSession1!!.play()
 
         sessionRule.waitForResult(completedStep4)
         sessionRule.waitForResult(completedStep5)
+        mediaSession1!!.pause()
         mediaSession1!!.nextTrack()
+        mediaSession1!!.play()
 
         sessionRule.waitForResult(completedStep6)
+        mediaSession1!!.pause()
         mediaSession1!!.nextTrack()
+        mediaSession1!!.play()
 
         sessionRule.waitForResult(completedStep7)
-        mediaSession1!!.previousTrack()
+        mediaSession1!!.pause()
 
-        sessionRule.waitForResult(completedStep8)
+        sessionRule.waitForResult(completedStep8a)
+        mediaSession1!!.previousTrack()
+        mediaSession1!!.play()
+
+        sessionRule.waitForResult(completedStep8b)
         sessionRule.waitForResult(completedStep9)
     }
 
     @Test
     fun defaultMetadataPlayback() {
         val onActivatedCalled = arrayOf(GeckoResult<Void>())
-        val onMetadataCalled = arrayOf(
-                GeckoResult<Void>(),
-                GeckoResult<Void>(),
-                GeckoResult<Void>(),
-                GeckoResult<Void>(),
-                GeckoResult<Void>())
         val onPlayCalled = arrayOf(GeckoResult<Void>(),
                 GeckoResult<Void>(),
                 GeckoResult<Void>(),
                 GeckoResult<Void>(),
                 GeckoResult<Void>(),
                 GeckoResult<Void>())
         val onPauseCalled = arrayOf(GeckoResult<Void>(),
                 GeckoResult<Void>(),
@@ -315,21 +319,19 @@ class MediaSessionTest : BaseSessionTest
                 GeckoResult<Void>(),
                 GeckoResult<Void>(),
                 GeckoResult<Void>())
 
         // Test:
         // 1. Load Media Session page which contains 1 audio track.
         // 2. Track 1 is played on page load.
         //    a. Ensure onActivated is called.
-        //    a. Ensure onMetadata (1) is called.
         //    b. Ensure onPlay (1) is called.
         val completedStep2 = GeckoResult.allOf(
                 onActivatedCalled[0],
-                onMetadataCalled[0],
                 onPlayCalled[0])
 
         // 3. Pause playback of track 1.
         //    a. Ensure onPause (1) is called.
         val completedStep3 = GeckoResult.allOf(
                 onPauseCalled[0])
 
         // 4. Resume playback (1).
@@ -353,57 +355,16 @@ class MediaSessionTest : BaseSessionTest
             @AssertCalled(count = 1)
             override fun onActivated(
                     session: GeckoSession,
                     mediaSession: MediaSession) {
                 onActivatedCalled[0].complete(null)
                 mediaSession1 = mediaSession
             }
 
-            /*
-            TODO: currently not called for non-media-session content.
-            @AssertCalled
-            override fun onFeatures(
-                    session: GeckoSession,
-                    mediaSession: MediaSession,
-                    features: Long) {
-
-                val play = (features and MediaSession.Feature.PLAY) != 0L
-                val pause = (features and MediaSession.Feature.PAUSE) != 0L
-                val stop = (features and MediaSession.Feature.PAUSE) != 0L
-
-                assertThat(
-                        "Core playback constrols should be supported",
-                        play && pause && stop,
-                        equalTo(true))
-            }
-            */
-
-            @AssertCalled(count = 1)
-            override fun onMetadata(
-                    session: GeckoSession,
-                    mediaSession: MediaSession,
-                    meta: MediaSession.Metadata) {
-                assertThat(
-                        "Title should match",
-                        meta.title,
-                        equalTo(DEFAULT_META[0].title))
-                assertThat(
-                        "Artist should match",
-                        meta.artist,
-                        equalTo(DEFAULT_META[0].artist))
-                assertThat(
-                        "Album should match",
-                        meta.album,
-                        equalTo(DEFAULT_META[0].album))
-
-                onMetadataCalled[sessionRule.currentCall.counter - 1]
-                        .complete(null)
-            }
-
             @AssertCalled(count = 2)
             override fun onPlay(
                     session: GeckoSession,
                     mediaSession: MediaSession) {
                 onPlayCalled[sessionRule.currentCall.counter - 1]
                         .complete(null)
             }
 
@@ -694,13 +655,15 @@ class MediaSessionTest : BaseSessionTest
         sessionRule.waitForResult(completedStep3)
 
         session2.loadTestPath(path)
         sessionRule.waitForResult(completedStep5)
 
         mediaSession2!!.pause()
         sessionRule.waitForResult(completedStep6)
 
+        mediaSession1!!.pause()
         mediaSession1!!.nextTrack()
+        mediaSession1!!.play()
         sessionRule.waitForResult(completedStep7)
         sessionRule.waitForResult(completedStep8)
     }
 }