/*jslint onevar: false, eqeqeq: false, browser: true*/
/*globals window XMLHttpRequest ActiveXObject sinon buster*/
/**
 * @author Christian Johansen (christian@cjohansen.no)
 * @license BSD
 *
 * Copyright (c) 2010-2012 Christian Johansen
 */
"use strict";

(function (global) {
    var globalXMLHttpRequest = global.XMLHttpRequest;
    var globalActiveXObject = global.ActiveXObject;

    var fakeXhrSetUp = function () {
        this.fakeXhr = sinon.useFakeXMLHttpRequest();
    };

    var fakeXhrTearDown = function () {
        if (typeof this.fakeXhr.restore == "function") {
            this.fakeXhr.restore();
        }
    };

    var runWithWorkingXHROveride = function(workingXHR,test) {
        try {
            var original = sinon.xhr.workingXHR;
            sinon.xhr.workingXHR = workingXHR;
            test();
        } finally {
            sinon.xhr.workingXHR = original;
        }
    };

    buster.testCase("sinon.FakeXMLHttpRequest", {
        tearDown: function () {
            delete sinon.FakeXMLHttpRequest.onCreate;
        },

        "is constructor": function () {
            assert.isFunction(sinon.FakeXMLHttpRequest);
            assert.same(sinon.FakeXMLHttpRequest.prototype.constructor, sinon.FakeXMLHttpRequest);
        },

        "implements readyState constants": function () {
            assert.same(sinon.FakeXMLHttpRequest.OPENED, 1);
            assert.same(sinon.FakeXMLHttpRequest.HEADERS_RECEIVED, 2);
            assert.same(sinon.FakeXMLHttpRequest.LOADING, 3);
            assert.same(sinon.FakeXMLHttpRequest.DONE, 4);
        },

        "calls onCreate if listener is set": function () {
            var onCreate = sinon.spy();
            sinon.FakeXMLHttpRequest.onCreate = onCreate;

            var xhr = new sinon.FakeXMLHttpRequest();

            assert(onCreate.called);
        },

        "passes new object to onCreate if set": function () {
            var onCreate = sinon.spy();
            sinon.FakeXMLHttpRequest.onCreate = onCreate;

            var xhr = new sinon.FakeXMLHttpRequest();

            assert.same(onCreate.getCall(0).args[0], xhr);
        },

        "open": {
            setUp: function () {
                this.xhr = new sinon.FakeXMLHttpRequest();
            },

            "is method": function () {
                assert.isFunction(this.xhr.open);
            },

            "sets properties on object": function () {
                this.xhr.open("GET", "/my/url", true, "cjno", "pass");

                assert.equals(this.xhr.method, "GET");
                assert.equals(this.xhr.url, "/my/url");
                assert.isTrue(this.xhr.async);
                assert.equals(this.xhr.username, "cjno");
                assert.equals(this.xhr.password, "pass");
            },

            "is async by default": function () {
                this.xhr.open("GET", "/my/url");

                assert.isTrue(this.xhr.async);
            },

            "sets async to false": function () {
                this.xhr.open("GET", "/my/url", false);

                