Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fb744fa7bffcf5728f7c8d6689c859bc439f0749 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2017 Vasili Gulevich and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *      Vasili Gulevich <vasili.gulevich@xored.com> - initial implementation
 *      Andrey Loskutov <loskutov@gmx.de> - polishing and integration
 *******************************************************************************/
package org.eclipse.ui.editors.tests;

import java.lang.reflect.Method;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.widgets.Display;

import org.eclipse.core.filesystem.EFS;

import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.ILogListener;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;

import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.WorkbenchException;
import org.eclipse.ui.ide.FileStoreEditorInput;
import org.eclipse.ui.internal.PartSite;

import org.eclipse.ui.texteditor.AbstractTextEditor;
import org.eclipse.ui.texteditor.IDocumentProvider;

import org.eclipse.ui.editors.text.EditorsUI;
import org.eclipse.ui.editors.text.ForwardingDocumentProvider;
import org.eclipse.ui.editors.text.TextEditor;

public class StatusEditorTest {
	private IWorkbenchWindow window;
	private Display display;
	private IWorkbenchPage page;

	@Before
	public void before() throws WorkbenchException {
		window = PlatformUI.getWorkbench().openWorkbenchWindow(null);
		display = window.getShell().getDisplay();
		page = window.getActivePage();
		processEvents();
	}

	@After
	public void after() {
		window.close();
		page = null;
		processEvents();
	}

	/*
	 * https://bugs.eclipse.org/bugs/attachment.cgi?bugid=320672
	 *
	 * No exceptions are thrown when a status editor displaying an erroneous status is activated with a mouse click.
	 * @throws Exception
	 */
	@Test
	public void doNotThrowOnActivationInStale() throws Exception {
		IEditorPart editor1 = openNonExistentFile(page, new URI("file:/1.txt"));
		openNonExistentFile(page, new URI("file:/2.txt"));
		ILog log = Platform.getLog(Platform.getBundle("org.eclipse.e4.ui.workbench"));
		List<String> logEvents = new ArrayList<>();
		ILogListener listener = (status, plugin) -> logEvents.add(status.toString());
		log.addLogListener(listener);
		// Clicks are not equivalent to activation from API, so we need this
		// hack to imitate tab clicks.
		CTabFolder folder = (CTabFolder) (((PartSite) editor1.getSite()).getModel().getParent().getWidget());
		try {
			folder.setSelection(0);
			processEvents();
			folder.setSelection(1);
			processEvents();
		} finally {
			log.removeLogListener(listener);
		}
		if(!logEvents.isEmpty()) {
			Assert.assertEquals("Unexpected errors logged", "", logEvents.toString());
		}
	}

	private void processEvents() {
		while (display.readAndDispatch()) {
			//
		}
	}

	private IEditorPart openNonExistentFile(IWorkbenchPage page1, URI uri) throws Exception {
		FileStoreEditorInput input = new FileStoreEditorInput(EFS.getStore(uri));
		TextEditor editor = (TextEditor) page1.openEditor(input, EditorsUI.DEFAULT_TEXT_EDITOR_ID, true);
		Method setMethod= AbstractTextEditor.class.getDeclaredMethod("setDocumentProvider", IDocumentProvider.class);
		setMethod.setAccessible(true);
		setMethod.invoke(editor, new ErrorDocumentProvider(editor.getDocumentProvider()));
		editor.setInput(input);
		processEvents();
		return editor;
	}

	private static class ErrorDocumentProvider extends ForwardingDocumentProvider {
		public ErrorDocumentProvider(IDocumentProvider parent) {
			super("", ignored -> { /**/	}, parent);
		}

		@Override
		public IStatus getStatus(Object element) {
			return new Status(IStatus.ERROR, "org.eclipse.ui.workbench.texteditor.tests",
					0, "This document provider always fails", null);
		}
	}
}

Back to the top