/*

Siesta 5.2.1
Copyright(c) 2009-2019 Bryntum AB
https://bryntum.com/contact
https://bryntum.com/products/siesta/license

*/
Class('Siesta.Test.Simulator', {

    does        : [
        Siesta.Util.Role.CanGetType,
        Siesta.Test.Browser.Role.CanRebindJQueryContext,

        Siesta.Test.Simulate.Event,
        Siesta.Test.Simulate.Mouse,
        Siesta.Test.Simulate.Keyboard,
        Siesta.Test.Simulate.Touch
    ],

    has : {
        type            : 'synthetic',

        test            : null,
        global          : null
    },


    methods : {

        initialize : function () {
            this.SUPER();

            this.onBlur = this.onBlur.bind(this);
        },

        onTestLaunch : function (test) {
            var me = this;

            me.test       = test
            me.global     = test.global

            // Synthetic events unfortunately doesn't trigger change events on blur of an INPUT
            if (me.type === 'synthetic') {
                var inputFiresChangeAfterLosingFocus  = Siesta.Project.Browser.FeatureSupport().supports.inputFiresChangeAfterLosingFocus;

                if (!inputFiresChangeAfterLosingFocus) {
                    me.global.document.documentElement.addEventListener('blur', me.onBlur, true);
                }
            }
        },

        onBlur : function (e) {
            if (e.target && this.test.isTextInput(e.target)) {
                this.maybeMimicChangeEvent(e.target);
            }
        },

        cleanup : function () {
            // Added check that global exists, made tests crash without
            this.global && this.global.document.documentElement.removeEventListener('blur', this.onBlur, true);

            this.test       = null
            this.global     = null
        }
    }
});