<!DOCTYPE html>
<html>
<head>
<script src="../../../resources/js-test.js"></script>
<script src="../../forms/resources/common.js"></script>
<script src="../../forms/resources/picker-common.js"></script>
<script src="resources/calendar-picker-common.js"></script>
</head>
<body>
<p id="description"></p>
<div id="console"></div>
<input type=week id=week value="2000-W01">
<script>
description("Tests if week picker mouse operations work as expected.");

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

openPicker(document.getElementById('week'), test);

function test() {
    shouldBeEqualToString('currentMonth()', '2000-01');
    shouldBeEqualToString('selectedValue()', '2000-W01');
    shouldBeEqualToString('selectedDayCells()', '2000-01-03,2000-01-04,2000-01-05,2000-01-06,2000-01-07,2000-01-08,2000-01-09');
    shouldBeEqualToString('highlightedValue()', '2000-W01');
    shouldBeEqualToString('highlightedDayCells()', '2000-01-03,2000-01-04,2000-01-05,2000-01-06,2000-01-07,2000-01-08,2000-01-09');

    debug('Check that hovering over an entry highlights it.');
    hoverOverDayCellAt(0, 0);
    shouldBeEqualToString('currentMonth()', '2000-01');
    shouldBeEqualToString('selectedValue()', '2000-W01');
    shouldBeEqualToString('selectedDayCells()', '2000-01-03,2000-01-04,2000-01-05,2000-01-06,2000-01-07,2000-01-08,2000-01-09');
    shouldBeEqualToString('highlightedValue()', '1999-W51');
    shouldBeEqualToString('highlightedDayCells()', '1999-12-26');

    hoverOverDayCellAt(6, 0);
    shouldBeEqualToString('currentMonth()', '2000-01');
    shouldBeEqualToString('selectedValue()', '2000-W01');
    shouldBeEqualToString('selectedDayCells()', '2000-01-03,2000-01-04,2000-01-05,2000-01-06,2000-01-07,2000-01-08,2000-01-09');
    shouldBeEqualToString('highlightedValue()', '1999-W52');
    shouldBeEqualToString('highlightedDayCells()', '1999-12-27,1999-12-28,1999-12-29,1999-12-30,1999-12-31,2000-01-01,2000-01-02');

    hoverOverDayCellAt(2, 1);
    shouldBeEqualToString('currentMonth()', '2000-01');
    shouldBeEqualToString('selectedValue()', '2000-W01');
    shouldBeEqualToString('selectedDayCells()', '2000-01-03,2000-01-04,2000-01-05,2000-01-06,2000-01-07,2000-01-08,2000-01-09');
    shouldBeEqualToString('highlightedValue()', '2000-W01');
    shouldBeEqualToString('highlightedDayCells()', '2000-01-03,2000-01-04,2000-01-05,2000-01-06,2000-01-07,2000-01-08,2000-01-09');

    hoverOverDayCellAt(6, 5);
    shouldBeEqualToString('currentMonth()', '2000-01');
    shouldBeEqualToString('selectedValue()', '2000-W01');
    shouldBeEqualToString('selectedDayCells()', '2000-01-03,2000-01-04,2000-01-05,2000-01-06,2000-01-07,2000-01-08,2000-01-09');
    shouldBeEqualToString('highlightedValue()', '2000-W05');
    shouldBeEqualToString('highlightedDayCells()', '2000-01-31,2000-02-01,2000-02-02,2000-02-03,2000-02-04,2000-02-05');

    debug('Check that mouse click sets the value.');
    setNoCloseOnCommit();
    clickDayCellAt(6, 0);
    shouldBeEqualToString('currentMonth()', '2000-01');
    shouldBeEqualToString('selectedValue()', '1999-W52');
    shouldBeEqualToString('selectedDayCells()', '1999-12-27,1999-12-28,1999-12-29,1999-12-30,1999-12-31,2000-01-01,2000-01-02');
    shouldBeEqualToString('highlightedValue()', '1999-W52');
    shouldBeEqualToString('highlightedDayCells()', '1999-12-27,1999-12-28,1999-12-29,1999-12-30,1999-12-31,2000-01-01,2000-01-02');
    shouldBeEqualToString('document.getElementById("week").value', '1999-W52');

    debug('Check that clicking the month popup button opens the month popup.');
    shouldBeFalse('popupWindow.global.picker.monthPopupView.isVisible');
    clickMonthPopupButton();
    shouldBeTrue('popupWindow.global.picker.monthPopupView.isVisible');
    shouldBeEqualToString('highlightedMonthButton()', '2000-01');

    debug('Check that hovering over a month button highlights it.');

    hoverOverMonthButton(2000, 9);
    shouldBeEqualToString('highlightedMonthButton()', '2000-10');

    debug('Check that month wheel scrolls the list in the month popup.');

    checkYearListViewScrollOffset();
    eventSender.mouseScrollBy(0, 120);
    shouldBeTrue('checkYearListViewScrollOffset() > 0');

    checkYearListViewScrollOffset();
    eventSender.mouseScrollBy(0, -120);
    shouldBeTrue('checkYearListViewScrollOffset() < 0');

    debug('Check that clicking the month popup sets the month.');

    clickMonthButton(2000, 5);
    shouldBeFalse('popupWindow.global.picker.monthPopupView.isVisible');
    shouldBeEqualToString('currentMonth()', '2000-06');

    debug('Check that clicking the month popup button opens the month popup.');
    shouldBeFalse('popupWindow.global.picker.monthPopupView.isVisible');
    clickMonthPopupButton();
    shouldBeTrue('popupWindow.global.picker.monthPopupView.isVisible');
    shouldBeEqualToString('highlightedMonthButton()', '2000-06');
    checkYearListViewScrollOffset();

    debug('Check that hovering over a month button highlights it.');

    hoverOverMonthButton(2000, 9);
    shouldBeEqualToString('highlightedMonthButton()', '2000-10');

    debug('Check that month wheel scrolls the list in the month popup.');

    eventSender.mouseScrollBy(0, 1);
    shouldBeTrue('checkYearListViewScrollOffset() > 0');

    eventSender.mouseScrollBy(0, -1);
    shouldBeTrue('checkYearListViewScrollOffset() < 0');

    debug('Check that clicking an year list cell opens it.');
    eventSender.mouseScrollBy(0, 10);
    shouldBeTrue('checkYearListViewScrollOffset() > 0');
    clickYearListCell(1999);
    shouldBeEqualToString('highlightedMonthButton()', '1999-10');

    debug('Check that clicking the month popup sets the month.');

    clickMonthButton(1999, 5);
    shouldBeFalse('popupWindow.global.picker.monthPopupView.isVisible');
    shouldBeEqualToString('currentMonth()', '1999-06');

    finishJSTest();
}
</script>
</body>
</html>