<!DOCTYPE HTML>
<title>Test of text field setSelectionRange</title>
<link rel="author" title="Takeharu.Oshida" href="mailto:georgeosddev@gmail.com">
<link rel="help" href="https://html.spec.whatwg.org/multipage/#dom-textarea/input-setselectionrange">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id="log"></div>
<div id="hide" style="display: block">
  <input id="a" type="text" value="abcde">
  <textarea id="b">abcde</textarea>
</div>
<script>
var expected_direction_none;
setup(function() {
  var input = document.createElement("input");
  input.setSelectionRange(0, 1, "none");
  var direction = input.selectionDirection;
  if (direction !== "none" && direction !== "forward") {
    throw new Error("Unexpected direction");
  }
  expected_direction_none = direction;
});

test(function() {
  var input = document.getElementById("a");
  test(function() {
    assert_equals(typeof(input.setSelectionRange), "function", "element must have 'setSelectionRange' function");
  },"input typeof(input.setSelectionRange)'");

  test(function() {
    assert_equals(input.setSelectionRange(0,1,"forward"),undefined,"setSelectionRange is void functuon");
  },"input setSelectionRange return void");

  test(function() {
    input.setSelectionRange(0,1)
    assert_equals(input.selectionStart, 0, "element.selectionStart should be 0");
    assert_equals(input.selectionEnd, 1, "element.selectionEnd should be 1");
  },'input setSelectionRange(0,1)');

  test(function() {
    input.setSelectionRange(0,input.value.length+1)
    assert_equals(input.selectionEnd, input.value.length, "Arguments greater than the length of the value of the text field must be treated as pointing at the end of the text field");
  },'input setSelectionRange(0,input.value.length+1)');

  test(function() {
    input.setSelectionRange(input.value.length+1,input.value.length+1)
    assert_equals(input.selectionStart, input.value.length, "Arguments (start) greater than the length of the value of the text field must be treated as pointing at the end of the text field");
    assert_equals(input.selectionEnd, input.value.length, "Arguments (end) greater than the length of the value of the text field must be treated as pointing at the end of the text field");
  },'input setSelectionRange(input.value.length+1,input.value.length+1)');

  test(function() {
    input.setSelectionRange(input.value.length+1,1)
    assert_equals(input.selectionStart, 1, "If end is less than or equal to start then the start of the selection and the end of the selection must both be placed immediately before the character with offset end");
    assert_equals(input.selectionEnd, 1, "element.selectionEnd should be 1");
  },'input setSelectionRange(input.value.length+1,1)');

  test(function() {
    input.setSelectionRange(2,2)
    assert_equals(input.selectionStart, 2, "If end is less than or equal to start then the start of the selection and the end of the selection must both be placed immediately before the character with offset end");
    assert_equals(input.selectionEnd, 2, "If end is less than or equal to start then the start of the selection and the end of the selection must both be placed immediately before the character with offset end");
  },'input setSelectionRange(2,2)');

  test(function() {
    input.setSelectionRange(2,1)
    assert_equals(input.selectionStart, 1, "If end is less than or equal to start then the start of the selection and the end of the selection must both be placed immediately before the character with offset end");
    assert_equals(input.selectionEnd, 1, "If end is less than or equal to start then the start of the selection and the end of the selection must both be placed immediately before the character with offset end");
  },'input setSelectionRange(2,1)');

  test(function() {
    input.setSelectionRange(0,1,"backward")
    assert_equals(input.selectionDirection, "backward", 'The direction of the selection must be set to backward if direction is a case-sensitive match for the string "backward"');
  },'input direction of setSelectionRange(0,1,"backward")');

  test(function() {
    input.setSelectionRange(0,1,"forward")
    assert_equals(input.selectionDirection, "forward", 'The direction of the selection must be set to forward if direction is a case-sensitive match for the string "forward"');
  },'input direction of setSelectionRange(0,1,"forward")');

  test(function() {
    input.setSelectionRange(0,1,"none")
    assert_equals(input.selectionDirection, expected_direction_none);
  },'input direction of setSelectionRange(0,1,"none")');

  test(function() {
    input.setSelectionRange(0,1,"hoge")
    assert_equals(input.selectionDirection, expected_direction_none);
  },'input direction of setSelectionRange(0,1,"hoge")');

  test(function() {
    input.setSelectionRange(0,1,"BACKWARD")
    assert_equals(input.selectionDirection, expected_direction_none);
  },'input direction of setSelectionRange(0,1,"BACKWARD")');

  test(function() {
    input.setSelectionRange(0,1)
    assert_equals(input.selectionDirection, expected_direction_none);
  },'input direction of setSelectionRange(0,1)');

  test(function() {
    input.setSelectionRange(1,-1);
    assert_equals(input.selectionStart, 1, "element.selectionStart should be 1");
    assert_equals(input.selectionEnd, input.value.length, "ECMAScript conversion to unsigned long");
  },'input setSelectionRange(1,-1)');

  test(function() {
    input.setSelectionRange(-1,1);
    assert_equals(input.selectionStart, 1, "ECMAScript conversion to unsigned long + if end is less than or equal to start then the start of the selection and the end of the selection must both be placed immediately before the character with offset end");
    assert_equals(input.selectionEnd, 1, "element.selectionEnd should be 1");
  },'input setSelectionRange(-1,1)');

  test(function() {
    input.setSelectionRange("string",1);
    assert_equals(input.selectionStart, 0, "element.selectionStart should be 0");
    assert_equals(input.selectionEnd, 1, "element.selectionEnd should be 1");
  },'input setSelectionRange("string",1)');

  test(function() {
    input.setSelectionRange(true,1);
    assert_equals(input.selectionStart, 1, "element.selectionStart should be 1");
    assert_equals(input.selectionEnd, 1, "element.selectionEnd should be 1");
  },'input setSelectionRange(true,1)');

  test(function() {
    input.setSelectionRange([],1);
    assert_equals(input.selectionStart, 0, "element.selectionStart should be 0");
    assert_equals(input.selectionEnd, 1, "element.selectionEnd should be 1");
  },'input setSelectionRange([],1)');

  test(function() {
    input.setSelectionRange({},1);
    assert_equals(input.selectionStart, 0, "element.selectionStart should be 0");
    assert_equals(input.selectionEnd, 1, "element.selectionEnd should be 1");
  },'input setSelectionRange({},1)');

  test(function() {
    input.setSelectionRange(NaN,1);
    assert_equals(input.selectionStart, 0, "element.selectionStart should be 0");
    assert_equals(input.selectionEnd, 1, "element.selectionEnd should be 1");
  },'input setSelectionRange(NaN,1)');

  test(function() {
    input.setSelectionRange(null,1);
    assert_equals(input.selectionStart, 0, "element.selectionStart should be 0");
    assert_equals(input.selectionEnd, 1, "element.selectionEnd should be 1");
  },'input setSelectionRange(null,1)');

  test(function() {
    input.setSelectionRange(undefined,1);
    assert_equals(input.selectionStart, 0, "element.selectionStart should be 0");
    assert_equals(input.selectionEnd, 1, "element.selectionEnd should be 1");
  },'input setSelectionRange(undefined,1)');

  test(function() {
    input.setSelectionRange(Math.pow(2,32) - 2, Math.pow(2,32) - 1);
    assert_equals(input.selectionStart, input.value.length,
                 "element.selectionStart should be value.length");
    assert_equals(input.selectionEnd, input.value.length,
                  "element.selectionEnd should be value.length");
  }, 'input setSelectionRange(Math.pow(2,32) - 2, Math.pow(2,32) - 1)');

  test(function() {
    input.setSelectionRange(Math.pow(2,31), Math.pow(2,32) - 1);
    assert_equals(input.selectionStart, input.value.length,
                 "element.selectionStart should be value.length");
    assert_equals(input.selectionEnd, input.value.length,
                  "element.selectionEnd should be value.length");
  }, 'input setSelectionRange(Math.pow(2,31), Math.pow(2,32) - 1)');
},"test of input.setSelectionRange");

