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
|
/*******************************************************************************
* Copyright (c) 2008, 2017 Ketan Padegaonkar 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:
* Ketan Padegaonkar - initial API and implementation
* Aparna Argade - Bug 508710
* Kunal Tayal - Bug 516098
*******************************************************************************/
package org.eclipse.swtbot.swt.finder.widgets;
import static org.eclipse.swtbot.swt.finder.SWTBotTestCase.assertSameWidget;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.util.ArrayList;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swtbot.swt.finder.test.AbstractControlExampleTest;
import org.eclipse.swtbot.swt.finder.utils.TableCollection;
import org.eclipse.swtbot.swt.finder.utils.TableRow;
import org.junit.Before;
import org.junit.Test;
/**
* @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com>
* @version $Id$
*/
public class SWTBotTableTest extends AbstractControlExampleTest {
private SWTBotTable table;
@Test
public void findsTable() throws Exception {
assertEquals(Table.class, table.widget.getClass());
}
@Test
public void getsRowCount() throws Exception {
assertEquals(16, table.rowCount());
}
@Test
public void getsColumnCount() throws Exception {
assertEquals(4, table.columnCount());
}
@Test
public void getsAllColumnHeadings() throws Exception {
bot.checkBox("Header Visible").click();
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("Name");
arrayList.add("Type");
arrayList.add("Size");
arrayList.add("Modified");
assertEquals(arrayList, table.columns());
}
@Test
public void getsColumnTextBasedOnRowColumnNumbers() throws Exception {
assertEquals("2556", table.cell(1, 2));
assertEquals("2556", table.cell(4, 2));
assertEquals("2556", table.cell(7, 2));
assertEquals("Index:5", table.cell(5, 0));
assertEquals("tomorrow", table.cell(7, 3));
}
@Test
public void getsTextCellBasedOnRowNumberColumnText() throws Exception {
assertEquals("2556", table.cell(1, "Size"));
assertEquals("2556", table.cell(4, "Size"));
assertEquals("2556", table.cell(7, "Size"));
assertEquals("Index:5", table.cell(5, "Name"));
assertEquals("tomorrow", table.cell(7, "Modified"));
}
@Test
public void getsSelectionCount() throws Exception {
TableCollection selection = table.selection();
assertEquals(new TableCollection(), selection);
assertEquals(0, selection.columnCount());
assertEquals(0, selection.rowCount());
assertEquals(0, table.selectionCount());
}
@Test
public void setsSelection() throws Exception {
table.select(5);
assertEquals(1, table.selectionCount());
}
@Test
public void settingSelectionOnInvalidRowTextThrowsException() throws Exception {
table.select("Index:15");
try {
table.select("Index:16");
fail("Expecting an exception");
} catch (Exception e) {
assertEquals("Could not find item:Index:16 in table", e.getMessage());
}
}
@Test
public void settingSelectionOnInvalidRowNumberThrowsException() throws Exception {
table.select(15);
try {
table.select(16);
fail("Expecting an exception");
} catch (Exception e) {
assertEquals("The row number: 16 does not exist in the table", e.getMessage());
}
}
@Test
public void unselectsSelection() throws Exception {
bot.radio("SWT.MULTI").click();
bot.checkBox("Listen").select();
bot.button("Clear").click();
table = bot.tableInGroup("Table");
table.select(new int[] { 1, 2, 3 });
SWTBotText text = bot.textInGroup("Listeners");
bot.button("Clear").click();
table.unselect();
assertEquals(0, table.selectionCount());
//event.item should contain the item getting unselected, stateMask should reflect CTRL key
assertEventMatches(text, "MouseDown [3]: MouseEvent{Table {} time=0 data=null button=1 stateMask=0x40000 x=0 y=0 count=1}");
assertEventMatches(text, "Selection [13]: SelectionEvent{Table {} time=0 data=null item=TableItem {Index:1} detail=0 x=0 y=0 width=0 height=0 stateMask=0xc0000 text=null doit=true}");
assertEventMatches(text, "MouseUp [4]: MouseEvent{Table {} time=0 data=null button=1 stateMask=0xc0000 x=0 y=0 count=1}");
assertEventMatches(text, "Selection [13]: SelectionEvent{Table {} time=0 data=null item=TableItem {Index:2} detail=0 x=0 y=0 width=0 height=0 stateMask=0xc0000 text=null doit=true}");
assertEventMatches(text, "Selection [13]: SelectionEvent{Table {} time=0 data=null item=TableItem {Index:3} detail=0 x=0 y=0 width=0 height=0 stateMask=0xc0000 text=null doit=true}");
}
@Test
public void setEmptySelection() throws Exception {
bot.radio("SWT.MULTI").click();
table = bot.tableInGroup("Table");
table.select(new int[] { 1, 2, 3 });
assertEquals(3, table.selectionCount());
table.select(new int[] {});
assertEquals(0, table.selectionCount());
table.select(new String[] { "Index:5", "Index:6" });
assertEquals(2, table.selectionCount());
table.select(new String[] {});
assertEquals(0, table.selectionCount());
}
@Test
public void getsSingleSelection() throws Exception {
table.select(10);
TableCollection selection = table.selection();
assertEquals(1, selection.rowCount());
assertEquals(4, selection.columnCount());
assertEquals(new TableCollection().add(new TableRow(new String[] { "Index:10", "databases", "2556", "tomorrow" })), selection);
}
@Test
public void findsAnyTable() throws Exception {
assertSameWidget(table.widget, bot.table().widget);
}
@Test
public void getsMultipleSingleSelection() throws Exception {
bot.radio("SWT.MULTI").click();
bot.checkBox("Listen").select();
bot.button("Clear").click();
table = bot.tableInGroup("Table");
table.select(new int[] { 5, 10 });
TableCollection selection = table.selection();
assertEquals("Index:5", selection.get(0, 0));
assertEquals("images", selection.get(0, 1));
assertEquals("91571", selection.get(0, 2));
assertEquals("yesterday", selection.get(0, 3));
assertEquals(new TableRow(new String[] { "Index:10", "databases", "2556", "tomorrow" }), selection.get(1));
SWTBotText text = bot.textInGroup("Listeners");
// first selection: event.item should contain the first selected item,
// stateMask should not reflect CTRL key
assertEventMatches(text, "MouseDown [3]: MouseEvent{Table {} time=0 data=null button=1 stateMask=0x0 x=0 y=0 count=1}");
assertEventMatches(text, "Selection [13]: SelectionEvent{Table {} time=0 data=null item=TableItem {Index:5} detail=0 x=0 y=0 width=0 height=0 stateMask=0x80000 text=null doit=true}");
assertEventMatches(text, "MouseUp [4]: MouseEvent{Table {} time=0 data=null button=1 stateMask=0x80000 x=0 y=0 count=1}");
// subsequent selection: event.item should contain the second selected item,
//stateMask should reflect CTRL key
assertEventMatches(text, "MouseDown [3]: MouseEvent{Table {} time=0 data=null button=1 stateMask=0x40000 x=0 y=0 count=1}");
assertEventMatches(text, "Selection [13]: SelectionEvent{Table {} time=0 data=null item=TableItem {Index:10} detail=0 x=0 y=0 width=0 height=0 stateMask=0xc0000 text=null doit=true}");
assertEventMatches(text, "MouseUp [4]: MouseEvent{Table {} time=0 data=null button=1 stateMask=0xc0000 x=0 y=0 count=1}");
}
@Test
public void settingMultipleSelectionOfInvalidIndicesThrowsException() throws Exception {
bot.radio("SWT.MULTI").click();
table = bot.tableInGroup("Table");
table.select(new int[] { 4, 15, 6 });
try {
table.select(new int[] { 4, 16, 6 });
fail("Expecting an exception");
} catch (Exception e) {
assertEquals("The row number: 16 does not exist in the table", e.getMessage());
}
}
@Test
public void throwsExceptionIfTheRowNumberIsIllegal() throws Exception {
try {
table.cell(100, 1);
fail("Was expecting an exception");
} catch (Exception e) {
assertEquals("The row number (100) is more than the number of rows(16) in the table.", e.getMessage());
}
}
@Test
public void throwsExceptionIfTheRowNumberIsIllegal2() throws Exception {
try {
table.cell(100, "Size");
fail("Was expecting an exception");
} catch (Exception e) {
assertEquals("The row number (100) is more than the number of rows(16) in the table.", e.getMessage());
}
}
@Test
public void throwsExceptionIfTheColumnNumberIsIllegal() throws Exception {
try {
table.cell(1, 100);
fail("Was expecting an exception");
} catch (Exception e) {
assertEquals("The column number (100) is more than the number of column(4) in the table.", e.getMessage());
}
}
@Test
public void throwsExceptionIfMultipleIndicesOnSingleSelect() throws Exception {
try {
table.select(new int[] {1,2,3});
fail("Was expecting an exception");
} catch (Exception e) {
assertEquals("Table does not support multi selection.", e.getMessage());
}
}
@Test
public void throwsExceptionIfTheColumnNameIsIllegal() throws Exception {
try {
table.cell(1, "some column");
fail("Was expecting an exception");
} catch (Exception e) {
assertEquals("The column `some column' is not found.", e.getMessage());
}
}
@Test
public void getsIndexOfItem() throws Exception {
assertEquals(7, table.indexOf("Index:7"));
assertEquals(-1, table.indexOf("non existent item"));
}
@Test
public void getsIndexOfItemInAColumnWithIndex() throws Exception {
assertEquals(7, table.indexOf("Index:7", 0));
assertEquals(1, table.indexOf("2556", 2));
assertEquals(-1, table.indexOf("non existent item", 3));
}
@Test
public void getsIndexOfItemInAColumnWithText() throws Exception {
bot.radio("SWT.MULTI").click();
table = bot.tableInGroup("Table");
assertEquals(7, table.indexOf("Index:7", "Name"));
assertEquals(1, table.indexOf("2556", "Size"));
assertEquals(-1, table.indexOf("non existent item", "foo"));
}
@Test
public void findsIndexOfColumn() throws Exception {
bot.radio("SWT.MULTI").click();
table = bot.tableInGroup("Table");
assertEquals(0, table.indexOfColumn("Name"));
assertEquals(2, table.indexOfColumn("Size"));
}
@Test
public void selectsItemsBasedOnString() throws Exception {
table.select("Index:9");
TableCollection selection = table.selection();
assertEquals("Index:9", selection.get(0, 0));
}
@Test
public void findsItemsBasedOnIndex() throws Exception {
SWTBotTableItem tableItem = table.getTableItem(3);
assertEquals("Index:3", tableItem.getText());
}
@Test
public void findsItemsBasedOnText() throws Exception {
SWTBotTableItem tableItem = table.getTableItem("Index:3");
assertEquals("Index:3", tableItem.getText());
}
@Test
public void searchText() throws Exception {
assertEquals(15, table.searchText("Index:15"));
assertEquals(1, table.searchText("databases"));
assertEquals(2, table.searchText("91571"));
assertEquals(0, table.searchText("today"));
assertEquals(-1, table.searchText("non existent item"));
}
@Test
public void containsText() throws Exception {
assertEquals(true, table.containsText("Index:7"));
assertEquals(true, table.containsText("images"));
assertEquals(true, table.containsText("2556"));
assertEquals(true, table.containsText("tomorrow"));
assertEquals(false, table.containsText("non existent item"));
}
@Before
public void prepareExample() throws Exception {
bot.tabItem("Table").activate();
table = bot.tableInGroup("Table");
}
}
|