Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a4b603d8f4d301515bccb60968e8039c2c5258b0 (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
/*******************************************************************************
 * Copyright (c) 2004, 2008 John Krasnay and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     John Krasnay - initial API and implementation
 *******************************************************************************/
package org.eclipse.vex.core.internal.layout;

import java.net.URL;

import junit.framework.TestCase;

import org.eclipse.vex.core.internal.core.DisplayDevice;
import org.eclipse.vex.core.internal.css.MockDisplayDevice;
import org.eclipse.vex.core.internal.css.StyleSheet;
import org.eclipse.vex.core.internal.css.StyleSheetReader;
import org.eclipse.vex.core.internal.css.Styles;
import org.eclipse.vex.core.internal.dom.RootElement;

public class TestStaticTextBox extends TestCase {

	FakeGraphics g;
	LayoutContext context;
	RootElement root = new RootElement("root");
	Styles styles;

	@Override
	protected void setUp() throws Exception {
		// TODO Auto-generated method stub
		super.setUp();
		DisplayDevice.setCurrent(new MockDisplayDevice(90, 90));
		final URL url = this.getClass().getResource("test.css");
		final StyleSheetReader reader = new StyleSheetReader();
		final StyleSheet ss = reader.read(url);

		g = new FakeGraphics();

		context = new LayoutContext();
		context.setBoxFactory(new CssBoxFactory());
		context.setGraphics(g);
		context.setStyleSheet(ss);

		styles = context.getStyleSheet().getStyles(root);

	}

	public TestStaticTextBox() throws Exception {

	}

	public void testSplit() throws Exception {

		final int width = g.getCharWidth();

		final StaticTextBox box = new StaticTextBox(context, root, "baggy orange trousers");
		assertEquals(box.getText().length() * width, box.getWidth());
		assertEquals(styles.getLineHeight(), box.getHeight());
		assertSplit(box, 22, false, "baggy orange ", "trousers");
		assertSplit(box, 21, false, "baggy orange ", "trousers");
		assertSplit(box, 20, false, "baggy orange ", "trousers");
		assertSplit(box, 13, false, "baggy orange ", "trousers");
		assertSplit(box, 12, false, "baggy ", "orange trousers");
		assertSplit(box, 6, false, "baggy ", "orange trousers");
		assertSplit(box, 5, false, null, "baggy orange trousers");
		assertSplit(box, 1, false, null, "baggy orange trousers");
		assertSplit(box, 0, false, null, "baggy orange trousers");
		assertSplit(box, -1, false, null, "baggy orange trousers");

		assertSplit(box, 22, true, "baggy orange ", "trousers");
		assertSplit(box, 21, true, "baggy orange ", "trousers");
		assertSplit(box, 20, true, "baggy orange ", "trousers");
		assertSplit(box, 13, true, "baggy orange ", "trousers");
		assertSplit(box, 12, true, "baggy ", "orange trousers");
		assertSplit(box, 6, true, "baggy ", "orange trousers");
		assertSplit(box, 5, true, "baggy ", "orange trousers");
		assertSplit(box, 4, true, "bagg", "y orange trousers");
		assertSplit(box, 3, true, "bag", "gy orange trousers");
		assertSplit(box, 2, true, "ba", "ggy orange trousers");
		assertSplit(box, 1, true, "b", "aggy orange trousers");
		assertSplit(box, 0, true, "b", "aggy orange trousers");
		assertSplit(box, -1, true, "b", "aggy orange trousers");
	}

	public void testSpaceSplit() throws Exception {
		final StaticTextBox box = new StaticTextBox(context, root, "red  green");
		assertSplit(box, 11, false, "red  ", "green");
		assertSplit(box, 10, false, "red  ", "green");
		assertSplit(box, 9, false, "red  ", "green");
		assertSplit(box, 5, false, "red  ", "green");

	}

	private void assertSplit(final StaticTextBox box, final int splitPos, final boolean force, final String left, final String right) {

		final Styles styles = context.getStyleSheet().getStyles(box.getElement());
		final int width = g.getCharWidth();

		final InlineBox.Pair pair = box.split(context, splitPos * width, force);

		final StaticTextBox leftBox = (StaticTextBox) pair.getLeft();
		final StaticTextBox rightBox = (StaticTextBox) pair.getRight();

		if (left == null) {
			assertNull(leftBox);
		} else {
			assertNotNull(leftBox);
			assertEquals(left, leftBox.getText());
			assertEquals(left.length() * width, leftBox.getWidth());
			assertEquals(styles.getLineHeight(), leftBox.getHeight());
		}

		if (right == null) {
			assertNull(rightBox);
		} else {
			assertNotNull(rightBox);
			assertEquals(right, rightBox.getText());
			assertEquals(right.length() * width, rightBox.getWidth());
			assertEquals(styles.getLineHeight(), rightBox.getHeight());
		}

	}
}

Back to the top