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 messageEvent = document.createEvent("MediaKeyMessageEvent");
15 testExpected("messageEvent", null, "!=");
16 testExpected("messageEvent instanceof window.MediaKeyMessageEvent", true);
17 var keyNeededEvent = document.createEvent("MediaKeyEvent");
18 testExpected("keyNeededEvent", null, "!=");
19 testExpected("keyNeededEvent instanceof window.MediaKeyEvent", true);
21 // Next, The test runs twice, once using on* and then using addEventListener().
22 var isFirstRun = true;
24 // The Initialization Data in test-encrypted.webm.
25 var expectedInitData = stringToUint8Array('0123456789012345');
26 // A 128-bit key. It is not the actual key for test-encrypted.webm.
27 var key = new Uint8Array([0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
28 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70]);
29 // This key will cause an asynchronous error because it is too short.
30 var invalidKey = new Uint8Array([0x61]);
32 // After the first keyMessage event, the sessionId should always be the same.
33 // Initialize it to an invalid value until then.
34 var keyMessageSessionId = -1;
35 // Remember the first ID to make sure the second one is different.
36 var firstRunKeyMessageSessionId = -1;
38 function keyAdded(event)
40 consoleWrite("keyadded event occurred");
42 testExpected("event.target", video);
43 testExpected("event instanceof window.MediaKeyEvent", true);
45 testExpected("event.keySystem", "webkit-org.w3.clearkey");
46 testExpected("event.sessionId == keyMessageSessionId", true);
47 // The other attributes are not used for this event.
48 testExpected("event.initData", null, "===");
49 testExpected("event.message", null, "===");
50 testExpected("event.defaultURL", "");
51 testExpected("event.errorCode", null, "===");
52 testExpected("event.systemCode", 0);
55 // Cause a keyerror by passing an invalid key.
56 run("video.webkitAddKey('webkit-org.w3.clearkey', invalidKey, null, event.sessionId)");
59 function keyError(event)
61 consoleWrite("keyerror event occurred");
63 testExpected("event.target", video);
64 testExpected("event instanceof window.MediaKeyEvent", true);
66 testExpected("event.keySystem", "webkit-org.w3.clearkey");
67 testExpected("event.sessionId == keyMessageSessionId", true);
68 // The next three attributes are not used for this event.
69 testExpected("event.initData", null, "===");
70 testExpected("event.message", null, "===");
71 testExpected("event.defaultURL", "");
72 testExpected("event.errorCode.code", MediaKeyError.MEDIA_KEYERR_UNKNOWN);
73 // systemCode is not supported by the Clear Key key system.
74 testExpected("event.systemCode", 0);
81 consoleWrite("Attributes are read-only.");
82 run("event.keySystem = 'blah'");
83 run("event.sessionId = 'blah'");
84 run("event.initData = new Uint8Array([0x12])");
85 run("event.message = new Uint8Array([0x12])");
86 run("event.defaultURL = 'example.com'");
87 run("event.errorCode.code = MediaKeyError.MEDIA_KEYERR_CLIENT");
88 run("event.systemCode = 123");
90 testExpected("event.keySystem", "webkit-org.w3.clearkey");
91 testExpected("event.sessionId == keyMessageSessionId", true);
92 testExpected("event.initData", null, "===");
93 testExpected("event.message", null, "===");
94 testExpected("event.defaultURL", "");
95 testExpected("event.errorCode.code", MediaKeyError.MEDIA_KEYERR_UNKNOWN);
96 testExpected("event.systemCode", 0);
102 function keyMessage(event)
104 consoleWrite("keymessage event occurred");
106 testExpected("event.target", video);
107 testExpected("event instanceof window.MediaKeyEvent", true);
109 testExpected("event.keySystem", "webkit-org.w3.clearkey");
111 consoleWrite("The sessionId should be a non-empty string containing an integer.");
112 testExpected("event.sessionId", "", "!=");
113 testExpected("event.sessionId", null, "!=");
114 testExpected("event.sessionId", undefined, "!=");
115 testExpected("isNaN(event.sessionId)", false);
116 // Make sure the number is not a float.
117 testExpected("String(event.sessionId) == String(parseInt(event.sessionId))", true);
118 consoleWrite("Implementations should avoid sessionIds of 0.");
119 testExpected("event.sessionId", 0, ">");
120 // All other events should have this same sessionId.
121 keyMessageSessionId = event.sessionId;
123 firstRunKeyMessageSessionId = keyMessageSessionId;
125 consoleWrite("The sessionsId should be different from the first run.");
126 testExpected("event.sessionId != firstRunKeyMessageSessionId", true);
129 // initData is not used for this event.
130 testExpected("event.initData", null, "===");
131 // At least for now, the Clear Key message is the initData.
132 testArraysEqual("event.message", expectedInitData);
133 // Not supported by the test file.
134 testExpected("event.defaultURL", "");
135 // The error attributes are not used for this event.
136 testExpected("event.errorCode", null, "===");
137 testExpected("event.systemCode", 0);
140 run("video.webkitAddKey('webkit-org.w3.clearkey', key, event.initData, event.sessionId)");
143 function needKey(event)
145 consoleWrite("needkey event occurred");
146 // Clear the handler (for the first case) to prevent the second needkey event
147 // (there will be one each for audio and video) from being handled.
148 video.onwebkitneedkey=null;
150 testExpected("event.target", video);
151 testExpected("event instanceof window.MediaKeyEvent", true);
153 testExpected("event.keySystem", "");
154 testExpected("event.sessionId", "");
155 testArraysEqual("event.initData", expectedInitData);
156 // The other attributes are not used for this event.
157 testExpected("event.message", null, "===");
158 testExpected("event.defaultURL", "");
159 testExpected("event.errorCode", null, "===");
160 testExpected("event.systemCode", 0);
163 run("video.webkitGenerateKeyRequest('webkit-org.w3.clearkey', event.initData)");
170 consoleWrite("*** Test events using on* attributes. ***");
171 video.onwebkitkeyadded=keyAdded;
172 video.onwebkitkeyerror=keyError;
173 video.onwebkitkeymessage=keyMessage;
174 video.onwebkitneedkey=needKey;
176 consoleWrite("*** Test events using addEventListener(). ***");
178 // Clear the on* handlers.
179 video.onwebkitkeyadded=null;
180 video.onwebkitkeyerror=null;
181 video.onwebkitkeymessage=null;
182 video.onwebkitneedkey=null;
184 waitForEvent('webkitkeyadded', keyAdded);
185 waitForEvent('webkitkeyerror', keyError);
186 waitForEvent('webkitkeymessage', keyMessage);
187 waitForEventOnce('webkitneedkey', needKey);
190 video.src = "../../content/test-encrypted.webm";
194 consoleWrite("*** Verify the presence of on* attributes. These would return undefined if they are not present. ***");
195 testExpected("video.onwebkitkeyadded", null, "===");
196 testExpected("video.onwebkitkeyerror", null, "===");
197 testExpected("video.onwebkitkeymessage", null, "===");
198 testExpected("video.onwebkitneedkey", null, "===");