Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 27a1f96c79db3fb06e291c23113545707a1d6ccd (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*******************************************************************************
 * Copyright (c) 2017, 2019 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.jface.text.tests.contentassist;

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

import java.util.List;

import org.junit.Test;

import org.eclipse.swt.SWT;
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.IDocument;
import org.eclipse.jface.text.contentassist.ContentAssistant;


public class ContextInformationTest extends AbstractContentAssistTest {


	private Shell infoShell;


	private ContentAssistant createBarContentAssist() {
		ContentAssistant contentAssistant= new ContentAssistant();
		contentAssistant.setContentAssistProcessor(new BarContentAssistProcessor(), IDocument.DEFAULT_CONTENT_TYPE);
		return contentAssistant;
	}

	@Test
	public void testContextInfo() throws Exception {
		setupSourceViewer(createBarContentAssist(), BarContentAssistProcessor.PROPOSAL);
		
		final List<Shell> beforeShells= getCurrentShells();
		
		selectAndReveal(4, 0);
		processEvents();
		
		triggerContextInformation();
		this.infoShell= findNewShell(beforeShells);
		assertEquals("idx= 0", getInfoText(this.infoShell));
		
		selectAndReveal(8, 0);
		processEvents();
		
		triggerContextInformation();
		this.infoShell= findNewShell(beforeShells);
		assertEquals("idx= 1", getInfoText(this.infoShell));
	}

	@Test
	public void testContextInfo_hide_Bug512251() throws Exception {
		setupSourceViewer(createBarContentAssist(), BarContentAssistProcessor.PROPOSAL);
		
		final List<Shell> beforeShells= getCurrentShells();
		
		selectAndReveal(4, 0);
		processEvents();
		
		triggerContextInformation();
		this.infoShell= findNewShell(beforeShells);
		
		selectAndReveal(8, 0);
		processEvents();
		
		triggerContextInformation();
		this.infoShell= findNewShell(beforeShells);
		
		// ITextEditorActionConstants.DELETE_LINE
		getDocument().set("");
		
		new Accessor(getContentAssistant(), ContentAssistant.class).invoke("hide", new Object[0]);
	}

	@Test
	public void testContextInfo_hide_focusOut() throws Exception {
		setupSourceViewer(createBarContentAssist(), BarContentAssistProcessor.PROPOSAL);
		
		final List<Shell> beforeShells = getCurrentShells();
		
		selectAndReveal(4, 0);
		processEvents();
		
		triggerContextInformation();
		this.infoShell= findNewShell(beforeShells);
		assertEquals("idx= 0", getInfoText(this.infoShell));
		
		selectAndReveal(8, 0);
		processEvents();
		
		triggerContextInformation();
		this.infoShell= findNewShell(beforeShells);
		assertEquals("idx= 1", getInfoText(this.infoShell));
		
		// Hide all
		getButton().setFocus();
		processEvents();
		assertTrue(this.infoShell.isDisposed() || !this.infoShell.isVisible());
	}

	@Test
	public void testContextInfo_hide_keyEsc() throws Exception {
		setupSourceViewer(createBarContentAssist(), BarContentAssistProcessor.PROPOSAL);
		
		final List<Shell> beforeShells = getCurrentShells();
		
		selectAndReveal(4, 0);
		processEvents();
		
		triggerContextInformation();
		this.infoShell= findNewShell(beforeShells);
		assertEquals("idx= 0", getInfoText(this.infoShell));
		
		selectAndReveal(8, 0);
		processEvents();
		
		triggerContextInformation();
		this.infoShell= findNewShell(beforeShells);
		assertEquals("idx= 1", getInfoText(this.infoShell));
		
		emulatePressEscKey();
		processEvents();
		this.infoShell= findNewShell(beforeShells);
		assertEquals("idx= 0", getInfoText(this.infoShell));
		
		emulatePressEscKey();
		processEvents();
		assertTrue(this.infoShell.isDisposed() || !this.infoShell.isVisible());
	}

	@Test
	public void testContextInfo_hide_validRange() throws Exception {
		setupSourceViewer(createBarContentAssist(), BarContentAssistProcessor.PROPOSAL + '\n');
		
		final List<Shell> beforeShells = getCurrentShells();
		
		selectAndReveal(4, 0);
		processEvents();
		
		triggerContextInformation();
		this.infoShell= findNewShell(beforeShells);
		assertEquals("idx= 0", getInfoText(this.infoShell));
		
		selectAndReveal(8, 0);
		processEvents();
		
		triggerContextInformation();
		this.infoShell= findNewShell(beforeShells);
		assertEquals("idx= 1", getInfoText(this.infoShell));
		
		emulatePressArrowKey(SWT.ARROW_LEFT);
		processEvents();
		this.infoShell= findNewShell(beforeShells);
		assertEquals("idx= 0", getInfoText(this.infoShell));
		
		emulatePressArrowKey(SWT.ARROW_DOWN);
		processEvents();
		assertTrue(this.infoShell.isDisposed() || !this.infoShell.isVisible());
	}


	static 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;
	}

}

Back to the top