1
/*******************************************************************************
2
* Copyright (c) 2005, 2007 IBM Corporation 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
* IBM Corporation - initial API and implementation
10
* Anton Leherbauer (Wind River Systems)
11
*******************************************************************************/
12
package org.eclipse.cdt.ui.tests.text;
14
import java.util.ListResourceBundle;
16
import junit.extensions.TestSetup;
17
import junit.framework.Test;
18
import junit.framework.TestCase;
19
import junit.framework.TestSuite;
21
import org.eclipse.jface.action.IAction;
22
import org.eclipse.jface.text.IDocument;
23
import org.eclipse.jface.text.source.SourceViewer;
25
import org.eclipse.cdt.core.CCorePlugin;
26
import org.eclipse.cdt.core.formatter.DefaultCodeFormatterConstants;
27
import org.eclipse.cdt.core.model.ICProject;
28
import org.eclipse.cdt.core.testplugin.CProjectHelper;
30
import org.eclipse.cdt.internal.ui.editor.CEditor;
35
public class FormatActionTest extends TestCase {
36
private static final String PROJECT= "FormatTests";
38
private static final class EmptyBundle extends ListResourceBundle {
39
protected Object[][] getContents() {
40
return new Object[0][];
44
protected static class FormatTestSetup extends TestSetup {
46
private ICProject fCProject;
48
public FormatTestSetup(Test test) {
52
protected void setUp() throws Exception {
55
fCProject= EditorTestHelper.createCProject(PROJECT, "resources/formatter");
56
fCProject.setOption(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.TAB);
59
protected void tearDown () throws Exception {
60
if (fCProject != null)
61
CProjectHelper.delete(fCProject);
67
private static final Class THIS= FormatActionTest.class;
68
public static Test suite() {
69
return new FormatTestSetup(new TestSuite(THIS));
72
private CEditor fEditor;
73
private SourceViewer fSourceViewer;
74
private IDocument fDocument;
77
* @see junit.framework.TestCase#setUp()
79
protected void setUp() throws Exception {
80
String filename= createFileName("Before");
81
fEditor= (CEditor) EditorTestHelper.openInEditor(ResourceTestHelper.findFile(filename), true);
82
fSourceViewer= EditorTestHelper.getSourceViewer(fEditor);
83
fDocument= fSourceViewer.getDocument();
87
* @see junit.framework.TestCase#tearDown()
89
protected void tearDown() throws Exception {
90
EditorTestHelper.closeEditor(fEditor);
93
private void assertFormatResult() throws Exception {
94
String afterFile= createFileName("After");
95
String expected= ResourceTestHelper.read(afterFile).toString();
97
IAction formatAction= fEditor.getAction("Format");
98
assertNotNull("No format action", formatAction);
101
assertEquals(expected, fDocument.get());
104
private String createFileName(String qualifier) {
105
String name= getName();
106
name= name.substring(4, 5).toLowerCase() + name.substring(5);
107
return "/" + PROJECT + "/src/" + name + "/" + qualifier + ".cpp";
110
private void selectAll() {
111
fSourceViewer.setSelectedRange(0, fDocument.getLength());
114
public void testTemplates() throws Exception {
116
assertFormatResult();
119
public void testPreview() throws Exception {
121
assertFormatResult();
124
public void testSample() throws Exception {
126
assertFormatResult();
129
public void testComplex() throws Exception {
131
assertFormatResult();
134
public void testBugs() throws Exception {
136
assertFormatResult();