Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cf9723449bee425fca1d52dbe69497dabb1628c8 (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
129
/*******************************************************************************
 * Copyright (c) 2017 Stephan Wahlbrink 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:
 *     Stephan Wahlbrink <sw@wahlbrink.eu>
 *******************************************************************************/
package org.eclipse.ui.genericeditor.tests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.Test;

import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import org.eclipse.text.tests.Accessor;

import org.eclipse.jface.text.contentassist.ContentAssistant;
import org.eclipse.jface.text.source.SourceViewer;

import org.eclipse.ui.genericeditor.tests.contributions.BarContentAssistProcessor;

import org.eclipse.ui.texteditor.ITextEditorActionConstants;
import org.eclipse.ui.texteditor.TextOperationAction;


public class ContextInfoTest extends AbstratGenericEditorTest {


	private Shell completionShell;


	@Test
	public void testContextInfo() throws Exception {
		cleanFileAndEditor();
		createAndOpenFile("foobar.txt", BarContentAssistProcessor.PROPOSAL);

		final Set<Shell> beforeShells = Arrays.stream(editor.getSite().getShell().getDisplay().getShells()).filter(Shell::isVisible).collect(Collectors.toSet());
		TextOperationAction action = (TextOperationAction) editor.getAction(ITextEditorActionConstants.CONTENT_ASSIST_CONTEXT_INFORMATION);

		editor.selectAndReveal(4, 0);
		action.update();
		action.run();
		waitAndDispatch(100);
		this.completionShell= findNewShell(beforeShells);
		assertEquals("idx= 0", getInfoText(this.completionShell));

		editor.selectAndReveal(8, 0);
		action.update();
		action.run();
		waitAndDispatch(100);
		this.completionShell= findNewShell(beforeShells);
		assertEquals("idx= 1", getInfoText(this.completionShell));
	}

	@Test
	public void testContextInfo_hide_Bug512251() throws Exception {
		cleanFileAndEditor();
		createAndOpenFile("foobar.txt", BarContentAssistProcessor.PROPOSAL);

		final Set<Shell> beforeShells = Arrays.stream(editor.getSite().getShell().getDisplay().getShells()).filter(Shell::isVisible).collect(Collectors.toSet());
		TextOperationAction action = (TextOperationAction) editor.getAction(ITextEditorActionConstants.CONTENT_ASSIST_CONTEXT_INFORMATION);

		editor.selectAndReveal(4, 0);
		action.update();
		action.run();
		waitAndDispatch(100);
		this.completionShell= findNewShell(beforeShells);

		editor.selectAndReveal(8, 0);
		action.update();
		action.run();
		waitAndDispatch(100);
		this.completionShell= findNewShell(beforeShells);

		editor.getAction(ITextEditorActionConstants.DELETE_LINE).run();

		SourceViewer sourceViewer= getSourceViewer();
		ContentAssistant assist= (ContentAssistant) new Accessor(sourceViewer, SourceViewer.class).get("fContentAssistant");
		new Accessor(assist, ContentAssistant.class).invoke("hide", new Object[0]);
	}


	private Shell findNewShell(Set<Shell> beforeShells) {
		Shell[] afterShells = Arrays.stream(editor.getSite().getShell().getDisplay().getShells())
				.filter(Shell::isVisible)
				.filter(shell -> !beforeShells.contains(shell))
				.toArray(Shell[]::new);
		assertEquals("No new shell found", 1, afterShells.length);
		return afterShells[0];
	}

	private String getInfoText(final Shell shell) {
		assertTrue(shell.isVisible());
		Control[] children= shell.getChildren();
		for (Control child : children) {
			if (child instanceof Text) {
				return ((Text) child).getText();
			}
			if (child instanceof StyledText) {
				return ((StyledText) child).getText();
			}
		}
		return null;
	}

	@After
	public void closeShell() {
		if (this.completionShell != null && !completionShell.isDisposed()) {
			completionShell.close();
		}
	}

}

Back to the top