Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 774ccc9133b7a5bcb7927e9130121f549363668b (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
/**
 *  Copyright (c) 2017 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> - Customize different line spacing of StyledText - Bug 522020
 */
package org.eclipse.swt.tests.junit;

import static org.junit.Assert.assertArrayEquals;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.custom.StyledTextLineSpacingProvider;
import org.eclipse.swt.widgets.Shell;
import org.junit.Before;
import org.junit.Test;

/**
 * Automated Test Suite for class
 * org.eclipse.swt.custom.StyledTextLineSpacingProvider
 *
 * @see org.eclipse.swt.custom.StyledTextLineSpacingProvider
 */
public class Test_org_eclipse_swt_custom_StyledTextLineSpacingProvider {

	class IntegerLineSpacingProvider implements StyledTextLineSpacingProvider {

		@Override
		public Integer getLineSpacing(int lineIndex) {
			try {
				String text = styledText.getLine(lineIndex);
				return Integer.parseInt(text);
			} catch (Exception e) {
				return null;
			}
		}
	}

	Shell shell;
	StyledText styledText;

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

	@Test
	public void test_setLineSpacingProvider() {
		styledText.setText("a\n45\nc\n57");
		// Tests with none line spacing provider
		assertLineSpacingEquals(0, 0, 0, 0, 0);
		// Tests with integer line spacing provider
		styledText.setLineSpacingProvider(new IntegerLineSpacingProvider());
		assertLineSpacingEquals(0, 0, 45, 0, 57);
		// Tests with reset line spacing provider
		styledText.setLineSpacingProvider(null);
		assertLineSpacingEquals(0, 0, 0, 0, 0);
	}

	@Test
	public void test_replaceTextRange() {
		styledText.setText("a\n45\nc\n57");
		styledText.setLineSpacingProvider(new IntegerLineSpacingProvider());
		assertLineSpacingEquals(0, 0, 45, 0, 57);
		// Replace 45 with b
		styledText.replaceTextRange(2, 2, "b");
		assertLineSpacingEquals(0, 0, 0, 0, 57);
		// Replace b with 45
		styledText.replaceTextRange(2, 1, "45");
		assertLineSpacingEquals(0, 0, 45, 0, 57);
	}

	private void assertLineSpacingEquals(int... expecteds) {
		int lineHeight = styledText.getLineHeight();
		int[] actuals = new int[expecteds.length];
		for (int i = 0; i < expecteds.length; i++) {
			actuals[i] = styledText.getLinePixel(i) - ((i > 0) ? styledText.getLinePixel(i - 1) + lineHeight : 0);
		}
		assertArrayEquals(expecteds, actuals);
	}
}

Back to the top