<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<script src="../../resources/js-test-pre.js"></script>
</head>
<body>
<script language="javascript" type="text/javascript">
description('Test that WebKit changes the dir attribute and sends an input event when we change the text direction.');

// The string used for storing the expected text direction when we receive an input event.
var expected = '';
var sentInputEvent = false;

function removeChildAndForceGC(child) {
    document.body.removeChild(child);
    gc();
}

// Create a textarea element and an input element. These elements are used for
// changing their text direction with Editor::setBaseWritingDirection() calls.
var textarea = document.createElement('textarea');
textarea.rows = 10;
textarea.cols = 10;
textarea.oninput = function() {
    shouldBe('expected', 'textarea.dir');
    sentInputEvent = true;

    // When we change the direction to ltr, we remove this element to verify WebKit
    // continue running without crashes.
    if (expected == 'ltr')
        removeChildAndForceGC(textarea);
}
document.body.appendChild(textarea);

var input = document.createElement('input');
input.type = 'text';
input.oninput = function() {
    shouldBe('expected', 'input.dir');
    sentInputEvent = true;

    // When we change the direction to ltr, we remove this element to verify WebKit
    // continue running without crashes.
    if (expected == 'ltr')
        removeChildAndForceGC(input);
}
document.body.appendChild(input);

// Change the text direction of the textarea element to RTL.
expected = 'rtl';
sentInputEvent = false;
textarea.focus();
testRunner.setTextDirection('rtl');
shouldBeTrue('sentInputEvent');

// Change the text direction of the textarea element to LTR.
// This also removes the element to verify WebKit works without crashes.
expected = 'ltr';
sentInputEvent = false;
textarea.focus();
testRunner.setTextDirection('ltr');
shouldBeTrue('sentInputEvent');

// Change the text direction of the input element to RTL.
expected = 'rtl';
sentInputEvent = false;
input.focus();
testRunner.setTextDirection('rtl');
shouldBeTrue('sentInputEvent');

// Change the text direction of the input element to LTR.
// This also removes the element to verify WebKit works without crashes.
expected = 'ltr';
sentInputEvent = false;
input.focus();
testRunner.setTextDirection('ltr');
shouldBeTrue('sentInputEvent');
</script>
<script src="../../resources/js-test-post.js"></script>
</body>
</html>
