Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 275393
Collapse All | Expand All

(-)Eclipse UI Tests/org/eclipse/ui/tests/fieldassist/FieldAssistTestSuite.java (-1 / +3 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2006 IBM Corporation and others.
2
 * Copyright (c) 2005, 2009 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 30-34 Link Here
30
	 */
30
	 */
31
	public FieldAssistTestSuite() {
31
	public FieldAssistTestSuite() {
32
		addTest(new TestSuite(FieldAssistAPITest.class));
32
		addTest(new TestSuite(FieldAssistAPITest.class));
33
		addTest(new TestSuite(ComboContentAssistCommandAdapterTest.class));
34
		addTest(new TestSuite(TextContentAssistCommandAdapterTest.class));
33
	}
35
	}
34
}
36
}
(-)Eclipse UI Tests/org/eclipse/ui/tests/fieldassist/FieldAssistAPITest.java (-346 lines)
Lines 15-42 Link Here
15
15
16
import junit.framework.TestCase;
16
import junit.framework.TestCase;
17
17
18
import org.eclipse.jface.dialogs.Dialog;
19
import org.eclipse.jface.dialogs.MessageDialog;
20
import org.eclipse.jface.fieldassist.ContentProposalAdapter;
21
import org.eclipse.jface.fieldassist.FieldDecorationRegistry;
18
import org.eclipse.jface.fieldassist.FieldDecorationRegistry;
22
import org.eclipse.jface.fieldassist.SimpleContentProposalProvider;
23
import org.eclipse.jface.fieldassist.TextContentAdapter;
24
import org.eclipse.jface.layout.GridDataFactory;
25
import org.eclipse.jface.layout.GridLayoutFactory;
26
import org.eclipse.swt.SWT;
27
import org.eclipse.swt.graphics.Image;
19
import org.eclipse.swt.graphics.Image;
28
import org.eclipse.swt.widgets.Composite;
29
import org.eclipse.swt.widgets.Control;
30
import org.eclipse.swt.widgets.Display;
31
import org.eclipse.swt.widgets.Event;
32
import org.eclipse.swt.widgets.Label;
33
import org.eclipse.swt.widgets.Shell;
34
import org.eclipse.swt.widgets.Text;
35
import org.eclipse.ui.IWorkbench;
36
import org.eclipse.ui.IWorkbenchCommandConstants;
37
import org.eclipse.ui.PlatformUI;
38
import org.eclipse.ui.fieldassist.ContentAssistCommandAdapter;
39
import org.eclipse.ui.handlers.IHandlerService;
40
import org.eclipse.ui.internal.ide.IDEInternalWorkbenchImages;
20
import org.eclipse.ui.internal.ide.IDEInternalWorkbenchImages;
41
21
42
/**
22
/**
Lines 46-53 Link Here
46
 */
26
 */
47
public class FieldAssistAPITest extends TestCase {
27
public class FieldAssistAPITest extends TestCase {
48
28
49
	private Dialog dialog;
50
51
	public FieldAssistAPITest() {
29
	public FieldAssistAPITest() {
52
		super();
30
		super();
53
	}
31
	}
Lines 59-75 Link Here
59
		super(name);
37
		super(name);
60
	}
38
	}
61
39
62
	protected void setUp() throws Exception {
63
		super.setUp();
64
	}
65
66
	protected void tearDown() throws Exception {
67
		if (dialog != null) {
68
			dialog.close();
69
		}
70
		super.tearDown();
71
	}
72
73
	public void testFieldDecorationRegistry() {
40
	public void testFieldDecorationRegistry() {
74
		int originalMaxHeight = FieldDecorationRegistry.getDefault()
41
		int originalMaxHeight = FieldDecorationRegistry.getDefault()
75
				.getMaximumDecorationHeight();
42
				.getMaximumDecorationHeight();
Lines 123-440 Link Here
123
				.getMaximumDecorationHeight() == originalMaxHeight);
90
				.getMaximumDecorationHeight() == originalMaxHeight);
124
		assertTrue(FieldDecorationRegistry.getDefault()
91
		assertTrue(FieldDecorationRegistry.getDefault()
125
				.getMaximumDecorationWidth() == originalMaxWidth);
92
				.getMaximumDecorationWidth() == originalMaxWidth);
126
127
	}
