OLD | NEW |
| (Empty) |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 // Test that cursor positions are correctly updated after adding new content. | |
6 | |
7 import 'test_try.dart'; | |
8 | |
9 void main() { | |
10 InteractionManager interaction = mockTryDartInteraction(); | |
11 | |
12 TestCase twoLines = | |
13 new TestCase('Test adding two lines programmatically.', () { | |
14 clearEditorPaneWithoutNotifications(); | |
15 mainEditorPane.appendText('\n\n'); | |
16 Text text = mainEditorPane.firstChild; | |
17 window.getSelection().collapse(text, 1); | |
18 checkSelectionIsCollapsed(text, 1); | |
19 }, checkAtBeginningOfSecondLine); | |
20 | |
21 runTests(<TestCase>[ | |
22 twoLines, | |
23 | |
24 new TestCase('Test adding a new text node.', () { | |
25 // This test relies on the previous test leaving two lines. | |
26 Text text = new Text('fisk'); | |
27 window.getSelection().getRangeAt(0).insertNode(text); | |
28 window.getSelection().collapse(text, text.length); | |
29 checkSelectionIsCollapsed(text, text.length); | |
30 }, checkAtEndOfSecondLineWithFisk), | |
31 | |
32 twoLines, | |
33 | |
34 new TestCase('Test adding a new wrapped text node.', () { | |
35 // This test relies on the previous test leaving two lines. | |
36 Text text = new Text('fisk'); | |
37 Node node = new SpanElement()..append(text); | |
38 window.getSelection().getRangeAt(0).insertNode(node); | |
39 window.getSelection().collapse(text, text.length); | |
40 checkSelectionIsCollapsed(text, text.length); | |
41 }, checkAtEndOfSecondLineWithFisk), | |
42 | |
43 new TestCase('Test adding a new line with mock key event.', () { | |
44 clearEditorPaneWithoutNotifications(); | |
45 checkSelectionIsCollapsed(mainEditorPane, 0); | |
46 simulateEnterKeyDown(interaction); | |
47 }, checkAtBeginningOfSecondLine), | |
48 | |
49 new TestCase('Clear and presetup the test', () { | |
50 clearEditorPaneWithoutNotifications(); | |
51 mainEditorPane.text = 'var greeting = "Hello, World!\n";'; | |
52 }, () { | |
53 checkLineCount(2); | |
54 }), | |
55 | |
56 new TestCase('Test removing a split line', () { | |
57 mainEditorPane.nodes.first.nodes.last.remove(); | |
58 }, () { | |
59 checkLineCount(1); | |
60 }), | |
61 ]); | |
62 } | |
63 | |
64 void simulateEnterKeyDown(InteractionManager interaction) { | |
65 interaction.onKeyUp( | |
66 new MockKeyboardEvent('keydown', keyCode: KeyCode.ENTER)); | |
67 } | |
68 | |
69 void checkSelectionIsCollapsed(Node node, int offset) { | |
70 var selection = window.getSelection(); | |
71 Expect.isTrue(selection.isCollapsed, 'selection.isCollapsed'); | |
72 Expect.equals(node, selection.anchorNode, 'selection.anchorNode'); | |
73 Expect.equals(offset, selection.anchorOffset, 'selection.anchorOffset'); | |
74 } | |
75 | |
76 void checkLineCount(int expectedLineCount) { | |
77 Expect.equals( | |
78 expectedLineCount, mainEditorPane.nodes.length, | |
79 'mainEditorPane.nodes.length'); | |
80 } | |
81 | |
82 void checkAtBeginningOfSecondLine() { | |
83 checkLineCount(2); | |
84 checkSelectionIsCollapsed(mainEditorPane.nodes[1].firstChild, 0); | |
85 } | |
86 | |
87 void checkAtEndOfSecondLineWithFisk() { | |
88 checkLineCount(2); | |
89 SpanElement secondLine = mainEditorPane.nodes[1]; | |
90 Text text = secondLine.firstChild.firstChild; | |
91 Expect.stringEquals('fisk', text.text); | |
92 Expect.equals(4, text.length); | |
93 Text newline = secondLine.firstChild.nextNode; | |
94 Expect.equals(newline, secondLine.lastChild); | |
95 /// Chrome and Firefox cannot agree on where to put the cursor. At the end | |
96 /// of [text] or at the beginning of [newline]. It's the same position. | |
97 if (window.getSelection().anchorOffset == 0) { | |
98 // Firefox. | |
99 checkSelectionIsCollapsed(newline, 0); | |
100 } else { | |
101 // Chrome. | |
102 checkSelectionIsCollapsed(text, 4); | |
103 } | |
104 } | |
105 | |
106 class MockKeyboardEvent extends KeyEvent { | |
107 final int keyCode; | |
108 | |
109 MockKeyboardEvent(String type, {int keyCode}) | |
110 : this.keyCode = keyCode, | |
111 super.wrap(new KeyEvent(type, keyCode: keyCode)); | |
112 | |
113 bool getModifierState(String keyArgument) => false; | |
114 } | |
OLD | NEW |