1
/*******************************************************************************
2
* Copyright (c) 2010 Google, Inc and others.
3
* All rights reserved. This program and the accompanying materials
4
* are made available under the terms of the Eclipse Public License v1.0
5
* which accompanies this distribution, and is available at
6
* http://www.eclipse.org/legal/epl-v10.html
9
* Sergey Prigogin (Google) - initial API and implementation
10
*******************************************************************************/
11
package org.eclipse.cdt.ui.tests.text;
13
import java.util.ListResourceBundle;
15
import junit.extensions.TestSetup;
16
import junit.framework.Test;
17
import junit.framework.TestSuite;
19
import org.eclipse.core.resources.IFile;
20
import org.eclipse.core.resources.ResourcesPlugin;
21
import org.eclipse.core.runtime.NullProgressMonitor;
22
import org.eclipse.core.runtime.Path;
23
import org.eclipse.jface.text.BadLocationException;
24
import org.eclipse.jface.text.IDocument;
25
import org.eclipse.jface.text.source.SourceViewer;
27
import org.eclipse.cdt.core.formatter.DefaultCodeFormatterConstants;
28
import org.eclipse.cdt.core.model.ICProject;
29
import org.eclipse.cdt.core.testplugin.CProjectHelper;
30
import org.eclipse.cdt.ui.testplugin.EditorTestHelper;
31
import org.eclipse.cdt.ui.testplugin.ResourceTestHelper;
32
import org.eclipse.cdt.ui.tests.BaseUITestCase;
33
import org.eclipse.cdt.internal.ui.editor.CEditor;
34
import org.eclipse.cdt.internal.ui.editor.SortLinesAction;
37
* Tests for the SortLinesAction.
41
public class SortLinesTest extends BaseUITestCase {
42
private static final String PROJECT = "SortLinesTest";
43
private static final String FILE = "test.cpp";
45
private static final class EmptyBundle extends ListResourceBundle {
47
protected Object[][] getContents() {
48
return new Object[0][];
52
protected static class SortLinesTestSetup extends TestSetup {
53
private ICProject fCProject;
55
public SortLinesTestSetup(Test test) {
60
protected void setUp() throws Exception {
63
fCProject= CProjectHelper.createCProject(PROJECT, null);
64
fCProject.setOption(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, DefaultCodeFormatterConstants.MIXED);
65
fCProject.setOption(DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE, String.valueOf(8));
66
fCProject.setOption(DefaultCodeFormatterConstants.FORMATTER_INDENTATION_SIZE, String.valueOf(4));
67
IFile file= EditorTestHelper.createFile(fCProject.getProject(), FILE, "", new NullProgressMonitor());
71
protected void tearDown () throws Exception {
72
EditorTestHelper.closeAllEditors();
73
if (fCProject != null) {
74
CProjectHelper.delete(fCProject);
80
private static final Class<?> THIS= SortLinesTest.class;
81
public static Test suite() {
82
return new SortLinesTestSetup(new TestSuite(THIS));
85
private CEditor fEditor;
86
private SourceViewer fSourceViewer;
87
private IDocument fDocument;
88
private SortLinesTestSetup fProjectSetup;
91
* @see junit.framework.TestCase#setUp()
94
protected void setUp() throws Exception {
95
if (!ResourcesPlugin.getWorkspace().getRoot().exists(new Path(PROJECT))) {
96
fProjectSetup= new SortLinesTestSetup(this);
97
fProjectSetup.setUp();
99
fEditor= (CEditor) EditorTestHelper.openInEditor(ResourceTestHelper.findFile(PROJECT + '/' + FILE), true);
100
fSourceViewer= EditorTestHelper.getSourceViewer(fEditor);
101
fDocument= fSourceViewer.getDocument();
106
* @see junit.framework.TestCase#tearDown()
109
protected void tearDown() throws Exception {
110
if (fProjectSetup != null) {
111
fProjectSetup.tearDown();
116
private void sortLines() throws Exception {
117
new SortLinesAction(fEditor).run();
121
* Selects part of the document.
123
* @param startLine First line of the selection. Zero based.
124
* @param startPosition Start position of the selection in startLine. Zero based.
125
* @param endLine Last line of the selection. Zero based.
126
* @param endPosition Position after the end of the selection in endLine. Zero based.
128
private void select(int startLine, int startPosition, int endLine, int endPosition)
129
throws BadLocationException {
130
int offset = fDocument.getLineOffset(startLine) + startPosition;
131
fSourceViewer.setSelectedRange(offset, fDocument.getLineOffset(endLine) + endPosition - offset);
135
* Selects the whole document.
137
private void selectAll() {
138
fSourceViewer.setSelectedRange(0, fDocument.getLength());
148
// #include "ccccc.h"
156
// #include "ccccc.h"
160
public void testSortLinesMixed() throws Exception {
161
StringBuffer[] contents= getContentsForTest(2);
162
String before= contents[0].toString();
163
String after= contents[1].toString();
164
fDocument.set(before);
167
assertEquals(after, fDocument.get());
181
public void testSortLinesCommentsOnly() throws Exception {
182
StringBuffer[] contents= getContentsForTest(2);
183
String before= contents[0].toString();
184
String after= contents[1].toString();
185
fDocument.set(before);
188
assertEquals(after, fDocument.get());