Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 02b92a35f1fc8f0869982ffcfe40f5b08c3956c4 (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
/*******************************************************************************
 * Copyright (c) 2006 Ruediger Herrmann 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:
 *     Ruediger Herrmann <rherrmann@innoopract.com> - initial API and implementation
 *******************************************************************************/
package org.eclipse.jface.tests.viewers;

import junit.framework.TestCase;

import org.eclipse.core.runtime.ISafeRunnable;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.util.ILogger;
import org.eclipse.jface.util.ISafeRunnableRunner;
import org.eclipse.jface.util.Policy;
import org.eclipse.jface.util.SafeRunnable;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.IElementComparer;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.StructuredViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class ComboViewerComparerTest extends TestCase {

	private Shell shell;

	private StructuredViewer<TestElement,TestElement[]> viewer;

	private static final class TestElement {

		private final String name;

		public TestElement(final String name) {
			this.name = name;
		}

		public String getName() {
			return name;
		}
	}

	public void testSetSelection() {
		viewer.setContentProvider(new ArrayContentProvider<String>(String.class));
		viewer.setComparer(new IElementComparer() {
			public boolean equals(final Object element1, final Object element2) {
				TestElement testElement1 = (TestElement) element1;
				TestElement testElement2 = (TestElement) element2;
				return testElement1.getName().equals(testElement2.getName());
			}

			public int hashCode(final Object element) {
				TestElement testElement = (TestElement) element;
				return testElement.getName().hashCode();
			}
		});
		viewer.setInput(new TestElement[] { new TestElement("a"),
				new TestElement("b") });
		// Select equal element with different identity
		TestElement aElement = new TestElement("a");
		viewer.setSelection(new StructuredSelection(aElement));
		StructuredSelection sel = ((StructuredSelection) viewer.getSelection());
		assertEquals(false, sel.isEmpty());
		TestElement selectedElement = (TestElement) sel.getFirstElement();
		assertEquals(aElement.getName(), selectedElement.getName());
	}

	protected void setUp() {
		Policy.setLog(new ILogger() {
			public void log(IStatus status) {
				fail(status.getMessage());
			}
		});
		SafeRunnable.setRunner(new ISafeRunnableRunner() {
			public void run(ISafeRunnable code) {
				try {
					code.run();
				} catch (Throwable th) {
					throw new RuntimeException(th);
				}
			}
		});
		Display display = Display.getCurrent();
		if (display == null) {
			display = new Display();
		}
		shell = new Shell(display);
		shell.setSize(500, 500);
		shell.setLayout(new FillLayout());
		viewer = createViewer(shell);
		shell.open();
	}

	protected void tearDown() {
		processEvents();
		viewer = null;
		if (shell != null) {
			shell.dispose();
			shell = null;
		}
	}

	protected StructuredViewer<TestElement,TestElement[]> createViewer(final Shell parent) {
		return new ComboViewer<TestElement,TestElement[]>(parent, SWT.NONE);
	}

	private void processEvents() {
		if (shell != null && !shell.isDisposed()) {
			Display display = shell.getDisplay();
			if (display != null) {
				while (display.readAndDispatch()) {
					// loop until there are no more events to dispatch
				}
			}
		}
	}
}

Back to the top