<!DOCTYPE html>
<meta name=fuzzy content="maxDifference=0-3; totalPixels=0-1000">
<script src="../../../resources/testharness.js"></script>
<script src="../../../resources/testharnessreport.js"></script>
<script src="../../../resources/testdriver.js"></script>
<script src="../../../resources/testdriver-vendor.js"></script>
<script src="../resources/common.js"></script>
<script src="../resources/picker-common.js"></script>
<script src="../calendar-picker/resources/calendar-picker-common.js"></script>
<input type="week" id="week" value="2020-W02" oninput="inputEventCount++;" onchange="changeEventCount++">
<script>
'use strict';
var inputEventCount = 0;
var changeEventCount = 0;
let weekElement = document.getElementById("week");
promise_test(() => {
assert_equals(internals.pagePopupWindow, null);
return openPicker(weekElement).then(() => {
assert_equals(weekElement.value, "2020-W02");
assert_not_equals(internals.pagePopupWindow, null);
assert_equals(inputEventCount, 0, "No input event should have not have fired before making changes.");
assert_equals(changeEventCount, 0, "No change event should have fired before popup is closed.");
eventSender.keyDown('ArrowRight');
eventSender.keyDown('ArrowDown');
assert_equals(weekElement.value, "2020-W04");
assert_equals(inputEventCount, 2, "One input event should have fired for each modification.");
assert_equals(changeEventCount, 0, "No change event should have fired before popup is closed.");
eventSender.keyDown('Enter');
assert_equals(internals.pagePopupWindow, null, "Popup should have closed from Enter key");
assert_equals(inputEventCount, 2, 'No extra input event should fire when closing popup.');
assert_equals(changeEventCount, 0, 'Change event is fired asynchronously after closing popup.');
return new Promise((resolve) => {
window.setTimeout(() => {
assert_equals(changeEventCount, 1, 'Change event should have fired (once) asynchronously after closing popup.');
resolve();
}, 1);
});
});
}, "Week picker: Test input and change event firing when popup value is modified");
promise_test(() => {
inputEventCount = changeEventCount = 0;
assert_equals(internals.pagePopupWindow, null);
return openPicker(weekElement).then(() => {
assert_equals(weekElement.value, "2020-W04");
assert_not_equals(internals.pagePopupWindow, null);
assert_equals(inputEventCount, 0, "No input event should have not have fired before making changes.");
assert_equals(changeEventCount, 0, "No change event should have fired before popup is closed.");
eventSender.keyDown('ArrowRight');
assert_equals(weekElement.value, "2020-W05");
assert_equals(inputEventCount, 1, "One input event should have fired for the modification.");
assert_equals(changeEventCount, 0, "No change event should have fired before popup is closed.");
eventSender.keyDown('Escape');
assert_equals(weekElement.value, "2020-W04");
assert_not_equals(internals.pagePopupWindow, null, "Popup should not have closed from single escape");
assert_equals(inputEventCount, 2, "One input event should have fired when restoring the initial value.");
assert_equals(changeEventCount, 0, "No change event should have fired before popup is closed.");
eventSender.keyDown('Escape');
assert_equals(internals.pagePopupWindow, null, "Popup should have closed from Enter key");
assert_equals(inputEventCount, 2, 'No extra input event should fire when closing popup.');
return new Promise((resolve) => {
window.setTimeout(() => {
assert_equals(changeEventCount, 0, 'No change event should fire if popup was submitted with value still matching the original.');
resolve();
}, 1);
});
});
}, "Week picker: Test that change does not fire if popup value is modified and then restored to original value.");
</script>