Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / media / encrypted-media / prefixed / encrypted-media-events.html
1 <!doctype html>
2 <html lang="en">
3     <head>
4     </head>
5     <body>
6         <video></video>
7         <p>Test all the key-related events.</p>
8
9         <script src="../encrypted-media-utils.js"></script>
10         <script src=../../media-file.js></script>
11         <script src=../../video-test.js></script>
12         <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);
20
21             // Next, The test runs twice, once using on* and then using addEventListener().
22             var isFirstRun = true;
23
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]);
31
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;
37
38             function keyAdded(event)
39             {
40                 consoleWrite("keyadded event occurred");
41
42                 testExpected("event.target", video);
43                 testExpected("event instanceof window.MediaKeyEvent", true);
44
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);
53
54                 consoleWrite("");
55                 // Cause a keyerror by passing an invalid key.
56                 run("video.webkitAddKey('webkit-org.w3.clearkey', invalidKey, null, event.sessionId)");
57             }
58
59             function keyError(event)
60             {
61                 consoleWrite("keyerror event occurred");
62
63                 testExpected("event.target", video);
64                 testExpected("event instanceof window.MediaKeyEvent", true);
65
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);
75
76                 if (isFirstRun) {
77                     isFirstRun = false;
78                     runTest();
79                 } else {
80                     consoleWrite("");
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");
89
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);
97
98                     endTest();
99                 }
100             }
101
102             function keyMessage(event)
103             {
104                 consoleWrite("keymessage event occurred");
105
106                 testExpected("event.target", video);
107                 testExpected("event instanceof window.MediaKeyEvent", true);
108
109                 testExpected("event.keySystem", "webkit-org.w3.clearkey");
110
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;
122                 if (isFirstRun)
123                     firstRunKeyMessageSessionId = keyMessageSessionId;
124                 else {
125                     consoleWrite("The sessionsId should be different from the first run.");
126                     testExpected("event.sessionId != firstRunKeyMessageSessionId", true);
127                 }
128
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);
138
139                 consoleWrite("");
140                 run("video.webkitAddKey('webkit-org.w3.clearkey', key, event.initData, event.sessionId)");
141             }
142
143             function needKey(event)
144             {
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;
149
150                 testExpected("event.target", video);
151                 testExpected("event instanceof window.MediaKeyEvent", true);
152
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);
161
162                 consoleWrite("");
163                 run("video.webkitGenerateKeyRequest('webkit-org.w3.clearkey', event.initData)");
164             }
165
166             function runTest()
167             {
168                 consoleWrite("");
169                 if (isFirstRun) {
170                     consoleWrite("*** Test events using on* attributes. ***");
171                     video.onwebkitkeyadded=keyAdded;
172                     video.onwebkitkeyerror=keyError;
173                     video.onwebkitkeymessage=keyMessage;
174                     video.onwebkitneedkey=needKey;
175                 } else {
176                     consoleWrite("*** Test events using addEventListener(). ***");
177
178                     // Clear the on* handlers.
179                     video.onwebkitkeyadded=null;
180                     video.onwebkitkeyerror=null;
181                     video.onwebkitkeymessage=null;
182                     video.onwebkitneedkey=null;
183
184                     waitForEvent('webkitkeyadded', keyAdded);
185                     waitForEvent('webkitkeyerror', keyError);
186                     waitForEvent('webkitkeymessage', keyMessage);
187                     waitForEventOnce('webkitneedkey', needKey);
188                 }
189
190                 video.src = "../../content/test-encrypted.webm";
191             }
192
193             consoleWrite("");
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, "===");
199
200             runTest();
201
202         </script>
203     </body>
204 </html>