aboutsummaryrefslogtreecommitdiffstats
blob: 5c9cc82c34d7c1313661f405459d26ad73830900 (plain)
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
/*******************************************************************************
 * Copyright (c) 2000, 2006 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jface.tests.viewers;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.viewers.AbstractTreeViewer;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Item;
import org.eclipse.swt.widgets.Widget;

public abstract class AbstractTreeViewerTest extends StructuredItemViewerTest {

    AbstractTreeViewer<TestElement,TestElement> fTreeViewer;

    public AbstractTreeViewerTest(String name) {
        super(name);
    }

    protected void assertEqualsArray(String s, Object[] a1, Object[] a2) {
        int s1 = a1.length;
        int s2 = a2.length;
        assertEquals(s, s1, s2);
        for (int i = 0; i < s1; i++) {
            assertEquals(s, a1[i], a2[i]);
        }
    }

	protected void assertSelectionEquals(String message, TestElement expected) {
	    ISelection selection = fViewer.getSelection();
	    assertTrue(selection instanceof IStructuredSelection);
	    List<TestElement> expectedList = new ArrayList<TestElement>();
	    expectedList.add(expected);
	    IStructuredSelection structuredSelection = (IStructuredSelection)selection;
	    assertEquals("selectionEquals - " + message, expectedList, (structuredSelection).toList());
	}

    protected abstract int getItemCount(TestElement element); //was IElement

    public void testBulkExpand() {
        // navigate
        TestElement first = fRootElement.getFirstChild();
        TestElement first2 = first.getFirstChild();
        TestElement last = fRootElement.getLastChild();

        // expand a few nodes
        fTreeViewer.expandToLevel(first, 2);
        fTreeViewer.expandToLevel(first2, 2);
        fTreeViewer.expandToLevel(last, 2);
        // get expand state
        Object[] list1 = fTreeViewer.getExpandedElements();
        // flush viewer
        setInput();
        processEvents();

        // restore old expand state
        fTreeViewer.collapseAll();
        fTreeViewer.expandToLevel(first, 2);
        fTreeViewer.expandToLevel(first2, 2);
        fTreeViewer.expandToLevel(last, 2);

        Object[] list2 = fTreeViewer.getExpandedElements();

        assertEqualsArray("old and new expand state are the same", list1, list2);
    }

    public void testDeleteChildExpanded() {
        TestElement first = fRootElement.getFirstChild();
        TestElement first2 = first.getFirstChild();
        fTreeViewer.expandToLevel(first2, 0);

        assertNotNull("first child is visible", fViewer.testFindItem(first2));
        first.deleteChild(first2);
        assertNull("first child is not visible", fViewer.testFindItem(first2));
    }

    public void testDeleteChildren() {
        TestElement first = fRootElement.getFirstChild();
        first.deleteChildren();
        assertTrue("no children", getItemCount(first) == 0);
    }

    public void testDeleteChildrenExpanded() {
        TestElement first = fRootElement.getFirstChild();
        TestElement first2 = first.getFirstChild();
        fTreeViewer.expandToLevel(first2, 0);
        assertNotNull("first child is visible", fViewer.testFindItem(first2));

        first.deleteChildren();
        assertTrue("no children", getItemCount(first) == 0);
    }

    public void testExpand() {
        TestElement first = fRootElement.getFirstChild();
        TestElement first2 = first.getFirstChild();
        assertNull("first child is not visible", fViewer.testFindItem(first2));
        fTreeViewer.expandToLevel(first2, 0);
        assertNotNull("first child is visible", fViewer.testFindItem(first2));
    }

    public void testExpandElement() {
        TestElement first = fRootElement.getFirstChild();
        TestElement first2 = first.getFirstChild();
        TestElement first3 = first2.getFirstChild();
        fTreeViewer.expandToLevel(first3, 0);
        assertNotNull("first3 is visible", fViewer.testFindItem(first3));
        assertNotNull("first2 is visible", fViewer.testFindItem(first2));
    }

    public void testExpandToLevel() {
        TestElement first = fRootElement.getFirstChild();
        TestElement first2 = first.getFirstChild();
        TestElement first3 = first2.getFirstChild();
        fTreeViewer.expandToLevel(3);
        assertNotNull("first2 is visible", fViewer.testFindItem(first2));
        assertNotNull("first3 is visible", fViewer.testFindItem(first3));
    }

    public void testFilterExpanded() {
        TestElement first = fRootElement.getFirstChild();
        TestElement first2 = first.getFirstChild();
        fTreeViewer.expandToLevel(first2, 0);

        fTreeViewer.addFilter(new TestLabelFilter());
        assertTrue("filtered count", getItemCount() == 5);
    }

    public void testInsertChildReveal() {
        TestElement first = fRootElement.getFirstChild();
        TestElement newElement = first.addChild(TestModelChange.INSERT
                | TestModelChange.REVEAL);
        assertNotNull("new sibling is visible", fViewer
                .testFindItem(newElement));
    }

    public void testInsertChildRevealSelect() {
        TestElement last = fRootElement.getLastChild();
        TestElement newElement = last.addChild(TestModelChange.INSERT
                | TestModelChange.REVEAL | TestModelChange.SELECT);
        assertNotNull("new sibling is visible", fViewer
                .testFindItem(newElement));
        assertSelectionEquals("new element is selected", newElement);
    }

    public void testInsertChildRevealSelectExpanded() {
        TestElement first = fRootElement.getFirstChild();
        TestElement newElement = first.addChild(TestModelChange.INSERT
                | TestModelChange.REVEAL | TestModelChange.SELECT);
        assertNotNull("new sibling is visible", fViewer
                .testFindItem(newElement));
        assertSelectionEquals("new element is selected", newElement);
    }

    /**
     * Regression test for 1GDN0PX: ITPUI:WIN2000 - SEVERE  - AssertionFailure when expanding Navigator
     * Problem was:
     *   - before addition, parent item had no children, and was expanded
     *   - after addition, during refresh(), updatePlus() added dummy node even though parent item was expanded
     *   - in updateChildren, it wasn't handling a dummy node
     */
    public void testRefreshWithAddedChildren() {
        TestElement parent = fRootElement.addChild(TestModelChange.INSERT);
        TestElement child = parent.addChild(TestModelChange.INSERT);
        ((AbstractTreeViewer<TestElement,TestElement>) fViewer).setExpandedState(parent, true);
        parent.deleteChild(child);
        child = parent.addChild(TestModelChange.STRUCTURE_CHANGE);
        // On some platforms (namely GTK), removing all children causes the
        // parent to collapse (actually it's worse than that: GTK doesn't
        // allow there to be an empty expanded tree item, even if you do a
        // setExpanded(true)).
        // This behaviour makes it impossible to do this regression test.
        // See bug 40797 for more details.
        processEvents();
        if (((AbstractTreeViewer<TestElement,TestElement>) fViewer).getExpandedState(parent)) {
            assertNotNull("new child is visible", fViewer.testFindItem(child));
        }
    }

    /**
     * Regression test for 1GBDB5A: ITPUI:WINNT - Exception in AbstractTreeViewer update.
     * Problem was:
     *   node has child A
     *   node gets duplicate child A
     *   viewer is refreshed rather than using add for new A
     *   AbstractTreeViewer.updateChildren(...) was not properly handling it
     */
    public void testRefreshWithDuplicateChild() {
        TestElement first = fRootElement.getFirstChild();
        TestElement newElement = (TestElement) first.clone();
        fRootElement.addChild(newElement, new TestModelChange(
                TestModelChange.STRUCTURE_CHANGE, fRootElement));
        assertNotNull("new sibling is visible", fViewer
                .testFindItem(newElement));
    }

    /**
     * Regression test for Bug 3840 [Viewers] free expansion of jar happening when deleting projects (1GEV2FL)
     * Problem was:
     *   - node has children A and B
     *   - A is expanded, B is not
     *   - A gets deleted
     *   - B gets expanded because it reused A's item
     */
    public void testRefreshWithReusedItems() {
        //        TestElement a= fRootElement.getFirstChild();
        //        TestElement aa= a.getChildAt(0);
        //        TestElement ab= a.getChildAt(1);
        //        fTreeViewer.expandToLevel(aa, 1);
        //		List expandedBefore = Arrays.asList(fTreeViewer.getExpandedElements());
        //		assertTrue(expandedBefore.contains(a));
        //		assertTrue(expandedBefore.contains(aa));
        //		assertFalse(expandedBefore.contains(ab));
        //        a.deleteChild(aa, new TestModelChange(TestModelChange.STRUCTURE_CHANGE, a));
        //        List expandedAfter = Arrays.asList(fTreeViewer.getExpandedElements());
        //        assertFalse(expandedAfter.contains(ab));
    }

    public void testRenameChildElement() {
        TestElement first = fRootElement.getFirstChild();
        TestElement first2 = first.getFirstChild();
        fTreeViewer.expandToLevel(first2, 0);
        assertNotNull("first child is visible", fViewer.testFindItem(first2));

        String newLabel = first2.getLabel() + " changed";
        first2.setLabel(newLabel);
        Widget widget = fViewer.testFindItem(first2);
        assertTrue(widget instanceof Item);
        assertEquals("changed label", first2.getID() + " " + newLabel,
                ((Item) widget).getText());
    }

    /**
     * Regression test for Bug 26698 [Viewers] stack overflow during debug session, causing IDE to crash
     * Problem was:
     *   - node A has child A
     *   - setExpanded with A in the list caused an infinite recursion
     */
    public void testSetExpandedWithCycle() {
        TestElement first = fRootElement.getFirstChild();
        first.addChild(first, new TestModelChange(TestModelChange.INSERT,
                first, first));
        fTreeViewer.setExpandedElements(new TestElement[] { first });

    }

    /**
     * Test for Bug 41710 - assertion that an object may not be added to a given
     * TreeItem more than once.
     */
    public void testSetDuplicateChild() {
        //Widget root = fViewer.testFindItem(fRootElement);
        //assertNotNull(root);
        TestElement parent = fRootElement.addChild(TestModelChange.INSERT);
        TestElement child = parent.addChild(TestModelChange.INSERT);
        int initialCount = getItemCount(parent);
        fRootElement.addChild(child, new TestModelChange(
                TestModelChange.INSERT, fRootElement, child));
        int postCount = getItemCount(parent);
        assertEquals("Same element added to a parent twice.", initialCount,
                postCount);
    }

    /* (non-Javadoc)
     * @see org.eclipse.jface.tests.viewers.ViewerTestCase#tearDown()
     */
    public void tearDown() {
    	super.tearDown();
    	fTreeViewer = null;
    }
}