tests moved to community

This commit is contained in:
Alexey Kudravtsev
2011-05-20 14:16:08 +04:00
parent c77a8f6053
commit 24dd47fc5e
6 changed files with 1534 additions and 0 deletions
@@ -0,0 +1,59 @@
package com.intellij.openapi.editor;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.event.DocumentAdapter;
import com.intellij.openapi.editor.event.DocumentEvent;
import com.intellij.openapi.editor.impl.DocumentImpl;
import com.intellij.testFramework.LightPlatformTestCase;
public class DocumentTest extends LightPlatformTestCase {
public void testCorrectlyAddingAndRemovingListeners() throws Exception {
new WriteCommandAction.Simple(getProject()) {
@Override
protected void run() throws Throwable {
final Document doc = new DocumentImpl("");
final StringBuilder b = new StringBuilder();
doc.addDocumentListener(new DocumentAdapter() {
@Override
public void beforeDocumentChange(DocumentEvent e) {
b.append("before1 ");
}
@Override
public void documentChanged(DocumentEvent e) {
b.append("after1 ");
}
});
doc.addDocumentListener(new DocumentAdapter() {
@Override
public void beforeDocumentChange(DocumentEvent event) {
doc.removeDocumentListener(this);
}
});
doc.addDocumentListener(new DocumentAdapter() {
@Override
public void documentChanged(DocumentEvent e) {
doc.removeDocumentListener(this);
}
});
doc.addDocumentListener(new DocumentAdapter() {
@Override
public void beforeDocumentChange(DocumentEvent e) {
b.append("before2 ");
}
@Override
public void documentChanged(DocumentEvent e) {
b.append("after2 ");
}
});
doc.setText("foo");
assertEquals("before2 before1 after1 after2 ", b.toString());
}
}.execute().throwException();
}
}
@@ -0,0 +1,104 @@
package com.intellij.openapi.editor;
import com.intellij.openapi.editor.impl.DocumentImpl;
import com.intellij.testFramework.LightPlatformTestCase;
import org.jetbrains.annotations.NonNls;
/**
* @author max
*/
public class FoldingTest extends LightPlatformTestCase {
public void testStressFoldingFromZeroOffset() throws Exception {
for (int len = 2; len < 25; len++) {
stress(len);
}
}
public void testStress8() throws Exception {
DocumentImpl doc = new DocumentImpl("0123456789\n123456789\n23456789");
Editor editor = EditorFactory.getInstance().createEditor(doc);
try {
final FoldingModel model = editor.getFoldingModel();
model.runBatchFoldingOperation(new Runnable() {
@Override
public void run() {
model.addFoldRegion(0, 8, "/*...*/").setExpanded(false);
model.addFoldRegion(10, 12, "/*...*/").setExpanded(false);
}
});
assertEquals(10, editor.logicalPositionToOffset(new LogicalPosition(0, 10)));
for (int line = 0; line <= 3; line++) {
for (int column = 0; column <= 100; column++) {
LogicalPosition log = new LogicalPosition(line, column);
editor.logicalToVisualPosition(log);
}
}
}
finally {
EditorFactory.getInstance().releaseEditor(editor);
}
}
private static void stress(final int len) {
DocumentImpl doc = new DocumentImpl("0123456789\n123456789\n23456789");
Editor editor = EditorFactory.getInstance().createEditor(doc);
try {
final FoldingModel model = editor.getFoldingModel();
model.runBatchFoldingOperation(new Runnable() {
@Override
public void run() {
model.addFoldRegion(0, len, "/*...*/").setExpanded(false);
model.addFoldRegion(len + 2, len + 4, "/*...*/").setExpanded(false);
}
});
for (int line = 0; line <= 3; line++) {
for (int column = 0; column <= 100; column++) {
LogicalPosition log = new LogicalPosition(line, column);
editor.logicalToVisualPosition(log);
}
}
}
finally {
EditorFactory.getInstance().releaseEditor(editor);
}
}
public void testIntersects () throws Exception {
@NonNls DocumentImpl doc = new DocumentImpl("I don't know what you mean by `glory,'\" Alice said" +
"Humpty Dumpty smiled contemptuously. \"Of course you don't -- till I tell you. I meant `there's a nice knock-down argument for you!'" +
"But glory doesn't mean `a nice knock-down argument,'\" Alice objected." +
"When I use a word,\" Humpty Dumpty said, in a rather scornful tone, \"it means just what I choose it to mean -- neither more nor less." +
"The question is,\" said Alice, \"whether you can make words mean so many different things." +
"The question is,\" said Humpty Dumpty, \"which is to be master -- that's all." );
Editor editor = EditorFactory.getInstance().createEditor(doc);
try {
final FoldingModel model = editor.getFoldingModel();
model.runBatchFoldingOperation(new Runnable() {
@Override
public void run() {
FoldRegion region = model.addFoldRegion(5, 10, ".");
assertNotNull(region);
region = model.addFoldRegion(7, 11, ".");
assertNull(region);
region = model.addFoldRegion(20, 30, ".");
assertNotNull(region);
region = model.addFoldRegion(9, 12, ".");
assertNull(region);
region = model.addFoldRegion(7, 10, ".");
assertNotNull(region);
region = model.addFoldRegion(7, 10, ".");
assertNull(region);
region = model.addFoldRegion(5, 30, ".");
assertNotNull(region);
}
});
}
finally {
EditorFactory.getInstance().releaseEditor(editor);
}
}
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,97 @@
package com.intellij.openapi.editor;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.ex.DocumentEx;
import com.intellij.openapi.editor.ex.EditorSettingsExternalizable;
import com.intellij.openapi.editor.impl.DocumentImpl;
import com.intellij.testFramework.LightPlatformCodeInsightTestCase;
import org.jdom.Element;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.io.IOException;
/**
* User: cdr
*/
public class StripTrailingSpacesTest extends LightPlatformCodeInsightTestCase {
private final Element oldSettings = new Element("temp");
@Override
protected void setUp() throws Exception {
super.setUp();
EditorSettingsExternalizable settings = EditorSettingsExternalizable.getInstance();
settings.writeExternal(oldSettings);
settings.setStripTrailingSpaces(EditorSettingsExternalizable.STRIP_TRAILING_SPACES_CHANGED);
settings.setVirtualSpace(false);
}
@Override
protected void tearDown() throws Exception {
EditorSettingsExternalizable settings = EditorSettingsExternalizable.getInstance();
settings.readExternal(oldSettings);
super.tearDown();
}
public void testDoNotStripModifiedOnCurrentLine() throws IOException {
doTest("xxx\n <caret>\nyyy",
"xxx\n <caret>\nyyy");
}
public void testDoStripModifiedOnCurrentLineIfCaretWouldNotJump() throws IOException {
doTest("xxx\n 222<caret> \nyyy",
"xxx\n 222<caret>\nyyy");
}
public void testModifyAndAltTabAway() throws IOException {
configureFromFileText("x.txt", "xxx<caret>\nyyy");
type(' ');
FocusEvent event = new FocusEvent(getEditor().getContentComponent(), 1005);
FocusListener[] listeners = getEditor().getContentComponent().getListeners(FocusListener.class);
for (FocusListener listener : listeners) {
listener.focusLost(event);
}
stripTrailingSpaces();
checkResultByText("xxx <caret>\nyyy");
}
public void testOnlyModifiedLinesGetStripped() throws IOException {
String text = "xxx<caret> \nyyy ";
configureFromFileText("x.txt", text);
((DocumentEx)myEditor.getDocument()).clearLineModificationFlags();
stripTrailingSpaces();
checkResultByText(text);
type('z');
stripTrailingSpaces();
checkResultByText("xxxz<caret>\nyyy ");
}
public void testOnlyModifiedLinesWhenDoesNotAllowCaretAfterEndOfLine() throws IOException {
configureFromFileText("x.txt", "xxx<caret> \nZ ");
type(' ');
myEditor.getCaretModel().moveToOffset(myEditor.getDocument().getText().indexOf("Z")+1);
type('Z');
stripTrailingSpaces();
checkResultByText("xxx\nZZ<caret>");
}
private void doTest(String before, String after) throws IOException {
configureFromFileText("x.txt", before);
type(' ');
backspace();
stripTrailingSpaces();
checkResultByText(after);
}
private static void stripTrailingSpaces() {
ApplicationManager.getApplication().runWriteAction(new Runnable() {
@Override
public void run() {
((DocumentImpl)getEditor().getDocument()).stripTrailingSpaces(true);
}
});
}
}
@@ -0,0 +1,75 @@
package com.intellij.openapi.editor.impl;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.RangeMarker;
import com.intellij.testFramework.LightPlatformCodeInsightTestCase;
/**
* @author cdr
*/
public class GuardBlockTest extends LightPlatformCodeInsightTestCase {
private static RangeMarker createGuard(final int start, final int end) {
final Document document = getEditor().getDocument();
return document.createGuardedBlock(start, end);
}
public void testZero() throws Exception {
configureFromFileText("x.txt", "xxxx");
RangeMarker guard = createGuard(0, 0);
guard.setGreedyToLeft(true);
guard.setGreedyToRight(true);
checkUnableToTypeIn(0);
checkCanTypeIn(1);
}
public void testInTheMiddle() throws Exception {
configureFromFileText("x.txt", "xxxxxxxx");
createGuard(1, 4);
checkUnableToTypeIn(2);
checkCanTypeIn(0);
}
public void testGreedy() throws Exception {
configureFromFileText("x.txt", "012345678");
{
RangeMarker guard = createGuard(0, 5);
guard.setGreedyToLeft(true);
guard.setGreedyToRight(false);
}
{
RangeMarker guard = createGuard(5, 6);
guard.setGreedyToLeft(false);
guard.setGreedyToRight(true);
}
checkCanTypeIn(5);
checkUnableToTypeIn(0);
}
public void testGreedyEnd() throws Exception {
configureFromFileText("x.txt", "012345678");
{
RangeMarker guard = createGuard(0, 5);
guard.setGreedyToLeft(true);
guard.setGreedyToRight(true);
}
checkUnableToTypeIn(5);
}
private static void checkUnableToTypeIn(int offset) {
String text = getEditor().getDocument().getText();
try {
getEditor().getCaretModel().moveToOffset(offset);
type("y");
}
catch (RuntimeException e) {
assertEquals("Unable to perform an action since it changes read-only fragments of the current document",e.getMessage());
assertEquals(text, getEditor().getDocument().getText());
return;
}
fail("must be read only at "+offset);
}
private static void checkCanTypeIn(int offset) {
getEditor().getCaretModel().moveToOffset(offset);
type("yy");
}
}
@@ -0,0 +1,158 @@
package com.intellij.openapi.editor.impl;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.highlighter.HighlighterIterator;
import com.intellij.openapi.editor.markup.TextAttributes;
import com.intellij.openapi.util.Condition;
import com.intellij.openapi.util.Conditions;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.tree.IElementType;
import gnu.trove.Equality;
import junit.framework.TestCase;
public class RangeIteratorTest extends TestCase {
private static final Equality ALL_EQUAL = new DummyEquality(true);
private static final Equality ALL_DIFFERENT = new DummyEquality(false);
private static final RangeIterator.Gaps NO_GAPS = new RangeIterator.Gaps() {
@Override
public boolean isGapAt(int offset) {
return false;
}
};
private static final RangeIterator.Gaps MY_GAPS = new RangeIterator.Gaps() {
@Override
public boolean isGapAt(int offset) {
return 3 <= offset && offset < 6;
}
};
private static final Condition<TextAttributes> ANY = Conditions.alwaysTrue();
public void testExpanded() {
RangeIterator iterator = createIterator(ALL_EQUAL, NO_GAPS);
checkPosition(iterator, 2, 4);
iterator.advance();
checkPosition(iterator, 5, 9);
assertTrue(iterator.atEnd());
}
public void testCollapsed() {
RangeIterator iterator = createIterator(ALL_EQUAL, MY_GAPS);
checkPosition(iterator, 2, 3);
iterator.advance();
checkPosition(iterator, 6, 9);
assertTrue(iterator.atEnd());
}
public void testDifferentExpanded() {
RangeIterator iterator = createIterator(ALL_DIFFERENT, NO_GAPS);
checkPosition(iterator, 2, 4);
iterator.advance();
checkPosition(iterator, 5, 7);
assertFalse(iterator.atEnd());
iterator.advance();
checkPosition(iterator, 7, 9);
assertTrue(iterator.atEnd());
}
public void testEndInFolding() {
RangeIterator iterator = createIterator(ALL_EQUAL, new int[]{1}, new int[]{5}, MY_GAPS);
checkPosition(iterator, 1, 3);
assertTrue(iterator.atEnd());
}
public void testDividedByFolding() {
RangeIterator iterator = createIterator(ALL_EQUAL, new int[]{1}, new int[]{9}, MY_GAPS);
checkPosition(iterator, 1, 3);
iterator.advance();
checkPosition(iterator, 6, 9);
assertTrue(iterator.atEnd());
}
public void testAllFolded() {
RangeIterator iterator = new RangeIterator(MY_GAPS, ALL_EQUAL,
new MyHighlighterIterator(new int[]{4}, new int[]{5}), ANY);
iterator.init(new TextRange(0, Integer.MAX_VALUE));
assertTrue(iterator.atEnd());
}
private void checkPosition(RangeIterator iterator, int start, int end) {
assertEquals(start, iterator.getStart());
assertEquals(end, iterator.getEnd());
}
private RangeIterator createIterator(Equality equality, RangeIterator.Gaps gaps) {
return createIterator(equality, new int[]{2, 5, 7}, new int[]{4, 7, 9}, gaps);
}
private RangeIterator createIterator(Equality equality, int[] starts, int[] ends, RangeIterator.Gaps gaps) {
RangeIterator iterator = new RangeIterator(gaps, equality,
new MyHighlighterIterator(starts, ends), ANY);
iterator.init(new TextRange(0, Integer.MAX_VALUE));
iterator.advance();
return iterator;
}
private static class MyHighlighterIterator implements HighlighterIterator {
private final int[] myStarts;
private final int[] myEnds;
private int myIndex = 0;
public MyHighlighterIterator(int[] starts, int[] ends) {
myStarts = starts;
myEnds = ends;
}
@Override
public TextAttributes getTextAttributes() {
return null;
}
@Override
public IElementType getTokenType() {
throw new UnsupportedOperationException();
}
@Override
public void advance() {
myIndex++;
}
@Override
public int getEnd() {
return myEnds[myIndex];
}
@Override
public int getStart() {
return myStarts[myIndex];
}
@Override
public boolean atEnd() {
return myIndex == myStarts.length;
}
@Override
public Document getDocument() {
throw new UnsupportedOperationException();
}
@Override
public void retreat() {
throw new UnsupportedOperationException();
}
}
private static class DummyEquality implements Equality {
private final boolean myResult;
public DummyEquality(boolean result) {
myResult = result;
}
@Override
public boolean equals(Object o1, Object o2) {
return myResult;
}
}
}