Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDani Megert2004-09-27 15:08:07 +0000
committerDani Megert2004-09-27 15:08:07 +0000
commit2aba5edae21bf9d0c1fd9f4db2c78097a0cecbfa (patch)
treed773e118c1ee10d6f18dee345a03aed123280287 /org.eclipse.ui.editors.tests/src
parentbde285c038fd11337744771a7960bdf228cf9cd0 (diff)
downloadeclipse.platform.text-2aba5edae21bf9d0c1fd9f4db2c78097a0cecbfa.tar.gz
eclipse.platform.text-2aba5edae21bf9d0c1fd9f4db2c78097a0cecbfa.tar.xz
eclipse.platform.text-2aba5edae21bf9d0c1fd9f4db2c78097a0cecbfa.zip
Added new tests
Diffstat (limited to 'org.eclipse.ui.editors.tests/src')
-rw-r--r--org.eclipse.ui.editors.tests/src/org/eclipse/ui/editors/tests/EncodingChangeTests.java176
-rw-r--r--org.eclipse.ui.editors.tests/src/org/eclipse/ui/editors/tests/GotoLineTest.java122
2 files changed, 298 insertions, 0 deletions
diff --git a/org.eclipse.ui.editors.tests/src/org/eclipse/ui/editors/tests/EncodingChangeTests.java b/org.eclipse.ui.editors.tests/src/org/eclipse/ui/editors/tests/EncodingChangeTests.java
new file mode 100644
index 000000000..f9e3535a0
--- /dev/null
+++ b/org.eclipse.ui.editors.tests/src/org/eclipse/ui/editors/tests/EncodingChangeTests.java
@@ -0,0 +1,176 @@
+/*******************************************************************************
+ * Copyright (c) 2000, 2004 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Common Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/cpl-v10.html
+ *
+ * Contributors:
+ * IBM Corporation - initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.ui.editors.tests;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.eclipse.swt.custom.ScrolledComposite;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Label;
+
+import org.eclipse.core.runtime.CoreException;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IFolder;
+
+import org.eclipse.core.filebuffers.FileBuffers;
+import org.eclipse.core.filebuffers.ITextFileBuffer;
+import org.eclipse.core.filebuffers.tests.ResourceHelper;
+
+import org.eclipse.text.tests.Accessor;
+
+import org.eclipse.ui.editors.text.DefaultEncodingSupport;
+import org.eclipse.ui.editors.text.IEncodingSupport;
+import org.eclipse.ui.editors.text.TextEditor;
+
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.IWorkbench;
+import org.eclipse.ui.IWorkbenchPage;
+import org.eclipse.ui.PartInitException;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.ide.IDE;
+import org.eclipse.ui.texteditor.StatusTextEditor;
+
+
+/**
+ * Tests changing of encoding via IFile and via encoding support.
+ *
+ * @since 3.1
+ */
+public class EncodingChangeTests extends TestCase {
+
+ private static final String ORIGINAL_CONTENT= "line1\nline2\nline3";
+
+ public static Test suite() {
+ return new TestSuite(EncodingChangeTests.class);
+ }
+
+ private IFile fFile;
+ private int fCount;
+
+ private String getOriginalContent() {
+ return ORIGINAL_CONTENT;
+ }
+
+ /*
+ * @see junit.framework.TestCase#setUp()
+ */
+ protected void setUp() throws Exception {
+ IFolder folder= ResourceHelper.createFolder("EncodingChangeTestProject/EncodingChangeTests/");
+ fFile= ResourceHelper.createFile(folder, "file" + fCount + ".txt", getOriginalContent());
+ fFile.setCharset(null, null);
+ fCount++;
+ }
+
+ /*
+ * @see junit.framework.TestCase#tearDown()
+ */
+ protected void tearDown() throws Exception {
+ while (PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell().getDisplay().readAndDispatch()) {
+ }
+ try {
+ ResourceHelper.deleteProject("EncodingChangeTestProject");
+ } catch (CoreException ex) {
+ // ignore
+ }
+ fFile= null;
+ while (PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell().getDisplay().readAndDispatch()) {
+ }
+ }
+
+ public void testChangeEncodingViaFile() {
+ IWorkbench workbench= PlatformUI.getWorkbench();
+ IWorkbenchPage page= workbench.getActiveWorkbenchWindow().getActivePage();
+ try {
+ fFile.setCharset("US-ASCII", null);
+ } catch (CoreException ex) {
+ fail();
+ }
+ try {
+ IEditorPart part= IDE.openEditor(page, fFile);
+ if (part instanceof TextEditor) {
+ TextEditor editor= (TextEditor)part;
+ Accessor accessor= new Accessor(editor, StatusTextEditor.class);
+ while (editor.getSite().getShell().getDisplay().readAndDispatch()) {
+ }
+ ScrolledComposite composite= (ScrolledComposite)accessor.get("fStatusControl");
+ assertNull(composite);
+ DefaultEncodingSupport encodingSupport= (DefaultEncodingSupport)editor.getAdapter(IEncodingSupport.class);
+ assertEquals("US-ASCII", encodingSupport.getEncoding());
+ } else
+ fail();
+ } catch (PartInitException e) {
+ fail();
+ }
+ }
+
+ public void testChangeEncodingViaEncodingSupport() {
+ IWorkbench workbench= PlatformUI.getWorkbench();
+ IWorkbenchPage page= workbench.getActiveWorkbenchWindow().getActivePage();
+ try {
+ IEditorPart part= IDE.openEditor(page, fFile);
+ if (part instanceof TextEditor) {
+ TextEditor editor= (TextEditor)part;
+ while (editor.getSite().getShell().getDisplay().readAndDispatch()) {
+ }
+ DefaultEncodingSupport encodingSupport= (DefaultEncodingSupport)editor.getAdapter(IEncodingSupport.class);
+ encodingSupport.setEncoding("US-ASCII");
+ Accessor accessor= new Accessor(editor, StatusTextEditor.class);
+ while (editor.getSite().getShell().getDisplay().readAndDispatch()) {
+ }
+ ScrolledComposite composite= (ScrolledComposite)accessor.get("fStatusControl");
+ assertNull(composite);
+ String actual= null;
+ try {
+ actual= fFile.getCharset(false);
+ } catch (CoreException e1) {
+ fail();
+ }
+ assertEquals("US-ASCII", actual);
+ } else
+ fail();
+ } catch (PartInitException e) {
+ fail();
+ }
+ }
+
+ public void testAInvalidEncoding() {
+ IWorkbench workbench= PlatformUI.getWorkbench();
+ IWorkbenchPage page= workbench.getActiveWorkbenchWindow().getActivePage();
+ try {
+ fFile.setCharset("nonexistent", null);
+ } catch (CoreException e2) {
+ fail();
+ }
+ try {
+ IEditorPart part= IDE.openEditor(page, fFile);
+ if (part instanceof TextEditor) {
+ TextEditor editor= (TextEditor)part;
+ Accessor accessor= new Accessor(editor, StatusTextEditor.class);
+ while (editor.getSite().getShell().getDisplay().readAndDispatch()) {
+ }
+ ITextFileBuffer fileBuffer= FileBuffers.getTextFileBufferManager().getTextFileBuffer(fFile.getFullPath());
+ DefaultEncodingSupport encodingSupport= (DefaultEncodingSupport)editor.getAdapter(IEncodingSupport.class);
+ String expected= encodingSupport.getStatusMessage(fileBuffer.getStatus());
+ ScrolledComposite composite= (ScrolledComposite)accessor.get("fStatusControl");
+ Label statusText= (Label)((Composite)composite.getContent()).getChildren()[5];
+ String actual= statusText.getText();
+ assertEquals(expected, actual);
+ } else
+ fail();
+ } catch (PartInitException e) {
+ fail();
+ }
+ }
+}
diff --git a/org.eclipse.ui.editors.tests/src/org/eclipse/ui/editors/tests/GotoLineTest.java b/org.eclipse.ui.editors.tests/src/org/eclipse/ui/editors/tests/GotoLineTest.java
new file mode 100644
index 000000000..07cdc7f9b
--- /dev/null
+++ b/org.eclipse.ui.editors.tests/src/org/eclipse/ui/editors/tests/GotoLineTest.java
@@ -0,0 +1,122 @@
+/*******************************************************************************
+ * Copyright (c) 2000, 2004 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Common Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/cpl-v10.html
+ *
+ * Contributors:
+ * IBM Corporation - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.ui.editors.tests;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.eclipse.swt.custom.StyledText;
+import org.eclipse.swt.widgets.Control;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IFolder;
+
+import org.eclipse.core.filebuffers.tests.ResourceHelper;
+
+import org.eclipse.text.tests.Accessor;
+
+import org.eclipse.jface.action.IAction;
+
+import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.text.IDocument;
+
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.IWorkbench;
+import org.eclipse.ui.IWorkbenchPage;
+import org.eclipse.ui.PartInitException;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.ide.IDE;
+import org.eclipse.ui.texteditor.GotoLineAction;
+import org.eclipse.ui.texteditor.ITextEditor;
+import org.eclipse.ui.texteditor.ITextEditorActionConstants;
+
+/**
+ * Tests the GotoLineAction.
+ *
+ * @since 3.1
+ */
+public class GotoLineTest extends TestCase {
+
+ private static final String ORIGINAL_CONTENT= "line1\nline2\nline3";
+
+ public static Test suite() {
+ return new TestSuite(GotoLineTest.class);
+ }
+
+ private IFile fFile;
+
+ private String getOriginalContent() {
+ return ORIGINAL_CONTENT;
+ }
+
+ /*
+ * @see junit.framework.TestCase#setUp()
+ */
+ protected void setUp() throws Exception {
+ IFolder folder= ResourceHelper.createFolder("GoToLineTestProject/goToLineTests/");
+ fFile= ResourceHelper.createFile(folder, "file.txt", getOriginalContent());
+ }
+
+ /*
+ * @see junit.framework.TestCase#tearDown()
+ */
+ protected void tearDown() throws Exception {
+ ResourceHelper.deleteProject("GoToLineTestProject");
+ fFile= null;
+ }
+
+ public void testGoToFirstLine() {
+ goToLine(0, 0);
+ }
+
+ public void testGoToLastLine() {
+ goToLine(2, 2);
+ }
+
+ public void testGoToInvalidLine() {
+ goToLine(1, 1);
+ goToLine(-1, 1);
+ goToLine(4, 1);
+ }
+
+ private void goToLine(int line, int expectedResult) {
+ IWorkbench workbench= PlatformUI.getWorkbench();
+ IWorkbenchPage page= workbench.getActiveWorkbenchWindow().getActivePage();
+ try {
+ IEditorPart part= IDE.openEditor(page, fFile);
+
+ if (part instanceof ITextEditor) {
+ ITextEditor editor= (ITextEditor) part;
+ IAction action= editor.getAction(ITextEditorActionConstants.GOTO_LINE);
+ Accessor accessor= new Accessor(action, GotoLineAction.class);
+ accessor.invoke("gotoLine", new Class[] {int.class}, new Integer[] {new Integer(line)});
+ Control control= (Control) part.getAdapter(Control.class);
+ if (control instanceof StyledText) {
+ int caretLine= -1;
+ StyledText styledText= (StyledText) control;
+ int caret= styledText.getCaretOffset();
+ try {
+ IDocument document= editor.getDocumentProvider().getDocument(editor.getEditorInput());
+ caretLine= document.getLineOfOffset(caret);
+ } catch (BadLocationException e1) {
+ fail();
+ }
+ assertEquals(expectedResult, caretLine);
+ } else
+ fail();
+ } else
+ fail();
+ } catch (PartInitException e) {
+ fail();
+ }
+ }
+}

Back to the top