// this test assumes lib/rangy-inputs has already been loaded and applied its
// plugin to jQuery, this is done in test-main.js
define([
    'jquery',
    'bitbucket/internal/util/focus-snapshot'
], function(
    $,
    focus
) {

    'use strict';

    var $fixture;

    module('util-focus-snapshot', {
        setup: function () {
            $fixture = $('#qunit-fixture');
        }
    });

    test('Saving selection', function() {
        var selectionStart = 6;
        var selectionEnd = 10;

        // Let's add a Textarea and some text
        $fixture.append('<textarea id="fixture-textarea">Hello world!</textarea>');

        var $textarea = $fixture.find('#fixture-textarea');

        $textarea.setSelection(selectionStart, selectionEnd).focus();

        ok($textarea.is(document.activeElement), 'Fixture textarea is the currently active element');

        var initialSelection = $textarea.getSelection();

        equal(initialSelection.start, selectionStart, 'Selection starts at the right place');
        equal(initialSelection.end, selectionEnd, 'Selection ends at the right place');

        focus.save();

        $textarea.collapseSelection();

        $(document.activeElement).blur();

        ok(!$textarea.is(document.activeElement), 'Fixture textarea is not the active element.');

        equal( $textarea.getSelection().text, "", 'There is no longer a selection on the element after it has been blurred');

        focus.restore();

        deepEqual($textarea.getSelection(), initialSelection, 'Selection has been restored to the initial selection');
    });
});