summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/encrypted-media/scripts/keystatuses-multiple-sessions.js
blob: e9bf10e8860e85eaa43369246a744eaf17749cd1 (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
function runTest(config,qualifier)
{
    var testname = testnamePrefix(qualifier, config.keysystem) + ', temporary, keystatuses, multiple sessions';

    var configuration = getSimpleConfigurationForContent(config.content);

    if (config.initDataType && config.initData) configuration.initDataTypes = [config.initDataType];

    async_test(function(test)
    {
        var mediaKeySession1;
        var mediaKeySession2;

        // Even though key ids are uint8, using printable values so that
        // they can be verified easily.
        var key1 = new Uint8Array(config.content.keys[0].kid),
            key2 = new Uint8Array(config.content.keys[1].kid),
            variant1 = config.content.keys[0].variantId,
            variant2 = config.content.keys[1].variantId;

        function onFailure(error) {
            forceTestFailureFromPromise(test,error);
        }

        function processMessage1(event)
        {
            // This should only be called for session1.
            assert_equals(event.target, mediaKeySession1);

            // No keys added yet.
            verifyKeyStatuses(mediaKeySession1.keyStatuses, {expected: [], unexpected: [key1, key2]});

            // Add key1 to session1.
            config.messagehandler(event.messageType, event.message, {variantId:variant1}).then(function(response) {
                return event.target.update(response);
            }).catch(onFailure);

        }

        function processKeyStatusesChange1(event)
        {
            // This should only be called for session1.
            assert_equals(event.target, mediaKeySession1);

            // Check that keyStatuses contains the expected key1 only.
            verifyKeyStatuses(mediaKeySession1.keyStatuses, {expected: [key1], unexpected: [key2]});

            // Now trigger a message event on session2.
            mediaKeySession2.generateRequest(config.initDataType, config.initData[1]).catch(onFailure);
        }

        function processMessage2(event)
        {
            // This should only be called for session2.
            assert_equals(event.target, mediaKeySession2);

            // session2 has no keys added yet.
            verifyKeyStatuses(mediaKeySession2.keyStatuses, {expected: [], unexpected: [key1, key2]});

            // session1 should still have 1 key.
            verifyKeyStatuses(mediaKeySession1.keyStatuses, {expected: [key1], unexpected: [key2]});

            // Add key2 to session2.
            config.messagehandler(event.messageType, event.message, {variantId:variant2}).then(function(response) {
                return event.target.update(response);
            }).catch(onFailure);
        }

        function processKeyStatusesChange2(event)
        {
            // This should only be called for session2.
            assert_equals(event.target, mediaKeySession2);

            // Check that keyStatuses contains the expected key2 only.
            verifyKeyStatuses(mediaKeySession2.keyStatuses, {expected: [key2], unexpected: [key1]});

            // session1 should still have 1 key.
            verifyKeyStatuses(mediaKeySession1.keyStatuses, {expected: [key1], unexpected: [key2]});

            test.done();
        }

        navigator.requestMediaKeySystemAccess(config.keysystem, [configuration]).then(function(access) {
            return access.createMediaKeys();
        }).then(function(mediaKeys) {
            mediaKeySession1 = mediaKeys.createSession();
            mediaKeySession2 = mediaKeys.createSession();

            // There should be no keys defined on either session.
            verifyKeyStatuses(mediaKeySession1.keyStatuses, {expected: [], unexpected: [key1, key2]});
            verifyKeyStatuses(mediaKeySession2.keyStatuses, {expected: [], unexpected: [key1, key2]});

            // Bind all the event handlers now.
            waitForEventAndRunStep('message', mediaKeySession1, processMessage1, test);
            waitForEventAndRunStep('message', mediaKeySession2, processMessage2, test);
            waitForEventAndRunStep('keystatuseschange', mediaKeySession1, processKeyStatusesChange1, test);
            waitForEventAndRunStep('keystatuseschange', mediaKeySession2, processKeyStatusesChange2, test);

            // Generate a request on session1.
            return mediaKeySession1.generateRequest(config.initDataType, config.initData[0]);
        }).catch(onFailure);
    },  testname );
}