128
129
	/**
130
	 * Tests that a ContentAssistCommandAdapter that has no autoactivation
131
	 * characters set will not have its proposals disappear when a user invokes
132
	 * content assist and then subsequently inserts a character that matches the
133
	 * first character of a suggested proposal.
134
	 * <p>
135
	 * <ol>
136
	 * <li>User invokes content assist</li>
137
	 * <li>"one", "two", "three"...shows up</li>
138
	 * <li>User hits the 'O' key</li>
139
	 * <li>The list shows up (the bug was reporting that the list disappeared)</li>
140
	 * </ol>
141
	 * 
142
	 * @see org.eclipse.jface.tests.fieldassist.FieldAssistAPITest
143
	 */
144
	public void testBug271339EmptyAutoActivationCharacters() throws Exception {
145
		IWorkbench workbench = PlatformUI.getWorkbench();
146
		Shell shell = workbench.getActiveWorkbenchWindow().getShell();
147
		Display display = shell.getDisplay();
148
149
		// record the number of shells we have up and active
150
		int shellCount = display.getShells().length;
151
152
		// the text control of our dialog
153
		final Text[] textField = { null };
154
155
		dialog = new MessageDialog(shell, null, null, null,
156
				MessageDialog.INFORMATION, new String[] { "OK" }, 0) {
157
			protected Control createCustomArea(Composite parent) {
158
				String[] proposals = new String[] { "one", "two", "three",
159
						"four", "five", "six", "seven", "eight", "nine", "ten" };
160
161
				Composite container = new Composite(parent, SWT.NULL);
162
				GridDataFactory.fillDefaults().grab(true, true).applyTo(
163
						container);
164
				GridLayoutFactory.swtDefaults().numColumns(2)
165
						.applyTo(container);
166
167
				Label label = new Label(container, SWT.NULL);
168
				label.setText("Test Content Assist bug 271339");
169
170
				textField[0] = new Text(container, SWT.FLAT);
171
				SimpleContentProposalProvider proposalProvider = new SimpleContentProposalProvider(
172
						proposals);
173
				proposalProvider.setFiltering(true);
174
175
				// use an empty character array because no characters should
176
				// prompt for autoactivation
177
				ContentAssistCommandAdapter adapter = new ContentAssistCommandAdapter(
178
						textField[0], new TextContentAdapter(),
179
						proposalProvider, null, new char[0], true);
180
				adapter
181
						.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);
182
183
				GridDataFactory.fillDefaults().grab(true, false).applyTo(
184
						textField[0]);
185
186
				return container;
187
			}
188
		};
189
190
		// we don't want to block the tests
191
		dialog.setBlockOnOpen(false);
192
		dialog.open();
193
194
		// grant the text field focus
195
		textField[0].setFocus();
196
		// spin the event loop to make sure the text field gets focus
197
		while (display.readAndDispatch())
198
			;
199
200
		// retrieve the content assist handler and run it
201
		IHandlerService handlerService = (IHandlerService) workbench
202
				.getService(IHandlerService.class);
203
		handlerService.executeCommand(
204
				IWorkbenchCommandConstants.EDIT_CONTENT_ASSIST, null);
205
206
		assertEquals(
207
				"There should be two more shells up, the dialog and the proposals dialog",
208
				shellCount + 2, display.getShells().length);
209
210
		// fake a KeyDown event
211
		Event event = new Event();
212
		event.type = SWT.KeyDown;
213
		event.character = 'o';
214
		textField[0].notifyListeners(SWT.KeyDown, event);
215
216
		// now we insert the character 'o', this will send out a Modify event
217
		textField[0].insert("o"); //$NON-NLS-1$
218
219
		assertEquals(
220
				"There should still be two more shells up, the dialog and the proposals dialog",
221
				shellCount + 2, display.getShells().length);
222
223
		// spin the event loop again because we have some asyncExec calls in the
224
		// ContentProposalAdapter class
225
		while (display.readAndDispatch())
226
			;
227
228
		// clean-up
229
		dialog.close();
230
	}
231
232
	/**
233
	 * Tests that a ContentAssistCommandAdapter that has no autoactivation
234
	 * characters set will not have its proposals appear when a user inserts a
235
	 * character that matches the first character of a suggested proposal.
236
	 * <p>
237
	 * <ol>
238
	 * <li>User hits the 'O' key</li>
239
	 * <li>While "one" matches, the proposals should not appear as no
240
	 * autoactivation characters have been set</li>
241
	 * </ol>
242
	 * 
243
	 * @see org.eclipse.jface.tests.fieldassist.FieldAssistAPITest
244
	 */
