001: package org.drools.eclipse.editors.rete;
002:
003: import java.io.ByteArrayInputStream;
004: import java.io.InputStream;
005:
006: import junit.framework.TestCase;
007:
008: import org.drools.eclipse.editors.DRLRuleEditor2;
009: import org.drools.eclipse.editors.ZoomInAction2;
010: import org.drools.eclipse.editors.ZoomOutAction2;
011: import org.eclipse.core.filebuffers.manipulation.ContainerCreator;
012: import org.eclipse.core.resources.IContainer;
013: import org.eclipse.core.resources.IFile;
014: import org.eclipse.core.resources.IFolder;
015: import org.eclipse.core.resources.IProject;
016: import org.eclipse.core.resources.IWorkspaceRoot;
017: import org.eclipse.core.resources.ResourcesPlugin;
018: import org.eclipse.core.runtime.CoreException;
019: import org.eclipse.core.runtime.IProgressMonitor;
020: import org.eclipse.core.runtime.NullProgressMonitor;
021: import org.eclipse.core.runtime.Path;
022: import org.eclipse.gef.ui.actions.ZoomComboContributionItem;
023: import org.eclipse.ui.IWorkbench;
024: import org.eclipse.ui.IWorkbenchPage;
025: import org.eclipse.ui.PartInitException;
026: import org.eclipse.ui.PlatformUI;
027: import org.eclipse.ui.ide.IDE;
028:
029: public class ZoomControlTest extends TestCase {
030:
031: private IFile fFile1;
032: private IFile fFile2;
033:
034: private final static IProgressMonitor NULL_MONITOR = new NullProgressMonitor();
035:
036: private static final String ORIGINAL_CONTENT = "package test\nrule \"a\"\nend\nrule \"b\"\nend";
037:
038: public ZoomControlTest(String name) {
039: super (name);
040: }
041:
042: private String getOriginalContent() {
043: return ORIGINAL_CONTENT;
044: }
045:
046: /*
047: * @see junit.framework.TestCase#setUp()
048: */
049: protected void setUp() throws Exception {
050: IFolder folder = createFolder("ZoomControlTestProject/multipleEditorTest/");
051: fFile1 = createFile(folder, "myfile1.drl", getOriginalContent());
052: fFile2 = createFile(folder, "myfile2.drl", getOriginalContent());
053: }
054:
055: /*
056: * @see junit.framework.TestCase#tearDown()
057: */
058: protected void tearDown() throws Exception {
059: deleteProject("ZoomControlTestProject");
060: fFile1 = null;
061: fFile2 = null;
062: }
063:
064: public void testMultipleEditors() throws PartInitException {
065:
066: IWorkbench workbench = PlatformUI.getWorkbench();
067: IWorkbenchPage page = workbench.getActiveWorkbenchWindow()
068: .getActivePage();
069:
070: DRLRuleEditor2 part1 = (DRLRuleEditor2) IDE.openEditor(page,
071: fFile1);
072: DRLRuleEditor2 part2 = (DRLRuleEditor2) IDE.openEditor(page,
073: fFile2);
074:
075: checkVisibility(part1, part2, false);
076:
077: // Editor1 active
078: page.activate(part1);
079: checkVisibility(part1, part2, false);
080: // TODO
081: // part1.setActivePage( 1 );
082: // checkVisibility( part1,
083: // part2,
084: // true );
085: //
086: // part1.setActivePage( 0 );
087: // checkVisibility( part1,
088: // part2,
089: // false );
090: //
091: // part1.setActivePage( 1 );
092: // checkVisibility( part1,
093: // part2,
094: // true );
095: //
096: // // Editor2 active
097: // page.activate( part2 );
098: // part2.setActivePage( 0 );
099: // checkVisibility( part1,
100: // part2,
101: // false );
102: //
103: // part2.setActivePage( 1 );
104: // checkVisibility( part1,
105: // part2,
106: // true );
107: //
108: // // Editor1 active
109: // page.activate( part1 );
110: // checkVisibility( part1,
111: // part2,
112: // true );
113: //
114: // // Editor2 active
115: // page.activate( part2 );
116: // checkVisibility( part1,
117: // part2,
118: // true );
119: //
120: // part2.setActivePage( 0 );
121: // checkVisibility( part1,
122: // part2,
123: // false );
124: //
125: // // Editor1 active
126: // page.activate( part1 );
127: // checkVisibility( part1,
128: // part2,
129: // true );
130: // part2.setActivePage( 0 );
131: // checkVisibility( part1,
132: // part2,
133: // false );
134:
135: }
136:
137: public void testSecondEditorAfterFirst() throws PartInitException {
138:
139: IWorkbench workbench = PlatformUI.getWorkbench();
140: IWorkbenchPage page = workbench.getActiveWorkbenchWindow()
141: .getActivePage();
142:
143: DRLRuleEditor2 part1 = (DRLRuleEditor2) IDE.openEditor(page,
144: fFile1);
145: // TODO
146: // // Editor1 active
147: // page.activate( part1 );
148: // part1.setActivePage( 1 );
149: // checkVisibility( part1,
150: // null,
151: // true );
152: //
153: // DRLRuleEditor2 part2 = (DRLRuleEditor2) IDE.openEditor( page,
154: // fFile2 );
155: // page.activate( part2 );
156: // checkVisibility( part1,
157: // part2,
158: // false );
159:
160: }
161:
162: private void checkVisibility(DRLRuleEditor2 part1,
163: DRLRuleEditor2 part2, boolean enabled) {
164: if (part1 != null) {
165: checkVisibility(part1, enabled);
166: }
167:
168: if (part2 != null) {
169: checkVisibility(part2, enabled);
170: }
171: }
172:
173: private void checkVisibility(DRLRuleEditor2 editor, boolean enabled) {
174: ZoomInAction2 zoomIn = (ZoomInAction2) editor
175: .getAdapter(ZoomInAction2.class);
176: ZoomOutAction2 zoomOut = (ZoomOutAction2) editor
177: .getAdapter(ZoomOutAction2.class);
178: ZoomComboContributionItem zitem = (ZoomComboContributionItem) editor
179: .getAdapter(ZoomComboContributionItem.class);
180:
181: assertEquals(enabled, zoomIn.isEnabled());
182: assertEquals(enabled, zoomOut.isEnabled());
183: assertEquals(enabled, zitem.getZoomManager() != null);
184:
185: }
186:
187: private IFile createFile(IFolder folder, String name,
188: String contents) throws CoreException {
189: IFile file = folder.getFile(name);
190: InputStream inputStream = new ByteArrayInputStream(contents
191: .getBytes());
192: file.create(inputStream, true, NULL_MONITOR);
193: return file;
194: }
195:
196: private IFolder createFolder(String portableFolderPath)
197: throws CoreException {
198: ContainerCreator creator = new ContainerCreator(ResourcesPlugin
199: .getWorkspace(), new Path(portableFolderPath));
200: IContainer container = creator.createContainer(NULL_MONITOR);
201: return (IFolder) container;
202: }
203:
204: private void deleteProject(String projectName) throws CoreException {
205: IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
206: IProject project = root.getProject(projectName);
207: if (project.exists())
208: project.delete(true, true, NULL_MONITOR);
209: }
210:
211: }
|