|
Lines 20-30
Link Here
|
| 20 |
import org.eclipse.rwt.Fixture; |
20 |
import org.eclipse.rwt.Fixture; |
| 21 |
import org.eclipse.rwt.graphics.Graphics; |
21 |
import org.eclipse.rwt.graphics.Graphics; |
| 22 |
import org.eclipse.swt.SWT; |
22 |
import org.eclipse.swt.SWT; |
| 23 |
import org.eclipse.swt.graphics.Font; |
23 |
import org.eclipse.swt.graphics.*; |
| 24 |
import org.eclipse.swt.graphics.Point; |
|
|
| 25 |
import org.eclipse.swt.internal.graphics.TextSizeDetermination.ICalculationItem; |
24 |
import org.eclipse.swt.internal.graphics.TextSizeDetermination.ICalculationItem; |
| 26 |
import org.eclipse.swt.internal.graphics.TextSizeProbeStore.IProbe; |
25 |
import org.eclipse.swt.internal.graphics.TextSizeProbeStore.IProbe; |
| 27 |
import org.eclipse.swt.internal.graphics.TextSizeProbeStore.IProbeResult; |
26 |
import org.eclipse.swt.internal.graphics.TextSizeProbeStore.IProbeResult; |
|
|
27 |
import org.eclipse.swt.widgets.Display; |
| 28 |
|
28 |
|
| 29 |
|
29 |
|
| 30 |
public class TextSizeDetermination_Test extends TestCase { |
30 |
public class TextSizeDetermination_Test extends TestCase { |
|
Lines 53-63
Link Here
|
| 53 |
Point storedSize = new Point( 100, 10 ); |
53 |
Point storedSize = new Point( 100, 10 ); |
| 54 |
IProbe[] probeRequests = TextSizeProbeStore.getProbeRequests(); |
54 |
IProbe[] probeRequests = TextSizeProbeStore.getProbeRequests(); |
| 55 |
assertEquals( 1, probeRequests.length ); |
55 |
assertEquals( 1, probeRequests.length ); |
| 56 |
assertSame( font, probeRequests[ 0 ].getFont() ); |
56 |
assertSame( font.getFontData()[ 0 ], probeRequests[ 0 ].getFontData() ); |
| 57 |
|
57 |
|
| 58 |
TextSizeProbeStore probeStore = TextSizeProbeStore.getInstance(); |
58 |
TextSizeProbeStore probeStore = TextSizeProbeStore.getInstance(); |
| 59 |
probeStore.createProbeResult( probeRequests[ 0 ], new Point( 10, 10 ) ); |
59 |
probeStore.createProbeResult( probeRequests[ 0 ], new Point( 10, 10 ) ); |
| 60 |
TextSizeDataBase.store( font, TEST_STRING, SWT.DEFAULT, storedSize ); |
60 |
TextSizeDataBase.store( font.getFontData()[ 0 ], |
|
|
61 |
TEST_STRING, |
| 62 |
SWT.DEFAULT, |
| 63 |
storedSize ); |
| 61 |
calculated = TextSizeDetermination.stringExtent( font, TEST_STRING ); |
64 |
calculated = TextSizeDetermination.stringExtent( font, TEST_STRING ); |
| 62 |
assertEquals( storedSize, calculated ); |
65 |
assertEquals( storedSize, calculated ); |
| 63 |
|
66 |
|
|
Lines 110-116
Link Here
|
| 110 |
|
113 |
|
| 111 |
probeRequests = TextSizeProbeStore.getProbeRequests(); |
114 |
probeRequests = TextSizeProbeStore.getProbeRequests(); |
| 112 |
assertEquals( 1, probeRequests.length ); |
115 |
assertEquals( 1, probeRequests.length ); |
| 113 |
assertSame( font0, probeRequests[ 0 ].getFont() ); |
116 |
assertSame( font0.getFontData()[ 0 ], probeRequests[ 0 ].getFontData() ); |
| 114 |
|
117 |
|
| 115 |
TextSizeProbeStore probeStore = TextSizeProbeStore.getInstance(); |
118 |
TextSizeProbeStore probeStore = TextSizeProbeStore.getInstance(); |
| 116 |
Point probeSize = new Point( 10, 13 ); |
119 |
Point probeSize = new Point( 10, 13 ); |
|
Lines 130-136
Link Here
|
| 130 |
|
133 |
|
| 131 |
probeRequests = TextSizeProbeStore.getProbeRequests(); |
134 |
probeRequests = TextSizeProbeStore.getProbeRequests(); |
| 132 |
assertEquals( 1, probeRequests.length ); |
135 |
assertEquals( 1, probeRequests.length ); |
| 133 |
assertSame( font0, probeRequests[ 0 ].getFont() ); |
136 |
assertSame( font0.getFontData()[ 0 ], probeRequests[ 0 ].getFontData() ); |
| 134 |
|
137 |
|
| 135 |
TextSizeProbeStore probeStore = TextSizeProbeStore.getInstance(); |
138 |
TextSizeProbeStore probeStore = TextSizeProbeStore.getInstance(); |
| 136 |
Point probeSize = new Point( TextSizeProbeStore.DEFAULT_PROBE.length() * 4, 10 ); |
139 |
Point probeSize = new Point( TextSizeProbeStore.DEFAULT_PROBE.length() * 4, 10 ); |
|
Lines 141-156
Link Here
|
| 141 |
|
144 |
|
| 142 |
public void testFontSizeDataBase() { |
145 |
public void testFontSizeDataBase() { |
| 143 |
Font font0 = Graphics.getFont( "arial", 10, SWT.NORMAL ); |
146 |
Font font0 = Graphics.getFont( "arial", 10, SWT.NORMAL ); |
|
|
147 |
FontData fontData0 = font0.getFontData()[ 0 ]; |
| 144 |
Font font1 = Graphics.getFont( "helvetia", 12, SWT.NORMAL ); |
148 |
Font font1 = Graphics.getFont( "helvetia", 12, SWT.NORMAL ); |
|
|
149 |
FontData fontData1 = font1.getFontData()[ 0 ]; |
| 145 |
|
150 |
|
| 146 |
Point textSize = TextSizeDataBase.lookup( font0, TEST_STRING, SWT.DEFAULT ); |
151 |
Point textSize; |
|
|
152 |
textSize = TextSizeDataBase.lookup( fontData0, TEST_STRING, SWT.DEFAULT ); |
| 147 |
assertNull( textSize ); |
153 |
assertNull( textSize ); |
| 148 |
textSize = TextSizeDataBase.lookup( font1, TEST_STRING, SWT.DEFAULT ); |
154 |
textSize = TextSizeDataBase.lookup( fontData1, TEST_STRING, SWT.DEFAULT ); |
| 149 |
assertNull( textSize ); |
155 |
assertNull( textSize ); |
| 150 |
|
156 |
|
| 151 |
try { |
157 |
try { |
| 152 |
Point point = new Point( 1, 1 ); |
158 |
Point point = new Point( 1, 1 ); |
| 153 |
TextSizeDataBase.store( font1, TEST_STRING, SWT.DEFAULT, point ); |
159 |
TextSizeDataBase.store( fontData1, TEST_STRING, SWT.DEFAULT, point ); |
| 154 |
fail( "No probe available." ); |
160 |
fail( "No probe available." ); |
| 155 |
} catch( final IllegalStateException ise ) { |
161 |
} catch( final IllegalStateException ise ) { |
| 156 |
} |
162 |
} |
|
Lines 163-226
Link Here
|
| 163 |
probeStore.createProbeResult( findRequestedProbe( 1 ), probeSize1 ); |
169 |
probeStore.createProbeResult( findRequestedProbe( 1 ), probeSize1 ); |
| 164 |
|
170 |
|
| 165 |
Point calculatedTextSize0 = new Point( 100, 10 ); |
171 |
Point calculatedTextSize0 = new Point( 100, 10 ); |
| 166 |
TextSizeDataBase.store( font0, |
172 |
TextSizeDataBase.store( fontData0, |
| 167 |
TEST_STRING, |
173 |
TEST_STRING, |
| 168 |
SWT.DEFAULT, |
174 |
SWT.DEFAULT, |
| 169 |
calculatedTextSize0 ); |
175 |
calculatedTextSize0 ); |
| 170 |
Point calculatedTextSize1 = new Point( 100, 12 ); |
176 |
Point calculatedTextSize1 = new Point( 100, 12 ); |
| 171 |
TextSizeDataBase.store( font1, |
177 |
TextSizeDataBase.store( fontData1, |
| 172 |
TEST_STRING, |
178 |
TEST_STRING, |
| 173 |
SWT.DEFAULT, |
179 |
SWT.DEFAULT, |
| 174 |
calculatedTextSize1 ); |
180 |
calculatedTextSize1 ); |
| 175 |
textSize = TextSizeDataBase.lookup( font0, TEST_STRING, SWT.DEFAULT ); |
181 |
textSize = TextSizeDataBase.lookup( fontData0, TEST_STRING, SWT.DEFAULT ); |
| 176 |
assertEquals( calculatedTextSize0, textSize ); |
182 |
assertEquals( calculatedTextSize0, textSize ); |
| 177 |
textSize = TextSizeDataBase.lookup( font1, TEST_STRING, SWT.DEFAULT ); |
183 |
textSize = TextSizeDataBase.lookup( fontData1, TEST_STRING, SWT.DEFAULT ); |
| 178 |
assertEquals( calculatedTextSize1, textSize ); |
184 |
assertEquals( calculatedTextSize1, textSize ); |
| 179 |
} |
185 |
} |
| 180 |
|
186 |
|
| 181 |
private IProbe findRequestedProbe( final int i ) { |
187 |
private IProbe findRequestedProbe( final int i ) { |
| 182 |
IProbe[] probeRequests = TextSizeProbeStore.getProbeRequests(); |
188 |
IProbe[] probeRequests = TextSizeProbeStore.getProbeRequests(); |
| 183 |
return TextSizeProbeStore.getProbe( probeRequests[ i ].getFont() ); |
189 |
return TextSizeProbeStore.getProbe( probeRequests[ i ].getFontData() ); |
| 184 |
} |
190 |
} |
| 185 |
|
191 |
|
| 186 |
public void testProbeStorage() { |
192 |
public void testProbeStorage() { |
| 187 |
Font font0 = Graphics.getFont( "arial", 10, SWT.NORMAL ); |
193 |
Font font0 = Graphics.getFont( "arial", 10, SWT.NORMAL ); |
|
|
194 |
FontData fontData0 = font0.getFontData()[ 0 ]; |
| 188 |
IProbe[] probeList = TextSizeProbeStore.getProbeList(); |
195 |
IProbe[] probeList = TextSizeProbeStore.getProbeList(); |
| 189 |
assertEquals( 0, probeList.length ); |
196 |
assertEquals( 0, probeList.length ); |
| 190 |
IProbe probe0 = TextSizeProbeStore.getProbe( font0 ); |
197 |
IProbe probe0 = TextSizeProbeStore.getProbe( fontData0 ); |
| 191 |
assertNull( probe0 ); |
198 |
assertNull( probe0 ); |
| 192 |
|
199 |
|
| 193 |
String probeText0 = "ProbeText0"; |
200 |
String probeText0 = "ProbeText0"; |
| 194 |
probe0 = TextSizeProbeStore.createProbe( font0, probeText0 ); |
201 |
probe0 = TextSizeProbeStore.createProbe( fontData0, probeText0 ); |
| 195 |
probeList = TextSizeProbeStore.getProbeList(); |
202 |
probeList = TextSizeProbeStore.getProbeList(); |
| 196 |
assertEquals( 1, probeList.length ); |
203 |
assertEquals( 1, probeList.length ); |
| 197 |
assertSame( probe0, probeList[ 0 ] ); |
204 |
assertSame( probe0, probeList[ 0 ] ); |
| 198 |
assertSame( probe0, TextSizeProbeStore.getProbe( font0 ) ); |
205 |
assertSame( probe0, TextSizeProbeStore.getProbe( fontData0 ) ); |
| 199 |
assertTrue( TextSizeProbeStore.containsProbe( font0 ) ); |
206 |
assertTrue( TextSizeProbeStore.containsProbe( fontData0 ) ); |
| 200 |
assertSame( probe0.getFont(), font0 ); |
207 |
assertSame( probe0.getFontData(), fontData0 ); |
| 201 |
assertSame( probe0.getString(), probeText0 ); |
208 |
assertSame( probe0.getString(), probeText0 ); |
| 202 |
|
209 |
|
| 203 |
Font font1 = Graphics.getFont( "arial", 12, SWT.NORMAL ); |
210 |
Font font1 = Graphics.getFont( "arial", 12, SWT.NORMAL ); |
| 204 |
assertFalse( TextSizeProbeStore.containsProbe( font1 ) ); |
211 |
FontData fontData1 = font1.getFontData()[ 0 ]; |
|
|
212 |
assertFalse( TextSizeProbeStore.containsProbe( fontData1 ) ); |
| 205 |
|
213 |
|
| 206 |
TextSizeProbeStore probeStore = TextSizeProbeStore.getInstance(); |
214 |
TextSizeProbeStore probeStore = TextSizeProbeStore.getInstance(); |
| 207 |
IProbeResult probeResult0 = probeStore.getProbeResult( font0 ); |
215 |
IProbeResult probeResult0 = probeStore.getProbeResult( fontData0 ); |
| 208 |
assertNull( probeResult0 ); |
216 |
assertNull( probeResult0 ); |
| 209 |
|
217 |
|
| 210 |
Point probeSize0 = new Point( 10, 10 ); |
218 |
Point probeSize0 = new Point( 10, 10 ); |
| 211 |
probeResult0 = probeStore.createProbeResult( probe0, probeSize0 ); |
219 |
probeResult0 = probeStore.createProbeResult( probe0, probeSize0 ); |
| 212 |
assertSame( probeResult0.getProbe(), probe0 ); |
220 |
assertSame( probeResult0.getProbe(), probe0 ); |
| 213 |
assertSame( probeResult0.getSize(), probeSize0 ); |
221 |
assertSame( probeResult0.getSize(), probeSize0 ); |
| 214 |
assertTrue( probeStore.containsProbeResult( font0 ) ); |
222 |
assertTrue( probeStore.containsProbeResult( fontData0 ) ); |
| 215 |
assertFalse( probeStore.containsProbeResult( font1 ) ); |
223 |
assertFalse( probeStore.containsProbeResult( fontData1 ) ); |
| 216 |
} |
224 |
} |
| 217 |
|
225 |
|
| 218 |
public void testDefaultFontSizeStorage() throws IOException { |
226 |
public void testDefaultFontSizeStorage() throws IOException { |
| 219 |
DefaultTextSizeStorage storage = new DefaultTextSizeStorage(); |
227 |
DefaultTextSizeStorage storage = new DefaultTextSizeStorage(); |
| 220 |
Font font0 = Graphics.getFont( "arial", 10, SWT.NORMAL ); |
228 |
Font font0 = Graphics.getFont( "arial", 10, SWT.NORMAL ); |
|
|
229 |
FontData fontData0 = font0.getFontData()[ 0 ]; |
| 221 |
Font font1 = Graphics.getFont( "helvetia", 12, SWT.NORMAL ); |
230 |
Font font1 = Graphics.getFont( "helvetia", 12, SWT.NORMAL ); |
| 222 |
storage.storeFont( font0 ); |
231 |
FontData fontData1 = font1.getFontData()[ 0 ]; |
| 223 |
storage.storeFont( font1 ); |
232 |
storage.storeFont( fontData0 ); |
|
|
233 |
storage.storeFont( fontData1 ); |
| 224 |
|
234 |
|
| 225 |
Point point0 = new Point( 9, 10 ); |
235 |
Point point0 = new Point( 9, 10 ); |
| 226 |
Integer key0 = new Integer( 0 ); |
236 |
Integer key0 = new Integer( 0 ); |
|
Lines 252-268
Link Here
|
| 252 |
assertEquals( point0, actual ); |
262 |
assertEquals( point0, actual ); |
| 253 |
actual = storage.lookupTextSize( key1 ); |
263 |
actual = storage.lookupTextSize( key1 ); |
| 254 |
assertEquals( point1, actual ); |
264 |
assertEquals( point1, actual ); |
| 255 |
Font[] fontList = storage.getFontList(); |
265 |
FontData[] fontDatas = storage.getFontList(); |
| 256 |
List fonts = Arrays.asList( fontList ); |
266 |
assertEquals( 2, fontDatas.length ); |
| 257 |
assertEquals( 2, fonts.size() ); |
267 |
List fontDataList = Arrays.asList( fontDatas ); |
| 258 |
assertTrue( fonts.contains( font0 ) ); |
268 |
assertTrue( fontDataList.contains( fontData0 ) ); |
| 259 |
assertTrue( fonts.contains( font1 ) ); |
269 |
assertTrue( fontDataList.contains( fontData1 ) ); |
| 260 |
} |
270 |
} |
| 261 |
|
271 |
|
| 262 |
public void testStorageOverflow() { |
272 |
public void testStorageOverflow() { |
| 263 |
DefaultTextSizeStorage storage = new DefaultTextSizeStorage(); |
273 |
DefaultTextSizeStorage storage = new DefaultTextSizeStorage(); |
| 264 |
int storeSize = DefaultTextSizeStorage.MIN_STORE_SIZE; |
274 |
int storeSize = DefaultTextSizeStorage.MIN_STORE_SIZE; |
| 265 |
DefaultTextSizeStorage.setStoreSize( storeSize ); |
275 |
storage.setStoreSize( storeSize ); |
| 266 |
|
276 |
|
| 267 |
for( int i = 0; i < storeSize - 1; i++ ) { |
277 |
for( int i = 0; i < storeSize - 1; i++ ) { |
| 268 |
Integer key = new Integer( i ); |
278 |
Integer key = new Integer( i ); |
|
Lines 286-300
Link Here
|
| 286 |
} |
296 |
} |
| 287 |
|
297 |
|
| 288 |
public void testTextSizeDatabaseKey() { |
298 |
public void testTextSizeDatabaseKey() { |
| 289 |
final Font font = Graphics.getFont( "name", 10, SWT.NORMAL ); |
299 |
Font font = Graphics.getFont( "name", 10, SWT.NORMAL ); |
|
|
300 |
final FontData fontData = font.getFontData()[ 0 ]; |
| 290 |
Set takenKeys = new HashSet(); |
301 |
Set takenKeys = new HashSet(); |
| 291 |
StringBuffer generatedText = new StringBuffer(); |
302 |
StringBuffer generatedText = new StringBuffer(); |
| 292 |
for( int i = 0; i < 100; i++ ) { |
303 |
for( int i = 0; i < 100; i++ ) { |
| 293 |
generatedText.append( "a" ); |
304 |
generatedText.append( "a" ); |
| 294 |
final String text = generatedText.toString(); |
305 |
final String text = generatedText.toString(); |
| 295 |
IProbe probe = new IProbe() { |
306 |
IProbe probe = new IProbe() { |
| 296 |
public Font getFont() { |
307 |
public FontData getFontData() { |
| 297 |
return font; |
308 |
return fontData; |
| 298 |
} |
309 |
} |
| 299 |
public String getString() { |
310 |
public String getString() { |
| 300 |
return text; |
311 |
return text; |
|
Lines 305-316
Link Here
|
| 305 |
}; |
316 |
}; |
| 306 |
Point size = new Point( 1, 2 ); |
317 |
Point size = new Point( 1, 2 ); |
| 307 |
TextSizeProbeStore.getInstance().createProbeResult( probe, size ); |
318 |
TextSizeProbeStore.getInstance().createProbeResult( probe, size ); |
| 308 |
Integer key = TextSizeDataBase.getKey( font, text, -1 ); |
319 |
Integer key = TextSizeDataBase.getKey( fontData, text, -1 ); |
| 309 |
assertFalse( takenKeys.contains( key ) ); |
320 |
assertFalse( takenKeys.contains( key ) ); |
| 310 |
takenKeys.add( key ); |
321 |
takenKeys.add( key ); |
| 311 |
} |
322 |
} |
| 312 |
} |
323 |
} |
| 313 |
|
324 |
|
|
|
325 |
public void testDisposedFont() { |
| 326 |
Display device = new Display(); |
| 327 |
Font disposedFont = new Font( device, "disposedFont", 10, SWT.NORMAL ); |
| 328 |
disposedFont.dispose(); |
| 329 |
try { |
| 330 |
TextSizeDetermination.stringExtent( disposedFont, "" ); |
| 331 |
} catch( Throwable e ) { |
| 332 |
fail( "Text size determination must also work with dispoed font" ); |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 314 |
protected void setUp() throws Exception { |
336 |
protected void setUp() throws Exception { |
| 315 |
Fixture.setUp(); |
337 |
Fixture.setUp(); |
| 316 |
TextSizeDataBase.reset(); |
338 |
TextSizeDataBase.reset(); |