Bug 1130275 - correct promise chaining for EME mochitests. r=edwin.
authorJW Wang <jwwang@mozilla.com>
Fri, 06 Feb 2015 19:16:28 +0800
changeset 241871 9b3ea7966b91bdcdb8c547d45fedb6a0a1724a0b
parent 241870 0f4fc6a418ed71564195cde9979d7588688515a5
child 241872 4298e517d29b530a240c49094ba8bad6d748d524
push id625
push userdburns@mozilla.com
push dateTue, 10 Feb 2015 14:14:40 +0000
reviewersedwin
bugs1130275
milestone38.0a1
Bug 1130275 - correct promise chaining for EME mochitests. r=edwin.
dom/media/test/eme.js
dom/media/test/test_eme_persistent_sessions.html
dom/media/test/test_eme_playback.html
--- a/dom/media/test/eme.js
+++ b/dom/media/test/eme.js
@@ -218,36 +218,53 @@ function SetupEME(test, token, params)
   v.addEventListener("encrypted", function(ev) {
     Log(token, "got encrypted event");
     var options = [
       {
         initDataType: ev.initDataType,
         videoType: test.type,
       }
     ];
-    navigator.requestMediaKeySystemAccess(KEYSYSTEM_TYPE, options)
-      .then(function(keySystemAccess) {
-        return keySystemAccess.createMediaKeys();
-      }, bail(token + " Failed to request key system access."))
 
-      .then(function(mediaKeys) {
-        Log(token, "created MediaKeys object ok");
-        mediaKeys.sessions = [];
-        return v.setMediaKeys(mediaKeys);
-      }, bail("failed to create MediaKeys object"))
+    function chain(promise, onReject) {
+      return promise.then(function(value) {
+        return Promise.resolve(value);
+      }).catch(function(reason) {
+        onReject(reason);
+        return Promise.reject();
+      })
+    }
+
+    var p = navigator.requestMediaKeySystemAccess(KEYSYSTEM_TYPE, options);
+    var r = bail(token + " Failed to request key system access.");
+    chain(p, r)
+    .then(function(keySystemAccess) {
+      var p = keySystemAccess.createMediaKeys();
+      var r = bail(token +  " Failed to create MediaKeys object");
+      return chain(p, r);
+    })
 
-      .then(function() {
-        Log(token, "set MediaKeys on <video> element ok");
-        var sessionType = (params && params.sessionType) ? params.sessionType : "temporary";
-        var session = v.mediaKeys.createSession(sessionType);
-        if (params && params.onsessioncreated) {
-          params.onsessioncreated(session);
-        }
-        session.addEventListener("message", UpdateSessionFunc(test, token, sessionType));
-        return session.generateRequest(ev.initDataType, ev.initData);
-      }, onSetKeysFail)
+    .then(function(mediaKeys) {
+      Log(token, "created MediaKeys object ok");
+      mediaKeys.sessions = [];
+      var p = v.setMediaKeys(mediaKeys);
+      return chain(p, onSetKeysFail);
+    })
 
-      .then(function() {
-        Log(token, "generated request");
-      }, bail(token + " Failed to request key system access2."));
+    .then(function() {
+      Log(token, "set MediaKeys on <video> element ok");
+      var sessionType = (params && params.sessionType) ? params.sessionType : "temporary";
+      var session = v.mediaKeys.createSession(sessionType);
+      if (params && params.onsessioncreated) {
+        params.onsessioncreated(session);
+      }
+      session.addEventListener("message", UpdateSessionFunc(test, token, sessionType));
+      var p = session.generateRequest(ev.initDataType, ev.initData);
+      var r = bail(token + ": session.generateRequest failed");
+      return chain(p, r);
+    })
+
+    .then(function() {
+      Log(token, ": session.generateRequest succeeded");
+    });
   });
   return v;
 }
