<!DOCTYPE html>
<html>
<head>
<script src="../../../resources/js-test.js"></script>
<script src="../../forms/resources/common.js"></script>
<script src="../../forms/resources/common-wheel-event.js"></script>
<script src="../../forms/resources/picker-common.js"></script>
<script src="resources/suggestion-picker-common.js"></script>
</head>
<body style="background-color: #bbbbbb;">
<p id="description"></p>
<div id="console"></div>
<input type=date id=date value="2012-12-24" list=suggestions>
<datalist id=suggestions>
    <option label="Today">2012-01-01</option>
    <option label="Tomorrow">2012-01-02</option>
    <option>2012-01-03</option>
    <option>2012-01-04</option>
    <option>2012-01-05</option>
    <option>2012-01-06</option>
    <option>2012-01-07</option>
    <option>2012-01-08</option>
    <option>2012-01-09</option>
    <option>2012-01-10</option>
    <option>2012-01-11</option>
    <option>2012-01-12</option>
    <option>2012-01-13</option>
    <option>2012-01-14</option>
    <option>2012-01-15</option>
    <option>2012-01-16</option>
    <option>2012-01-17</option>
    <option>2012-01-18</option>
    <option>2012-01-19</option>
    <option>2012-01-20</option>
    <option label="-">2012-01-21</option>
    <option>2012-01-22</option>
    <option>2012-01-23</option>
    <option>2012-01-24</option>
    <option>2012-01-25</option>
    <option>2012-01-26</option>
    <option>2012-01-27</option>
    <option>2012-01-28</option>
    <option>2012-01-29</option>
    <option>2012-01-30</option>
    <option>2012-01-31</option>
    <option>2012-02-01</option>
    <option>2012-02-02</option>
    <option>2012-02-03</option>
    <option>2012-02-04</option>
    <option>2012-02-05</option>
    <option>2012-02-06</option>
    <option>2012-02-07</option>
    <option>2012-02-08</option>
    <option>2012-02-09</option>
    <option label="-">2012-02-10</option>
    <option>2012-02-11</option>
    <option>2012-02-12</option>
    <option>2012-02-13</option>
    <option>2012-02-14</option>
    <option>2012-02-15</option>
    <option>2012-02-16</option>
    <option>2012-02-17</option>
    <option>2012-02-18</option>
    <option>2012-02-19</option>
    <option>2012-02-20</option>
    <option>2012-02-21</option>
    <option>2012-02-22</option>
    <option>2012-02-23</option>
    <option>2012-02-24</option>
    <option>2012-02-25</option>
    <option>2012-02-26</option>
    <option>2012-02-27</option>
    <option>2012-02-28</option>
    <option>2012-02-29</option>
    <option>2012-03-01</option>
    <option>2012-03-02</option>
    <option>2012-03-03</option>
    <option>2012-03-04</option>
    <option>2012-03-05</option>
    <option>2012-03-06</option>
    <option>2012-03-07</option>
    <option>2012-03-08</option>
    <option>2012-01-00</option> <!--invalid-->
    <option>foo</option> <!--invalid-->
</datalist>
<script>

debug('Check that page popup doesn\'t exist at first.');
shouldBeNull('$("mock-page-popup")');

openPicker($('date'), test1);

function test1() {
    debug('Check that page popup exists.');
    shouldBeEqualToString('popupWindow.pagePopupController.toString()', '[object PagePopupController]');

    debug('Check that hovering over an entry highlights it.');
    hoverOverElement(popupWindow.document.querySelector(".suggestion-list-entry:nth-child(2)"));
    shouldBeEqualToString('highlightedEntry()', '2012-01-02');

    debug('Check that moving the mouse outside the popup de-highlights entries.');
    hoverOverElement(document.getElementById('description'));
    shouldBeNull('highlightedEntry()');

    debug('Check that mouse click closes the popup and sets the value.');
    clickElement(popupWindow.document.querySelector(".suggestion-list-entry:nth-child(3)"));
    waitUntilClosing(test1AfterClosing);
}

function test1AfterClosing() {
    shouldBeNull('$("mock-page-popup")');
    shouldBeEqualToString('document.getElementById("date").value', '2012-01-03');

    openPicker($('date'), test2);
}

var scrollTopBeforeWheelEvent = 0;
var scrollTopAfterWheelEvent = 0;
var suggestionList;

function test2() {
    suggestionList = popupWindow.document.getElementsByClassName("suggestion-list")[0];

    debug('Check that you can scroll using the mouse wheel.');

    function finishTest() {
        popupWindow.focus();
        eventSender.keyDown('escape');
        waitUntilClosing(test2AfterClosing);
    }

    function scrollUp() {
        scrollTopBeforeWheelEvent = suggestionList.scrollTop;
        dispatchWheelEvent(suggestionList, 0, 100);
        shouldBecomeEqual('scrollTopBeforeWheelEvent > suggestionList.scrollTop', 'true', finishTest);
    }

    function scrollDown() {
        scrollTopBeforeWheelEvent = suggestionList.scrollTop;
        dispatchWheelEvent(suggestionList, 0, -100);
        shouldBecomeEqual('scrollTopBeforeWheelEvent < suggestionList.scrollTop', 'true', scrollUp);
    }

    scrollDown();
}

function test2AfterClosing() {
    shouldBeNull('$("mock-page-popup")');

    openPicker($('date'), test3);
}

function test3() {
    debug('Check that you can scroll using the scroll bar.');

    var suggestionList = popupWindow.document.getElementsByClassName("suggestion-list")[0];
    var suggestionListOffset = cumulativeOffset(suggestionList);
    var scrollbarCenterX = suggestionListOffset[0] + suggestionList.offsetWidth - popupWindow.getScrollbarWidth() / 2;

    scrollTopBeforeWheelEvent = suggestionList.scrollTop;
    eventSender.mouseMoveTo(scrollbarCenterX, suggestionListOffset[1] + 20); // 20 is the height of the up arrow button plus a few pixels
    eventSender.mouseDown();
    eventSender.mouseMoveTo(scrollbarCenterX, suggestionListOffset[1] + suggestionList.offsetHeight);
    eventSender.mouseUp();
    scrollTopAfterWheelEvent = suggestionList.scrollTop;
    shouldBeTrue('scrollTopBeforeWheelEvent < scrollTopAfterWheelEvent');

    finishJSTest();
}

</script>
</body>
</html>