245
	public void testBug271339EmptyAutoActivationCharacters2() throws Exception {
246
		IWorkbench workbench = PlatformUI.getWorkbench();
247
		Shell shell = workbench.getActiveWorkbenchWindow().getShell();
248
		Display display = shell.getDisplay();
249
250
		// record the number of shells we have up and active
251
		int shellCount = display.getShells().length;
252
253
		// the text control of our dialog
254
		final Text[] textField = { null };
255
256
		dialog = new MessageDialog(shell, null, null, null,
257
				MessageDialog.INFORMATION, new String[] { "OK" }, 0) {
258
			protected Control createCustomArea(Composite parent) {
259
				String[] proposals = new String[] { "one", "two", "three",
260
						"four", "five", "six", "seven", "eight", "nine", "ten" };
261
262
				Composite container = new Composite(parent, SWT.NULL);
263
				GridDataFactory.fillDefaults().grab(true, true).applyTo(
264
						container);
265
				GridLayoutFactory.swtDefaults().numColumns(2)
266
						.applyTo(container);
267
268
				Label label = new Label(container, SWT.NULL);
269
				label.setText("Test Content Assist bug 271339");
270
271
				textField[0] = new Text(container, SWT.FLAT);
272
				SimpleContentProposalProvider proposalProvider = new SimpleContentProposalProvider(
273
						proposals);
274
				proposalProvider.setFiltering(true);
275
276
				// use an empty character array because no characters should
277
				// prompt for autoactivation
278
				ContentAssistCommandAdapter adapter = new ContentAssistCommandAdapter(
279
						textField[0], new TextContentAdapter(),
280
						proposalProvider, null, new char[0], true);
281
				adapter
282
						.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);
283
284
				GridDataFactory.fillDefaults().grab(true, false).applyTo(
285
						textField[0]);
286
287
				return container;
288
			}
289
		};
290
291
		// we don't want to block the tests
292
		dialog.setBlockOnOpen(false);
293
		dialog.open();
294
295
		// grant the text field focus
296
		textField[0].setFocus();
297
		// spin the event loop to make sure the text field gets focus
298
		while (display.readAndDispatch())
299
			;
300
301
		// fake a KeyDown event
302
		Event event = new Event();
303
		event.type = SWT.KeyDown;
304
		event.character = 'o';
305
		textField[0].notifyListeners(SWT.KeyDown, event);
306
307
		// now we insert the character 'o', this will send out a Modify event
308
		textField[0].insert("o"); //$NON-NLS-1$
309
310
		// we have no autoactivation characters, the proposals should not appear
311
		assertEquals(
312
				"There should only be one more extra shell, the dialog itself",
313
				shellCount + 1, display.getShells().length);
314
315
		// spin the event loop again because we have some asyncExec calls in the
316
		// ContentProposalAdapter class
317
		while (display.readAndDispatch())
318
			;
319
320
		// clean-up
321
		dialog.close();
322
	}
323
324
	/**
325
	 * Tests that a ContentAssistCommandAdapter that has no autoactivation
326
	 * characters set will stay open if the user backspaces over a narrowing
327
	 * proposal character.
328
	 * <p>
329
	 * <ol>
330
	 * <li>User invokes content assist</li>
331
	 * <li>"one", "two", "three"...shows up</li>
332
	 * <li>User hits the 'O' key</li>
333
	 * <li>The list narrows</li>
334
	 * <li>user hits backspace</li>
335
	 * <li>the popup should remain open</li>
336
	 * </ol>
337
	 * 
338
	 * @see org.eclipse.jface.tests.fieldassist.FieldAssistAPITest
339
	 */
340
	public void testBug271339EmptyAutoActivationCharacters3() throws Exception {
341
		IWorkbench workbench = PlatformUI.getWorkbench();
342
		Shell shell = workbench.getActiveWorkbenchWindow().getShell();
343
		Display display = shell.getDisplay();
344
345
		// record the number of shells we have up and active
346
		int shellCount = display.getShells().length;
347
348
		// the text control of our dialog
349
		final Text[] textField = { null };
350
351
		dialog = new MessageDialog(shell, null, null, null,
352
				MessageDialog.INFORMATION, new String[] { "OK" }, 0) {
353
			protected Control createCustomArea(Composite parent) {
354
				String[] proposals = new String[] { "one", "two", "three",
355
						"four", "five", "six", "seven", "eight", "nine", "ten" };
356
357
				Composite container = new Composite(parent, SWT.NULL);
358
				GridDataFactory.fillDefaults().grab(true, true).applyTo(
359
						container);
360
				GridLayoutFactory.swtDefaults().numColumns(2)
361
						.applyTo(container);
362
363
				Label label = new Label(container, SWT.NULL);
364
				label.setText("Test Content Assist bug 271339");
365
366
				textField[0] = new Text(container, SWT.FLAT);
367
				SimpleContentProposalProvider proposalProvider = new SimpleContentProposalProvider(
368
						proposals);
369
				proposalProvider.setFiltering(true);
370
371
				// use an empty character array because no characters should
372
				// prompt for autoactivation
373
				ContentAssistCommandAdapter adapter = new ContentAssistCommandAdapter(
374
						textField[0], new TextContentAdapter(),
375
						proposalProvider, null, new char[0], true);
376
				adapter
377
						.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);
378
379
				GridDataFactory.fillDefaults().grab(true, false).applyTo(
380
						textField[0]);
381
382
				return container;
383
			}
384
		};
