<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
    <head>
        <script src="../../resources/js-test-pre.js"></script>
    </head>
    <body>
        <script>
            description("Tests the RTCPeerConnection stats interface.");

            var pc = null;
            var result;
            var timestamp;
            var local;

            function getUserMedia(dictionary, callback) {
                try {
                    navigator.webkitGetUserMedia(dictionary, callback, error);
                } catch (e) {
                    testFailed('webkitGetUserMedia threw exception :' + e);
                    finishJSTest();
                }
            }

            function error() {
                testFailed('Stream generation failed.');
                finishJSTest();
            }

            function statsHandler1(status)
            {
                testPassed("statsHandler1 was called");
                shouldBeNonNull('status');
                result = status.result();
                shouldBe('result.length', '0');
                shouldNotThrow('getUserMedia({audio:true, video:true}).then(gotStream)');
            }

            function gotStream(s) {
                testPassed('Got a stream.');
                stream = s;

                pc.addTrack(stream.getVideoTracks()[0]);
                // FIXME: Test getStats error callback: https://webkit.org/b/129860
                shouldNotThrow('pc.getStats().then(statsHandler2, statsError)');
            }

            function statsHandler2(status)
            {
                testPassed("statsHandler2 was called");
                // Status must be a global variable to be used in test statements.
                status_g = status;
                result = status.result();
                shouldBeGreaterThanOrEqual('result.length', '2');
                // The "local" interface is deprecated. Keep test until interface deleted.
                local = result[0].local;
                timestamp = local.timestamp;
                shouldBeGreaterThanOrEqual('timestamp', 'startTime');
                shouldBeGreaterThanOrEqual('local.names().length', '1');
                shouldBeGreaterThanOrEqual('local.names().indexOf("type")', '0');
                shouldBe('local.stat("type")', '"audio"');
                // Newer interface.
                res = result[0];
                shouldBeGreaterThanOrEqual('res.timestamp', 'startTime');
                shouldBeNonNull('res.id');
                shouldBeNonNull('res.type');
                shouldBeGreaterThanOrEqual('res.names().length', '1');
                shouldBeGreaterThanOrEqual('res.names().indexOf("type")', '0');
                shouldBe('res.stat("type")', '"audio"');
                // Named getter: Can access results by their ID values.
                shouldBeNonNull('status_g.namedItem(res.id)');
                shouldBeNonNull('status_g[res.id]');
                finishJSTest();
            }

            function statsError()
            {
                testFailed("Error in getStats.")
                finishJSTest();
            }

            var startTime = new Date().getTime();
            shouldNotThrow("pc = new RTCPeerConnection({iceServers:[{urls:'stun:foo.com'}]})");
            // FIXME: Test getStats error callback: https://webkit.org/b/129860
            shouldNotThrow('pc.getStats().then(statsHandler1, statsError)');

            window.jsTestIsAsync = true;
            window.successfullyParsed = true;
        </script>
        <script src="../../resources/js-test-post.js"></script>
    </body>
</html>
