7 <p>Test all the key-related events.
</p>
9 <script src=
"../encrypted-media-utils.js"></script>
10 <script src=../../media-file.js
></script>
11 <script src=../../video-test.js
></script>
13 // First, try explicitly creating those events with specific IDL.
14 var keyNeededEvent
= document
.createEvent("MediaKeyEvent");
15 testExpected("keyNeededEvent", null, "!=");
16 testExpected("keyNeededEvent instanceof window.MediaKeyEvent", true);
18 // Next, The test runs twice, once using on* and then using addEventListener().
19 var isFirstRun
= true;
21 // The Initialization Data in test-encrypted.webm.
22 var expectedInitData
= stringToUint8Array('0123456789012345');
23 // A 128-bit key. It is not the actual key for test-encrypted.webm.
24 var key
= new Uint8Array([0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
25 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70]);
26 // This key will cause an asynchronous error because it is too short.
27 var invalidKey
= new Uint8Array([0x61]);
29 // After the first keyMessage event, the sessionId should always be the same.
30 // Initialize it to an invalid value until then.
31 var keyMessageSessionId
= -1;
32 // Remember the first ID to make sure the second one is different.
33 var firstRunKeyMessageSessionId
= -1;
35 function keyAdded(event
)
37 consoleWrite("keyadded event occurred");
39 testExpected("event.target", video
);
40 testExpected("event instanceof window.MediaKeyEvent", true);
42 testExpected("event.keySystem", "webkit-org.w3.clearkey");
43 testExpected("event.sessionId == keyMessageSessionId", true);
44 // The other attributes are not used for this event.
45 testExpected("event.initData", null, "===");
46 testExpected("event.message", null, "===");
47 testExpected("event.defaultURL", "");
48 testExpected("event.errorCode", null, "===");
49 testExpected("event.systemCode", 0);
52 // Cause a keyerror by passing an invalid key.
53 run("video.webkitAddKey('webkit-org.w3.clearkey', invalidKey, null, event.sessionId)");
56 function keyError(event
)
58 consoleWrite("keyerror event occurred");
60 testExpected("event.target", video
);
61 testExpected("event instanceof window.MediaKeyEvent", true);
63 testExpected("event.keySystem", "webkit-org.w3.clearkey");
64 testExpected("event.sessionId == keyMessageSessionId", true);
65 // The next three attributes are not used for this event.
66 testExpected("event.initData", null, "===");
67 testExpected("event.message", null, "===");
68 testExpected("event.defaultURL", "");
69 testExpected("event.errorCode.code", MediaKeyError
.MEDIA_KEYERR_UNKNOWN
);
70 // systemCode is not supported by the Clear Key key system.
71 testExpected("event.systemCode", 0);
78 consoleWrite("Attributes are read-only.");
79 run("event.keySystem = 'blah'");
80 run("event.sessionId = 'blah'");
81 run("event.initData = new Uint8Array([0x12])");
82 run("event.message = new Uint8Array([0x12])");
83 run("event.defaultURL = 'example.com'");
84 run("event.errorCode.code = MediaKeyError.MEDIA_KEYERR_CLIENT");
85 run("event.systemCode = 123");
87 testExpected("event.keySystem", "webkit-org.w3.clearkey");
88 testExpected("event.sessionId == keyMessageSessionId", true);
89 testExpected("event.initData", null, "===");
90 testExpected("event.message", null, "===");
91 testExpected("event.defaultURL", "");
92 testExpected("event.errorCode.code", MediaKeyError
.MEDIA_KEYERR_UNKNOWN
);
93 testExpected("event.systemCode", 0);
99 function keyMessage(event
)
101 consoleWrite("keymessage event occurred");
103 testExpected("event.target", video
);
104 testExpected("event instanceof window.MediaKeyEvent", true);
106 testExpected("event.keySystem", "webkit-org.w3.clearkey");
108 consoleWrite("The sessionId should be a non-empty string containing an integer.");
109 testExpected("event.sessionId", "", "!=");
110 testExpected("event.sessionId", null, "!=");
111 testExpected("event.sessionId", undefined, "!=");
112 testExpected("isNaN(event.sessionId)", false);
113 // Make sure the number is not a float.
114 testExpected("String(event.sessionId) == String(parseInt(event.sessionId))", true);
115 consoleWrite("Implementations should avoid sessionIds of 0.");
116 testExpected("event.sessionId", 0, ">");
117 // All other events should have this same sessionId.
118 keyMessageSessionId
= event
.sessionId
;
120 firstRunKeyMessageSessionId
= keyMessageSessionId
;
122 consoleWrite("The sessionsId should be different from the first run.");
123 testExpected("event.sessionId != firstRunKeyMessageSessionId", true);
126 // initData is not used for this event.
127 testExpected("event.initData", null, "===");
128 // At least for now, the Clear Key message is the initData.
129 testArraysEqual("event.message", expectedInitData
);
130 // Not supported by the test file.
131 testExpected("event.defaultURL", "");
132 // The error attributes are not used for this event.
133 testExpected("event.errorCode", null, "===");
134 testExpected("event.systemCode", 0);
137 run("video.webkitAddKey('webkit-org.w3.clearkey', key, event.initData, event.sessionId)");
140 function needKey(event
)
142 consoleWrite("needkey event occurred");
143 // Clear the handler (for the first case) to prevent the second needkey event
144 // (there will be one each for audio and video) from being handled.
145 video
.onwebkitneedkey
=null;
147 testExpected("event.target", video
);
148 testExpected("event instanceof window.MediaKeyEvent", true);
150 testExpected("event.keySystem", "");
151 testExpected("event.sessionId", "");
152 testArraysEqual("event.initData", expectedInitData
);
153 // The other attributes are not used for this event.
154 testExpected("event.message", null, "===");
155 testExpected("event.defaultURL", "");
156 testExpected("event.errorCode", null, "===");
157 testExpected("event.systemCode", 0);
160 run("video.webkitGenerateKeyRequest('webkit-org.w3.clearkey', event.initData)");
167 consoleWrite("*** Test events using on* attributes. ***");
168 video
.onwebkitkeyadded
=keyAdded
;
169 video
.onwebkitkeyerror
=keyError
;
170 video
.onwebkitkeymessage
=keyMessage
;
171 video
.onwebkitneedkey
=needKey
;
173 consoleWrite("*** Test events using addEventListener(). ***");
175 // Clear the on* handlers.
176 video
.onwebkitkeyadded
=null;
177 video
.onwebkitkeyerror
=null;
178 video
.onwebkitkeymessage
=null;
179 video
.onwebkitneedkey
=null;
181 waitForEvent('webkitkeyadded', keyAdded
);
182 waitForEvent('webkitkeyerror', keyError
);
183 waitForEvent('webkitkeymessage', keyMessage
);
184 waitForEventOnce('webkitneedkey', needKey
);
187 video
.src
= "../../content/test-encrypted.webm";
191 consoleWrite("*** Verify the presence of on* attributes. These would return undefined if they are not present. ***");
192 testExpected("video.onwebkitkeyadded", null, "===");
193 testExpected("video.onwebkitkeyerror", null, "===");
194 testExpected("video.onwebkitkeymessage", null, "===");
195 testExpected("video.onwebkitneedkey", null, "===");