Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e70e77e420c4b4cb2d83a6030036575fafb65fcf (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
/**
 *  Copyright (c) 2018 Angelo ZERR.
 *
 *  This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License 2.0
 *  which accompanies this distribution, and is available at
 *  https://www.eclipse.org/legal/epl-2.0/
 *
 *  SPDX-License-Identifier: EPL-2.0
 *
 *  Contributors:
 *  Angelo Zerr <angelo.zerr@gmail.com> - StyledText.setStyleRange resets less cache - Bug 530019
 */
package org.eclipse.swt.tests.junit;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.TextLayout;
import org.eclipse.swt.widgets.Shell;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

/**
 * Automated Test Suite for class org.eclipse.swt.custom.StyledText to check there is no regression with Bug 530019
 *
 * @see org.eclipse.swt.custom.StyledText
 */
public class Test_org_eclipse_swt_custom_StyledText_VariableLineHeight {

	Shell shell;
	StyledText styledText;

	@Before
	public void setUp() {
		shell = new Shell();
		styledText = new StyledText(shell, SWT.NULL);
		styledText.setLineSpacingProvider(l -> 0);
	}

	@After
	public void tearDown() {
		shell.dispose();
	}

	@Test
	public void testRise() {
		styledText.setText("a\nb\nc\nd");
		int length = styledText.getCharCount();

		// No style
		assertVariableLineHeightEquals(0, 0);

		// style with rise=1
		StyleRange style = new StyleRange();
		style.start = 0;
		style.length = length;
		style.rise = 1;
		styledText.replaceStyleRanges(0, length, new StyleRange[] { style });
		assertVariableLineHeightEquals(1, 0);

		// style with rise=0
		style = new StyleRange();
		style.start = 0;
		style.length = length;
		style.rise = 0;
		styledText.replaceStyleRanges(0, length, new StyleRange[] { style });
		assertVariableLineHeightEquals(0, 0);

		// style with rise=2
		style = new StyleRange();
		style.start = 0;
		style.length = length;
		style.rise = 2;
		styledText.replaceStyleRanges(0, length, new StyleRange[] { style });
		assertVariableLineHeightEquals(2, 0);

		// style with rise=2 again
		style = new StyleRange();
		style.start = 0;
		style.length = length;
		style.rise = 2;
		styledText.replaceStyleRanges(0, length, new StyleRange[] { style });
		assertVariableLineHeightEquals(2, 0);
	}

	@Test
	public void testFont() {
		styledText.setText("a\nb\nc\nd");

		// Create little, big font
		Font initialFont = styledText.getFont();
		FontData[] fontData = initialFont.getFontData();
		for (int i = 0; i < fontData.length; i++) {
			fontData[i].setHeight(24);
		}
		Font bigFont = new Font(styledText.getDisplay(), fontData);
		fontData = initialFont.getFontData();
		for (int i = 0; i < fontData.length; i++) {
			fontData[i].setHeight(fontData[i].getHeight() - 1);
		}
		Font littleFont = new Font(styledText.getDisplay(), fontData);

		int length = styledText.getCharCount();

		// No style
		assertVariableLineHeightEquals(0, 0);

		// style with big font
		StyleRange style = new StyleRange();
		style.start = 0;
		style.length = length;
		style.foreground = styledText.getDisplay().getSystemColor(SWT.COLOR_BLUE);
		style.font = bigFont;
		styledText.replaceStyleRanges(0, length, new StyleRange[] { style });
		assertVariableLineHeightEquals(0, 0, bigFont);

		// style with no font
		style = new StyleRange();
		style.start = 0;
		style.length = length;
		style.foreground = styledText.getDisplay().getSystemColor(SWT.COLOR_BLUE);
		style.font = null;
		styledText.replaceStyleRanges(0, length, new StyleRange[] { style });
		assertVariableLineHeightEquals(0, 0);

		// style with big font
		style = new StyleRange();
		style.start = 0;
		style.length = length;
		style.foreground = styledText.getDisplay().getSystemColor(SWT.COLOR_BLUE);
		style.font = bigFont;
		styledText.replaceStyleRanges(0, length, new StyleRange[] { style });
		assertVariableLineHeightEquals(0, 0, bigFont);

		// style with little font
		style = new StyleRange();
		style.start = 0;
		style.length = length;
		style.foreground = styledText.getDisplay().getSystemColor(SWT.COLOR_BLUE);
		style.font = littleFont;
		styledText.replaceStyleRanges(0, length, new StyleRange[] { style });
		assertVariableLineHeightEquals(0, 0);

		// style with no font
		style = new StyleRange();
		style.start = 0;
		style.length = length;
		style.foreground = styledText.getDisplay().getSystemColor(SWT.COLOR_BLUE);
		style.font = null;
		styledText.replaceStyleRanges(0, length, new StyleRange[] { style });
		assertVariableLineHeightEquals(0, 0);

		// style with little font
		style = new StyleRange();
		style.start = 0;
		style.length = length;
		style.foreground = styledText.getDisplay().getSystemColor(SWT.COLOR_BLUE);
		style.font = littleFont;
		styledText.replaceStyleRanges(0, length, new StyleRange[] { style });
		assertVariableLineHeightEquals(0, 0);

		bigFont.dispose();
		littleFont.dispose();
	}

	@Test
	public void testLittleFontSeveralRanges() {
		styledText.setText("abc\nd");

		// Create little, big font
		Font initialFont = styledText.getFont();
		FontData[] fontData = initialFont.getFontData();
		for (int i = 0; i < fontData.length; i++) {
			fontData[i].setHeight(fontData[i].getHeight() / 2);
		}
		Font littleFont1 = new Font(styledText.getDisplay(), fontData);
		fontData = initialFont.getFontData();
		for (int i = 0; i < fontData.length; i++) {
			fontData[i].setHeight(fontData[i].getHeight() / 4);
		}
		Font littleFont2 = new Font(styledText.getDisplay(), fontData);

		StyleRange[] ranges = new StyleRange[2];
		ranges[0] = new StyleRange();
		ranges[0].start = 0;
		ranges[0].length = 1;
		ranges[0].font = littleFont1;

		ranges[1] = new StyleRange();
		ranges[1].start = 1;
		ranges[1].length = 1;
		ranges[1].font = littleFont2;

		styledText.replaceStyleRanges(0, 3,ranges);
		// 2 little fonts, line height is the height of the default font of StyledText
		assertVariableLineHeightEquals(0, 0);

		littleFont1.dispose();
		littleFont2.dispose();
	}

	@Test
	public void testBigFontSeveralRanges() {
		styledText.setText("abc\nd");

		// Create little, big font
		Font initialFont = styledText.getFont();
		FontData[] fontData = initialFont.getFontData();
		for (int i = 0; i < fontData.length; i++) {
			fontData[i].setHeight(fontData[i].getHeight() * 2);
		}
		Font bigFont = new Font(styledText.getDisplay(), fontData);
		fontData = initialFont.getFontData();
		for (int i = 0; i < fontData.length; i++) {
			fontData[i].setHeight(fontData[i].getHeight() / 2);
		}
		Font littleFont = new Font(styledText.getDisplay(), fontData);


		StyleRange[] ranges = new StyleRange[2];
		ranges[0] = new StyleRange();
		ranges[0].start = 0;
		ranges[0].length = 1;
		ranges[0].font = bigFont;

		ranges[1] = new StyleRange();
		ranges[1].start = 1;
		ranges[1].length = 1;
		ranges[1].font = littleFont;

		styledText.replaceStyleRanges(0, 3,ranges);
		// line height is the height of the big font
		assertVariableLineHeightEquals(0, 0, bigFont);

		bigFont.dispose();
		littleFont.dispose();
	}

	private void assertVariableLineHeightEquals(int expected, int lineIndex) {
		assertVariableLineHeightEquals(expected, lineIndex, null);
	}

	private void assertVariableLineHeightEquals(int expected, int lineIndex, Font font) {
		int fontHeight = styledText.getLineHeight();
		if (font != null) {
			TextLayout layout = new TextLayout(font.getDevice());
			layout.setFont(font);
			layout.setText("a");
			fontHeight = layout.getBounds().height;
			layout.dispose();
		}
		int actual = styledText.getLinePixel(lineIndex + 1) - styledText.getLinePixel(lineIndex) - fontHeight;
		Assert.assertEquals(expected, actual);
	}

}

Back to the top