385
386
		// we don't want to block the tests
387
		dialog.setBlockOnOpen(false);
388
		dialog.open();
389
390
		// grant the text field focus
391
		textField[0].setFocus();
392
		// spin the event loop to make sure the text field gets focus
393
		while (display.readAndDispatch())
394
			;
395
396
		// retrieve the content assist handler and run it
397
		IHandlerService handlerService = (IHandlerService) workbench
398
				.getService(IHandlerService.class);
399
		handlerService.executeCommand(
400
				IWorkbenchCommandConstants.EDIT_CONTENT_ASSIST, null);
401
402
		assertEquals(
403
				"There should be two more shells up, the dialog and the proposals dialog",
404
				shellCount + 2, display.getShells().length);
405
406
		// fake a KeyDown event
407
		Event event = new Event();
408
		event.type = SWT.KeyDown;
409
		event.character = 'o';
410
		textField[0].notifyListeners(SWT.KeyDown, event);
411
412
		// now we insert the character 'o', this will send out a Modify event
413
		textField[0].insert("o"); //$NON-NLS-1$
414
415
		assertEquals(
416
				"There should still be two more shells up, the dialog and the proposals dialog",
417
				shellCount + 2, display.getShells().length);
418
		
419
		// fake a Backspace
420
		event = new Event();
421
		event.type = SWT.KeyDown;
422
		event.character = SWT.BS;
423
		textField[0].notifyListeners(SWT.KeyDown, event);
424
		
425
		// now we remove the o, this will trigger a modify
426
		textField[0].setText("");  //$NON-NLS-1$
427
	
428
		assertEquals(
429
				"There should still be two more shells up, the dialog and the proposals dialog",
430
				shellCount + 2, display.getShells().length);
431
432
		// spin the event loop again because we have some asyncExec calls in the
433
		// ContentProposalAdapter class
434
		while (display.readAndDispatch())
435
			;
436
437
		// clean-up
438
		dialog.close();
439
	}
93
	}
