Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2006 Brad Reynolds 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 |
* Brad Reynolds - initial API and implementation |
10 |
******************************************************************************/ |
11 |
|
12 |
package org.eclipse.jface.tests.internal.databinding.provisional.viewers; |
13 |
|
14 |
import junit.framework.TestCase; |
15 |
|
16 |
import org.eclipse.jface.examples.databinding.model.Adventure; |
17 |
import org.eclipse.jface.examples.databinding.model.Catalog; |
18 |
import org.eclipse.jface.examples.databinding.model.Category; |
19 |
import org.eclipse.jface.examples.databinding.model.Lodging; |
20 |
import org.eclipse.jface.examples.databinding.model.SampleData; |
21 |
import org.eclipse.jface.internal.databinding.provisional.Binding; |
22 |
import org.eclipse.jface.internal.databinding.provisional.DataBindingContext; |
23 |
import org.eclipse.jface.internal.databinding.provisional.description.Property; |
24 |
import org.eclipse.jface.internal.databinding.provisional.description.TableModelDescription; |
25 |
import org.eclipse.jface.internal.databinding.provisional.viewers.TableViewerEditorManager; |
26 |
import org.eclipse.jface.viewers.CellEditor; |
27 |
import org.eclipse.jface.viewers.CheckboxCellEditor; |
28 |
import org.eclipse.jface.viewers.ICellModifier; |
29 |
import org.eclipse.jface.viewers.TableViewer; |
30 |
import org.eclipse.jface.viewers.TextCellEditor; |
31 |
import org.eclipse.swt.SWT; |
32 |
import org.eclipse.swt.layout.RowLayout; |
33 |
import org.eclipse.swt.widgets.Shell; |
34 |
|
35 |
/** |
36 |
* @since 3.2 |
37 |
*/ |
38 |
public class TableViewerEditorManagerTest extends TestCase { |
39 |
private Shell shell; |
40 |
|
41 |
private TableViewer viewer; |
42 |
|
43 |
private DataBindingContext context; |
44 |
|
45 |
private TableModelDescription description; |
46 |
|
47 |
private TableViewerEditorManager manager; |
48 |
|
49 |
private Catalog model; |
50 |
|
51 |
protected void setUp() throws Exception { |
52 |
shell = new Shell(); |
53 |
shell.setLayout(new RowLayout()); |
54 |
|
55 |
viewer = new TableViewer(shell, SWT.NONE); |
56 |
context = SampleData.getDatabindingContext(shell); |
57 |
model = SampleData.CATALOG_2005; |
58 |
|
59 |
description = new TableModelDescription(new Property(model, |
60 |
"lodgings", Lodging.class, Boolean.TRUE), |
61 |
new String[] { "name", "description" }); |
62 |
|
63 |
context.bind(viewer, description, null); |
64 |
|
65 |
manager = new TableViewerEditorManager(viewer, description.getColumnIDs(), context); |
66 |
} |
67 |
|
68 |
/* |
69 |
* (non-Javadoc) |
70 |
* |
71 |
* @see junit.framework.TestCase#tearDown() |
72 |
*/ |
73 |
protected void tearDown() throws Exception { |
74 |
if (shell != null && !shell.isDisposed()) { |
75 |
shell.dispose(); |
76 |
shell = null; |
77 |
} |
78 |
} |
79 |
|
80 |
public void testCreateControlIfItDoesntExist() throws Exception { |
81 |
TextCellEditor editor = new TextCellEditor(); |
82 |
|
83 |
manager.setEditor(editor, "name", String.class, null); |
84 |
assertNotNull(viewer.getCellEditors()[0]); |
85 |
} |
86 |
|
87 |
public void testSetupViewerForEditing() throws Exception { |
88 |
assertNotNull(viewer.getCellModifier()); |
89 |
|
90 |
assertNotNull(viewer.getColumnProperties()); |
91 |
assertEquals(2, viewer.getColumnProperties().length); |
92 |
assertNotNull(viewer.getColumnProperties()[0]); |
93 |
assertEquals("name", viewer.getColumnProperties()[0]); |
94 |
|
95 |
assertNotNull(viewer.getCellEditors()); |
96 |
assertEquals(2, viewer.getCellEditors().length); |
97 |
assertNull(viewer.getCellEditors()[0]); |
98 |
|
99 |
TextCellEditor editor = new TextCellEditor(); |
100 |
manager.setEditor(editor, "name", String.class, null); |
101 |
|
102 |
assertEquals(editor, viewer.getCellEditors()[0]); |
103 |
} |
104 |
|
105 |
public void testSetValueInTextCellEditorOnEdit() throws Exception { |
106 |
TextCellEditor editor = new TextCellEditor(); |
107 |
manager.setEditor(editor, "name", String.class, null); |
108 |
|
109 |
viewer.editElement(model.getLodgings()[0], 0); |
110 |
|
111 |
Lodging lodging = model.getLodgings()[0]; |
112 |
assertEquals(lodging.getName(), editor.getValue()); |
113 |
} |
114 |
|
115 |
public void testUpdateModelFromTextCellEditorAfterEdit() throws Exception { |
116 |
TextCellEditor editor = new TextCellEditor(); |
117 |
manager.setEditor(editor, "name", String.class, null); |
118 |
|
119 |
Lodging lodging = model.getLodgings()[0]; |
120 |
viewer.editElement(lodging, 0); |
121 |
|
122 |
assertTrue(editor.isActivated()); |
123 |
|
124 |
String newValue = lodging.getName() + " new"; |
125 |
editor.setValue(newValue); |
126 |
|
127 |
editor.getControl().notifyListeners(SWT.FocusOut, null); |
128 |
|
129 |
assertFalse(editor.isActivated()); |
130 |
assertEquals(newValue, lodging.getName()); |
131 |
} |
132 |
|
133 |
public void testToggleCheckboxCellEditorValueOnEdit() throws Exception { |
134 |
Category model = SampleData.WINTER_CATEGORY; |
135 |
description = new TableModelDescription(new Property(model, |
136 |
"adventures", Adventure.class, Boolean.TRUE), |
137 |
new String[] { "petsAllowed" }); |
138 |
|
139 |
context.bind(viewer, description, null); |
140 |
|
141 |
manager = new TableViewerEditorManager(viewer, description.getColumnIDs(), context); |
142 |
|
143 |
CheckboxCellEditor editor = new CheckboxCellEditor(); |
144 |
manager.setEditor(editor, "petsAllowed", boolean.class, null); |
145 |
|
146 |
Adventure adventure = model.getAdventures()[0]; |
147 |
boolean old = adventure.isPetsAllowed(); |
148 |
|
149 |
viewer.editElement(adventure, 0); |
150 |
|
151 |
assertEquals(!old, adventure.isPetsAllowed()); |
152 |
} |
153 |
|
154 |
public void testRemoveEditor() throws Exception { |
155 |
TextCellEditor editor = new TextCellEditor(); |
156 |
Binding binding = manager.setEditor(editor, "name", String.class, null); |
157 |
assertNotNull(binding); |
158 |
assertFalse(binding.isDisposed()); |
159 |
|
160 |
ICellModifier cellModifier = viewer.getCellModifier(); |
161 |
Lodging lodging = model.getLodgings()[0]; |
162 |
assertTrue(cellModifier.canModify(lodging, "name")); |
163 |
|
164 |
manager.setEditor(null, "name", String.class, null); |
165 |
|
166 |
assertTrue(binding.isDisposed()); |
167 |
assertFalse(cellModifier.canModify(lodging, "name")); |
168 |
} |
169 |
|
170 |
public void testSetEditorForExistingEditableColumn() throws Exception { |
171 |
TextCellEditor firstEditor = new TextCellEditor(); |
172 |
Binding firstBinding = manager.setEditor(firstEditor, "name", String.class, null); |
173 |
assertFalse(firstBinding.isDisposed()); |
174 |
|
175 |
TextCellEditor secondEditor = new TextCellEditor(); |
176 |
Binding secondBinding = manager.setEditor(secondEditor, "name", String.class, null); |
177 |
assertTrue(firstBinding.isDisposed()); |
178 |
assertFalse(secondBinding.isDisposed()); |
179 |
|
180 |
ICellModifier cellModifier = viewer.getCellModifier(); |
181 |
Lodging lodging = model.getLodgings()[0]; |
182 |
assertTrue(cellModifier.canModify(lodging, "name")); |
183 |
} |
184 |
|
185 |
public void testSetEditorForInvalidColumnID() throws Exception { |
186 |
try { |
187 |
TextCellEditor editor = new TextCellEditor(); |
188 |
manager.setEditor(editor, "bogus ID", String.class, null); |
189 |
fail("exception should have been thrown"); |
190 |
} catch (IllegalArgumentException e) { |
191 |
} |
192 |
} |
193 |
|
194 |
public void testSetNullColumnID() throws Exception { |
195 |
try { |
196 |
TextCellEditor editor = new TextCellEditor(); |
197 |
manager.setEditor(editor, null, String.class, null); |
198 |
} catch (IllegalArgumentException e) { |
199 |
} |
200 |
} |
201 |
|
202 |
public void testSetNullPropertyType() throws Exception { |
203 |
try { |
204 |
TextCellEditor editor = new TextCellEditor(); |
205 |
manager.setEditor(editor, "name", null, null); |
206 |
} catch (IllegalArgumentException e) { |
207 |
} |
208 |
} |
209 |
|
210 |
public void testCellModifierOnlyAllowsEditingForColumnsWithAnEditor() throws Exception { |
211 |
ICellModifier modifier = viewer.getCellModifier(); |
212 |
Lodging lodging = model.getLodgings()[0]; |
213 |
|
214 |
TextCellEditor editor = new TextCellEditor(); |
215 |
manager.setEditor(editor, "name", String.class, null); |
216 |
|
217 |
assertTrue(modifier.canModify(lodging, "name")); |
218 |
assertFalse(modifier.canModify(lodging, "description")); |
219 |
} |
220 |
|
221 |
public void testConstructWithNullTableViewer() throws Exception { |
222 |
try { |
223 |
manager = new TableViewerEditorManager(null, description.getColumnIDs(), context); |
224 |
fail("exception should have been thrown"); |
225 |
} catch (IllegalArgumentException e) { |
226 |
} |
227 |
} |
228 |
|
229 |
public void testConstructWithNullTableModelDescription() throws Exception { |
230 |
try { |
231 |
manager = new TableViewerEditorManager(viewer, null, context); |
232 |
fail("exception should have been thrown"); |
233 |
} catch (IllegalArgumentException e) { |
234 |
} |
235 |
} |
236 |
|
237 |
public void testConstructWithNullDataBindingContext() throws Exception { |
238 |
try { |
239 |
manager = new TableViewerEditorManager(viewer, description.getColumnIDs(), null); |
240 |
fail("exception should have been thrown"); |
241 |
} catch (IllegalArgumentException e) { |
242 |
} |
243 |
} |
244 |
|
245 |
public void testGetEditor() throws Exception { |
246 |
assertNull(manager.getEditor("name")); |
247 |
|
248 |
TextCellEditor editor = new TextCellEditor(); |
249 |
manager.setEditor(editor, "name", String.class, null); |
250 |
|
251 |
CellEditor nameEditor = manager.getEditor("name"); |
252 |
assertNotNull(nameEditor); |
253 |
assertEquals(editor, nameEditor); |
254 |
} |
255 |
|
256 |
public void testGetInvalidEditor() throws Exception { |
257 |
try { |
258 |
manager.getEditor("column ID that does not exist"); |
259 |
fail("exception should have been thrown"); |
260 |
} catch (IllegalArgumentException e) { |
261 |
} |
262 |
} |
263 |
|
264 |
public void testGetBinding() throws Exception { |
265 |
assertNull(manager.getBinding("name")); |
266 |
|
267 |
TextCellEditor editor = new TextCellEditor(); |
268 |
Binding binding = manager.setEditor(editor, "name", String.class, null); |
269 |
|
270 |
Binding nameBinding = manager.getBinding("name"); |
271 |
assertNotNull(nameBinding); |
272 |
assertEquals(binding, nameBinding); |
273 |
} |
274 |
|
275 |
public void testGetInvalidBinding() throws Exception { |
276 |
try { |
277 |
manager.getBinding("column ID that does not exist"); |
278 |
fail("exception should have been thrown"); |
279 |
} catch (IllegalArgumentException e) { |
280 |
} |
281 |
} |
282 |
} |