<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<script src="../../resources/js-test-pre.js"></script>
</head>
<body>
<p id="description"></p>
<div id="console"></div>
<script>
description('This tests that deselecting an option won&apos;t cause unnecessary scrolling.');

function mouseDownOnSelect(selId, index, modifier) {
    var sl = document.getElementById(selId);
    var itemHeight = Math.floor(sl.offsetHeight / sl.size);
    var border = 1;
    var y = border + index * itemHeight - window.pageYOffset;
    if (window.eventSender) {
        eventSender.mouseMoveTo(sl.offsetLeft + border, sl.offsetTop + y);
        eventSender.mouseDown(0, [modifier]);
        eventSender.mouseUp(0, [modifier]);
    }
}

function selectionPattern(select) {
    var result = '';
    for (var i = 0; i < select.options.length; i++)
        result += select.options[i].selected ? '1' : '0';
    return result;
}

var parent = document.createElement('div');
parent.innerHTML = '<select id="sl" multiple="multiple" size="5">'
    + '<option value="Accessibility">Accessibility</option>'
    + '<option value="CSS">CSS</option>'
    + '<option value="Drosera">Drosera</option>'
    + '<option value="Evangelism">Evangelism</option>'
    + '<option value="Forms">Forms</option>'
    + '<option value="Frames">Frames</option>'
    + '<option value="History">History</option>'
    + '<option value="HTML DOM">HTML DOM</option>'
    + '<option value="HTML Editing">HTML Editing</option>'
    + '<option value="Images">Images</option>'
    + '<option>SCROLL UP</option>'
    + '</select>';
document.body.appendChild(parent);

var sl = document.getElementById('sl');
sl.focus();
document.execCommand("SelectAll");
sl.scrollTop = Math.floor(sl.offsetHeight / sl.size) * 4 + 6;
var scrollBeforeClick = sl.scrollTop;
mouseDownOnSelect("sl", 3, "addSelectionKey");
shouldBe('sl.scrollTop', 'scrollBeforeClick');
shouldBe('selectionPattern(sl)', '"11111110111"');
</script>
<script src="../../resources/js-test-post.js"></script>
</body>
</html>
