Skip to main content
summaryrefslogtreecommitdiffstats
blob: 986868beec6593ab6a923872415c068e12083913 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2009, 2011 Wind River Systems 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:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipe.debug.tests.viewer.model;

import junit.framework.Assert;
import junit.framework.TestCase;

import org.eclipe.debug.tests.viewer.model.TestModel.TestElement;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.debug.internal.ui.viewers.model.IInternalTreeModelViewer;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IModelDelta;
import org.eclipse.debug.internal.ui.viewers.model.provisional.ModelDelta;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;

/**
 * Tests that verify that the viewer property retrieves all the content 
 * from the model.
 * 
 * @since 3.6
 */
abstract public class LazyTests extends TestCase implements ITestModelUpdatesListenerConstants {
    
    Display fDisplay;
    Shell fShell;
    IInternalTreeModelViewer fViewer;
    TestModelUpdatesListener fListener;
    
    public LazyTests(String name) {
        super(name);
    }

    /**
     * @throws java.lang.Exception
     */
    protected void setUp() throws Exception {
        fDisplay = PlatformUI.getWorkbench().getDisplay();
        fShell = new Shell(fDisplay);
        fShell.setMaximized(true);
        fShell.setLayout(new FillLayout());

        fViewer = createViewer(fDisplay, fShell);
        
        fListener = new TestModelUpdatesListener(fViewer, true, true);

        fShell.open ();
    }

    abstract protected IInternalTreeModelViewer createViewer(Display display, Shell shell);
        /**
     * @throws java.lang.Exception
     */
    protected void tearDown() throws Exception {
        fListener.dispose();
        fViewer.getPresentationContext().dispose();
        
        // Close the shell and exit.
        fShell.close();
        while (!fShell.isDisposed()) if (!fDisplay.readAndDispatch ()) Thread.sleep(0);
    }

    protected void runTest() throws Throwable {
        try {
            super.runTest();
        } catch (Throwable t) {
            throw new ExecutionException("Test failed: " + t.getMessage() + "\n fListener = " + fListener.toString(), t);
        }
    }
    
    /**
     * Creates a model in the pattern of:
     * 
     * root
     *   1
     *     1.1
     *     1.2
     *     1.3
     *     ...
     *     1.(size)
     */
    private TestModel largeSubtreeModel(int size) {
        TestModel model = new TestModel();
        TestElement[] children = new TestElement[size];
        for (int i = 0; i < size; i++) {
            children[i] = new TestElement(model, "1." + i, new TestElement[0]);
        }
        TestElement element = new TestElement(model, "1", children);
        model.setRoot(new TestElement(model, "root", new TestElement[] { element }));
        
        return model;
    }

    /**
     * Test to make sure that if an element is expanded its children are 
     * not automatically materialized.
     * (bug 305739 and bug 304277)
     */
    public void testExpandLargeSubTree() throws InterruptedException {
        // Create test model with lots of children.
        TestModel model = largeSubtreeModel(1000); 
        
        // NOTE: WE ARE NOT EXPANDING ANY CHILDREN

        // Populate initial view content
        fListener.reset(TreePath.EMPTY, model.getRootElement(), 1, true, true); 
        fViewer.setInput(model.getRootElement());
        while (!fListener.isFinished(ALL_UPDATES_COMPLETE)) if (!fDisplay.readAndDispatch ()) Thread.sleep(0);

        // Create delta to expand the "1" element.
        TestElement rootElement = model.getRootElement();
        ModelDelta rootDelta = new ModelDelta(rootElement, IModelDelta.NO_CHANGE);
        ModelDelta expandDelta = model.getBaseDelta(rootDelta);
        TestElement expandElement = rootElement.getChildren()[0];
        expandDelta.addNode(expandElement, 0, IModelDelta.EXPAND, expandElement.getChildren().length);

        // Add first 250 elements as acceptable to materialize
        fListener.reset(); 
        fListener.setFailOnRedundantUpdates(true);
        TreePath expandElementPath = model.findElement("1");
        fListener.addChildreCountUpdate(expandElementPath);
        fListener.addLabelUpdate(expandElementPath); // TODO: not sure why label is updated upon expand?
        for (int i = 0; i < 250; i++) {
            fListener.addChildreUpdate(expandElementPath, i);
            TreePath childPath = expandElementPath.createChildPath(expandElement.getChildren()[i]);
            fListener.addLabelUpdate(childPath);
            fListener.addHasChildrenUpdate(childPath);
        }
        model.postDelta(rootDelta);

        while (!fListener.isFinished(CONTENT_SEQUENCE_COMPLETE | MODEL_CHANGED_COMPLETE | LABEL_SEQUENCE_COMPLETE)) 
            if (!fDisplay.readAndDispatch ()) Thread.sleep(0);
    }