async_test(function() {
  var q = false;
  var input = document.getElementById("a");
  input.addEventListener("select", this.step_func_done(function(e) {
    assert_true(q, "event should be queued");
    assert_true(e.isTrusted, "event is trusted");
    assert_true(e.bubbles, "event bubbles");
    assert_false(e.cancelable, "event is not cancelable");
  }));
  input.setSelectionRange(0, 1);
  q = true;
}, "input setSelectionRange fires a select event");

test(function() {
  var textarea = document.getElementById("b");
  test(function() {
    assert_equals(typeof(textarea.setSelectionRange), "function", "element must have 'setSelectionRange' function");
  },"textarea typeof(input.setSelectionRange)'");

  test(function() {
    assert_equals(textarea.setSelectionRange(0,1,"forward"),undefined,"setSelectionRange is void functuon");
  },"textarea setSelectionRange return void");

  test(function() {
    textarea.setSelectionRange(0,1)
    assert_equals(textarea.selectionStart, 0, "element.selectionStart should be 0");
    assert_equals(textarea.selectionEnd, 1, "element.selectionEnd should be 1");
  },'textarea setSelectionRange(0,1)');

  test(function() {
    textarea.setSelectionRange(0,textarea.value.length+1)
    assert_equals(textarea.selectionEnd, textarea.value.length, "Arguments greater than the length of the value of the text field must be treated as pointing at the end of the text field");
  },'textarea setSelectionRange(0,textarea.value.length+1)');

  test(function() {
    textarea.setSelectionRange(2,2)
    assert_equals(textarea.selectionStart, 2, "If end is less than or equal to start then the start of the selection and the end of the selection must both be placed immediately before the character with offset end");
    assert_equals(textarea.selectionEnd, 2, "If end is less than or equal to start then the start of the selection and the end of the selection must both be placed immediately before the character with offset end");
  },'textarea setSelectionRange(2,2)');

  test(function() {
    textarea.setSelectionRange(2,1)
    assert_equals(textarea.selectionStart, 1, "If end is less than or equal to start then the start of the selection and the end of the selection must both be placed immediately before the character with offset end");
    assert_equals(textarea.selectionEnd, 1, "If end is less than or equal to start then the start of the selection and the end of the selection must both be placed immediately before the character with offset end");
  },'textarea setSelectionRange(2,1)');

  test(function() {
    textarea.setSelectionRange(0,1,"backward")
    assert_equals(textarea.selectionDirection, "backward", 'The direction of the selection must be set to backward if direction is a case-sensitive match for the string "backward"');
  },'textarea direction of setSelectionRange(0,1,"backward")');

  test(function() {
    textarea.setSelectionRange(0,1,"forward")
    assert_equals(textarea.selectionDirection, "forward", 'The direction of the selection must be set to forward if direction is a case-sensitive match for the string "forward"');
  },'textarea direction of setSelectionRange(0,1,"forward")');

  test(function() {
    textarea.setSelectionRange(0,1,"none")
    assert_equals(textarea.selectionDirection, expected_direction_none);
  },'textarea direction of setSelectionRange(0,1,"none")');

  test(function() {
    textarea.setSelectionRange(0,1,"hoge")
    assert_equals(textarea.selectionDirection, expected_direction_none);
  },'textarea direction of setSelectionRange(0,1,"hoge")');

  test(function() {
    textarea.setSelectionRange(0,1,"BACKWARD")
    assert_equals(textarea.selectionDirection, expected_direction_none);
  },'textarea direction of setSelectionRange(0,1,"BACKWARD")');

  test(function() {
    textarea.setSelectionRange(0,1)
    assert_equals(textarea.selectionDirection, expected_direction_none);
  },'textarea direction of setSelectionRange(0,1)');

  test(function() {
    textarea.setSelectionRange("string",1);
    assert_equals(textarea.selectionStart, 0, "element.selectionStart should be 0");
    assert_equals(textarea.selectionEnd, 1, "element.selectionStart should be 1");
  },'textarea setSelectionRange("string",1)');

  test(function() {
    textarea.setSelectionRange(true,1);
    assert_equals(textarea.selectionStart, 1, "element.selectionStart should be 1");
    assert_equals(textarea.selectionEnd, 1, "element.selectionStart should be 1");
  },'textarea setSelectionRange(true,1)');

  test(function() {
    textarea.setSelectionRange([],1);
    assert_equals(textarea.selectionStart, 0, "element.selectionStart should be 0");
    assert_equals(textarea.selectionEnd, 1, "element.selectionStart should be 1");
  },'textarea setSelectionRange([],1)');

  test(function() {
    textarea.setSelectionRange({},1);
    assert_equals(textarea.selectionStart, 0, "element.selectionStart should be 0");
    assert_equals(textarea.selectionEnd, 1, "element.selectionStart should be 1");
  },'textarea setSelectionRange({},1)');

  test(function() {
    textarea.setSelectionRange(NaN,1);
    assert_equals(textarea.selectionStart, 0, "element.selectionStart should be 0");
    assert_equals(textarea.selectionEnd, 1, "element.selectionStart should be 1");
  },'textarea setSelectionRange(NaN,1)');

  test(function() {
    textarea.setSelectionRange(null,1);
    assert_equals(textarea.selectionStart, 0, "element.selectionStart should be 0");
    assert_equals(textarea.selectionEnd, 1, "element.selectionStart should be 1");
  },'textarea setSelectionRange(null,1)');

  test(function() {
    textarea.setSelectionRange(undefined,1);
    assert_equals(textarea.selectionStart, 0, "element.selectionStart should be 0");
    assert_equals(textarea.selectionEnd, 1, "element.selectionStart should be 1");
  },'textarea setSelectionRange(undefined,1)');

  test(function() {
    textarea.setSelectionRange(Math.pow(2,32) - 2, Math.pow(2,32) - 1);
    assert_equals(textarea.selectionStart, textarea.value.length,
                 "element.selectionStart should be value.length");
    assert_equals(textarea.selectionEnd, textarea.value.length,
                  "element.selectionEnd should be value.length");
  }, 'textarea setSelectionRange(Math.pow(2,32) - 2, Math.pow(2,32) - 1)');

  test(function() {
    textarea.setSelectionRange(Math.pow(2,31), Math.pow(2,32) - 1);
    assert_equals(textarea.selectionStart, textarea.value.length,
                 "element.selectionStart should be value.length");
    assert_equals(textarea.selectionEnd, textarea.value.length,
                  "element.selectionEnd should be value.length");
  }, 'textarea setSelectionRange(Math.pow(2,31), Math.pow(2,32) - 1)');
},"test of textarea.setSelectionRange");
</script>
