| <html> |
| <head> |
| <script src="../../http/tests/inspector/inspector-test.js"></script> |
| <script src="editor-test.js"></script> |
| <script> |
| function test() |
| { |
| function dumpTextModel(prefix, textModel, range) |
| { |
| var text = InspectorTest.textWithSelection(textModel.text(), range); |
| InspectorTest.addResult(prefix + text); |
| } |
| |
| function typeText(textModel, startRange, text) |
| { |
| var range = startRange; |
| for (var i = 0; i < text.length; ++i) |
| range = textModel.editRange(range, text[i]).collapseToEnd(); |
| return range; |
| } |
| |
| function typeDelete(textModel, startRange, count) |
| { |
| var count = count || 1; |
| var range = startRange; |
| for (var i = 0; i < count; ++i) { |
| var deleteRange = range.isEmpty() ? textModel.growRangeRight(range) : range; |
| range = textModel.editRange(deleteRange, "", range).collapseToEnd(); |
| } |
| return range; |
| } |
| |
| function typeBackspace(textModel, startRange, count) |
| { |
| var count = count || 1; |
| var range = startRange; |
| for (var i = 0; i < count; ++i) { |
| var backspaceRange = range.isEmpty() ? textModel.growRangeLeft(range) : range; |
| range = textModel.editRange(backspaceRange, "", range).collapseToEnd(); |
| } |
| return range; |
| } |
| |
| InspectorTest.runTestSuite([ |
| function testUndoRedoTab(next) |
| { |
| var textModel = new WebInspector.TextEditorModel(); |
| textModel.setText("1\n2\n3\n"); |
| dumpTextModel("Text before edit:\n", textModel); |
| var range = typeText(textModel, new WebInspector.TextRange(1, 0, 1, 0), "\t") |
| dumpTextModel("Text after edit:\n", textModel, range); |
| range = textModel.undo(); |
| dumpTextModel("Text after undo:\n", textModel, range); |
| range = textModel.redo(); |
| dumpTextModel("Text after redo:\n", textModel, range); |
| next(); |
| }, |
| |
| function testConsecutiveCharactersAndNewLines(next) |
| { |
| var textModel = new WebInspector.TextEditorModel(); |
| textModel.setText("function foo()\n{\n\n}\n"); |
| dumpTextModel("Text before edit:\n", textModel); |
| var range = typeText(textModel, new WebInspector.TextRange(2, 0, 2, 0), " bar();\n baz();\n foo();"); |
| dumpTextModel("Text after edit:\n", textModel, range); |
| range = textModel.undo(); |
| dumpTextModel("Text after first undo:\n", textModel, range); |
| range = textModel.undo(); |
| dumpTextModel("Text after second undo:\n", textModel, range); |
| range = textModel.undo(); |
| dumpTextModel("Text after third undo:\n", textModel, range); |
| range = textModel.redo(); |
| dumpTextModel("Text after first redo:\n", textModel, range); |
| range = textModel.redo(); |
| dumpTextModel("Text after second redo:\n", textModel, range); |
| range = textModel.redo(); |
| dumpTextModel("Text after third redo:\n", textModel, range); |
| next(); |
| }, |
| |
| function testEditRangeWithTheSameTextAsOriginal(next) |
| { |
| var textModel = new WebInspector.TextEditorModel(); |
| var functionText = " bar();\n baz();\n foo();"; |
| textModel.setText("function foo()\n{\n\n}\n"); |
| dumpTextModel("Text before edit:\n", textModel); |
| var range = typeText(textModel, new WebInspector.TextRange(2, 0, 2, 0), " bar();\n baz();\n foo();"); |
| dumpTextModel("Text after edit:\n", textModel, range); |
| textModel.editRange(new WebInspector.TextRange(3, 0, 4, 0), " baz();\n"); |
| dumpTextModel("Text after paste of the same text:\n", textModel, range); |
| range = textModel.undo(); |
| dumpTextModel("Text after first undo:\n", textModel, range); |
| range = textModel.undo(); |
| dumpTextModel("Text after second undo:\n", textModel, range); |
| range = textModel.redo(); |
| dumpTextModel("Text after first redo:\n", textModel, range); |
| range = textModel.redo(); |
| dumpTextModel("Text after second redo:\n", textModel, range); |
| next(); |
| }, |
| |
| function testDelete(next) |
| { |
| var textModel = new WebInspector.TextEditorModel(); |
| var functionText = " bar();\n baz();\n foo();"; |
| textModel.setText("function foo()\n{\n\n}\n"); |
| dumpTextModel("Text before edit:\n", textModel); |
| var range = typeText(textModel, new WebInspector.TextRange(2, 0, 2, 0), " bar();\n baz();\n foo();"); |
| dumpTextModel("Text after edit:\n", textModel, range); |
| range = typeDelete(textModel, new WebInspector.TextRange(2, 0, 2, 0), functionText.length); |
| dumpTextModel("Text after deletes:\n", textModel, range); |
| range = textModel.undo(); |
| dumpTextModel("Text after first undo:\n", textModel, range); |
| range = textModel.undo(); |
| dumpTextModel("Text after second undo:\n", textModel, range); |
| range = textModel.redo(); |
| dumpTextModel("Text after first redo:\n", textModel, range); |
| range = textModel.redo(); |
| dumpTextModel("Text after second redo:\n", textModel, range); |
| next(); |
| }, |
| |
| function testBackspace(next) |
| { |
| var textModel = new WebInspector.TextEditorModel(); |
| var functionText = " bar();\n baz();\n foo();"; |
| textModel.setText("function foo()\n{\n\n}\n"); |
| dumpTextModel("Text before edit:\n", textModel); |
| var range = typeText(textModel, new WebInspector.TextRange(2, 0, 2, 0), " bar();\n baz();\n foo();"); |
| dumpTextModel("Text after edit:\n", textModel, range); |
| range = typeBackspace(textModel, range, functionText.length); |
| dumpTextModel("Text after backspaces:\n", textModel, range); |
| range = textModel.undo(); |
| dumpTextModel("Text after first undo:\n", textModel, range); |
| range = textModel.undo(); |
| dumpTextModel("Text after second undo:\n", textModel, range); |
| range = textModel.redo(); |
| dumpTextModel("Text after first redo:\n", textModel, range); |
| range = textModel.redo(); |
| dumpTextModel("Text after second redo:\n", textModel, range); |
| next(); |
| }, |
| |
| function testBackspaceAndDeleteInDifferentLines(next) |
| { |
| var textModel = new WebInspector.TextEditorModel(); |
| var functionText = " bar();\n baz();\n foo();"; |
| textModel.setText("function foo()\n{\n\n}\n"); |
| dumpTextModel("Text before edit:\n", textModel); |
| var range = typeText(textModel, new WebInspector.TextRange(2, 0, 2, 0), " bar();\n baz();\n foo();"); |
| dumpTextModel("Text after edit:\n", textModel, range); |
| range = typeBackspace(textModel, range); |
| dumpTextModel("Text after backspace:\n", textModel, range); |
| range = typeDelete(textModel, new WebInspector.TextRange(2, 0, 2, 0)); |
| dumpTextModel("Text after delete:\n", textModel, range); |
| range = textModel.undo(); |
| dumpTextModel("Text after first undo:\n", textModel, range); |
| range = textModel.undo(); |
| dumpTextModel("Text after second undo:\n", textModel, range); |
| range = textModel.undo(); |
| dumpTextModel("Text after third undo:\n", textModel, range); |
| range = textModel.redo(); |
| dumpTextModel("Text after first redo:\n", textModel, range); |
| range = textModel.redo(); |
| dumpTextModel("Text after second redo:\n", textModel, range); |
| range = textModel.redo(); |
| dumpTextModel("Text after third redo:\n", textModel, range); |
| next(); |
| }, |
| |
| function testPasteSeveralTimes(next) |
| { |
| var textModel = new WebInspector.TextEditorModel(); |
| var functionText = " bar();\n baz();\n foo();"; |
| textModel.setText("function foo()\n{\n\n}\n"); |
| dumpTextModel("Text before edit:\n", textModel); |
| var range = typeText(textModel, new WebInspector.TextRange(2, 0, 2, 0), " bar();\n baz();\n foo();"); |
| dumpTextModel("Text after edit:\n", textModel, range); |
| range = textModel.editRange(range, "42").collapseToEnd(); |
| dumpTextModel("Text after first paste:\n", textModel, range); |
| range = textModel.editRange(range, "42").collapseToEnd(); |
| dumpTextModel("Text after second paste:\n", textModel, range); |
| range = textModel.undo(); |
| dumpTextModel("Text after first undo:\n", textModel, range); |
| range = textModel.undo(); |
| dumpTextModel("Text after second undo:\n", textModel, range); |
| range = textModel.undo(); |
| dumpTextModel("Text after third undo:\n", textModel, range); |
| range = textModel.redo(); |
| dumpTextModel("Text after first redo:\n", textModel, range); |
| range = textModel.redo(); |
| dumpTextModel("Text after second redo:\n", textModel, range); |
| range = textModel.redo(); |
| dumpTextModel("Text after third redo:\n", textModel, range); |
| next(); |
| }, |
| |
| function testSelectionAfterUndoRedo(next) |
| { |
| var textModel = new WebInspector.TextEditorModel(); |
| var functionText = " bar();\n baz();\n foo();"; |
| textModel.setText("function foo()\n{\n\n}\n"); |
| dumpTextModel("Text before edit:\n", textModel); |
| range = typeBackspace(textModel, new WebInspector.TextRange(2, 0, 2, 0), 1); |
| dumpTextModel("Text after backspace:\n", textModel, range); |
| range = textModel.undo(); |
| dumpTextModel("Text after first undo:\n", textModel, range); |
| range = textModel.redo(); |
| dumpTextModel("Text after first redo:\n", textModel, range); |
| range = textModel.undo(); |
| dumpTextModel("Text after second undo:\n", textModel, range); |
| range = typeBackspace(textModel, new WebInspector.TextRange(1, 1, 2, 0), 1); |
| dumpTextModel("Text after deleting selection:\n", textModel, range); |
| range = textModel.undo(); |
| dumpTextModel("Text after first undo:\n", textModel, range); |
| range = textModel.redo(); |
| dumpTextModel("Text after first redo:\n", textModel, range); |
| range = textModel.undo(); |
| dumpTextModel("Text after second undo:\n", textModel, range); |
| next(); |
| } |
| ]); |
| } |
| </script> |
| </head> |
| <body onload="runTest()"> |
| <p>Tests undo/redo operations in the editor model.</p> |
| </body> |
| </html> |