    /**
     * Test to make sure that if an element that is previously selected, is 
     * then selected and replaced, that no extra elements are retrieved.
     * (bug 304277 comment #24, and bug 305739 comment #9).
     */
    public void testReplaceAndSelectInSubTreeTree() throws InterruptedException {
        // Create test model with lots of children.
        TestModel model = largeSubtreeModel(1000); 
        
        // Expand all children
        fViewer.setAutoExpandLevel(-1);
        
        // Populate initial view content, watch for all updates but only wait 
        // for the content update sequence to finish (elements off screen will
        // not be updated).
        // TODO: child count for element 1 is updated multiple times.
        fListener.reset();
        fListener.setFailOnMultipleModelUpdateSequences(true); 
        fListener.setFailOnRedundantUpdates(false);
        fViewer.setInput(model.getRootElement());
        fListener.addLabelUpdate(model.findElement("1.0"));
        while (!fListener.isFinished(CONTENT_SEQUENCE_COMPLETE | LABEL_COMPLETE)) if (!fDisplay.readAndDispatch ()) Thread.sleep(0);

        // Set selection so that the initial selection is not empty
        fViewer.setSelection(new TreeSelection(new TreePath[] { model.findElement("1.0")} ));
        
        // Create delta to select the "1" element.
        TestElement rootElement = model.getRootElement();
        ModelDelta rootDelta = new ModelDelta(rootElement, IModelDelta.NO_CHANGE);
        ModelDelta baseDelta = model.getBaseDelta(rootDelta);
        TestElement _1Element = rootElement.getChildren()[0];
        ModelDelta _1Delta = baseDelta.addNode(_1Element, 0, IModelDelta.NO_CHANGE, _1Element.getChildren().length);

        // Add the delta to select the "1.1" element.
        TestElement _1_0_newElement = new TestElement(model, "1.0 - new", new TestElement[0]);
        TreePath _1ElementPath = model.findElement("1");
        model.replaceElementChild(_1ElementPath, 0, _1_0_newElement);
        _1Delta.addNode(_1_0_newElement, 0, IModelDelta.SELECT);

        // Add element label update and post the delta
        fListener.reset(); 
        fListener.setFailOnRedundantUpdates(true);
        TreePath _1_0_newElementPath = model.findElement("1.0 - new");
        fListener.addLabelUpdate(_1_0_newElementPath);
        model.postDelta(rootDelta);

        while (!fListener.isFinished(MODEL_CHANGED_COMPLETE |  LABEL_COMPLETE)) 
            if (!fDisplay.readAndDispatch ()) Thread.sleep(0);


        Assert.assertEquals(((IStructuredSelection)fViewer.getSelection()).getFirstElement(), _1_0_newElement);
    }

    /**
     */
    public void testContentRefresh() throws InterruptedException {
        // Create test model with lots of children.
        TestModel model = largeSubtreeModel(1000); 
        
        // Expand children all
        fViewer.setAutoExpandLevel(-1);

        // Populate initial view content
        fListener.reset(TreePath.EMPTY, model.getRootElement(), -1, true, true); 
        fViewer.setInput(model.getRootElement());
        while (!fListener.isFinished(CONTENT_SEQUENCE_COMPLETE | LABEL_SEQUENCE_COMPLETE)) if (!fDisplay.readAndDispatch ()) Thread.sleep(0);

        // Turn off autoexpand
        fViewer.setAutoExpandLevel(0);

        // Reposition the viewer to middle of list
        fListener.reset();
        fListener.setFailOnRedundantUpdates(false);
        fViewer.reveal(model.findElement("1"), 500);
        while (!fListener.isFinished(CONTENT_SEQUENCE_COMPLETE)) if (!fDisplay.readAndDispatch ()) Thread.sleep(0);
        
        // Create delta to refresh the "1" element.
        TestElement rootElement = model.getRootElement();
        ModelDelta rootDelta = new ModelDelta(rootElement, IModelDelta.NO_CHANGE);
        ModelDelta expandDelta = model.getBaseDelta(rootDelta);
        TestElement expandElement = rootElement.getChildren()[0];
        expandDelta.addNode(expandElement, 0, IModelDelta.CONTENT, expandElement.getChildren().length);

        // Rinse and repeast.  The refresh in bug 335734 is only triggered 
        // only on the second time.
        for (int repeatCount = 0; repeatCount < 3; repeatCount++) {
            // Add first 250 elements (after element 500) as acceptable to materialize
            fListener.reset(); 
            fListener.setFailOnRedundantUpdates(true);
            TreePath refreshElementPath = model.findElement("1");
            fListener.addRedundantExceptionChildCount(refreshElementPath);
            fListener.addRedundantExceptionLabel(refreshElementPath);
            fListener.addChildreUpdate(TreePath.EMPTY, 0);
            fListener.addHasChildrenUpdate(refreshElementPath);
            fListener.addChildreCountUpdate(refreshElementPath);
            fListener.addLabelUpdate(refreshElementPath); // TODO: not sure why label is updated upon expand?
            for (int i = 499; i < 750; i++) {
                fListener.addChildreUpdate(refreshElementPath, i);
                TreePath childPath = refreshElementPath.createChildPath(expandElement.getChildren()[i]);
                fListener.addLabelUpdate(childPath);
                fListener.addHasChildrenUpdate(childPath);
            }
            model.postDelta(rootDelta);
    
            while (!fListener.isFinished(CONTENT_SEQUENCE_COMPLETE | MODEL_CHANGED_COMPLETE)) 
                if (!fDisplay.readAndDispatch ()) Thread.sleep(0);
        }
    }

}

Back to the top