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