summaryrefslogtreecommitdiff
path: root/dom/media/test/test_eme_waitingforkey.html
blob: ce808342bb8b4521b124d5d4b79f7d52e57abde6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<!DOCTYPE HTML>
<html>
<head>
  <title>Test Encrypted Media Extensions</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
  <script type="text/javascript" src="manifest.js"></script>
  <script type="text/javascript" src="http://test1.mochi.test:8888/tests/dom/media/test/eme.js"></script>
</head>
<body>
<pre id="test">
<script class="testbody" type="text/javascript">
var manager = new MediaTestManager;

function startTest(test, token)
{
  // Test if the appropriate preconditions are met such that we can start
  // prcoessing delayed sessions.
  function TestIfDoneDelaying()
  {
    var got = "Got:";
    if (loaded) { got += " loaded,"; }
    got += " " + gotEncrypted + "/" + test.sessionCount + " sessions,";
    got += " " + gotWaitingForKey + " waiting for key events"
    if (loaded && gotEncrypted == test.sessionCount && gotWaitingForKey > 0) {
      Log(token, got + " -> Update sessions with keys");
      params.ProcessSessions();
    } else {
      Log(token, got + " -> Wait for more...");
    }
  }

  manager.started(token);

  var updatedSessionsCount = 0;
  var loaded = false;

  var params = {
    // params will be populated with a ProcessSessions() callback, that can be
    // called to process delayed sessions.
    delaySessions: true,
    // Function to be called once we start processing and updating sessions.
    // This should only be called once the preconditions in TestIfDoneDealying
    // are met.
    onsessionupdated: function(session) {
      updatedSessionsCount += 1;
      if (updatedSessionsCount == test.sessionCount) {
          info(TimeStamp(token) + " Updated all sessions, loading complete -> Play");
          v.play();
      } else {
         info(TimeStamp(token) + " Updated " + updatedSessionsCount + "/" + test.sessionCount + " sessions so far");
      }
    },
  };
  var v = SetupEME(test, token, params);

  document.body.appendChild(v);

  var gotEncrypted = 0;
  var gotWaitingForKey = 0;
  var gotOnwaitingforkey = 0;

  v.addEventListener("encrypted", function() {
    gotEncrypted += 1;
    TestIfDoneDelaying();
  });

  v.addEventListener("waitingforkey", function() {
    gotWaitingForKey += 1;
    TestIfDoneDelaying()
  });

  v.onwaitingforkey = function() {
    gotOnwaitingforkey += 1;
  };

  v.addEventListener("loadedmetadata", function() {
    ok(SpecialPowers.do_lookupGetter(v, "isEncrypted").apply(v),
       TimeStamp(token) + " isEncrypted should be true");
    is(v.isEncrypted, undefined, "isEncrypted should not be accessible from content");
  });

  v.addEventListener("ended", function() {
    ok(true, TimeStamp(token) + " got ended event");
    // We expect only one waitingForKey as we delay until all sessions are ready.
    // I.e. one waitingForKey should be fired, after which, we process all sessions,
    // so it should not be possible to be blocked by a key after that point.
    ok(gotWaitingForKey == 1,  "Expected number 1 wait, got: " + gotWaitingForKey);
    ok(gotOnwaitingforkey == gotWaitingForKey, "Should have as many event listener calls as event handler calls, got: " + gotOnwaitingforkey);

    v.closeSessions().then(() => manager.finished(token));
  });

  LoadTest(test, v, token)
  .then(function() {
    loaded = true;
    TestIfDoneDelaying();
  }).catch(function() {
    ok(false, token + " failed to load");
    manager.finished(token);
  });
}

function beginTest() {
  manager.runTests(gEMETests, startTest);
}

if (!IsMacOSSnowLeopardOrEarlier()) {
  SimpleTest.waitForExplicitFinish();
  SetupEMEPref(beginTest);
} else {
  todo(false, "Test disabled on this platform.");
}
</script>
</pre>
</body>
</html>