3 <script src=
"../../http/tests/inspector/inspector-test.js"></script>
4 <script src=
"../../http/tests/inspector/console-test.js"></script>
6 function populateConsoleWithMessages(count
)
8 for (var i
= 0; i
< count
- 1; ++i
)
9 console
.log("Message #" + i
);
10 console
.log("hello %cworld", "color: blue");
12 //# sourceURL=console-viewport-selection.html
18 InspectorTest
.fixConsoleViewportDimensions(600, 200);
19 var consoleView
= WebInspector
.ConsolePanel
._view();
20 var viewport
= consoleView
._viewport
;
21 const minimumViewportMessagesCount
= 10;
22 const messagesCount
= 150;
23 const middleMessage
= messagesCount
/ 2;
24 var viewportMessagesCount
;
27 function verifyViewportIsTallEnough(next
)
29 viewport
.invalidate();
30 viewport
.forceScrollItemToBeFirst(0);
31 viewportMessagesCount
= viewport
.lastVisibleIndex() - viewport
.firstVisibleIndex() + 1;
32 if (viewportMessagesCount
< minimumViewportMessagesCount
) {
33 InspectorTest
.addResult(String
.sprintf("Test cannot be run as viewport is not tall enough. It is required to contain at least %d messages, but %d only fit", minimumViewportMessagesCount
, viewportMessagesCount
));
34 InspectorTest
.completeTest();
37 InspectorTest
.addResult(String
.sprintf("Viewport contains %d messages", viewportMessagesCount
));
41 function testSelectionSingleLineText(next
)
43 selectMessages(middleMessage
, 2, middleMessage
, 7);
45 dumpViewportRenderedItems();
49 function testReversedSelectionSingleLineText(next
)
51 selectMessages(middleMessage
, 7, middleMessage
, 2);
53 dumpViewportRenderedItems();
57 function testSelectionMultiLineText(next
)
59 selectMessages(middleMessage
- 1, 4, middleMessage
+ 1, 7);
61 dumpViewportRenderedItems();
65 function testSimpleVisibleSelection(next
)
67 selectMessages(middleMessage
- 3, 6, middleMessage
+ 2, 6);
69 dumpViewportRenderedItems();
73 function testHalfScrollSelectionUp(next
)
75 viewport
.forceScrollItemToBeFirst(middleMessage
);
77 dumpViewportRenderedItems();
81 function testHalfScrollSelectionDown(next
)
83 viewport
.forceScrollItemToBeFirst(middleMessage
- viewportMessagesCount
+ 1);
85 dumpViewportRenderedItems();
89 function testScrollSelectionAwayUp(next
)
91 viewport
.forceScrollItemToBeFirst(0);
93 dumpViewportRenderedItems();
97 function testScrollSelectionAwayDown(next
)
99 consoleView
._immediatelyScrollToBottom();
101 dumpSelectionModel();
102 dumpViewportRenderedItems();
106 function testShiftClickSelectionOver(next
)
108 emulateShiftClickOnMessage(minimumViewportMessagesCount
);
109 dumpSelectionModel();
110 dumpViewportRenderedItems();
114 function testShiftClickSelectionBelow(next
)
116 emulateShiftClickOnMessage(messagesCount
- minimumViewportMessagesCount
);
117 dumpSelectionModel();
118 dumpViewportRenderedItems();
122 function testRemoveSelection(next
)
124 var selection
= window
.getSelection();
125 selection
.removeAllRanges();
126 dumpSelectionModel();
127 dumpViewportRenderedItems();
131 function testReversedVisibleSelection(next
)
133 selectMessages(middleMessage
+ 1, 6, middleMessage
- 4, 6);
134 dumpSelectionModel();
135 dumpViewportRenderedItems();
139 function testShiftClickReversedSelectionOver(next
)
141 emulateShiftClickOnMessage(minimumViewportMessagesCount
);
142 dumpSelectionModel();
143 dumpViewportRenderedItems();
147 function testShiftClickReversedSelectionBelow(next
)
149 emulateShiftClickOnMessage(messagesCount
- minimumViewportMessagesCount
);
150 dumpSelectionModel();
151 dumpViewportRenderedItems();
155 function testZeroOffsetSelection(next
)
157 viewport
.forceScrollItemToBeLast(messagesCount
- 1);
158 var lastMessageElement
= viewport
.renderedElementAt(messagesCount
- 1);
159 // there is a blue-colored "world" span in last message.
160 var blueSpan
= lastMessageElement
;
161 while (blueSpan
.nodeName
!== "SPAN" || blueSpan
.textContent
!== "world")
162 blueSpan
= blueSpan
.traverseNextNode();
164 window
.getSelection().setBaseAndExtent(blueSpan
, 0, blueSpan
, blueSpan
.textContent
.length
);
165 InspectorTest
.addResult("Selected text: " + viewport
._selectedText());
169 function testSelectAll(next
)
171 viewport
.forceScrollItemToBeFirst(0);
173 // Set some initial selection in console.
174 var base
= consoleView
.itemElement(messagesCount
- 2);
175 var extent
= consoleView
.itemElement(messagesCount
- 1);
176 window
.getSelection().setBaseAndExtent(base
, 0, extent
, 0);
178 // Try to select all messages.
179 document
.execCommand("selectAll");
181 var text
= viewport
._selectedText();
182 var count
= text
? text
.split("\n").length
: 0;
183 InspectorTest
.addResult(count
=== messagesCount
? "Selected all " + count
+ " messages." : "Selected " + count
+ " messages instead of " + messagesCount
);
188 var awaitingMessagesCount
= messagesCount
;
189 function messageAdded()
191 if (!--awaitingMessagesCount
)
192 InspectorTest
.runTestSuite(testSuite
);
195 InspectorTest
.addConsoleSniffer(messageAdded
, true);
196 InspectorTest
.evaluateInPage(String
.sprintf("populateConsoleWithMessages(%d)", messagesCount
));
198 function dumpSelectionModelElement(model
)
202 return String
.sprintf("{item: %d, offset: %d}", model
.item
, model
.offset
);
205 function dumpSelectionModel()
208 var text
= String
.sprintf("anchor = %s, head = %s", dumpSelectionModelElement(viewport
._anchorSelection
), dumpSelectionModelElement(viewport
._headSelection
));
209 InspectorTest
.addResult(text
);
212 function dumpSelectionText()
215 var text
= viewport
._selectedText();
216 InspectorTest
.addResult("Selected text:<<<EOL\n" + text
+ "\nEOL");
219 function dumpViewportRenderedItems()
222 var firstVisibleIndex
= viewport
.firstVisibleIndex();
223 var lastVisibleIndex
= viewport
.lastVisibleIndex();
224 var renderedEnoughElements
= viewportMessagesCount
<= lastVisibleIndex
- firstVisibleIndex
+ 1;
225 InspectorTest
.addResult("first visible message index: " + firstVisibleIndex
+ " viewport is covered: " + (renderedEnoughElements
? "YES" : "NO"));
228 function emulateShiftClickOnMessage(messageIndex
)
231 var selection
= window
.getSelection();
232 if (!selection
|| !selection
.rangeCount
) {
233 InspectorTest
.addResult("FAILURE: There's no selection");
236 viewport
.forceScrollItemToBeFirst(Math
.max(messageIndex
- minimumViewportMessagesCount
/ 2, 0));
237 var element
= consoleView
.itemElement(messageIndex
).element();
238 selection
.setBaseAndExtent(selection
.anchorNode
, selection
.anchorOffset
, element
, 0);
242 function selectionContainerAndOffset(container
, offset
)
245 var node
= container
;
246 while (node
= node
.traverseNextTextNode(true)) {
247 var length
= node
.textContent
.length
;
248 if (charCount
+ length
>= offset
) {
251 offset
: offset
- charCount
259 function selectMessages(fromMessage
, fromTextOffset
, toMessage
, toTextOffset
)
261 if (Math
.abs(toMessage
- fromMessage
) > minimumViewportMessagesCount
) {
262 InspectorTest
.addResult(String
.sprintf("FAILURE: Cannot select more then %d messages (requested to select from %d to %d", minimumViewportMessagesCount
, fromMessage
, toMessage
));
263 InspectorTest
.completeTest();
266 viewport
.forceScrollItemToBeFirst(Math
.min(fromMessage
, toMessage
));
268 var from = selectionContainerAndOffset(consoleView
.itemElement(fromMessage
).element(), fromTextOffset
);
269 var to
= selectionContainerAndOffset(consoleView
.itemElement(toMessage
).element(), toTextOffset
);
270 window
.getSelection().setBaseAndExtent(from.container
, from.offset
, to
.container
, to
.offset
);
276 <body onload=
"runTest()">
278 Tests that console viewport handles selection properly.