440
}
94
}
(-)Eclipse (+36 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2009 Remy Chi Jian Suen and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Remy Chi Jian Suen <remy.suen@gmail.com> - initial API and implementation
10
 ******************************************************************************/
11
package org.eclipse.jface.tests.fieldassist;
12
13
import org.eclipse.jface.fieldassist.ComboContentAdapter;
14
import org.eclipse.jface.fieldassist.IControlContentAdapter;
15
import org.eclipse.swt.SWT;
16
import org.eclipse.swt.widgets.Combo;
17
import org.eclipse.swt.widgets.Composite;
18
import org.eclipse.swt.widgets.Control;
19
20
public class ComboFieldAssistWindow extends AbstractFieldAssistWindow {
21
	
22
	protected IControlContentAdapter getControlContentAdapter() {
23
		return new ComboContentAdapter();
24
	}
25
26
	/*
27
	 * (non-Javadoc)
28
	 * 
29
	 * @seeorg.eclipse.jface.tests.fieldassist.AbstractFieldAssistWindow#
30
	 * createFieldAssistControl(org.eclipse.swt.widgets.Composite)
31
	 */
32
	protected Control createFieldAssistControl(Composite parent) {
33
		return new Combo(parent, SWT.DROP_DOWN);
34
	}
35
36
}
(-)Eclipse (+27 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2009 Remy Chi Jian Suen and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Remy Chi Jian Suen <remy.suen@gmail.com> - initial API and implementation
10
 ******************************************************************************/
11
package org.eclipse.ui.tests.fieldassist;
12
13
import org.eclipse.jface.fieldassist.ContentProposalAdapter;
14
import org.eclipse.jface.tests.fieldassist.TextFieldAssistWindow;
15
import org.eclipse.swt.widgets.Control;
16
import org.eclipse.ui.fieldassist.ContentAssistCommandAdapter;
17
18
public class TextCommandFieldAssistWindow extends TextFieldAssistWindow {
19
20
	protected ContentProposalAdapter createContentProposalAdapter(
21
			Control control) {
22
		return new ContentAssistCommandAdapter(control,
23
				getControlContentAdapter(), getContentProposalProvider(), null,
24
				getAutoActivationCharacters());
25
	}
26
27
}
(-)Eclipse (+22 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2009 Remy Chi Jian Suen and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Remy Chi Jian Suen <remy.suen@gmail.com> - initial API and implementation
10
 ******************************************************************************/
11
package org.eclipse.ui.tests.fieldassist;
12
13
import org.eclipse.jface.tests.fieldassist.AbstractFieldAssistWindow;
14
15
public class TextContentAssistCommandAdapterTest extends
16
		AbstractContentAssistCommandAdapterTest {
17
18
	protected AbstractFieldAssistWindow createFieldAssistWindow() {
19
		return new TextCommandFieldAssistWindow();
20
	}
21
22
}
(-)Eclipse (+144 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2009 Remy Chi Jian Suen and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Remy Chi Jian Suen <remy.suen@gmail.com> - initial API and implementation
10
 ******************************************************************************/
11
package org.eclipse.jface.tests.fieldassist;
12
13
import junit.framework.TestCase;
14
15
import org.eclipse.swt.SWT;
16
import org.eclipse.swt.widgets.Combo;
17
import org.eclipse.swt.widgets.Control;
18
import org.eclipse.swt.widgets.Display;
19
import org.eclipse.swt.widgets.Event;
20
import org.eclipse.swt.widgets.Text;
21
22
public abstract class AbstractFieldAssistTestCase extends TestCase {
23
24
	/**
25
	 * The window that is being tested.
26
	 */
27
	private AbstractFieldAssistWindow window;
28
29
	/**
30
	 * The original number of shells at the beginning of the test.
31
	 */
32
	private int originalShellCount;
33
34
	/**
35
	 * Sets up this field assist test case. Subclasses should extend but not
36
	 * override.
37
	 */
38
	protected void setUp() throws Exception {
39
		super.setUp();
40
		originalShellCount = Display.getDefault().getShells().length;
41
		window = createFieldAssistWindow();
42
		assertNotNull(window);
43
	}
44
45
	/**
46
	 * Tears down this field assist test case. Subclasses should extend but not
47
	 * override.
48
	 */
49
	protected void tearDown() throws Exception {
50
		if (window != null) {
51
			spinEventLoop();
52
53
			// clean-up
54
			window.close();
55
			window = null;
56
		}
57
58
		super.tearDown();
59
	}
60
61
	/**
62
	 * Creates the field assist window that is to be tested.
63
	 */
64
	protected abstract AbstractFieldAssistWindow createFieldAssistWindow();
65
66
	/**
67
	 * Returns the created field assist window. May be null if
68
	 * {@link #createFieldAssistWindow()} has not been called yet or if the test
69
	 * is being torn down.
70
	 */
71
	protected AbstractFieldAssistWindow getFieldAssistWindow() {
72
		return window;
73
	}
74
75
	protected void spinEventLoop() {
76
		// spin the event loop again because we have some asyncExec calls in the
77
		// ContentProposalAdapter class
78
		while (window.getDisplay().readAndDispatch())
79
			;
80
	}
81
82
	protected void ensurePopupIsUp() {
83
		// if our autoactivation delay is zero, we use an asyncExec to get the
84
		// popup up, hence, we need to spin the event loop
85
		if (window.getAutoActivationDelay() == 0) {
86
			spinEventLoop();
87
		}
88
	}
89
90
	/**
91
	 * Sends an SWT FocisIn event to the field assist control.
92
	 */
93
	protected void sendFocusInToControl() {
94
		Event event = new Event();
95
		event.type = SWT.FocusIn;
96
		window.getFieldAssistControl().notifyListeners(SWT.FocusIn, event);
97
	}
98
99
	/**
100
	 * Sends an SWT KeyDown event for the specified character to the field
101
	 * assist control.
102
	 * 
103
	 * @param character
104
	 *            the character that has been pressed
105
	 */
106
	protected void sendKeyDownToControl(char character) {
107
		// fake a KeyDown event
108
		Event event = new Event();
109
		event.type = SWT.KeyDown;
110
		event.character = character;
111
		window.getFieldAssistControl().notifyListeners(SWT.KeyDown, event);
112
	}
113
114
	/**
115
	 * Sets the text of the field assist control.
116
	 */
117
	protected void setText(String string) {
118
		Control control = window.getFieldAssistControl();
119
		if (control instanceof Combo) {
120
			((Combo) control).setText(string);
121
		} else if (control instanceof Text) {
122
			((Text) control).setText(string);
123
		}
124
	}
125
126
	/**
127
	 * Checks that there is only one shell up, the original field assist window.
128
	 */
129
	protected void assertOneShellUp() {
130
		assertEquals("There should only be one shell up, the dialog",
131
				originalShellCount + 1, window.getDisplay().getShells().length);
132
	}
133
134
	/**
135
	 * Checks that there are two shells up, the original field assist window and
136
	 * the proposals popup.
137
	 */
138
	protected void assertTwoShellsUp() {
139
		assertEquals(
140
				"There should two shells up, the dialog and the proposals dialog",
141
				originalShellCount + 2, window.getDisplay().getShells().length);
142
	}
143
144
}
(-)Eclipse (+147 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2009 Remy Chi Jian Suen and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Remy Chi Jian Suen <remy.suen@gmail.com> - initial API and implementation
10
 ******************************************************************************/
11
package org.eclipse.jface.tests.fieldassist;
12
13
import org.eclipse.core.runtime.Assert;
14
import org.eclipse.jface.bindings.keys.KeyStroke;
15
import org.eclipse.jface.fieldassist.ContentProposalAdapter;
16
import org.eclipse.jface.fieldassist.IContentProposalProvider;
17
import org.eclipse.jface.fieldassist.IControlContentAdapter;
18
import org.eclipse.jface.fieldassist.SimpleContentProposalProvider;
19
import org.eclipse.jface.window.Window;
20
import org.eclipse.swt.layout.FillLayout;
21
import org.eclipse.swt.widgets.Composite;
22
import org.eclipse.swt.widgets.Control;
23
import org.eclipse.swt.widgets.Display;
24
import org.eclipse.swt.widgets.Shell;
25
26
public abstract class AbstractFieldAssistWindow extends Window {
27
28
	private Control fieldAssistControl;
29
30
	private IContentProposalProvider proposalProvider;
31
	private KeyStroke keyStroke = null;
32
	private char[] autoActivationCharacters = null;
33
	private int filterStyle = ContentProposalAdapter.FILTER_NONE;
34
	private boolean propagateKeys = true;
35
	private int acceptance = ContentProposalAdapter.PROPOSAL_INSERT;
36
	private int autoActivationDelay = 0;
37
38
	public AbstractFieldAssistWindow() {
39
		super((Shell) null);
40
	}
41
42
	public Display getDisplay() {
43
		return getShell().getDisplay();
44
	}
45
46
	protected Control createContents(Composite parent) {
47
		Composite content = (Composite) super.createContents(parent);
48
		content.setLayout(new FillLayout());
49
50
		fieldAssistControl = createFieldAssistControl(parent);
51
		Assert.isNotNull(fieldAssistControl);
52
53
		ContentProposalAdapter adapter = createContentProposalAdapter(fieldAssistControl);
54
		adapter.setAutoActivationDelay(autoActivationDelay);
55
		adapter.setFilterStyle(filterStyle);
56
		adapter.setPropagateKeys(propagateKeys);
57
		adapter.setProposalAcceptanceStyle(acceptance);
58
59
		return content;
60
	}
61
62
	/**
63
	 * Create and return the content proposal adapter that will be used by this
64
	 * field assist window.
65
	 * 
66
	 * @param control
67
	 *            the SWT control to provide field assist for
68
	 */
69
	protected ContentProposalAdapter createContentProposalAdapter(
70
			Control control) {
71
		return new ContentProposalAdapter(control, getControlContentAdapter(),
72
				getContentProposalProvider(), getKeyStroke(),
73
				getAutoActivationCharacters());
74
	}
75
76
	protected abstract IControlContentAdapter getControlContentAdapter();
77
78
	public Control getFieldAssistControl() {
79
		return fieldAssistControl;
80
	}
81
82
	protected abstract Control createFieldAssistControl(Composite parent);
83
84
	public void setAutoActivationDelay(int autoActivationDelay) {
85
		this.autoActivationDelay = autoActivationDelay;
86
	}
87
88
	public final int getAutoActivationDelay() {
89
		return autoActivationDelay;
90
	}
91
92
	protected boolean shouldFilterProposals() {
93
		return true;
94
	}
95
96
	protected String[] getProposals() {
97
		return new String[] { "one", "two", "three", "four", "five", "six",
98
				"seven", "eight", "nine", "ten" };
99
	}
100
101
	protected char[] getAutoActivationCharacters() {
102
		return autoActivationCharacters;
103
	}
104
105
	public void setAutoActivationCharacters(char[] autoActivationCharacters) {
106
		this.autoActivationCharacters = autoActivationCharacters;
107
	}
108
109
	protected IContentProposalProvider createContentProposalProvider() {
110
		SimpleContentProposalProvider proposalProvider = new SimpleContentProposalProvider(
111
				getProposals());
112
		proposalProvider.setFiltering(shouldFilterProposals());
113
		return proposalProvider;
114
	}
115
116
	protected IContentProposalProvider getContentProposalProvider() {
117
		if (proposalProvider == null) {
118
			proposalProvider = createContentProposalProvider();
119
		}
120
		return proposalProvider;
121
	}
122
123
	public void setContentProposalProvider(
124
			IContentProposalProvider proposalProvider) {
125
		this.proposalProvider = proposalProvider;
126
	}
127
128
	public void setFilterStyle(int filterStyle) {
129
		this.filterStyle = filterStyle;
130
	}
131
132
	public void setPropagateKeys(boolean propagateKeys) {
133
		this.propagateKeys = propagateKeys;
134
	}
135
136
	public void setProposalAcceptanceStyle(int acceptance) {
137
		this.acceptance = acceptance;
138
	}
139
140
	protected KeyStroke getKeyStroke() {
141
		return keyStroke;
142
	}
143
144
	public void setKeyStroke(KeyStroke keyStroke) {
145
		this.keyStroke = keyStroke;
146
	}
147
}
(-)Eclipse (+143 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2009 Remy Chi Jian Suen and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Remy Chi Jian Suen <remy.suen@gmail.com> - initial API and implementation
10
 ******************************************************************************/
11
12
package org.eclipse.ui.tests.fieldassist;
13
14
import org.eclipse.jface.tests.fieldassist.AbstractFieldAssistTestCase;
15
import org.eclipse.swt.SWT;
16
import org.eclipse.ui.IWorkbenchCommandConstants;
17
import org.eclipse.ui.PlatformUI;
18
import org.eclipse.ui.handlers.IHandlerService;
19
20
public abstract class AbstractContentAssistCommandAdapterTest extends
21
		AbstractFieldAssistTestCase {
22
23
	protected void executeContentAssistHandler() throws Exception {
24
		// retrieve the content assist handler and run it
25
		IHandlerService handlerService = (IHandlerService) PlatformUI
26
				.getWorkbench().getService(IHandlerService.class);
27
		handlerService.executeCommand(
28
				IWorkbenchCommandConstants.EDIT_CONTENT_ASSIST, null);
29
	}
30
	
31
	public void testHandlerPromptsPopup() throws Exception {
32
		getFieldAssistWindow().open();
33
34
		sendFocusInToControl();
35
		executeContentAssistHandler();
36
37
		assertTwoShellsUp();
38
	}
39
40
	/**
41
	 * Tests that a ContentAssistCommandAdapter that has no autoactivation
42
	 * characters set will not have its proposals disappear when a user invokes
43
	 * content assist and then subsequently inserts a character that matches the
44
	 * first character of a suggested proposal.
45
	 * <p>
46
	 * <ol>
47
	 * <li>User invokes content assist</li>
48
	 * <li>"one", "two", "three"...shows up</li>
49
	 * <li>User hits the 'O' key</li>
50
	 * <li>The list shows up (the bug was reporting that the list disappeared)</li>
51
	 * </ol>
52
	 */
53
	public void testBug271339EmptyAutoActivationCharacters() throws Exception {
54
		getFieldAssistWindow().open();
55
56
		sendFocusInToControl();
57
		executeContentAssistHandler();
58
59
		assertTwoShellsUp();
60
61
		sendKeyDownToControl('o');
62
		setText("o"); //$NON-NLS-1$
63
64
		assertTwoShellsUp();
65
	}
66
67
	/**
68
	 * Tests that a ContentAssistCommandAdapter that has no autoactivation
69
	 * characters set will not have its proposals appear when a user inserts a
70
	 * character that matches the first character of a suggested proposal.
71
	 * <p>
72
	 * <ol>
73
	 * <li>User hits the 'O' key</li>
74
	 * <li>While "one" matches, the proposals should not appear as no
75
	 * autoactivation characters have been set</li>
76
	 * </ol>
77
	 */
78
	public void testBug271339EmptyAutoActivationCharacters2() throws Exception {
79
		getFieldAssistWindow().open();
80
81
		sendFocusInToControl();
82
83
		sendKeyDownToControl('o');
84
		setText("o"); //$NON-NLS-1$
85
86
		assertOneShellUp();
87
	}
88
89
	/**
90
	 * Tests that a ContentAssistCommandAdapter that has no autoactivation
91
	 * characters set will stay open if the user backspaces over a narrowing
92
	 * proposal character.
93
	 * <p>
94
	 * <ol>
95
	 * <li>User invokes content assist</li>
96
	 * <li>"one", "two", "three"...shows up</li>
97
	 * <li>User hits the 'O' key</li>
98
	 * <li>The list narrows</li>
99
	 * <li>user hits backspace</li>
100
	 * <li>the popup should remain open</li>
101
	 * </ol>
102
	 */
103
	public void testBug271339EmptyAutoActivationCharacters3() throws Exception {
104
		getFieldAssistWindow().open();
105
106
		sendFocusInToControl();
107
		executeContentAssistHandler();
108
109
		assertTwoShellsUp();
110
111
		sendKeyDownToControl('o');
112
		setText("o"); //$NON-NLS-1$
113
114
		assertTwoShellsUp();
115
116
		sendKeyDownToControl(SWT.BS);
117
		setText("");
118
119
		assertTwoShellsUp();
120
	}
121
	
122
//	public void testX() throws Exception {
123
//		getFieldAssistWindow().setAutoActivationCharacters(new char[] { 'o', 't' });
124
//		getFieldAssistWindow().open();
125
//
126
//		setFocusToControl();
127
//
128
//		sendKeyDownToControl('o');
129
//		setText("o"); //$NON-NLS-1$
130
//
131
//		assertOneShellUp();
132
//		
133
//		executeContentAssistHandler();
134
//
135
//		assertTwoShellsUp();
136
//
137
//		sendKeyDownToControl(SWT.BS);
138
//		setText("");
139
//
140
//		assertTwoShellsUp();
141
//	}
142
143
}
(-)Eclipse (+22 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2009 Remy Chi Jian Suen and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Remy Chi Jian Suen <remy.suen@gmail.com> - initial API and implementation
10
 ******************************************************************************/
11
package org.eclipse.ui.tests.fieldassist;
12
13
import org.eclipse.jface.tests.fieldassist.AbstractFieldAssistWindow;
14
15
public class ComboContentAssistCommandAdapterTest extends
16
		AbstractContentAssistCommandAdapterTest {
17
18
	protected AbstractFieldAssistWindow createFieldAssistWindow() {
19
		return new ComboCommandFieldAssistWindow();
20
	}
21
22
}
(-)Eclipse (+27 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2009 Remy Chi Jian Suen and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Remy Chi Jian Suen <remy.suen@gmail.com> - initial API and implementation
10
 ******************************************************************************/
11
package org.eclipse.ui.tests.fieldassist;
12
13
import org.eclipse.jface.fieldassist.ContentProposalAdapter;
14
import org.eclipse.jface.tests.fieldassist.ComboFieldAssistWindow;
15
import org.eclipse.swt.widgets.Control;
16
import org.eclipse.ui.fieldassist.ContentAssistCommandAdapter;
17
18
public class ComboCommandFieldAssistWindow extends ComboFieldAssistWindow {
19
20
	protected ContentProposalAdapter createContentProposalAdapter(
21
			Control control) {
22
		return new ContentAssistCommandAdapter(control,
23
				getControlContentAdapter(), getContentProposalProvider(), null,
24
				getAutoActivationCharacters());
25
	}
26
27
}
(-)Eclipse (+39 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2009 Remy Chi Jian Suen and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Remy Chi Jian Suen <remy.suen@gmail.com> - initial API and implementation
10
 ******************************************************************************/
11
package org.eclipse.jface.tests.fieldassist;
12
13
import org.eclipse.jface.fieldassist.IControlContentAdapter;
14
import org.eclipse.jface.fieldassist.TextContentAdapter;
15
import org.eclipse.swt.SWT;
16
import org.eclipse.swt.widgets.Composite;
17
import org.eclipse.swt.widgets.Control;
18
import org.eclipse.swt.widgets.Text;
19
20
public class TextFieldAssistWindow extends AbstractFieldAssistWindow {
21
22
	/*
23
	 * (non-Javadoc)
24
	 * 
25
	 * @seeorg.eclipse.jface.tests.fieldassist.AbstractFieldAssistWindow#
26
	 * createFieldAssistControl(org.eclipse.swt.widgets.Composite)
27
	 */
28
	protected Control createFieldAssistControl(Composite parent) {
29
		return new Text(parent, SWT.SINGLE);
30
	}
31
32
	/* (non-Javadoc)
33
	 * @see org.eclipse.jface.tests.fieldassist.AbstractFieldAssistWindow#getControlContentAdapter()
34
	 */
35
	protected IControlContentAdapter getControlContentAdapter() {
36
		return new TextContentAdapter();
37
	}
38
39
}

Return to bug 275393