--- a/dom/media/test/test_eme_persistent_sessions.html
+++ b/dom/media/test/test_eme_persistent_sessions.html
@@ -25,43 +25,44 @@ function UsableKeyIdsMatch(usableKeyIds,
       return false;
     }
   }
   return true;
 }
 
 function AwaitAllKeysUsable(session, keys, token) {
   return new Promise(function(resolve, reject) {
-    function listener(event) {
+    function check() {
       var map = session.keyStatuses;
       var usableKeyIds = [];
       for (var [key, val] of map.entries()) {
         is(val, "usable", token + ": key status should be usable");
         usableKeyIds.push(key);
       }
       if (UsableKeyIdsMatch(usableKeyIds, keys)) {
-        Log(token, "resolving AwaitAllKeysUsable promise");
-        session.removeEventListener("keystatuseschange", listener);
+        session.removeEventListener("keystatuseschange", check);
         resolve();
       }
     }
-    session.addEventListener("keystatuseschange", listener);
+    session.addEventListener("keystatuseschange", check);
+    check(); // in case all keys are already usable
   });
 }
 
 function AwaitAllKeysNotUsable(session, token) {
   return new Promise(function(resolve, reject) {
-    function listener(event) {
+    function check() {
       var map = session.keyStatuses;
       if (map.size == 0) {
-        session.removeEventListener("keystatuseschange", listener);
+        session.removeEventListener("keystatuseschange", check);
         resolve();
       }
     }
-    session.addEventListener("keystatuseschange", listener);
+    session.addEventListener("keystatuseschange", check);
+    check(); // in case all keys are already removed
   });
 }
 
 function startTest(test, token)
 {
   manager.started(token);
 
   var recreatedSession; // will have remove() called on it.
@@ -72,62 +73,79 @@ function startTest(test, token)
     {
       onsessioncreated: function(session) {
         Log(token, "Session created");
         var sessionId;
         initialSession = session;
 
         // Once the session has loaded and has all its keys usable, close
         // all sessions without calling remove() on them.
-        AwaitAllKeysUsable(session, test.keys, token).then(
-          function() {
-            sessionId = session.sessionId;
-            Log(token, "Closing session with id=" + sessionId);
-            session.close();
-          }
-        );
+        AwaitAllKeysUsable(session, test.keys, token)
+        .then(function() {
+          sessionId = session.sessionId;
+          Log(token, "Closing session with id=" + sessionId);
+          return session.close();
+        })
 
         // Once the session is closed, reload the MediaKeys and reload the session
-        session.closed.then(function() {
-          return navigator.requestMediaKeySystemAccess(KEYSYSTEM_TYPE)
-        }, bail("close promise rejected"))
+        .then(function() {
+          return navigator.requestMediaKeySystemAccess(KEYSYSTEM_TYPE);
+        })
 
         .then(function(requestedKeySystemAccess) {
           keySystemAccess = requestedKeySystemAccess;
           return keySystemAccess.createMediaKeys();
-        }, bail(token + " Failed to request key system access."))
+        })
 
         .then(function(mediaKeys) {
           Log(token, "re-created MediaKeys object ok");
           recreatedSession = mediaKeys.createSession("persistent");
           Log(token, "Created recreatedSession, loading sessionId=" + sessionId);
-          return Promise.all([AwaitAllKeysUsable(recreatedSession, test.keys, token), recreatedSession.load(sessionId)]);
-        }, bail(token + " failed to create mediaKeys"))
+          return recreatedSession.load(sessionId);
+        })
+
+        .then(function(suceeded) {
+          if (suceeded) {
+            return Promise.resolve();
+          } else {
+            return Promise.reject("Fail to load recreatedSession, sessionId=" + sessionId);
+          }
+        })
+
+        .then(function() {
+          return AwaitAllKeysUsable(recreatedSession, test.keys, token);
+        })
 
         .then(function() {
           Log(token, "re-loaded persistent session, all keys still usable");
           return Promise.all([AwaitAllKeysNotUsable(recreatedSession, token), recreatedSession.remove()]);
-        }, bail(token + " failed to get reload session or keys"))
+        })
 
         .then(function() {
           Log(token, "removed session, all keys unusable.");
           // Attempt to recreate the session, the attempt should fail.
           return keySystemAccess.createMediaKeys();
-        }, bail(token + " failed to remove session"))
+        })
 
         .then(function(mediaKeys) {
           Log(token, "re-re-created MediaKeys object ok");
           // Trying to load the removed persistent session should fail.
           return mediaKeys.createSession("persistent").load(sessionId);
-        }, bail(token + " failed to create mediaKeys"))
+        })
 
         .then(function(suceeded) {
           is(suceeded, false, token + " we expect the third session creation to fail, as the session should have been removed.");
           manager.finished(token);
-        }, bail(token + " failure to load session."));
+        })
+
+        .catch(function(reason) {
+          // Catch rejections if any.
+          ok(false, token + " rejected, reason=" + reason);
+          manager.finished(token);
+        });
 
       },
       sessionType: "persistent",
     }
   );
 
   LoadTest(test, v, token);
 }
--- a/dom/media/test/test_eme_playback.html
+++ b/dom/media/test/test_eme_playback.html
@@ -83,17 +83,23 @@ function startTest(test, token)
       for (var kid in session.keyIdsReceived) {
         ok(session.keyIdsReceived[kid], TimeStamp(token) + " key with id " + kid + " was usable as expected");
       }
     }
 
     manager.finished(token);
    });
 
-  LoadTest(test, v, token).then(function(){v.play();}, bail(token + " failed to load"));
+  LoadTest(test, v, token)
+  .then(function() {
+    v.play();
+  }).catch(function() {
+    ok(false, token + " failed to load");
+    manager.finished(token);
+  });
 }
 
 function beginTest() {
   manager.runTests(gEMETests, startTest);
 }
 
 var prefs = [
   [ "media.mediasource.enabled", true ],