Skip to content

Commit 2e46a0b

Browse files
ptzieglerazoitl
authored andcommitted
Migrate Label test case from WindowBuilder project to Draw2D
This test case is used by the WindowBuilder project to verify the correctness of its forked Label class. Because this class is about to be deleted in favor of the Draw2D class, the corresponding test case should be moved to the GEF project.
1 parent 7bb421e commit 2e46a0b

File tree

2 files changed

+165
-2
lines changed

2 files changed

+165
-2
lines changed

org.eclipse.draw2d.tests/src/org/eclipse/draw2d/test/Draw2dTestSuite.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2024 IBM Corporation and others.
2+
* Copyright (c) 2000, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License 2.0 which is available at
@@ -59,7 +59,8 @@
5959
ShapeTest.class,
6060
InsetsTest.class,
6161
DirectedGraphLayoutTest.class,
62-
ScrollPaneTests.class
62+
ScrollPaneTests.class,
63+
LabelTest.class
6364
})
6465
public class Draw2dTestSuite {
6566
}
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2011, 2025 Google, Inc. and others.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License 2.0 which is available at
6+
* https://www.eclipse.org/legal/epl-2.0.
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*
10+
* Contributors:
11+
* Google, Inc. - initial API and implementation
12+
*******************************************************************************/
13+
package org.eclipse.draw2d.test;
14+
15+
import org.eclipse.swt.SWT;
16+
import org.eclipse.swt.graphics.Font;
17+
import org.eclipse.swt.graphics.GC;
18+
import org.eclipse.swt.widgets.Display;
19+
20+
import org.eclipse.draw2d.CompoundBorder;
21+
import org.eclipse.draw2d.Label;
22+
import org.eclipse.draw2d.LineBorder;
23+
import org.eclipse.draw2d.MarginBorder;
24+
import org.eclipse.draw2d.geometry.Dimension;
25+
import org.eclipse.draw2d.geometry.Insets;
26+
import org.eclipse.draw2d.test.utils.TestFigure;
27+
import org.eclipse.draw2d.test.utils.TestLogger;
28+
29+
import org.junit.jupiter.api.AfterAll;
30+
import org.junit.jupiter.api.BeforeAll;
31+
import org.junit.jupiter.api.Test;
32+
33+
/**
34+
* @author lobas_av
35+
*/
36+
public class LabelTest extends BaseTestCase {
37+
38+
private static Font testFont;
39+
40+
@BeforeAll
41+
public static void setUpAll() {
42+
testFont = new Font(null, "", 100, SWT.NONE); //$NON-NLS-1$
43+
}
44+
45+
@AfterAll
46+
public static void tearDownAll() {
47+
testFont.dispose();
48+
}
49+
50+
////////////////////////////////////////////////////////////////////////////
51+
//
52+
// Test
53+
//
54+
////////////////////////////////////////////////////////////////////////////
55+
@Test
56+
@SuppressWarnings("static-method")
57+
public void test_text() throws Exception {
58+
// check text for new empty Label
59+
assertEquals("", new Label().getText()); //$NON-NLS-1$
60+
//
61+
// check text for Label created by constructor Label(String)
62+
assertEquals("Column: 1", new Label("Column: 1").getText()); //$NON-NLS-1$ //$NON-NLS-2$
63+
//
64+
Label label = new Label();
65+
//
66+
// check work setText()/getText()
67+
label.setText("123ZzzzZ"); //$NON-NLS-1$
68+
assertEquals("123ZzzzZ", label.getText()); //$NON-NLS-1$
69+
//
70+
// check work setText()/getText()
71+
label.setText("Row: 0"); //$NON-NLS-1$
72+
assertEquals("Row: 0", label.getText()); //$NON-NLS-1$
73+
//
74+
// check work setText()/getText()
75+
label.setText(null);
76+
assertEquals("", label.getText()); //$NON-NLS-1$
77+
}
78+
79+
@Test
80+
@SuppressWarnings("static-method")
81+
public void test_resetState() throws Exception {
82+
TestLogger actualLogger = new TestLogger();
83+
//
84+
TestFigure parentFigure = new TestFigure(actualLogger);
85+
//
86+
TestLogger expectedLogger = new TestLogger();
87+
//
88+
Label label = new Label();
89+
parentFigure.add(label);
90+
actualLogger.clear();
91+
//
92+
// check no reset state during setText() if text not change
93+
label.setText(""); //$NON-NLS-1$
94+
actualLogger.assertEmpty();
95+
//
96+
// check no reset state during setText() if text not change
97+
label.setText(null);
98+
actualLogger.assertEmpty();
99+
//
100+
// check reset state during setText()
101+
label.setText("123"); //$NON-NLS-1$
102+
expectedLogger.log("invalidate()"); //$NON-NLS-1$
103+
expectedLogger.log("repaint(0, 0, 0, 0)"); //$NON-NLS-1$
104+
actualLogger.assertEquals(expectedLogger);
105+
//
106+
// check no reset state during setText() if text not change
107+
label.setText("123"); //$NON-NLS-1$
108+
actualLogger.assertEmpty();
109+
//
110+
// check reset state during setText()
111+
label.setText("231"); //$NON-NLS-1$
112+
expectedLogger.log("invalidate()"); //$NON-NLS-1$
113+
expectedLogger.log("repaint(0, 0, 0, 0)"); //$NON-NLS-1$
114+
actualLogger.assertEquals(expectedLogger);
115+
//
116+
// check reset state during setText()
117+
label.setText(null);
118+
expectedLogger.log("invalidate()"); //$NON-NLS-1$
119+
expectedLogger.log("repaint(0, 0, 0, 0)"); //$NON-NLS-1$
120+
actualLogger.assertEquals(expectedLogger);
121+
}
122+
123+
@Test
124+
@SuppressWarnings("static-method")
125+
public void test_getPreferredSize() throws Exception {
126+
Label label = new Label();
127+
assertTextSize(label);
128+
Dimension size1 = label.getPreferredSize();
129+
//
130+
// check calc preferred size if text is changed
131+
label.setText("1234"); //$NON-NLS-1$
132+
Dimension size2 = label.getPreferredSize();
133+
assertTextSize(label);
134+
assertNotSame(size1, size2);
135+
assertSame(size2, label.getPreferredSize());
136+
//
137+
// check calc preferred size if font is changed
138+
label.setFont(new Font(null, "", 100, SWT.NONE)); //$NON-NLS-1$
139+
assertNotSame(size2, label.getPreferredSize());
140+
assertTextSize(label);
141+
//
142+
// check calc preferred size if set border
143+
label.setBorder(new CompoundBorder(new LineBorder(), new MarginBorder(2)));
144+
assertTextSize(label);
145+
}
146+
147+
private static final void assertTextSize(Label label) throws Exception {
148+
// create calc GC
149+
GC gc = new GC(Display.getDefault());
150+
// set label font
151+
gc.setFont(label.getFont());
152+
// calc text size
153+
org.eclipse.swt.graphics.Point size = gc.textExtent(label.getText());
154+
// dispose calc GC
155+
gc.dispose();
156+
// get label border insets and calc expected preferred size
157+
Insets insets = label.getInsets();
158+
Dimension expectedSize = new Dimension(size).expand(insets.getWidth(), insets.getHeight());
159+
//
160+
assertEquals(expectedSize, label.getPreferredSize());
161+
}
162+
}

0 commit comments

Comments
 (0)