1 /*******************************************************************************
2  * Copyright (c) 2008 Micah Hainline and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     Micah Hainline <micah_hainline@yahoo.com> - initial API and implementation (bug 208335)
13  *     Tom Schindl <tom.schindl@bestsolution.at> - fixed GTK problem
14  ******************************************************************************/
15 
16 package org.eclipse.jface.tests.layout;
17 
18 import org.eclipse.jface.layout.TableColumnLayout;
19 import org.eclipse.jface.viewers.ColumnPixelData;
20 import org.eclipse.jface.viewers.ColumnWeightData;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.layout.FillLayout;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Display;
25 import org.eclipse.swt.widgets.Shell;
26 import org.eclipse.swt.widgets.Table;
27 import org.eclipse.swt.widgets.TableColumn;
28 
29 import junit.framework.TestCase;
30 
31 /**
32  * @since 3.4
33  *
34  */
35 public final class AbstractColumnLayoutTest extends TestCase {
36 	Display display;
37 	Shell shell;
38 
39 	@Override
setUp()40 	protected void setUp() throws Exception {
41 		display = Display.getCurrent();
42 		if (display == null) {
43 			display = new Display();
44 		}
45 		shell = new Shell(display, SWT.SHELL_TRIM);
46 		shell.setSize(500, 500);
47 		shell.setLayout(new FillLayout());
48 
49 	}
50 
51 	@Override
tearDown()52 	protected void tearDown() throws Exception {
53 		shell.dispose();
54 	}
55 
56 	/**
57 	 * Ensures that the minimum size is not taken into account in a shell unless
58 	 * the weighted size falls below the minimum.
59 	 */
testIgnoreMinimumSize()60 	public void testIgnoreMinimumSize() {
61 		Composite composite = new Composite(shell, SWT.NONE);
62 		TableColumnLayout layout = new TableColumnLayout();
63 		composite.setLayout(layout);
64 
65 		Table table = new Table(composite, SWT.BORDER | SWT.H_SCROLL
66 				| SWT.V_SCROLL);
67 		TableColumn col1 = new TableColumn(table, SWT.LEFT);
68 		TableColumn col2 = new TableColumn(table, SWT.LEFT);
69 		TableColumn col3 = new TableColumn(table, SWT.LEFT);
70 		TableColumn col4 = new TableColumn(table, SWT.LEFT);
71 
72 		layout.setColumnData(col1, new ColumnWeightData(2, 100));
73 		layout.setColumnData(col2, new ColumnWeightData(2));
74 		layout.setColumnData(col3, new ColumnWeightData(1, 30));
75 		// Needed because last column on GTK always maximized
76 		layout.setColumnData(col4, new ColumnPixelData(1));
77 
78 
79 		composite.layout(true, true);
80 		shell.open();
81 
82 		assertTrue(col1.getWidth() > 100);
83 		assertTrue(col1.getWidth() == col2.getWidth());
84 		assertTrue(Math.abs(col1.getWidth() - 2 * col3.getWidth()) <= 1);
85 	}
86 
87 	/**
88 	 * Ensures that width values based on weight are recalculated when a column falls below minimums.
89 	 */
testRecalculatePreferredSize()90 	public void testRecalculatePreferredSize() {
91 		Composite composite = new Composite(shell, SWT.NONE);
92 		TableColumnLayout layout = new TableColumnLayout();
93 		composite.setLayout(layout);
94 
95 		Table table = new Table(composite, SWT.BORDER | SWT.H_SCROLL
96 				| SWT.V_SCROLL);
97 		TableColumn col1 = new TableColumn(table, SWT.LEFT);
98 		TableColumn col2 = new TableColumn(table, SWT.LEFT);
99 		TableColumn col3 = new TableColumn(table, SWT.LEFT);
100 		TableColumn col4 = new TableColumn(table, SWT.LEFT);
101 
102 		layout.setColumnData(col1, new ColumnWeightData(4,40));
103 		layout.setColumnData(col2, new ColumnWeightData(1,200));
104 		layout.setColumnData(col3, new ColumnWeightData(2,30));
105 		// Needed because last column on GTK always maximized
106 		layout.setColumnData(col4, new ColumnPixelData(1));
107 
108 
109 		composite.layout(true, true);
110 		shell.open();
111 
112 		assertTrue(col1.getWidth() >= 40);
113 		assertTrue(col2.getWidth() >= 200);
114 		assertTrue(col3.getWidth() >= 30);
115 		assertTrue(Math.abs(col1.getWidth() - 2 * col3.getWidth()) <= 1);
116 	}
117 
118 	/**
119 	 * Ensures that computeSize doesn't rely on the current size. That strategy
120 	 * can lead to endless growth on {@link Shell#pack()}.
121 	 */
testComputeSize()122 	public void testComputeSize() {
123 		Composite composite = new Composite(shell, SWT.NONE);
124 		TableColumnLayout layout = new TableColumnLayout();
125 		composite.setLayout(layout);
126 
127 		Table table = new Table(composite, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
128 		TableColumn col1 = new TableColumn(table, SWT.LEFT);
129 		TableColumn col2 = new TableColumn(table, SWT.LEFT);
130 
131 		layout.setColumnData(col1, new ColumnWeightData(1, 40));
132 		layout.setColumnData(col2, new ColumnWeightData(1, 200));
133 
134 		shell.pack();
135 		shell.open();
136 
137 		assertTrue(col1.getWidth() >= 40);
138 		assertTrue(col2.getWidth() >= 200);
139 
140 		int width1 = col1.getWidth();
141 		int width2 = col2.getWidth();
142 		shell.pack();
143 		assertEquals(width1, col1.getWidth());
144 		assertEquals(width2, col2.getWidth());
145 
146 	}
147 }