25
25
import org .eclipse .jface .viewers .StructuredSelection ;
26
26
import org .eclipse .jface .viewers .StructuredViewer ;
27
27
import org .eclipse .jface .viewers .TableViewer ;
28
+ import org .eclipse .jface .viewers .internal .ExpandableNode ;
28
29
import org .eclipse .swt .SWT ;
29
30
import org .eclipse .swt .graphics .Rectangle ;
30
31
import org .eclipse .swt .widgets .Composite ;
@@ -49,6 +50,7 @@ public void testLimitedItemsCreatedWithExpansionNode() {
49
50
}
50
51
51
52
public void testAddElement () {
53
+ processEvents ();
52
54
Table table = tableViewer .getTable ();
53
55
assertLimitedItems (table .getItems ());
54
56
DataModel data = (DataModel ) table .getItems ()[2 ].getData ();
@@ -73,9 +75,31 @@ public void testAddElement() {
73
75
assertLimitedItems (table .getItems ());
74
76
data = (DataModel ) table .getItems ()[2 ].getData ();
75
77
assertEquals ("wrong item is found at given location" , Integer .valueOf (3 ), data .id );
78
+
79
+ // Add elements one by one
80
+ rootModel = createModel (1 );
81
+ fViewer .setInput (rootModel );
82
+ processEvents ();
83
+
84
+ while (rootModel .size () < VIEWER_LIMIT ) {
85
+ DataModel element = new DataModel (Integer .valueOf (rootModel .size () + 1 ));
86
+ rootModel .add (element );
87
+ tableViewer .add (element );
88
+ processEvents ();
89
+ TableItem [] items = table .getItems ();
90
+ Object last = items [items .length - 1 ].getData ();
91
+ assertFalse ("Last item shouln't be expandable: " + last , tableViewer .isExpandableNode (last ));
92
+ }
93
+
94
+ DataModel element = new DataModel (Integer .valueOf (rootModel .size () + 1 ));
95
+ rootModel .add (element );
96
+ tableViewer .add (element );
97
+ processEvents ();
98
+ assertLimitedItems (table .getItems ());
76
99
}
77
100
78
101
public void testRemoveElement () {
102
+ processEvents ();
79
103
Table table = tableViewer .getTable ();
80
104
assertLimitedItems (table .getItems ());
81
105
DataModel data = (DataModel ) table .getItems ()[2 ].getData ();
@@ -85,19 +109,35 @@ public void testRemoveElement() {
85
109
DataModel removed = rootModel .remove (2 );
86
110
tableViewer .remove (removed );
87
111
processEvents ();
88
- // check items and label after addition .
112
+ // check items and label after removal .
89
113
assertLimitedItems (table .getItems ());
90
114
data = (DataModel ) table .getItems ()[2 ].getData ();
91
115
assertEquals ("wrong item is found at given location" , Integer .valueOf (6 ), data .id );
92
116
93
117
// this element must not be visible only expandable node label must be updated.
94
- DataModel removed1 = rootModel .remove (7 );
95
- tableViewer .remove (removed1 );
118
+ removed = rootModel .remove (7 );
119
+ tableViewer .remove (removed );
96
120
processEvents ();
97
- // check items and label after addition .
121
+ // check items and label after removal .
98
122
assertLimitedItems (table .getItems ());
99
123
data = (DataModel ) table .getItems ()[2 ].getData ();
100
124
assertEquals ("wrong item is found at given location" , Integer .valueOf (6 ), data .id );
125
+
126
+ while (rootModel .size () > VIEWER_LIMIT + 1 ) {
127
+ removed = rootModel .remove (rootModel .size () - 1 );
128
+ tableViewer .remove (removed );
129
+
130
+ processEvents ();
131
+ TableItem [] items = table .getItems ();
132
+ // check items and label after removal.
133
+ if (rootModel .size () > VIEWER_LIMIT + 1 ) {
134
+ assertLimitedItems (items );
135
+ } else {
136
+ Object last = items [items .length - 1 ].getData ();
137
+ assertFalse ("Last item shouln't be expandable: " + last , tableViewer .isExpandableNode (last ));
138
+ }
139
+ }
140
+
101
141
}
102
142
103
143
public void testClickExpandableNode () {
@@ -109,10 +149,10 @@ public void testClickExpandableNode() {
109
149
Item [] itemsBefore = table .getItems ();
110
150
assertEquals ("There are more/less items rendered than viewer limit" , VIEWER_LIMIT * 2 + 1 , itemsBefore .length );
111
151
Item item = itemsBefore [itemsBefore .length - 1 ];
112
- assertTrue ("Last node must be an Expandable Node" , tableViewer .isExpandableNode (item .getData ()));
152
+ Object data = item .getData ();
153
+ assertTrue ("Last node must be an Expandable Node" , tableViewer .isExpandableNode (data ));
113
154
114
- String expected = JFaceResources .format ("ExpandableNode.defaultLabel" , Integer .valueOf (VIEWER_LIMIT * 2 + 1 ),
115
- Integer .valueOf (VIEWER_LIMIT * 3 ), Integer .valueOf (rootModel .size ()));
155
+ String expected = calculateExpandableLabel (data );
116
156
assertEquals ("Expandable node has an incorrect text" , expected , item .getText ());
117
157
118
158
// click until all expandable nodes are expanded.
@@ -220,21 +260,40 @@ public void testRefresh() {
220
260
private void assertLimitedItems (TableItem [] itemsBefore ) {
221
261
assertEquals ("There are more/less items rendered than viewer limit" , VIEWER_LIMIT + 1 , itemsBefore .length );
222
262
TableItem tableItem = itemsBefore [itemsBefore .length - 1 ];
223
- assertTrue ("Last node must be an Expandable Node" , tableViewer .isExpandableNode (tableItem .getData ()));
263
+ Object data = tableItem .getData ();
264
+ assertTrue ("Last node must be an Expandable Node" , tableViewer .isExpandableNode (data ));
224
265
225
- String expectedLabel = JFaceResources .format ("ExpandableNode.defaultLabel" , Integer .valueOf (VIEWER_LIMIT + 1 ),
226
- Integer .valueOf (VIEWER_LIMIT + VIEWER_LIMIT ), Integer .valueOf (rootModel .size ()));
266
+ String expectedLabel = calculateExpandableLabel (data );
227
267
assertEquals ("Expandable node has an incorrect text" , expectedLabel , tableItem .getText ());
228
268
}
229
269
270
+ @ SuppressWarnings ("boxing" )
271
+ private String calculateExpandableLabel (Object data ) {
272
+ ExpandableNode node = (ExpandableNode ) data ;
273
+ int all = rootModel .size ();
274
+ int remaining = all - node .getOffset ();
275
+ String expectedLabel ;
276
+ if (remaining > node .getLimit ()) {
277
+ if (remaining == node .getLimit () + 1 ) {
278
+ String suffix = remaining == 1 ? "" : "s" ; //$NON-NLS-1$ //$NON-NLS-2$
279
+ return JFaceResources .format ("ExpandableNode.showRemaining" , remaining , suffix ); //$NON-NLS-1$ ;
280
+ }
281
+ expectedLabel = JFaceResources .format ("ExpandableNode.defaultLabel" , node .getLimit (), remaining ); //$NON-NLS-1$
282
+ } else {
283
+ String suffix = remaining == 1 ? "" : "s" ; //$NON-NLS-1$
284
+ expectedLabel = JFaceResources .format ("ExpandableNode.showRemaining" , remaining , suffix ); //$NON-NLS-1$
285
+ }
286
+ return expectedLabel ;
287
+ }
288
+
230
289
public void testSetInput () {
231
290
List <DataModel > rootModel = new ArrayList <>();
232
291
DataModel rootLevel = new DataModel (Integer .valueOf (100 ));
233
292
rootModel .add (rootLevel );
234
293
tableViewer .setInput (rootModel );
235
294
processEvents ();
236
295
assertEquals ("there must be only one item" , 1 , tableViewer .getTable ().getItems ().length );
237
- tableViewer .setInput (createModel ());
296
+ tableViewer .setInput (createModel (DEFAULT_ELEMENTS_COUNT ));
238
297
processEvents ();
239
298
assertLimitedItems (tableViewer .getTable ().getItems ());
240
299
}
@@ -309,7 +368,7 @@ protected StructuredViewer createViewer(Composite parent) {
309
368
310
369
@ Override
311
370
protected void setInput () {
312
- rootModel = createModel ();
371
+ rootModel = createModel (DEFAULT_ELEMENTS_COUNT );
313
372
fViewer .setInput (rootModel );
314
373
}
315
374
0 commit comments