/*global defineSuite*/
defineSuite([
        'Core/loadWithXhr',
        'Core/loadImage'
    ], function(
        loadWithXhr,
        loadImage) {
    "use strict";

    describe('data URI loading', function() {
        it('can load URI escaped text with default response type', function() {
            return loadWithXhr({
                url : 'data:,Hello%2C%20World!'
            }).then(function(result) {
                expect(result).toEqual('Hello, World!');
            });
        });

        it('can load URI escaped text with responseType=text', function() {
            return loadWithXhr({
                url : 'data:,Hello%2C%20World!',
                responseType : 'text'
            }).then(function(result) {
                expect(result).toEqual('Hello, World!');
            });
        });

        it('can load Base64 encoded text with default response type', function() {
            return loadWithXhr({
                url : 'data:text/plain;base64,SGVsbG8sIFdvcmxkIQ=='
            }).then(function(result) {
                expect(result).toEqual('Hello, World!');
            });
        });

        it ('can load Base64 encoded text with responseType=text', function() {
            return loadWithXhr({
                url : 'data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==',
                responseType : 'text'
            }).then(function(result) {
                expect(result).toEqual('Hello, World!');
            });
        });

        it('can load Base64 & URI encoded text with default responseType', function() {
            return loadWithXhr({
                url : 'data:text/plain;base64,SGVsbG8sIFdvcmxkIQ%3D%3D'
            }).then(function(result) {
                expect(result).toEqual('Hello, World!');
            });
        });

        it('can load Base64 & URI encoded text with responseType=text', function() {
            return loadWithXhr({
                url : 'data:text/plain;base64,SGVsbG8sIFdvcmxkIQ%3D%3D',
                responseType : 'text'
            }).then(function(result) {
                expect(result).toEqual('Hello, World!');
            });
        });

        it('can load URI escaped HTML as text with default responseType', function() {
            return loadWithXhr({
                url : 'data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E'
            }).then(function(result) {
                expect(result).toEqual('<h1>Hello, World!</h1>');
            });
        });

        it('can load URI escaped HTML as text with responseType=text', function() {
            return loadWithXhr({
                url : 'data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E',
                responseType : 'text'
            }).then(function(result) {
                expect(result).toEqual('<h1>Hello, World!</h1>');
            });
        });

        it('can load URI escaped text as JSON', function() {
            return loadWithXhr({
                url : 'data:application/json,%7B%22key%22%3A%22value%22%7D',
                responseType : 'json'
            }).then(function(result) {
                expect(result.key).toEqual('value');
            });
        });

        it('can load Base64 encoded text as JSON', function() {
            return loadWithXhr({
                url : 'data:application/json;base64,eyJrZXkiOiJ2YWx1ZSJ9',
                responseType : 'json'
            }).then(function(result) {
                expect(result.key).toEqual('value');
            });
        });

        it('can load URI escaped HTML as document', function() {
            return loadWithXhr({
                url : 'data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E',
                responseType : 'document'
            }).then(function(result) {
                expect(result.querySelector('h1')).not.toBeNull();
            });
        });

        var tile = 'data:;base64, ';

        it('can load Base64 encoded data as arraybuffer', function() {
            return loadWithXhr({
                url : tile,
                responseType : 'arraybuffer'
            }).then(function(result) {
                expect(result.byteLength).toEqual(3914);
            });
        });

        var image = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAEISURBVEhLvVXBDYQwDOuojHKj8LhBbpTbpBCEkZsmIVTXq1RVQGrHiWlLmTTqPiZBlyLgy/KSZQ5JSHDQ/mCYCsC8106kDU0AdwRnvYZArWRcAl0dcYJq1hWCb3hBrumbDAVMwAC82WoRvgMnVMDBnB0nYZFTbE6BBvdUGqVqCbjBIk3PyFFR/NU7EKzru+qZsau3ryPwwCRLKYOzutZuCL6fUmWeJGzNzL/RxAMrUmASSCkkAayk2IxPlwhAAYGpsiHQjbLccfdOY5gKkCXAMi7SscAwbQpAnKyctWyUZ6z8ja3OGMepwD8asz+9FnSvbhU8uVOHFIwQsI3/p0CfhuqCSQuxLqsN6mu8SS+N42MAAAAASUVORK5CYII=';

        it('can load Base64 encoded data as blob', function() {
            return loadWithXhr({
                url : image,
                responseType : 'blob'
            }).then(function(result) {
                expect(result.type).toEqual('image/png');

                /*global URL*/
                var blobUrl = URL.createObjectURL(result);

                return loadImage(blobUrl).then(function(image) {
                    expect(image.width).toEqual(24);
                    expect(image.height).toEqual(24);
                });
            });
        });

        it('can support 2xx HTTP status (other than 200)', function(){
            return loadWithXhr({
                method: 'POST',
                url: 'http://jsonplaceholder.typicode.com/posts',
                data: {
                    title: 'foo',
                    body: 'bar',
                    userId: 1
                }
            }).then(function(result){
                expect(JSON.parse(result).id).toEqual(101);
            });
        });
    });
});
