-
Notifications
You must be signed in to change notification settings - Fork 0
/
EmfParsleyEditableTableTests.java
394 lines (335 loc) · 12.3 KB
/
EmfParsleyEditableTableTests.java
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*******************************************************************************
* Copyright (c) 2015 RCP Vision (http://www.rcp-vision.com) 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:
* Lorenzo Bettini - Initial contribution and API
*******************************************************************************/
package org.eclipse.emf.parsley.tests.swtbot;
import static org.eclipse.swtbot.swt.finder.waits.Conditions.shellCloses;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.eclipse.emf.parsley.tests.models.testmodels.EnumForControls;
import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotCCombo;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotCheckBox;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTable;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
/**
* @author Lorenzo Bettini
*
*/
@RunWith(SWTBotJunit4ClassRunner.class)
public class EmfParsleyEditableTableTests extends EmfParsleySWTBotAbstractTests {
private static final String MULTI_REFERENCE_FEATURE_LABEL = "Multi Reference Feature";
private static final String CLASS_WITH_NAME_LABEL = "Class With Name";
private static final String SECOND_CLASS_WITH_NAME = CLASS_WITH_NAME_LABEL + " Second Class With Name";
private static final int ROW = 0;
// the indexes must correspond to the order of the features specified in the
// module.parsley of the tests.views project
private static final int BOOLEAN_FEATURE = 0;
private static final int BOOLEAN_OBJECT_FEATURE = 1;
private static final int BOOLEAN_DATATYPE_FEATURE = 2;
private static final int BOOLEAN_PRIMITIVE_DATATYPE_FEATURE = 3;
private static final int ENUM_FEATURE = 4;
private static final int STRING_FEATURE = 5;
private static final int STRING_DATATYPE_FEATURE = 6;
private static final int STRING_WITH_DEFAULT_FEATURE = 7;
private static final int BIG_DECIMAL_FEATURE = 8;
private static final int BIG_INTEGER_FEATURE = 9;
private static final int DOUBLE_FEATURE = 10;
private static final int DOUBLE_OBJECT_FEATURE = 11;
private static final int INT_FEATURE = 12;
private static final int INT_OBJECT_FEATURE = 13;
private static final int FLOAT_FEATURE = 14;
private static final int FLOAT_OBJECT_FEATURE = 15;
private static final int SHORT_FEATURE = 16;
private static final int SHORT_OBJECT_FEATURE = 17;
private static final int BYTE_FEATURE = 18;
private static final int BYTE_OBJECT_FEATURE = 19;
private static final int DATE_FEATURE = 20;
private static final int SINGLE_REFERENCE_FEATURE = 21;
private static final int MULTI_REFERENCE_FEATURE = 22;
private SWTBotTable table;
@Before
public void setupProject() {
createMyTestProject();
table = openTestEditableTableView();
}
@After
public void closeView() {
closeView(TEST_MODEL_EDITABLE_TABLE_VIEW);
}
@Test
public void testBooleanCell() {
clickBooleanCell(BOOLEAN_FEATURE, "false");
}
@Test
public void testBooleanObjectCell() {
clickBooleanCell(BOOLEAN_OBJECT_FEATURE, "");
}
@Test
public void testBooleanDataTypeCell() {
clickBooleanCell(BOOLEAN_DATATYPE_FEATURE, "");
}
@Test
public void testBooleanPrimitiveDataTypeCell() {
clickBooleanCell(BOOLEAN_PRIMITIVE_DATATYPE_FEATURE, "false");
}
@Test
public void testEnumCell() {
clickComboCell(ENUM_FEATURE, EnumForControls.get(EnumForControls.FIRST_VALUE).toString(),
EnumForControls.get(EnumForControls.SECOND_VALUE).toString());
}
@Test
public void testStringCell() {
clickStringCell(STRING_FEATURE, "", "a new value");
}
@Test
public void testStringDatatypeCell() {
clickStringCell(STRING_DATATYPE_FEATURE, "", "a new value");
}
@Test
public void testStringWithDefaultCell() {
clickStringCell(STRING_WITH_DEFAULT_FEATURE, "default", "a new value");
}
@Test
public void testStringCellWithoutModification() {
clickStringCellWithoutModification(STRING_WITH_DEFAULT_FEATURE, "default");
}
@Test
public void testBigDecimalCell() {
clickStringCell(BIG_DECIMAL_FEATURE, "", "100.9");
}
@Test
public void testBigIntegerCell() {
clickStringCell(BIG_INTEGER_FEATURE, "", "100");
}
@Test
public void testDoubleCell() {
clickStringCell(DOUBLE_FEATURE, "0.0", "100", "100.0");
}
@Test
public void testDoubleObjectCell() {
clickStringCell(DOUBLE_OBJECT_FEATURE, "", "100", "100.0");
}
@Test
public void testIntCell() {
clickStringCell(INT_FEATURE, "0", "100");
}
@Test
public void testIntObjectCell() {
clickStringCell(INT_OBJECT_FEATURE, "", "100");
}
@Test
public void testFloatCell() {
clickStringCell(FLOAT_FEATURE, "0.0", "100", "100.0");
}
@Test
public void testFloatObjectCell() {
clickStringCell(FLOAT_OBJECT_FEATURE, "", "100", "100.0");
}
@Test
public void testShortCell() {
clickStringCell(SHORT_FEATURE, "0", "100");
}
@Test
public void testShortObjectCell() {
clickStringCell(SHORT_OBJECT_FEATURE, "", "100");
}
@Test
public void testByteCell() {
clickStringCell(BYTE_FEATURE, "0", "100");
}
@Test
public void testByteObjectCell() {
clickStringCell(BYTE_OBJECT_FEATURE, "", "100");
}
@Test
public void testDateCell() {
int column = DATE_FEATURE;
String originalValue = "";
String newValue = "2000-11-11";
// don't specify the whole effective new value:
// it might contain CET or EST depending on the system
String effectiveNewValue = "Sat Nov 11 00:00:00";
table.click(ROW, column);
bot.text(originalValue).setText(newValue);
// we leave the cell by clicking on the enum cell
leaveEditingCellClickingOnAnotherCell(STRING_FEATURE);
String cellContents = table.cell(ROW, column);
// just assert string containment for the reason above
assertTrue(
effectiveNewValue + " not found in " + cellContents,
cellContents.contains(effectiveNewValue));
assertDirtyAndSave();
undo("Set");
cellContents = table.cell(ROW, column);
assertEquals(originalValue, cellContents);
assertDirtyAndSave();
redo("Set");
cellContents = table.cell(ROW, column);
assertTrue(
effectiveNewValue + " not found in " + cellContents,
cellContents.contains(effectiveNewValue));
assertDirtyAndSave();
}
@Test
public void testDateCellWithWrongDate() {
table.click(ROW, DATE_FEATURE);
bot.text("").setText("2000");
// we leave the cell by clicking on the enum cell
leaveEditingCellClickingOnAnotherCell(STRING_FEATURE);
// the date was not valid so the original value has not changed
assertEquals("", table.cell(ROW, DATE_FEATURE));
assertSaveableViewIsDirty(false, TEST_MODEL_EDITABLE_TABLE_VIEW);
}
@Test
public void testSingleReferenceCell() {
clickComboCell(SINGLE_REFERENCE_FEATURE, "", SECOND_CLASS_WITH_NAME);
}
@Test
public void testMultiReferenceCell() {
if (isIndigo())
return; // this test does not seem to work when building against Indigo
clickMultiReferenceCell(MULTI_REFERENCE_FEATURE, CLASS_FOR_CONTROLS_LABEL, "", SECOND_CLASS_WITH_NAME);
}
/**
* Uses the combo to select true for the boolean feature
*
* @param column
* @param originalValue
*/
private void clickBooleanCell(int column, String originalValue) {
if (getOrgEclipseEmfEditUiMinorVersion() < 14) {
clickComboCell(column, originalValue, "true");
} else {
// In EMF 2.14 for booleans we have checkboxes instead of
// combo box
table.click(ROW, column);
SWTBotCheckBox checkBox = bot.checkBox();
// we always start from an unchecked checkbox
assertFalse("should not be checked", checkBox.isChecked());
checkBox.click(); // checked
leaveEditingCellClickingOnAnotherCell(STRING_FEATURE);
assertDirtyAndSave();
undo("Set");
assertEquals(originalValue, table.cell(ROW, column));
assertDirtyAndSave();
redo("Set");
// for Boolean objects (not primitive boolean) or
// Boolean datatype we have 3 state checkbox:
// null, false, true
// for Boolean and Boolean datatype we pass an empty originalValue
// and redoing leads to "false" (instead of "true"),
// since we redo one state (different from clicking?)
assertEquals(originalValue.isEmpty() ? "false" : "true", table.cell(ROW, column));
assertDirtyAndSave();
}
}
private void clickComboCell(int column, String firstValue, String secondValue) {
table.click(ROW, column);
if (getOrgEclipseEmfEditUiMinorVersion() >= 14) {
// in EMF 2.14 the combo box automatically shows the dropdown list
// as soon as the cell is selected, see
// org.eclipse.emf.common.ui.celleditor.ExtendedComboBoxCellEditor.ExtendedComboBoxCellEditor(Composite, List<?>, ILabelProvider, boolean, int, ValueHandler, boolean)
// so we need to close the list window, otherwise the original combo
// cannot be found by SWTBot
bot.activeShell().close();
}
final SWTBotCCombo ccomboBox = bot.ccomboBox(firstValue);
ccomboBox.setSelection(secondValue);
leaveEditingCellClickingOnAnotherCell(STRING_FEATURE);
assertDirtyAndSave();
undo("Set");
assertEquals(firstValue, table.cell(ROW, column));
assertDirtyAndSave();
redo("Set");
assertEquals(secondValue, table.cell(ROW, column));
assertDirtyAndSave();
}
private void clickStringCell(int column, String originalValue, String newValue) {
clickStringCell(column, originalValue, newValue, newValue);
}
/**
* After the insertion of a new value, the effective value might be different
* depending on the type, e.g., setting 100 for float will result in an effective
* 100.0.
*
* @param column
* @param originalValue
* @param newValue
* @param effectiveNewValue
*/
private void clickStringCell(int column, String originalValue, String newValue, String effectiveNewValue) {
table.click(ROW, column);
bot.text(originalValue).setText(newValue);
// we leave the cell by clicking on the enum cell
leaveEditingCellClickingOnAnotherCell(INT_FEATURE);
assertEquals(effectiveNewValue, table.cell(ROW, column));
assertDirtyAndSave();
undo("Set");
assertEquals(originalValue, table.cell(ROW, column));
assertDirtyAndSave();
redo("Set");
assertEquals(effectiveNewValue, table.cell(ROW, column));
assertDirtyAndSave();
}
private void clickStringCellWithoutModification(int column, String value) {
table.click(ROW, column);
bot.text(value).setText(value);
// we leave the cell by clicking on the enum cell
leaveEditingCellClickingOnAnotherCell(INT_FEATURE);
assertEquals(value, table.cell(ROW, column));
assertSaveableViewIsDirty(false, TEST_MODEL_EDITABLE_TABLE_VIEW);
}
private void clickMultiReferenceCell(int column, String containerName, String originalValue, String newValue) {
table.click(ROW, column);
// The following is not required and would fail in 2019-12
// bot.text(originalValue);
// TODO: only when originalValue is empty?
bot.button("...").click();
SWTBotShell shell = bot.shell(MULTI_REFERENCE_FEATURE_LABEL + " -- " + containerName);
shell.activate();
// add a reference in the dialog
bot.table(0).select(newValue); // left table
bot.button("Add").click();
bot.button("OK").click();
bot.waitUntil(shellCloses(shell), SWTBotPreferences.TIMEOUT);
// we leave the cell by clicking on the enum cell
leaveEditingCellClickingOnAnotherCell(STRING_FEATURE);
assertEquals(newValue, table.cell(ROW, column));
assertDirtyAndSave();
undo("Set");
assertEquals(originalValue, table.cell(ROW, column));
assertDirtyAndSave();
redo("Set");
assertEquals(newValue, table.cell(ROW, column));
assertDirtyAndSave();
}
private void assertDirtyAndSave() {
assertDirtyThenSaveAndAssertNotDirty(TEST_MODEL_EDITABLE_TABLE_VIEW);
}
/**
* So that the model is updated
* @param column you must make sure you specify a column which is different
* from the one being tested and that does not fire another model update
*/
private void leaveEditingCellClickingOnAnotherCell(int column) {
// we need to click on another cell so that the model is updated
table.click(ROW, column);
}
protected SWTBotTable openTestEditableTableView() {
openTestView(TEST_MODEL_EDITABLE_TABLE_VIEW);
return bot.table();
}
}