aboutsummaryrefslogtreecommitdiffstats
blob: 2a4dc936bb6c9be6d32be52fab5723382424e5c7 (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
/*******************************************************************************
 * Copyright (c) 2000, 2013 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
 *     Tom Schindl <tom.schindl@bestsolution.at> - test case for bug 157309, 177619
 *     Brad Reynolds - test case for bug 141435
 *******************************************************************************/
package org.eclipse.jface.tests.viewers;

import org.eclipse.jface.util.Util;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.StructuredViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.List;

public class ListViewerTest extends StructuredViewerTest {

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

    protected StructuredViewer createViewer(Composite parent) {
        ListViewer viewer = new ListViewer(parent);
        viewer.setContentProvider(new TestModelContentProvider());
        return viewer;
    }

    protected int getItemCount() {
        TestElement first = fRootElement.getFirstChild();
        List list = (List) fViewer.testFindItem(first);
        return list.getItemCount();
    }

    protected String getItemText(int at) {
        List list = (List) fViewer.getControl();
        return list.getItem(at);
    }

    public static void main(String args[]) {
        junit.textui.TestRunner.run(ListViewerTest.class);
    }
    
    public void testInsert() {
    	ListViewer v = ((ListViewer)fViewer);
    	TestElement element = new TestElement(fModel, fRootElement);
    	v.insert(element, 1);
    	assertSame("test insert", element, v.getElementAt(1));
    	assertEquals("test insert", element.toString(), v.getList().getItem(1));
    	
    	v.addFilter(new ViewerFilter() {

			public boolean select(Viewer viewer, Object parentElement, Object element) {
				return true;
			}
    	});
    	
    	TestElement element1 = new TestElement(fModel, fRootElement);
    	v.insert(element1, 1);
    	assertNotSame("test insert", element1, v.getElementAt(1));
    	
    	v.setFilters(new ViewerFilter[0]);
    	v.remove(element);
    	v.remove(element1);
    }
    
    public void testRevealBug69076() {
    	// TODO remove the Mac OS check when SWT has fixed the bug in List.java
    	// see bug 116105
    	if (Util.isCarbon() || Util.isLinux()) {
    		return;
    	}
		fViewer = null;
		if (fShell != null) {
			fShell.dispose();
			fShell = null;
		}
		openBrowser();
		for (int i = 40; i < 45; i++) {
			fRootElement = TestElement.createModel(1, i);
			fModel = fRootElement.getModel();
			fViewer.setInput(fRootElement);
			for (int j = 30; j < fRootElement.getChildCount(); j++) {
				fViewer.setSelection(new StructuredSelection(fRootElement
						.getFirstChild()), true);
				TestElement child = fRootElement.getChildAt(j);
				fViewer.reveal(child);
				List list = ((ListViewer) fViewer).getList();
				int topIndex = list.getTopIndex();
				// even though we pass in reveal=false, SWT still scrolls to show the selection (since 20020815)
				fViewer.setSelection(new StructuredSelection(child), false);
				assertEquals("topIndex should not change on setSelection", topIndex, list
						.getTopIndex());
				list.showSelection();
				assertEquals("topIndex should not change on showSelection", topIndex, list
						.getTopIndex());
			}
		}
	}
    
    /**
     * Asserts the ability to refresh a List that contains no selection without losing vertically scrolled state.
     * 
     * @throws Exception
     */
    public void testRefreshBug141435() throws Exception {
    	fViewer = null;
		if (fShell != null) {
			fShell.dispose();
			fShell = null;
		}
		openBrowser();
		TestElement model = TestElement.createModel(1, 50);
		fViewer.setInput(model);
		
		int lastIndex = model.getChildCount() - 1;
		
		//Scroll...
		fViewer.reveal(model.getChildAt(lastIndex));
		List list = (List) fViewer.getControl();
		int topIndex = list.getTopIndex();
		
		assertTrue("Top item should not be the first item.", topIndex != 0);
		fViewer.refresh();
		processEvents();
		assertEquals("Top index was not preserved after refresh.", topIndex, list.getTopIndex());
		
		//Assert that when the previous top index after refresh is invalid no exceptions are thrown.
		model.deleteChildren();
		
		try {
			fViewer.refresh();
			assertEquals(0, list.getTopIndex());
		} catch (Exception e) {
			fail("Refresh failure when refreshing with an empty model.");
		}		
	}
    
    public void testSelectionRevealBug177619() throws Exception {
    	TestElement model = TestElement.createModel(1, 100);
		fViewer.setInput(model);
		
    	fViewer.setSelection(new StructuredSelection(((ListViewer)fViewer).getElementAt(50)),true);
    	assertTrue(((ListViewer)fViewer).getList().getTopIndex() != 0);
	}
	
	public void testSelectionNoRevealBug177619() throws Exception {
		TestElement model = TestElement.createModel(1, 100);
		fViewer.setInput(model);
		
		fViewer.setSelection(new StructuredSelection(((ListViewer)fViewer).getElementAt(50)),false);
		assertTrue(((ListViewer)fViewer).getList().getTopIndex() == 0);
	}
}