Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 86b86ab449f50c17ad8ab2cafff96e9de13cb59d (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
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
/*******************************************************************************
 * Copyright (c) 2009, 2013 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
 *     IBM Corporation - clean-up
 *******************************************************************************/
package org.eclipe.debug.tests.viewer.model;

import java.util.Arrays;
import java.util.List;

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.ITreeModelViewer;
import org.eclipse.debug.internal.ui.viewers.model.provisional.ModelDelta;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ITreeSelection;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;

/**
 * Tests to verify that the viewer property retrieves and processes the 
 * model deltas generated by the test model. 
 */
abstract public class DeltaTests extends TestCase implements ITestModelUpdatesListenerConstants {
    Display fDisplay;
    Shell fShell;
    ITreeModelViewer fViewer;
    TestModelUpdatesListener fListener;
    
    public DeltaTests(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, false, false);

        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);
        }
    }
    
    public void testUpdateLabel() throws InterruptedException {
        //TreeModelViewerAutopopulateAgent autopopulateAgent = new TreeModelViewerAutopopulateAgent(fViewer);
        
        TestModel model = TestModel.simpleSingleLevel();
        fViewer.setAutoExpandLevel(-1);

        // Create the listener
        fListener.reset(TreePath.EMPTY, model.getRootElement(), -1, true, false); 

        // Set the input into the view and update the view.
        fViewer.setInput(model.getRootElement());
        while (!fListener.isFinished()) if (!fDisplay.readAndDispatch ()) Thread.sleep(0);
        model.validateData(fViewer, TreePath.EMPTY);
        
        // Update the model
        TestElement element = model.getRootElement().getChildren()[0];
        TreePath elementPath = new TreePath(new Object[] { element });
        ModelDelta delta = model.appendElementLabel(elementPath, "-modified");
        
        fListener.reset(elementPath, element, -1, true, false); 
        model.postDelta(delta);
        while (!fListener.isFinished(LABEL_COMPLETE | MODEL_CHANGED_COMPLETE)) 
            if (!fDisplay.readAndDispatch ()) Thread.sleep(0);
        model.validateData(fViewer, TreePath.EMPTY);
    }

    public void testRefreshStruct() throws InterruptedException {
        //TreeModelViewerAutopopulateAgent autopopulateAgent = new TreeModelViewerAutopopulateAgent(fViewer);
        
        TestModel model = TestModel.simpleSingleLevel();
        fViewer.setAutoExpandLevel(-1);

        // Create the listener
        fListener.reset(TreePath.EMPTY, model.getRootElement(), -1, true, false); 

        // Set the input into the view and update the view.
        fViewer.setInput(model.getRootElement());
        while (!fListener.isFinished()) if (!fDisplay.readAndDispatch ()) Thread.sleep(0);
        model.validateData(fViewer, TreePath.EMPTY);
        
        // Update the model
        TestElement element = model.getRootElement().getChildren()[0];
        TreePath elementPath = new TreePath(new Object[] { element });
        TestElement[] newChildren = new TestElement[] {
            new TestElement(model, "1.1 - new", new TestElement[0]),
            new TestElement(model, "1.2 - new", new TestElement[0]),
            new TestElement(model, "1.3 - new", new TestElement[0]),
        };
        ModelDelta delta = model.setElementChildren(elementPath, newChildren);
        
        fListener.reset(elementPath, element, -1, true, false); 
        model.postDelta(delta);
        while (!fListener.isFinished()) if (!fDisplay.readAndDispatch ()) Thread.sleep(0);
        model.validateData(fViewer, TreePath.EMPTY);
    }

    public void testRefreshStruct2() throws InterruptedException {
        //TreeModelViewerAutopopulateAgent autopopulateAgent = new TreeModelViewerAutopopulateAgent(fViewer);
        
        TestModel model = TestModel.simpleMultiLevel();
        fViewer.setAutoExpandLevel(-1);

        // Create the listener
        fListener.reset(TreePath.EMPTY, model.getRootElement(), -1, true, false); 

        // Set the input into the view and update the view.
        fViewer.setInput(model.getRootElement());
        while (!fListener.isFinished()) if (!fDisplay.readAndDispatch ()) Thread.sleep(0);
        model.validateData(fViewer, TreePath.EMPTY);

        String prefix = "new - ";
        model.setElementChildren(TreePath.EMPTY, new TestElement[] {
            new TestElement(model, prefix + "1", new TestElement[0]),
            new TestElement(model, prefix + "2", true, false, new TestElement[] {
                new TestElement(model, prefix + "2.1", true, true, new TestElement[0]),
                new TestElement(model, prefix + "2.2", false, true, new TestElement[0]),
                new TestElement(model, prefix + "2.3", true, false, new TestElement[0]),
            }),
            new TestElement(model, prefix + "3", new TestElement[] {
                new TestElement(model, prefix + "3.1", new TestElement[] {
                    new TestElement(model, prefix + "3.1.1", new TestElement[0]),
                    new TestElement(model, prefix + "3.1.2", new TestElement[0]),
                    new TestElement(model, prefix + "3.1.3", new TestElement[0]),
                }),
                new TestElement(model, prefix + "3.2", new TestElement[] {
                    new TestElement(model, prefix + "3.2.1", new TestElement[0]),
                    new TestElement(model, prefix + "3.2.2", new TestElement[0]),
                    new TestElement(model, prefix + "3.2.3", new TestElement[0]),
                }),
                new TestElement(model, prefix + "3.3", new TestElement[] {
                    new TestElement(model, prefix + "3.3.1", new TestElement[0]),
                    new TestElement(model, prefix + "3.3.2", new TestElement[0]),
                    new TestElement(model, prefix + "3.3.3", new TestElement[0]),
                }),
            })
        });
        
        TestElement element = model.getRootElement();
        fListener.reset(TreePath.EMPTY, element, -1, false, false);
        
        model.postDelta(new ModelDelta(element, IModelDelta.CONTENT));
        while (!fListener.isFinished(ALL_UPDATES_COMPLETE | MODEL_CHANGED_COMPLETE)) 
            if (!fDisplay.readAndDispatch ()) Thread.sleep(0);
        model.validateData(fViewer, TreePath.EMPTY);
    }

    public void testRefreshCoalesceStruct() throws InterruptedException {
        //TreeModelViewerAutopopulateAgent autopopulateAgent = new TreeModelViewerAutopopulateAgent(fViewer);
        
        // Create a single level model and add a single child to each element.
        TestModel model = TestModel.simpleSingleLevel();
        TestElement[] rootChildren = model.getRootElement().getChildren(); 
        for (int i = 0; i < rootChildren.length; i++) {
            model.setElementChildren(
                new TreePath(new Object[] { rootChildren[i]} ), 
                new TestElement[] { new TestElement(model, i + ".1", new TestElement[0]) });
        }
        
        fViewer.setAutoExpandLevel(-1);

        // Create the listener
        fListener.reset(TreePath.EMPTY, model.getRootElement(), -1, true, false); 

        // Set the input into the view and update the view.
        fViewer.setInput(model.getRootElement());
        while (!fListener.isFinished()) if (!fDisplay.readAndDispatch ()) Thread.sleep(0);
        model.validateData(fViewer, TreePath.EMPTY);
        
        fListener.reset(TreePath.EMPTY, model.getRootElement(), -1, false, false);
        model.postDelta(new ModelDelta(model.getRootElement(), IModelDelta.CONTENT));
        while (!fListener.isFinished(ALL_UPDATES_COMPLETE | MODEL_CHANGED_COMPLETE)) 
            if (!fDisplay.readAndDispatch ()) Thread.sleep(0);
        
        model.validateData(fViewer, TreePath.EMPTY);
        assertTrue( fListener.checkCoalesced(TreePath.EMPTY, 0, 6) );
    }
    
    
    public void testInsert() throws InterruptedException {
        //TreeModelViewerAutopopulateAgent autopopulateAgent = new TreeModelViewerAutopopulateAgent(fViewer);
        
        TestModel model = TestModel.simpleSingleLevel();
        fViewer.setAutoExpandLevel(-1);

        // Create the listener
        fListener.reset(TreePath.EMPTY, model.getRootElement(), -1, true, false); 

        // Set the input into the view and update the view.
        fViewer.setInput(model.getRootElement());
        
        while (!fListener.isFinished()) if (!fDisplay.readAndDispatch ()) Thread.sleep(0);
        model.validateData(fViewer, TreePath.EMPTY);
        
        // Update the model
        TestElement element = new TestElement(model, "7", new TestElement[0]);
        TreePath elementPath = new TreePath(new Object[] { element });
        ModelDelta delta = model.insertElementChild(TreePath.EMPTY, 6, element);
        
        // Insert causes the update of element's data, label and children.
        // TODO: update of element's data after insert seems redundant
        // but it's probably not a big inefficiency
        fListener.reset();
        fListener.addChildreUpdate(TreePath.EMPTY, 6);
        fListener.addHasChildrenUpdate(elementPath);
        fListener.addLabelUpdate(elementPath);
        // TODO: redundant label updates on insert!
        fListener.setFailOnRedundantUpdates(false);
        model.postDelta(delta);
        while (!fListener.isFinished(ALL_UPDATES_COMPLETE | MODEL_CHANGED_COMPLETE)) 
            if (!fDisplay.readAndDispatch ()) Thread.sleep(0);
        model.validateData(fViewer, TreePath.EMPTY);
    }
    
    /**
     * This test checks that insert and select delta flags are processed in correct order:
     * insert then select.
     */
    public void testInsertAndSelect() throws InterruptedException {
        //TreeModelViewerAutopopulateAgent autopopulateAgent = new TreeModelViewerAutopopulateAgent(fViewer);
        
        TestModel model = TestModel.simpleSingleLevel();
        fViewer.setAutoExpandLevel(-1);

        // Create the listener
        fListener.reset(TreePath.EMPTY, model.getRootElement(), -1, true, false);

	    // Set the input into the view and update the view.
	    fViewer.setInput(model.getRootElement());
	      
	    while (!fListener.isFinished())
	        if (!fDisplay.readAndDispatch ()) Thread.sleep(0);
	
	    model.validateData(fViewer, TreePath.EMPTY);        
        
        // Update the model
        // Insert two new elements at once
        TestElement element0 = new TestElement(model, "00", new TestElement[] {});
        TestElement element1 = new TestElement(model, "01", new TestElement[] {});
        TreePath elementPath0 = new TreePath(new Object[] { element0 });
        TreePath elementPath1 = new TreePath(new Object[] { element1 });
        ModelDelta rootDelta = model.insertElementChild(TreePath.EMPTY, 0, element0);
        rootDelta = model.insertElementChild(rootDelta, TreePath.EMPTY, 1, element1);
        
        // Set the select flag on the first added node.
        ModelDelta delta0 = rootDelta.getChildDelta(element0);
        delta0.setFlags(delta0.getFlags() | IModelDelta.SELECT);

        fListener.reset();
        fListener.addHasChildrenUpdate(elementPath0);
        fListener.addHasChildrenUpdate(elementPath1);
        fListener.addLabelUpdate(elementPath0);
        fListener.addLabelUpdate(elementPath1);
        
        // TODO: list full set of expected updates.
        fListener.setFailOnRedundantUpdates(false);

        model.postDelta(rootDelta);
        while (!fListener.isFinished()) 
            if (!fDisplay.readAndDispatch ()) Thread.sleep(0);
        
        model.validateData(fViewer, TreePath.EMPTY);
    }

    /**
     * This test checks that insert and remove deltas are processed in correct order:
     * remove deltas are processed first then insert deltas.  
     */
    public void testInsertAndRemove() throws InterruptedException {
        //TreeModelViewerAutopopulateAgent autopopulateAgent = new TreeModelViewerAutopopulateAgent(fViewer);
        
        TestModel model = TestModel.simpleSingleLevel();
        fViewer.setAutoExpandLevel(-1);
        fListener.reset(TreePath.EMPTY, model.getRootElement(), -1, true, false);
        fViewer.setInput(model.getRootElement());
          
        while (!fListener.isFinished())
            if (!fDisplay.readAndDispatch ()) Thread.sleep(0);
    
        model.validateData(fViewer, TreePath.EMPTY);        
        
        // Update the model
        // Remove one element then insert a new one
        IModelDelta removeDelta = model.removeElementChild(TreePath.EMPTY, 3).getChildDeltas()[0];
        
        // Insert new elements at once
        TestElement element = new TestElement(model, "00", new TestElement[] {});
        TreePath elementPath = new TreePath(new Object[] { element });
        IModelDelta insertDelta = model.insertElementChild(TreePath.EMPTY, 1, element).getChildDeltas()[0];

        // Create a combined delta where the insert child delta is first and the remove child delta is second.
        ModelDelta combinedDelta = new ModelDelta(model.getRootElement(), IModelDelta.NO_CHANGE, 0, model.getRootElement().getChildren().length);
        combinedDelta.addNode(insertDelta.getElement(), insertDelta.getIndex(), insertDelta.getFlags(), insertDelta.getChildCount());
        combinedDelta.addNode(removeDelta.getElement(), removeDelta.getIndex(), removeDelta.getFlags(), removeDelta.getChildCount());
        
        // Set the select flag on the first added node.
        fListener.reset();
        fListener.addHasChildrenUpdate(elementPath);
        fListener.addLabelUpdate(elementPath);
        
        // TODO: list full set of expected updates.
        fListener.setFailOnRedundantUpdates(false);

        model.postDelta(combinedDelta);
        while (!fListener.isFinished(ALL_UPDATES_COMPLETE | MODEL_CHANGED_COMPLETE)) 
            if (!fDisplay.readAndDispatch ()) Thread.sleep(0);
        
        model.validateData(fViewer, TreePath.EMPTY);
    }

    
    public void testAddElement() throws InterruptedException {
        //TreeModelViewerAutopopulateAgent autopopulateAgent = new TreeModelViewerAutopopulateAgent(fViewer);
        
        TestModel model = TestModel.simpleSingleLevel();
        fViewer.setAutoExpandLevel(-1);

        // Create the listener
        fListener.reset(TreePath.EMPTY, model.getRootElement(), -1, true, false); 

        // Set the input into the view and update the view.
        fViewer.setInput(model.getRootElement());
        while (!fListener.isFinished()) if (!fDisplay.readAndDispatch ()) Thread.sleep(0);
        model.validateData(fViewer, TreePath.EMPTY);
        
        // Update the model
        TestElement element = new TestElement(model, "7", new TestElement[0]);
        TreePath elementPath = new TreePath(new Object[] { element });
        ModelDelta delta = model.addElementChild(TreePath.EMPTY, null, 6, element);
        
        // Add causes the update of parent child count and element's children.
        fListener.reset(elementPath, element, -1, true, false); 
        fListener.addChildreUpdate(TreePath.EMPTY, 6);
        // TODO: redundant updates on add!
        fListener.setFailOnRedundantUpdates(false);
        model.postDelta(delta);
        while (!fListener.isFinished(ALL_UPDATES_COMPLETE | MODEL_CHANGED_COMPLETE)) 
            if (!fDisplay.readAndDispatch ()) Thread.sleep(0);
        model.validateData(fViewer, TreePath.EMPTY);
    }

    // This test currently fails.  When (if) bug 311442 gets address we should re-enable it. 
    public void _x_testAddUnexpandedElement() throws InterruptedException {
        //TreeModelViewerAutopopulateAgent autopopulateAgent = new TreeModelViewerAutopopulateAgent(fViewer);
        
        TestModel model = TestModel.simpleMultiLevel();

        // Turn off auto-expansion
        fViewer.setAutoExpandLevel(0);

        // Create the listener
        fListener.reset(TreePath.EMPTY, model.getRootElement(), 1, true, false); 

        // Set the input into the view and update the view.
        fViewer.setInput(model.getRootElement());
        while (!fListener.isFinished()) if (!fDisplay.readAndDispatch ()) Thread.sleep(0);

        // Update the model
        TreePath parentPath = model.findElement("1");
        ModelDelta rootDelta = model.addElementChild(parentPath, null, 0, new TestElement(model, "1.1", new TestElement[0]));
        model.addElementChild(parentPath, rootDelta, 1, new TestElement(model, "1.2", new TestElement[0]));
        model.addElementChild(parentPath, rootDelta, 2, new TestElement(model, "1.3", new TestElement[0]));
        model.addElementChild(parentPath, rootDelta, 3, new TestElement(model, "1.4", new TestElement[0]));
        
        // Add causes the update of parent child count and element's children.
        fListener.reset();
        fListener.setFailOnRedundantUpdates(false);
        model.postDelta(rootDelta);
        while (!fListener.isFinished(MODEL_CHANGED_COMPLETE | CONTENT_SEQUENCE_COMPLETE)) 
            if (!fDisplay.readAndDispatch ()) Thread.sleep(0);

        // Update the elements that were added.
        fListener.reset();
        fListener.addUpdates((IInternalTreeModelViewer)fViewer, TreePath.EMPTY, model.getRootElement(), -1, ALL_UPDATES_COMPLETE);
        rootDelta = new ModelDelta(model.getRootElement(), IModelDelta.CONTENT);
        model.getElementDelta(rootDelta, model.findElement("1.1"), true).setFlags(IModelDelta.CONTENT);
        model.getElementDelta(rootDelta, model.findElement("1.2"), true).setFlags(IModelDelta.CONTENT);
        model.getElementDelta(rootDelta, model.findElement("1.3"), true).setFlags(IModelDelta.CONTENT);
        model.getElementDelta(rootDelta, model.findElement("1.4"), true).setFlags(IModelDelta.CONTENT);
        
        model.postDelta(rootDelta);
        while (!fListener.isFinished(MODEL_CHANGED_COMPLETE | ALL_UPDATES_COMPLETE)) 
            if (!fDisplay.readAndDispatch ()) Thread.sleep(0);
        
        fListener.reset(parentPath, model.getElement(parentPath), 1, false, true);
        ((IInternalTreeModelViewer)fViewer).expandToLevel(parentPath, 1);

        while (fListener.isFinished(CONTENT_SEQUENCE_STARTED) && !fListener.isFinished(CONTENT_SEQUENCE_COMPLETE)) 
            if (!fDisplay.readAndDispatch ()) Thread.sleep(0);

        model.validateData(fViewer, parentPath);
    }

    public void _x_testRefreshUnexpandedElementsChildren() throws InterruptedException {
        //TreeModelViewerAutopopulateAgent autopopulateAgent = new TreeModelViewerAutopopulateAgent(fViewer);
        
        TestModel model = TestModel.simpleMultiLevel();

        // Turn off auto-expansion
        fViewer.setAutoExpandLevel(0);

        // Create the listener
        fListener.reset(TreePath.EMPTY, model.getRootElement(), 1, true, false); 

        // Set the input into the view and update the view.
        fViewer.setInput(model.getRootElement());
        while (!fListener.isFinished()) if (!fDisplay.readAndDispatch ()) Thread.sleep(0);

        // Expand elment "2"
        TreePath parentPath = model.findElement("2");
        fListener.reset(parentPath, model.getElement(parentPath), 1, false, true);
        ((IInternalTreeModelViewer)fViewer).expandToLevel(parentPath, 1);

        while (fListener.isFinished(CONTENT_SEQUENCE_STARTED) && !fListener.isFinished(CONTENT_SEQUENCE_COMPLETE)) 
            if (!fDisplay.readAndDispatch ()) Thread.sleep(0);

        // Collapse back element "2"
        ((IInternalTreeModelViewer)fViewer).setExpandedState(parentPath, false);
        
        // Update the children of element "2".
        fListener.reset();
        fListener.addUpdates((IInternalTreeModelViewer)fViewer, TreePath.EMPTY, model.getRootElement(), -1, ALL_UPDATES_COMPLETE);
        ModelDelta rootDelta = new ModelDelta(model.getRootElement(), IModelDelta.CONTENT);
        model.getElementDelta(rootDelta, model.findElement("2.1"), true).setFlags(IModelDelta.CONTENT);
        model.getElementDelta(rootDelta, model.findElement("2.2"), true).setFlags(IModelDelta.CONTENT);
        model.getElementDelta(rootDelta, model.findElement("2.3"), true).setFlags(IModelDelta.CONTENT);
        
        model.postDelta(rootDelta);
        while (!fListener.isFinished(MODEL_CHANGED_COMPLETE | ALL_UPDATES_COMPLETE)) 
            if (!fDisplay.readAndDispatch ()) Thread.sleep(0);

        // Expand back element "2"
        fListener.reset(parentPath, model.getElement(parentPath), 1, false, true);
        ((IInternalTreeModelViewer)fViewer).expandToLevel(parentPath, 1);

        while (fListener.isFinished(CONTENT_SEQUENCE_STARTED) && !fListener.isFinished(CONTENT_SEQUENCE_COMPLETE)) 
            if (!fDisplay.readAndDispatch ()) Thread.sleep(0);

        model.validateData(fViewer, parentPath, true);
    }

    
    public void testRemove() throws InterruptedException {
        //TreeModelViewerAutopopulateAgent autopopulateAgent = new TreeModelViewerAutopopulateAgent(fViewer);
        
        TestModel model = TestModel.simpleSingleLevel();
        fViewer.setAutoExpandLevel(-1);

        // Create the listener
        fListener.reset(TreePath.EMPTY, model.getRootElement(), -1, true, false); 

        // Set the input into the view and update the view.
        fViewer.setInput(model.getRootElement());
        while (!fListener.isFinished()) if (!fDisplay.readAndDispatch ()) Thread.sleep(0);
        model.validateData(fViewer, TreePath.EMPTY);
        
        // Update the model
        ModelDelta delta = model.removeElementChild(TreePath.EMPTY, 5);
        
        // Remove delta should generate no new updates, but we still need to wait for the event to
        // be processed.
        fListener.reset(); 
        model.postDelta(delta);
        while (!fListener.isFinished(MODEL_CHANGED_COMPLETE)) 
            if (!fDisplay.readAndDispatch ()) Thread.sleep(0);
        model.validateData(fViewer, TreePath.EMPTY);
    }
    
    public void testExpandAndSelect() throws InterruptedException {
        TestModel model = TestModel.simpleMultiLevel();
        
        // Create the listener
        fListener.reset(TreePath.EMPTY, model.getRootElement(), 1, true, false);

        // Set the input into the view and update the view.
        fViewer.setInput(model.getRootElement());
        while (!fListener.isFinished()) if (!fDisplay.readAndDispatch ()) Thread.sleep(0);
        model.validateData(fViewer, TreePath.EMPTY, true);

        // Create the delta
        fListener.reset();
        // TODO Investigate: there seem to be unnecessary updates being issued 
        // by the viewer.  These include the updates that are commented out:  
        // For now disable checking for extra updates.
        fListener.setFailOnRedundantUpdates(false);
        TestElement element = model.getRootElement();
        TreePath path_root = TreePath.EMPTY;
        ModelDelta delta= new ModelDelta(model.getRootElement(), -1, IModelDelta.EXPAND, element.getChildren().length);
        ModelDelta deltaRoot = delta;
        element = element.getChildren()[2];
        TreePath path_root_3 = path_root.createChildPath(element);
        delta = delta.addNode(element, 2, IModelDelta.EXPAND, element.fChildren.length);
        fListener.addChildreUpdate(path_root_3, 0);
        TreePath path_root_3_1 = path_root_3.createChildPath(element.getChildren()[0]);
        fListener.addHasChildrenUpdate(path_root_3_1);
        fListener.addLabelUpdate(path_root_3_1);
        TreePath path_root_3_3 = path_root_3.createChildPath(element.getChildren()[2]);
        fListener.addHasChildrenUpdate(path_root_3_3);
        fListener.addLabelUpdate(path_root_3_3);
        //TODO unnecessary update: fListener.addChildreUpdate(path1, 1); 
        fListener.addChildreUpdate(path_root_3, 2);
        element = element.getChildren()[1];
        TreePath path_root_3_2 = path_root_3.createChildPath(element);
        delta = delta.addNode(element, 1, IModelDelta.EXPAND, element.fChildren.length);
        fListener.addLabelUpdate(path_root_3_2);
        TreePath path_root_3_2_1 = path_root_3_2.createChildPath(element.getChildren()[0]);
        fListener.addHasChildrenUpdate(path_root_3_2_1);
        fListener.addLabelUpdate(path_root_3_2_1);
        TreePath path_root_3_2_3 = path_root_3_2.createChildPath(element.getChildren()[2]);
        fListener.addHasChildrenUpdate(path_root_3_2_3);
        fListener.addLabelUpdate(path_root_3_2_3);
        // TODO unnecessary update: fListener.addChildreCountUpdate(path2);
        fListener.addChildreUpdate(path_root_3_2, 0);
        // TODO unnecessary update: fListener.addChildreUpdate(path2, 1); 
        fListener.addChildreUpdate(path_root_3_2, 2);
        element = element.getChildren()[1];
        TreePath path_root_3_2_2 = path_root_3_2.createChildPath(element);
        delta = delta.addNode(element, 1, IModelDelta.SELECT, element.fChildren.length);
        fListener.addLabelUpdate(path_root_3_2_2);
        fListener.addHasChildrenUpdate(path_root_3_2_2);

        // Validate the expansion state BEFORE posting the delta.
        
        IInternalTreeModelViewer contentProviderViewer = (IInternalTreeModelViewer)fViewer; 
        assertFalse(contentProviderViewer.getExpandedState(path_root_3));
        assertFalse(contentProviderViewer.getExpandedState(path_root_3_2));
        assertFalse(contentProviderViewer.getExpandedState(path_root_3_2_2));
        
        model.postDelta(deltaRoot);
        while (!fListener.isFinished(ALL_UPDATES_COMPLETE | MODEL_CHANGED_COMPLETE)) 
            if (!fDisplay.readAndDispatch ()) Thread.sleep(0);
        model.validateData(fViewer, TreePath.EMPTY, true);

        // Validate the expansion state AFTER posting the delta.
        assertTrue(contentProviderViewer.getExpandedState(path_root_3));
        assertTrue(contentProviderViewer.getExpandedState(path_root_3_2));
        assertFalse(contentProviderViewer.getExpandedState(path_root_3_2_2));
        
        // Verify selection
        ISelection selection = fViewer.getSelection();
        if (selection instanceof ITreeSelection) {
            List selectionPathsList = Arrays.asList( ((ITreeSelection)selection).getPaths() );
            assertTrue(selectionPathsList.contains(path_root_3_2_2));
        } else {
            fail("Not a tree selection");
        }
    }

    /**
     * This test verifies that expand and select updates are being ignored.
     */
    public void testExpandAndSelect_simple() throws InterruptedException {
        TestModel model = TestModel.simpleMultiLevel();
        
        // Create the listener
        fListener.reset(TreePath.EMPTY, model.getRootElement(), 1, true, false);

        // Set the input into the view and update the view.
        fViewer.setInput(model.getRootElement());
        while (!fListener.isFinished()) if (!fDisplay.readAndDispatch ()) Thread.sleep(0);
        model.validateData(fViewer, TreePath.EMPTY, true);

        // Create the delta
        fListener.reset();
        // TODO Investigate: there seem to be unnecessary updates being issued 
        // by the viewer.  These include the updates that are commented out:  
        // For now disable checking for extra updates.
        fListener.setFailOnRedundantUpdates(false);
        TestElement element = model.getRootElement();
        TreePath path_root = TreePath.EMPTY;
        ModelDelta delta= new ModelDelta(model.getRootElement(), -1, IModelDelta.EXPAND, element.getChildren().length);
        ModelDelta deltaRoot = delta;
        element = element.getChildren()[2];
        TreePath path_root_3 = path_root.createChildPath(element);
        delta.addNode(element, 2, IModelDelta.SELECT | IModelDelta.EXPAND, element.fChildren.length);

        // Validate the expansion state BEFORE posting the delta.
        
        IInternalTreeModelViewer contentProviderViewer = (IInternalTreeModelViewer)fViewer; 
        assertFalse(contentProviderViewer.getExpandedState(path_root_3));
        
        model.postDelta(deltaRoot);
        while (true) {
            if (fListener.isFinished(MODEL_CHANGED_COMPLETE)) {
                if (fListener.isFinished(CONTENT_SEQUENCE_COMPLETE | LABEL_SEQUENCE_COMPLETE) ) {
                    break;
                }
            }
            if (!fDisplay.readAndDispatch ()) Thread.sleep(0);
        }
        model.validateData(fViewer, TreePath.EMPTY, true);

        // Validate the expansion state AFTER posting the delta.
        assertTrue(contentProviderViewer.getExpandedState(path_root_3));
        
        // Verify selection
        ISelection selection = fViewer.getSelection();
        if (selection instanceof ITreeSelection) {
            List selectionPathsList = Arrays.asList( ((ITreeSelection)selection).getPaths() );
            assertTrue(selectionPathsList.contains(path_root_3));
        } else {
            fail("Not a tree selection");
        }
    }

    public void testCompositeModelRefreshStruct() throws InterruptedException {
        //TreeModelViewerAutopopulateAgent autopopulateAgent = new TreeModelViewerAutopopulateAgent(fViewer);
        
        TestModel model = TestModel.compositeMultiLevel();
        fViewer.setAutoExpandLevel(-1);

        // Create the listener
        // TODO: redundant updates on install deltas 
        fListener.reset(TreePath.EMPTY, model.getRootElement(), -1, false, false);

        // Set the input into the view and update the view.
        fViewer.setInput(model.getRootElement());
        while (!fListener.isFinished()) if (!fDisplay.readAndDispatch ()) Thread.sleep(0);
        model.validateData(fViewer, TreePath.EMPTY, true);

        // Update the model
        TreePath m4_2_1Path = model.findElement("m4.2.1");
        TestElement m4_2_1 = model.getElement(m4_2_1Path);
        TestModel m4 = m4_2_1.getModel();
        TestElement[] newChildren = new TestElement[] {
            new TestElement(m4, "4.2.1.new-1", new TestElement[0]),
            new TestElement(m4, "4.2.1.new-2", new TestElement[0]),
            new TestElement(m4, "4.2.1.new-3", new TestElement[0]),
        };

        ModelDelta delta = m4.setElementChildren(m4_2_1Path, newChildren);
        
        fListener.reset(m4_2_1Path, m4_2_1, -1, true, false); 
        model.postDelta(delta);
        while (!fListener.isFinished()) if (!fDisplay.readAndDispatch ()) Thread.sleep(0);
        model.validateData(fViewer, TreePath.EMPTY);
    }

    public void testCompositeModelAddElement() throws InterruptedException {
        TestModel model = TestModel.compositeMultiLevel();
        fViewer.setAutoExpandLevel(-1);

        // Create the listener
        // TODO: redundant updates on install deltas 
        fListener.reset(TreePath.EMPTY, model.getRootElement(), -1, false, false);

        // Set the input into the view and update the view.
        fViewer.setInput(model.getRootElement());
        while (!fListener.isFinished()) if (!fDisplay.readAndDispatch ()) Thread.sleep(0);
        model.validateData(fViewer, TreePath.EMPTY, true);
        
        TreePath m3_1Path = model.findElement("m3.1");
        TestElement m3_1 = model.getElement(m3_1Path);
        TestModel m3 = m3_1.getModel();
        TestElement m3_1_new = new TestElement(m3, "m3.1-new", new TestElement[0]);
        TreePath m3_1_newPath = m3_1Path.createChildPath(m3_1_new);
        ModelDelta delta = m3.addElementChild(m3_1Path, null, 0, m3_1_new);
        
        fListener.reset(m3_1_newPath, m3_1_new, -1, true, false); 
        fListener.addChildreUpdate(m3_1Path, 0);
        fListener.setFailOnRedundantUpdates(false);
        
        m3.postDelta(delta);
        while (!fListener.isFinished(ALL_UPDATES_COMPLETE | MODEL_CHANGED_COMPLETE)) 
            if (!fDisplay.readAndDispatch ()) Thread.sleep(0);
        
        model.validateData(fViewer, TreePath.EMPTY);
    }
    
    public void testBug292322() throws InterruptedException {
        //TreeModelViewerAutopopulateAgent autopopulateAgent = new TreeModelViewerAutopopulateAgent(fViewer);
        TestModel model = TestModel.simpleMultiLevel();
        fListener.reset(TreePath.EMPTY, model.getRootElement(), 1, true, false); 

        // Set the input into the view and update the view.
        fViewer.setInput(model.getRootElement());
        while (!fListener.isFinished()) if (!fDisplay.readAndDispatch ()) Thread.sleep(0);
        model.validateData(fViewer, TreePath.EMPTY, true);
        
        // Update the model: remove one child of an un-expanded element, then
        // make sure that the number of children is correct.
        TreePath parentPath = model.findElement("2");
        TestElement parentElement = model.getElement(parentPath);
        ModelDelta delta = model.removeElementChild(parentPath, 0);
        
        // Update the viewer
        fListener.reset(parentPath, parentElement, 0, false, false);
        //fListener.addChildreCountUpdate(parentPath);
        model.postDelta(delta);
        while (!fListener.isFinished(MODEL_CHANGED_COMPLETE | CONTENT_COMPLETE)) 
            if (!fDisplay.readAndDispatch ()) Thread.sleep(0);

        // Validate the viewer data.
        model.validateData(fViewer, TreePath.EMPTY, true);

        // Update the model: remove the remaining children and make sure that 
        // the element children are updated to false.
        model.removeElementChild(parentPath, 0);
        
        // Update the viewer
        fListener.reset(parentPath, parentElement, 0, false, false);
        model.postDelta(delta);
        while (!fListener.isFinished(MODEL_CHANGED_COMPLETE | CONTENT_COMPLETE)) 
            if (!fDisplay.readAndDispatch ()) Thread.sleep(0);

        // Validate the viewer data.
        model.validateData(fViewer, TreePath.EMPTY, true);

        // Update the model: remove the remaining children and make sure that 
        // the element children are updated to false.
        model.removeElementChild(parentPath, 0);
        
        // Update the viewer
        fListener.reset(parentPath, parentElement, 0, false, false);
        model.postDelta(delta);
        while (!fListener.isFinished(MODEL_CHANGED_COMPLETE | CONTENT_COMPLETE)) 
            if (!fDisplay.readAndDispatch ()) Thread.sleep(0);

        // Validate the viewer data.
        model.validateData(fViewer, TreePath.EMPTY, true);
    }

}

Back to the top