Skip to content

Commit 70c7bf6

Browse files
ShahzaibIbrahimfedejeanne
authored andcommitted
Unit test writing for controls to test HiDpi
Since HiDpi support is added in all controls of SWT, we have provided individual unit tests wherever possible
1 parent 826393d commit 70c7bf6

File tree

2 files changed

+290
-3
lines changed

2 files changed

+290
-3
lines changed

bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/widgets/WidgetWin32Tests.java

Lines changed: 290 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,16 @@
1111
package org.eclipse.swt.widgets;
1212

1313
import static org.junit.Assert.assertEquals;
14+
import static org.junit.Assert.assertTrue;
15+
16+
import java.io.*;
1417

1518
import org.eclipse.swt.*;
19+
import org.eclipse.swt.custom.*;
20+
import org.eclipse.swt.graphics.*;
1621
import org.eclipse.swt.internal.*;
22+
import org.eclipse.swt.internal.win32.*;
23+
import org.eclipse.swt.layout.*;
1724
import org.junit.jupiter.api.*;
1825

1926
class WidgetWin32Tests extends Win32AutoscaleTestBase {
@@ -28,8 +35,288 @@ public void testWidgetZoomShouldChangeOnZoomLevelChange() {
2835
button.setText("Widget Test");
2936
button.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_CYAN));
3037
shell.open();
31-
assertEquals("The initial zoom is wrong", zoom, button.getZoom()); //pre-condition
38+
assertEquals("The initial zoom is wrong", zoom, button.getZoom()); // pre-condition
39+
changeDPIZoom(scaledZoom);
40+
assertEquals("The Zoom Level should be updated for button on zoom change event on its shell", scaledZoom,
41+
button.getZoom());
42+
}
43+
44+
@Test
45+
public void testButtonPointsAfterZooming() throws NoSuchMethodException, IllegalAccessException {
46+
int zoom = DPIUtil.getDeviceZoom();
47+
int scaledZoom = zoom * 2;
48+
49+
shell = new Shell(display);
50+
shell.setBounds(0, 0, 100, 160);
51+
shell.setLayout(new FillLayout());
52+
shell.pack();
53+
54+
Button button = new Button(shell, SWT.PUSH);
55+
button.setText("Button");
56+
button.setBounds(0, 0, 100, 200);
57+
Point sizeBeforeEvent = button.getSize();
58+
Point p1 = button.computeSizeInPixels(sizeBeforeEvent.x, sizeBeforeEvent.y, false);
3259
changeDPIZoom(scaledZoom);
33-
assertEquals("The Zoom Level should be updated for button on zoom change event on its shell", scaledZoom, button.getZoom());
60+
Point sizeAfterEvent = button.getSize();
61+
Point p2 = button.computeSizeInPixels(sizeAfterEvent.x, sizeAfterEvent.y, false);
62+
63+
assertEquals("Width should be half in points after zooming to 200", p1.x / 2 , p2.x);
64+
assertEquals("Height should be half in points after zooming to 200", p1.y / 2, p2.y);
65+
}
66+
67+
@Test
68+
public void testImagePixelsWithDoubleZoomLevel() {
69+
int zoom = DPIUtil.getDeviceZoom();
70+
int scaledZoom = zoom * 2;
71+
72+
InputStream inputStream = WidgetWin32Tests.class.getResourceAsStream("folder.png");
73+
Image image = new Image(display, inputStream);
74+
75+
Point buttonImageSizeBeforeEvent = getImageDimension(image, zoom);
76+
Point buttonImageSizeAfterEvent = getImageDimension(image, scaledZoom);
77+
78+
assertEquals("Width of a button image should be doubled after zooming to 200",
79+
buttonImageSizeBeforeEvent.x * 2, buttonImageSizeAfterEvent.x);
80+
assertEquals("Height of a button image should be doubled after zooming to 200",
81+
buttonImageSizeBeforeEvent.y * 2, buttonImageSizeAfterEvent.y);
82+
}
83+
84+
private Point getImageDimension(Image image, Integer zoomLevel) {
85+
BITMAP bm = new BITMAP();
86+
OS.GetObject(Image.win32_getHandle(image, zoomLevel), BITMAP.sizeof, bm);
87+
int imgWidth = bm.bmWidth;
88+
int imgHeight = bm.bmHeight;
89+
return new Point(imgWidth, imgHeight);
90+
}
91+
92+
@Test
93+
public void testButtonFontAfterZooming() {
94+
int zoom = DPIUtil.getDeviceZoom();
95+
int scaledZoom = zoom * 2;
96+
97+
shell = new Shell(display);
98+
shell.setBounds(0, 0, 100, 160);
99+
shell.setLayout(new FillLayout());
100+
shell.pack();
101+
102+
Button button = new Button(shell, SWT.PUSH);
103+
button.setText("Button");
104+
button.setBounds(0, 0, 100, 200);
105+
Font font = new Font(display, "Arial", 12, SWT.BOLD);
106+
button.setFont(font);
107+
108+
int heightBeforeZoom = button.getFont().getFontData()[0].data.lfHeight;
109+
changeDPIZoom(scaledZoom);
110+
int heightAfterZoom = button.getFont().getFontData()[0].data.lfHeight;
111+
112+
assertEquals("Height of a font of the button should be doubled after zooming to 200",
113+
heightBeforeZoom * 2, heightAfterZoom);
114+
}
115+
116+
@Test
117+
public void testCoolItemAfterZooming() {
118+
int zoom = DPIUtil.getDeviceZoom();
119+
int scaledZoom = zoom * 2;
120+
121+
shell = new Shell(display);
122+
shell.setBounds(0, 0, 100, 160);
123+
shell.setLayout(new FillLayout());
124+
shell.pack();
125+
126+
CoolBar coolBar = new CoolBar(shell, SWT.NONE);
127+
CoolItem item1 = new CoolItem(coolBar, SWT.NONE);
128+
Label label1 = new Label(coolBar, SWT.NONE);
129+
label1.setText("Label 1");
130+
label1.setSize(new Point(10, 20));
131+
item1.setControl(label1);
132+
item1.setPreferredSize(label1.computeSize(SWT.DEFAULT, SWT.DEFAULT));
133+
item1.setSize(item1.getPreferredSize());
134+
135+
var preferredControlSize = item1.getPreferredSizeInPixels();
136+
int xBeforeZoom = preferredControlSize.x;
137+
int yBeforeZoom = preferredControlSize.y;
138+
changeDPIZoom(scaledZoom);
139+
var preferredControlSize2 = item1.getPreferredSizeInPixels();
140+
int xAfterZoom = preferredControlSize2.x;
141+
int yAfterZoom = preferredControlSize2.y;
142+
143+
// Adding tolerance of +- 5 because OS doesn't scale coolitem up exactly twice
144+
int tolerance = 5;
145+
int yExpectedValue = yAfterZoom / 2;
146+
int lowerBound = yExpectedValue - tolerance;
147+
int upperBound = yExpectedValue + tolerance;
148+
149+
assertEquals("Width of a Item should be twice after zooming to 200", xBeforeZoom * 2, xAfterZoom);
150+
assertTrue("Height of a Item should be twice (+/- 5) after zooming to 200",
151+
yBeforeZoom >= lowerBound && yBeforeZoom <= upperBound);
152+
}
153+
154+
@Test
155+
public void testExpandItemAfterZooming() {
156+
int zoom = DPIUtil.getDeviceZoom();
157+
int scaledZoom = zoom * 2;
158+
159+
shell = new Shell(display);
160+
shell.setBounds(0, 0, 100, 160);
161+
shell.setLayout(new FillLayout());
162+
shell.pack();
163+
164+
ExpandBar coolBar = new ExpandBar(shell, SWT.NONE);
165+
ExpandItem item1 = new ExpandItem(coolBar, SWT.NONE);
166+
Label label1 = new Label(coolBar, SWT.NONE);
167+
label1.setText("Label 1");
168+
item1.setControl(label1);
169+
item1.setHeight(10);
170+
item1.setExpanded(true);
171+
172+
var heightBeforeZoom = item1.getHeightInPixels();
173+
changeDPIZoom(scaledZoom);
174+
var heightAfterZoom = item1.getHeightInPixels();
175+
176+
assertEquals("Height of a font of the button should be doubled after zooming to 200",
177+
heightBeforeZoom * 2, heightAfterZoom);
34178
}
35-
}
179+
180+
@Test
181+
public void testTabFolderSizeAfterZooming() {
182+
int zoom = DPIUtil.getDeviceZoom();
183+
int scaledZoom = zoom * 2;
184+
185+
shell = new Shell(display);
186+
shell.setBounds(0, 0, 100, 160);
187+
shell.setLayout(new FillLayout());
188+
shell.pack();
189+
190+
TabFolder tabFolder = new TabFolder(shell, SWT.NONE);
191+
tabFolder.setBounds(20, 20, 360, 240);
192+
193+
TabItem tabItem = new TabItem(tabFolder, SWT.NONE);
194+
tabItem.setText("Tab 1");
195+
Label label = new Label(tabFolder, SWT.NONE);
196+
label.setSize(200, 200);
197+
tabItem.setControl(label);
198+
199+
Point tabItemSizeBeforeEvent = tabItem.getControl().getSize();
200+
changeDPIZoom(scaledZoom);
201+
Point tabItemSizeAfterEvent = tabItem.getControl().getSize();
202+
203+
assertEquals("Width of a tab folder item should be halved in points after zooming to 200",
204+
tabItemSizeBeforeEvent.x / 2, tabItemSizeAfterEvent.x);
205+
assertEquals("Height of a tab folder item should be halved in points after zooming to 200",
206+
tabItemSizeBeforeEvent.y / 2, tabItemSizeAfterEvent.y);
207+
}
208+
209+
@Test
210+
public void testTableAfterZooming() {
211+
int zoom = DPIUtil.getDeviceZoom();
212+
int scaledZoom = zoom * 2;
213+
214+
shell = new Shell(display);
215+
shell.setBounds(0, 0, 100, 160);
216+
shell.setLayout(new FillLayout());
217+
shell.pack();
218+
219+
Table table = new Table(shell, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
220+
table.setBounds(20, 20, 360, 240);
221+
table.setHeaderVisible(true);
222+
table.setLinesVisible(true);
223+
224+
TableColumn column = new TableColumn(table, SWT.NONE);
225+
column.setText("Column 1");
226+
column.setWidth(200);
227+
228+
Font font = new Font(display, "Arial", 12, SWT.BOLD);
229+
230+
TableItem item1 = new TableItem(table, SWT.NONE);
231+
item1.setText("Item 1");
232+
item1.setFont(font);
233+
234+
for (TableColumn col : table.getColumns()) {
235+
col.pack();
236+
}
237+
238+
int fontHeightBefore = item1.getFont().getFontData()[0].data.lfHeight;
239+
changeDPIZoom(scaledZoom);
240+
int fontHeightAfter = item1.getFont().getFontData()[0].data.lfHeight;
241+
242+
assertEquals("Height of a font for table item should be doubled after zooming to 200",
243+
fontHeightBefore * 2, fontHeightAfter);
244+
}
245+
246+
@Test
247+
public void testTreeAfterZooming() {
248+
int zoom = DPIUtil.getDeviceZoom();
249+
int scaledZoom = zoom * 2;
250+
251+
shell = new Shell(display);
252+
shell.setBounds(0, 0, 100, 160);
253+
shell.setLayout(new FillLayout());
254+
shell.pack();
255+
256+
Tree tree = new Tree(shell, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
257+
tree.setBounds(20, 20, 360, 240);
258+
tree.setHeaderVisible(true);
259+
tree.setLinesVisible(true);
260+
261+
TreeColumn column = new TreeColumn(tree, SWT.NONE);
262+
column.setText("Column 1");
263+
column.setWidth(200);
264+
265+
Font font = new Font(display, "Arial", 12, SWT.BOLD);
266+
TreeItem item1 = new TreeItem(tree, SWT.NONE);
267+
item1.setText("Item 1");
268+
item1.setFont(font);
269+
for (TreeColumn col : tree.getColumns()) {
270+
col.pack();
271+
}
272+
273+
int fontHeightBefore = item1.getFont().getFontData()[0].data.lfHeight;
274+
changeDPIZoom(scaledZoom);
275+
int fontHeightAfter = item1.getFont().getFontData()[0].data.lfHeight;
276+
277+
assertEquals("Height of a font for tree item should be doubled after zooming to 200",
278+
fontHeightBefore * 2, fontHeightAfter);
279+
}
280+
281+
@Test
282+
public void testCaretInStyledTextAfterZooming() {
283+
int zoom = DPIUtil.getDeviceZoom();
284+
int scaledZoom = zoom * 2;
285+
286+
shell = new Shell(display);
287+
shell.setBounds(0, 0, 100, 160);
288+
shell.setLayout(new FillLayout());
289+
shell.pack();
290+
291+
// Create the StyledText widget
292+
StyledText styledText = new StyledText(shell, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
293+
styledText.setBounds(10, 10, 360, 200);
294+
295+
// Set some text with different styles
296+
styledText.setText("This is an example of styled text.");
297+
298+
// Apply bold style to a range of text
299+
StyleRange styleRange1 = new StyleRange();
300+
styleRange1.start = 10;
301+
styleRange1.length = 7;
302+
styleRange1.fontStyle = SWT.BOLD;
303+
styledText.setStyleRange(styleRange1);
304+
305+
// Apply italic style to another range of text
306+
StyleRange styleRange2 = new StyleRange();
307+
styleRange2.start = 18;
308+
styleRange2.length = 6;
309+
styleRange2.fontStyle = SWT.ITALIC;
310+
styledText.setStyleRange(styleRange2);
311+
312+
// Get the caret size
313+
Point caretSize = styledText.getCaret().getSizeInPixels();
314+
changeDPIZoom(scaledZoom);
315+
Point caretSize2 = styledText.getCaret().getSizeInPixels();
316+
317+
assertEquals("Height of a Caret for Styled Text should be doubled after zooming to 200", caretSize.y * 2,
318+
caretSize2.y);
319+
320+
}
321+
322+
}
921 Bytes
Loading

0 commit comments

Comments
 (0)