1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
/*******************************************************************************
* Copyright (c) 2016 Red Hat Inc. and others
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Sopot Cela (Red Hat Inc.)
*******************************************************************************/
package org.eclipse.ui.genericeditor.tests;
import java.io.ByteArrayInputStream;
import org.junit.After;
import org.junit.Before;
import org.eclipse.swt.widgets.Display;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.text.tests.Accessor;
import org.eclipse.jface.text.source.SourceViewer;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.internal.genericeditor.ExtensionBasedTextEditor;
import org.eclipse.ui.intro.IIntroPart;
import org.eclipse.ui.part.FileEditorInput;
import org.eclipse.ui.texteditor.AbstractTextEditor;
/**
* Closes intro, create {@link #project}, create {@link #file} and open {@link #editor}; and clean up.
* Also contains additional utility methods
* @since 1.0
*/
public class AbstratGenericEditorTest {
protected IProject project;
protected IFile file;
protected ExtensionBasedTextEditor editor;
/**
* Closes intro, create {@link #project}, create {@link #file} and open {@link #editor}
* @throws Exception ex
*/
@Before
public void setUp() throws Exception {
closeIntro();
project = ResourcesPlugin.getWorkspace().getRoot().getProject(getClass().getName() + System.currentTimeMillis());
project.create(null);
project.open(null);
project.setDefaultCharset("UTF-8", null);
createAndOpenFile();
}
protected void createAndOpenFile() throws Exception {
createAndOpenFile("foo.txt", "bar 'bar'");
}
/**
* Creates a new file in the project, opens it, and associate that file with the test state
* @param name name of the file in the project
* @param contents content of the file
* @throws Exception ex
* @since 1.1
*/
protected void createAndOpenFile(String name, String contents) throws Exception {
this.file = project.getFile(name);
this.file.create(new ByteArrayInputStream(contents.getBytes("UTF-8")), true, null);
this.file.setCharset("UTF-8", null);
this.editor = (ExtensionBasedTextEditor) PlatformUI.getWorkbench().getActiveWorkbenchWindow()
.getActivePage().openEditor(new FileEditorInput(this.file), "org.eclipse.ui.genericeditor.GenericEditor");
}
/**
* Closes editor and delete file. Keeps project open.
* @throws Exception ex
* @since 1.1
*/
protected void cleanFileAndEditor() throws Exception {
if (editor != null) {
editor.close(false);
editor = null;
}
while(Display.getDefault().readAndDispatch()) {}
if (file != null) {
file.delete(true, new NullProgressMonitor());
file = null;
}
}
protected SourceViewer getSourceViewer() {
SourceViewer sourceViewer= (SourceViewer) new Accessor(editor, AbstractTextEditor.class).invoke("getSourceViewer", new Object[0]);
return sourceViewer;
}
@After
public void tearDown() throws Exception {
cleanFileAndEditor();
if (project != null) {
project.delete(true, null);
}
}
private static void closeIntro() {
IIntroPart intro = PlatformUI.getWorkbench().getIntroManager().getIntro();
if (intro != null) {
PlatformUI.getWorkbench().getIntroManager().closeIntro(intro);
}
}
public static void waitAndDispatch(long milliseconds) {
long timeout = milliseconds; //ms
long start = System.currentTimeMillis();
while (start + timeout > System.currentTimeMillis()) {
Display.getDefault().readAndDispatch();
}
}
}
|