Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9fed7b53080429ce3fc116e63505d9859caf01b8 (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
/*******************************************************************************
 * 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 - bug fixing
 *******************************************************************************/
package org.eclipse.debug.tests.viewer.model;

import java.util.Arrays;

import junit.framework.TestCase;

import org.eclipse.debug.internal.ui.viewers.model.provisional.IColumnPresentation;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IColumnPresentation2;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IColumnPresentationFactory;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IPresentationContext;
import org.eclipse.debug.internal.ui.viewers.model.provisional.PresentationContext;
import org.eclipse.debug.internal.ui.viewers.model.provisional.TreeModelViewer;
import org.eclipse.debug.tests.viewer.model.TestModel.TestElement;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.XMLMemento;

/**
 * Tests to verify that the viewer properly handles initial columns width.
 */
public class ColumnPresentationTests extends TestCase implements ITestModelUpdatesListenerConstants {
	private Display fDisplay;
	private Shell fShell;
	private TreeModelViewer fViewer;
	private TestModelUpdatesListener fListener;
	private boolean fResized = false;

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

	/**
	 * @throws java.lang.Exception
	 */
	@Override
	protected void setUp() throws Exception {
		createViewer();
	}

	/**
	 * @throws java.lang.Exception
	 */
	@Override
	protected void tearDown() throws Exception {
		destroyViewer();
	}

	void createViewer() {
		fDisplay = PlatformUI.getWorkbench().getDisplay();
        fShell = new Shell(fDisplay);
        fShell.setSize(800, 600);
		fShell.setLayout(new FillLayout());
		fViewer = new TreeModelViewer(fShell, SWT.VIRTUAL, new PresentationContext("TestViewer")); //$NON-NLS-1$
        fViewer.getTree().addControlListener(new ControlListener() {
            @Override
			public void controlResized(ControlEvent e) {
                fResized = true;
            }

            @Override
			public void controlMoved(ControlEvent e) {
            }
        });
		fListener = new TestModelUpdatesListener(fViewer, false, false);
		fShell.open();
	}

	void destroyViewer() throws InterruptedException {
		fListener.dispose();
		fViewer.getPresentationContext().dispose();
		// Close the shell.
		fShell.close();
		while (!fShell.isDisposed()) {
			if (!fDisplay.readAndDispatch()) {
				Thread.sleep(0);
			}
		}
	}

	void recreateViewer() throws InterruptedException {
		destroyViewer();
		createViewer();
	}

	static class MyColumnPresentation implements IColumnPresentation {
		private IPresentationContext fContext;
		protected String[] columnIds;

		MyColumnPresentation() {
		    this (makeDefaultColumnIds());
        }

		static String[] makeDefaultColumnIds() {
            String[] columnIds = new String[5];
            for (int i = 0; i < columnIds.length; i++) {
				columnIds[i] = "ColumnId_" + i; //$NON-NLS-1$
            }
            return columnIds;
		}

		MyColumnPresentation(String[] columnIds) {
			this.columnIds = columnIds;
		}

		@Override
		public void init(IPresentationContext context) {
			fContext = context;
		}

		@Override
		public void dispose() {
			fContext = null;
		}

		@Override
		public ImageDescriptor getImageDescriptor(String id) {
			return null;
		}

		protected IPresentationContext getPresentationContext() {
			return fContext;
		}

		@Override
		public String[] getAvailableColumns() {
			return columnIds;
		}

		@Override
		public String[] getInitialColumns() {
			return columnIds;
		}

		@Override
		public String getHeader(String id) {
		    if (Arrays.asList(columnIds).indexOf(id) != -1) {
		        return id;
		    }
		    return null;
		}

		@Override
		public String getId() {
			return "MyColumnPresentationID"; //$NON-NLS-1$
		}

		@Override
		public boolean isOptional() {
			return true;
		}

	}

	static class MyColumnPresentation2 extends MyColumnPresentation implements IColumnPresentation2 {
		int[] repliedWidths;

		MyColumnPresentation2() {
			super();
			for (int i = 0; i < columnIds.length; i++) {
				columnIds[i] += "_2"; //$NON-NLS-1$
			}
			repliedWidths = new int[columnIds.length];
			Arrays.fill(repliedWidths, -2);
		}

		@Override
		public int getInitialColumnWidth(String id, int treeWidgetWidth,
				String[] visibleColumnIds) {
			for (int i = 0; i < columnIds.length; i++) {
				if (id.equals(columnIds[i]) && i == 0) {
					repliedWidths[i] = 2 * (treeWidgetWidth / visibleColumnIds.length);
					return repliedWidths[i];
				}
				if (id.equals(columnIds[i]) && (i == 1 || i == 2)) {
					repliedWidths[i] = (treeWidgetWidth / visibleColumnIds.length) / 2;
					return repliedWidths[i];
				}
				if (id.equals(columnIds[i]) && i == 3) {
					repliedWidths[i] = (treeWidgetWidth / visibleColumnIds.length);
					return repliedWidths[i];
				}
				if (id.equals(columnIds[i]) && i == 4) {
					repliedWidths[i] = -1;
					return repliedWidths[i];
				}
			}
			return -1;
		}

		@Override
		public String getId() {
			return "MyColumnPresentation2ID"; //$NON-NLS-1$
		}
	}

	class MyModel extends TestModel implements IColumnPresentationFactory {
		MyColumnPresentation colPresenation;

		MyModel(MyColumnPresentation cp1) {
			colPresenation = cp1;
		}

		@Override
		public IColumnPresentation createColumnPresentation(
				IPresentationContext context, Object element) {
			if (colPresenation != null) {
				return colPresenation;
			}
			return null;
		}

		@Override
		public String getColumnPresentationId(IPresentationContext context,
				Object element) {
			if (colPresenation != null) {
				return colPresenation.getId();
			}
			return null;
		}

	}

	private TestModel makeModel(MyColumnPresentation cp, String rootSufffix) throws InterruptedException {
		MyModel model = new MyModel(cp);
		model.setRoot(new TestElement(model, "root" + rootSufffix, new TestElement[] { //$NON-NLS-1$
		new TestElement(model, "1", true, true, new TestElement[0]), //$NON-NLS-1$
		new TestElement(model, "2", true, false, new TestElement[0]), //$NON-NLS-1$
		new TestElement(model, "3", false, true, new TestElement[0]), //$NON-NLS-1$
		new TestElement(model, "4", false, false, new TestElement[0]), //$NON-NLS-1$
		new TestElement(model, "5", new TestElement[0]), //$NON-NLS-1$
		new TestElement(model, "6", new TestElement[0]) })); //$NON-NLS-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);
		return model;
	}

	/**
	 * In this test: verify that tree viewer can handle IColumnPresentation
	 * implementation without requiring the presentation being
	 * IColumnPresentation2 (i.e. ensure we do not break backward compatibility
	 * of interface). Also, we verify that the initial columns width is the
	 * average of tree width / number of visible columns, which is the logic
	 * in InternalTreeModelViewer.
	 */
	public void testInitialColumnAverageWidth() throws InterruptedException {
        // Try to wait for the shell painting to settle
        if (!fDisplay.readAndDispatch ()) {
			Thread.sleep(0);
		}
        fResized = false;

		MyColumnPresentation colPre = new MyColumnPresentation();
		makeModel(colPre, "m1"); //$NON-NLS-1$
		Tree tree = fViewer.getTree();
		TreeColumn[] columns = tree.getColumns();
		int treeWidth = tree.getSize().x;
		int avgWidth = treeWidth / columns.length;

        // Resizing the tree invalidates the test.
		if (fResized) {
		    return;
		}

		for (int i = 0; i < columns.length - 1; i++) {
			assertEquals(avgWidth, columns[i].getWidth());
		}
		// Checking of the width of the last column is not reliable.
		// I.e. it's handled differenty on different platforms.
        //int remainder = treeWidth % columns.length;
        //assertEquals(avgWidth + remainder, columns[columns.length - 1].getWidth());
	}

	/**
	 * In this test: verify that tree viewer can handle IColumnPresentation2.
	 * Also, we verify that the initial columns width is the width computed by
	 * the IColumnPresentation2 implementation.
	 */
	public void testInitialColumnWidth() throws InterruptedException {
        // Try to wait for the shell painting to settle
        if (!fDisplay.readAndDispatch ()) {
			Thread.sleep(0);
		}
        fResized = false;

        MyColumnPresentation2 colPre = new MyColumnPresentation2();
		makeModel(colPre, "m2"); //$NON-NLS-1$
		Tree tree = fViewer.getTree();
		TreeColumn[] columns = tree.getColumns();

        // Resizing the tree invalidates the test.
		if (fResized) {
		    return;
		}

		for (int i = 0; i < columns.length; i++) {
			int width = colPre.repliedWidths[i];
			if (width != -1) {
	            assertEquals(width, columns[i].getWidth());
			}
		}
	}

	/**
	 * In this test: verify that tree viewer can handle IColumnPresentation2.
	 * Also, we verify that the initial columns width from IColumnPresentation2
	 * is not used when there are user settings inside the viewer which are
	 * created from user resizing columns.
	 */
	public void testRespectUserSettings() throws InterruptedException {
		MyColumnPresentation2 colPre = new MyColumnPresentation2();
		makeModel(colPre, "m2"); //$NON-NLS-1$
		TreeColumn[] columns = fViewer.getTree().getColumns();
		// simulate user resizing each column width
		int[] newWidths = new int[columns.length];
		for (int i = 0; i < columns.length; i++) {
			newWidths[i] = columns[i].getWidth() + 10;
			columns[i].setWidth(newWidths[i]);
		}
		for (int i = 0; i < columns.length; i++) {
			assertEquals(newWidths[i], columns[i].getWidth());
		}
		// get InternalTreeModelViewer to rebuild columns due to change of
		// model and presentation - first set to another model and column
		// presentation, then switch to a model with original presentation.
		makeModel(new MyColumnPresentation(), "m1"); //$NON-NLS-1$
		makeModel(colPre, "m3"); //$NON-NLS-1$
		// verify user resized widths are used instead of the initial widths from IColumnPresentation2
		columns = fViewer.getTree().getColumns();
		for (int i = 0; i < columns.length; i++) {
			assertEquals(newWidths[i], columns[i].getWidth());
		}

		// get InternalTreeModelViewer to rebuild columns due to hide and show columns
		fViewer.setShowColumns(false);
		do {
			if (!fDisplay.readAndDispatch()) {
				Thread.sleep(0);
			}
		} while (fViewer.getTree().getColumns().length > 0);
		fViewer.setShowColumns(true);
		do {
			if (!fDisplay.readAndDispatch()) {
				Thread.sleep(0);
			}
		} while (fViewer.getTree().getColumns().length != newWidths.length);
		// verify user resized widths are used instead of the initial widths from IColumnPresentation2
		columns = fViewer.getTree().getColumns();
		for (int i = 0; i < columns.length; i++) {
			assertEquals(newWidths[i], columns[i].getWidth());
		}
	}

	/**
	 * In this test: verify that tree viewer can handle IColumnPresentation2.
	 * Also, we verify that the initial columns width from IColumnPresentation2
	 * is not used when there are user settings inside the viewer which are
	 * restored from memento, e.g., restoring workspace, etc.
	 */
	public void testRespectMemento() throws InterruptedException {
		MyColumnPresentation2 colPre = new MyColumnPresentation2();
		makeModel(colPre, "m2"); //$NON-NLS-1$
		TreeColumn[] columns = fViewer.getTree().getColumns();
		// simulate user resizing each column width
		int[] newWidths = new int[columns.length];
		for (int i = 0; i < columns.length; i++) {
			newWidths[i] = columns[i].getWidth() + 10;
			columns[i].setWidth(newWidths[i]);
		}
		for (int i = 0; i < columns.length; i++) {
			assertEquals(newWidths[i], columns[i].getWidth());
		}
		// save memento
		final XMLMemento memento = XMLMemento.createWriteRoot("TEST"); //$NON-NLS-1$
		fViewer.saveState(memento);
		// throw away any settings inside the viewer and create a new viewer
		// with memento settings, this is the same effect resulted from closing
		// and opening workspace again.
		recreateViewer();
		fViewer.initState(memento);
		// get InternalTreeModelViewer to rebuild columns
		makeModel(colPre, "m2"); //$NON-NLS-1$
		// verify widths from memento are used instead of the initial widths from IColumnPresentation2
		columns = fViewer.getTree().getColumns();
		for (int i = 0; i < columns.length; i++) {
			assertEquals(newWidths[i], columns[i].getWidth());
		}
	}

	/**
     * In this test: verify that tree viewer can handle the column presentation changing
     * its available column IDs between runs (bug 360015).
     */
    public void testChangedColumnIds() throws InterruptedException {
        MyColumnPresentation colPre = new MyColumnPresentation();

		makeModel(colPre, "m1"); //$NON-NLS-1$
        TreeColumn[] columns = fViewer.getTree().getColumns();
        // Select visible columns
        fViewer.setVisibleColumns(new String[] { colPre.columnIds[0] });
        do {
            if (!fDisplay.readAndDispatch()) {
                Thread.sleep(0);
            }
        } while (fViewer.getTree().getColumns().length != 1);

        // get InternalTreeModelViewer to rebuild columns due to change of
        // model and presentation - first set to another model and column
        // presentation, then switch to a model with original presentation.
		makeModel(new MyColumnPresentation2(), "m2"); //$NON-NLS-1$

        String[] newColumnIds = MyColumnPresentation.makeDefaultColumnIds();
		newColumnIds[0] = "new_column_id"; //$NON-NLS-1$
        colPre = new MyColumnPresentation(newColumnIds);

		makeModel(colPre, "m3"); //$NON-NLS-1$

        // verify user resized widths are used instead of the initial widths from IColumnPresentation2
        columns = fViewer.getTree().getColumns();
        for (int i = 0; i < columns.length; i++) {
            assertEquals(newColumnIds[i], columns[i].getText());
        }
    }

}

Back to the top