summaryrefslogtreecommitdiffstats
path: root/third_party/WebKit/LayoutTests/editing/selection/programmatic-selection-on-mac-is-directionless.html
blob: 8453e12685660fa90447468afe9a0812a8d4fec2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<!DOCTYPE html>
<html>
<head>
<script src="../../fast/js/resources/js-test-pre.js"></script>
<script src="resources/js-test-selection-shared.js"></script>
</head>
<body>
<p id="description"></p>
<div id="console"></div>

<div id="test"> 
    <div id="regular-div">
        line 1<br>
        line 2<br>
        line 3
    </div> 
    <div contenteditable="true" id="editable-div">
         line 1<br>
         line 2<br>
         line 3
    </div> 
    <textarea id="text-area" cols="60" rows="7">
line 1
line 2
line 3</textarea>
    <input type="text" id="text-input" value="line 2"/>
</div>

<script>
description("This test ensures that programmatically set selections are directionless on mac, and that they are not on other platforms.");

function selectLine(node) {
    var selection = window.getSelection();
    selection.empty();

    if (node.localName == 'div') {
        var container = node.childNodes[2];
        var range = document.createRange();
        range.setStart(container, container.data.search('ine 2'));
        range.setEnd(container, range.startOffset + 'in'.length);
        selection.addRange(range);
    } else {
       node.selectionDirection = 'none';
       node.selectionStart = node.value.search('ine 2');
       node.selectionEnd = node.selectionStart + 'in'.length;
    }
}

function runTestsAndVerify(name, selectionModifier, expectedDirection, expectedText) {
    var currentTest = makeTest(name, selectionModifier, expectedDirection, expectedText);

    currentTest('no action');
    currentTest('delete');
    currentTest('forwardDelete');
    currentTest('cut');
    currentTest('bold');
    currentTest('insertText');
}

function makeTest(name, selectionModifier, expectedDirection, expectedText) {
    return function (action) {
        selectLine(name);
        var actionString = '';

        if (action != 'no action') {
            document.execCommand(action, false, 'word');
            document.execCommand('undo');
            actionString = ', after undoing ' + action;
        }

        var description = selectionModifier() + ' in ' + name.id + actionString;
        var selection = window.getSelection();
        var actualDirection = selection.baseOffset < selection.extentOffset ? 'forward' : (selection.baseOffset > selection.extentOffset ? 'backward' : 'none');

        if (selection.toString() != expectedText)
            testFailed(description + ', expected "' + expectedText + '" but got "' + selection.toString() + '"');
        else if (description.split(' ')[4] == 'div' && expectedDirection != actualDirection)
            testFailed(description + ', expected ' + expectedDirection + ' direction but got ' + actualDirection);
        else
            testPassed(description);
    }
}

function makeSelectionModifier(direction, granularity) {
    return function() {
        window.getSelection().modify('extend', direction, granularity);
        var testDescription = direction + ' by ' + granularity;
        return testDescription;
    }
}

function runTestsOn(platform, node) {
    var macOrNonMac = platform == 'mac' ? 'mac' : 'non-mac';
    var backwardOnMacAndForwardOtherwise = {'mac': 'backward', 'non-mac': 'forward'}[macOrNonMac];

    var extendSelectionLeftByCharacter = makeSelectionModifier('left', 'character');
    var extendSelectionRightByCharacter = makeSelectionModifier('right', 'character');
    var extendSelectionLeftByLine = makeSelectionModifier('left', 'line');
    var extendSelectionRightByLine = makeSelectionModifier('right', 'line');

    runTestsAndVerify(node, extendSelectionLeftByCharacter, backwardOnMacAndForwardOtherwise, {'mac': 'lin', 'non-mac': 'i'}[macOrNonMac]);
    runTestsAndVerify(node, extendSelectionRightByCharacter, 'forward', 'ine');

    if (node.localName == 'input')
        expectedResult = {'mac': 'lin', 'non-mac': 'l'};
    else if (node.localName == 'textarea')
        expectedResult = {'mac': 'ine 1\nlin', 'non-mac': 'e 1\nl'}
    else if (node.localName == 'div')
        expectedResult = {'mac': 'ine 1\nlin', 'non-mac': 'e 1\nl'}

    runTestsAndVerify(node, extendSelectionLeftByLine, backwardOnMacAndForwardOtherwise, expectedResult[macOrNonMac]);

    if (node.localName == 'input')
        expectedResult = 'ine 2';
    else if (node.localName == 'textarea')
        expectedResult = 'ine 2\nlin';
    else if (node.localName == 'div')
        expectedResult = 'ine 2\nlin';

    runTestsAndVerify(node, extendSelectionRightByLine, 'forward', expectedResult);
}

function runTestsFor(platform) {
    debug(platform + ':');
    internals.settings.setEditingBehavior(platform);
    var listOfTestNodes = document.getElementById('test').childNodes;

    for (var i = 0; i < listOfTestNodes.length; i++) {
        if (listOfTestNodes[i].nodeName != '#text')
            runTestsOn(platform, listOfTestNodes[i]);
    }
}

if (window.internals) {
    runTestsFor('mac');
    runTestsFor('win');
    runTestsFor('unix');
    runTestsFor('android');

    document.getElementById('test').innerHTML = '';
}

</script>
<script src="../../fast/js/resources/js-test-post.js"></script>
</body>
</html>