|
Removed
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2000, 2005 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* IBM Corporation - initial API and implementation |
| 10 |
*******************************************************************************/ |
| 11 |
package org.eclipse.jdt.ui.tests.core; |
| 12 |
|
| 13 |
import junit.framework.Test; |
| 14 |
import junit.framework.TestCase; |
| 15 |
import junit.framework.TestSuite; |
| 16 |
|
| 17 |
import org.eclipse.swt.SWT; |
| 18 |
import org.eclipse.swt.custom.StyleRange; |
| 19 |
import org.eclipse.swt.graphics.TextStyle; |
| 20 |
|
| 21 |
import org.eclipse.jdt.internal.ui.viewsupport.ColoredString; |
| 22 |
import org.eclipse.jdt.internal.ui.viewsupport.ColoredString.Style; |
| 23 |
|
| 24 |
public class ColoredStringsTest extends TestCase { |
| 25 |
|
| 26 |
public static class TestStyle extends Style { |
| 27 |
|
| 28 |
public final int borderStyle; |
| 29 |
|
| 30 |
public TestStyle(int borderStyle) { |
| 31 |
this.borderStyle= borderStyle; |
| 32 |
} |
| 33 |
|
| 34 |
public void applyStyles(TextStyle textStyle) { |
| 35 |
textStyle.borderStyle= borderStyle; |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
public static final TestStyle STYLE1= new TestStyle(SWT.BORDER_DOT); |
| 40 |
public static final TestStyle STYLE2= new TestStyle(SWT.BORDER_DASH); |
| 41 |
|
| 42 |
|
| 43 |
public static Test allTests() { |
| 44 |
return new TestSuite(ColoredStringsTest.class); |
| 45 |
} |
| 46 |
|
| 47 |
public static Test suite() { |
| 48 |
return allTests(); |
| 49 |
} |
| 50 |
|
| 51 |
public void testEmpty() { |
| 52 |
ColoredString coloredString= new ColoredString(); |
| 53 |
|
| 54 |
String str= ""; |
| 55 |
|
| 56 |
assertEquals(str.length(), coloredString.length()); |
| 57 |
assertEquals(str, coloredString.getString()); |
| 58 |
assertEquals(coloredString.getStyleRanges().length, 0); |
| 59 |
} |
| 60 |
|
| 61 |
public void testAppendString1() { |
| 62 |
ColoredString coloredString= new ColoredString(); |
| 63 |
|
| 64 |
String str= "Hello"; |
| 65 |
|
| 66 |
coloredString.append(str, STYLE1); |
| 67 |
|
| 68 |
assertEquals(str.length(), coloredString.length()); |
| 69 |
assertEquals(str, coloredString.getString()); |
| 70 |
StyleRange[] styleRanges= coloredString.getStyleRanges(); |
| 71 |
assertEquals(1, styleRanges.length); |
| 72 |
|
| 73 |
assertEquals(styleRanges[0], STYLE1, 0, str.length()); |
| 74 |
} |
| 75 |
|
| 76 |
public void testAppendString2() { |
| 77 |
ColoredString coloredString= new ColoredString(); |
| 78 |
|
| 79 |
String str1= "Hello"; |
| 80 |
String str2= "You"; |
| 81 |
coloredString.append(str1); |
| 82 |
coloredString.append(str2, STYLE1); |
| 83 |
|
| 84 |
String res= str1 + str2; |
| 85 |
|
| 86 |
assertEquals(res.length(), coloredString.length()); |
| 87 |
assertEquals(res, coloredString.getString()); |
| 88 |
StyleRange[] styleRanges= coloredString.getStyleRanges(); |
| 89 |
assertEquals(1, styleRanges.length); |
| 90 |
assertEquals(styleRanges[0], STYLE1, str1.length(), str2.length()); |
| 91 |
} |
| 92 |
|
| 93 |
public void testAppendString3() { |
| 94 |
ColoredString coloredString= new ColoredString(); |
| 95 |
|
| 96 |
String str1= "Hello"; |
| 97 |
String str2= "You"; |
| 98 |
coloredString.append(str1, STYLE1); |
| 99 |
coloredString.append(str2); |
| 100 |
|
| 101 |
String res= str1 + str2; |
| 102 |
|
| 103 |
assertEquals(res.length(), coloredString.length()); |
| 104 |
assertEquals(res, coloredString.getString()); |
| 105 |
StyleRange[] styleRanges= coloredString.getStyleRanges(); |
| 106 |
assertEquals(1, styleRanges.length); |
| 107 |
assertEquals(styleRanges[0], STYLE1, 0, str1.length()); |
| 108 |
} |
| 109 |
|
| 110 |
public void testAppendString4() { |
| 111 |
ColoredString coloredString= new ColoredString(); |
| 112 |
|
| 113 |
String str1= "Hello"; |
| 114 |
String str2= "You"; |
| 115 |
coloredString.append(str1); |
| 116 |
coloredString.append(str2, STYLE1); |
| 117 |
coloredString.append(str2, STYLE1); |
| 118 |
|
| 119 |
String res= str1 + str2 + str2; |
| 120 |
|
| 121 |
assertEquals(res.length(), coloredString.length()); |
| 122 |
assertEquals(res, coloredString.getString()); |
| 123 |
StyleRange[] styleRanges= coloredString.getStyleRanges(); |
| 124 |
assertEquals(1, styleRanges.length); |
| 125 |
assertEquals(styleRanges[0], STYLE1, str1.length(), str2.length() * 2); |
| 126 |
} |
| 127 |
|
| 128 |
public void testAppendString5() { |
| 129 |
ColoredString coloredString= new ColoredString(); |
| 130 |
|
| 131 |
String str1= "Hello"; |
| 132 |
String str2= "You"; |
| 133 |
String str3= "Me"; |
| 134 |
coloredString.append(str1); |
| 135 |
coloredString.append(str2, STYLE1); |
| 136 |
coloredString.append(str3, STYLE2); |
| 137 |
|
| 138 |
String res= str1 + str2 + str3; |
| 139 |
|
| 140 |
assertEquals(res.length(), coloredString.length()); |
| 141 |
assertEquals(res, coloredString.getString()); |
| 142 |
StyleRange[] styleRanges= coloredString.getStyleRanges(); |
| 143 |
assertEquals(2, styleRanges.length); |
| 144 |
assertEquals(styleRanges[0], STYLE1, str1.length(), str2.length()); |
| 145 |
assertEquals(styleRanges[1], STYLE2, str1.length() + str2.length(), str3.length()); |
| 146 |
} |
| 147 |
|
| 148 |
public void testAppendString6() { |
| 149 |
ColoredString coloredString= new ColoredString(); |
| 150 |
|
| 151 |
String str1= "Hello"; |
| 152 |
String str2= "You"; |
| 153 |
String str3= "Me"; |
| 154 |
coloredString.append(str1, STYLE1); |
| 155 |
coloredString.append(str2); |
| 156 |
coloredString.append(str3, STYLE2); |
| 157 |
|
| 158 |
String res= str1 + str2 + str3; |
| 159 |
|
| 160 |
assertEquals(res.length(), coloredString.length()); |
| 161 |
assertEquals(res, coloredString.getString()); |
| 162 |
StyleRange[] styleRanges= coloredString.getStyleRanges(); |
| 163 |
assertEquals(2, styleRanges.length); |
| 164 |
assertEquals(styleRanges[0], STYLE1, 0, str1.length()); |
| 165 |
assertEquals(styleRanges[1], STYLE2, str1.length() + str2.length(), str3.length()); |
| 166 |
} |
| 167 |
|
| 168 |
public void testAppendString7() { |
| 169 |
ColoredString coloredString= new ColoredString(); |
| 170 |
|
| 171 |
String str1= "Hello"; |
| 172 |
String str2= ""; |
| 173 |
String str3= "Me"; |
| 174 |
coloredString.append(str1, STYLE1); |
| 175 |
coloredString.append(str2, STYLE2); |
| 176 |
coloredString.append(str3, STYLE1); |
| 177 |
|
| 178 |
String res= str1 + str2 + str3; |
| 179 |
|
| 180 |
assertEquals(res.length(), coloredString.length()); |
| 181 |
assertEquals(res, coloredString.getString()); |
| 182 |
StyleRange[] styleRanges= coloredString.getStyleRanges(); |
| 183 |
assertEquals(1, styleRanges.length); |
| 184 |
assertEquals(styleRanges[0], STYLE1, 0, res.length()); |
| 185 |
} |
| 186 |
|
| 187 |
public void testAppendChar1() { |
| 188 |
ColoredString coloredString= new ColoredString(); |
| 189 |
|
| 190 |
coloredString.append('H', STYLE1); |
| 191 |
coloredString.append('2', STYLE2); |
| 192 |
coloredString.append('O', STYLE1); |
| 193 |
|
| 194 |
String res= "H2O"; |
| 195 |
|
| 196 |
assertEquals(res.length(), coloredString.length()); |
| 197 |
assertEquals(res, coloredString.getString()); |
| 198 |
StyleRange[] styleRanges= coloredString.getStyleRanges(); |
| 199 |
assertEquals(3, styleRanges.length); |
| 200 |
assertEquals(styleRanges[0], STYLE1, 0, 1); |
| 201 |
assertEquals(styleRanges[1], STYLE2, 1, 1); |
| 202 |
assertEquals(styleRanges[2], STYLE1, 2, 1); |
| 203 |
} |
| 204 |
|
| 205 |
public void testAppendChar2() { |
| 206 |
ColoredString coloredString= new ColoredString(); |
| 207 |
|
| 208 |
coloredString.append('H', STYLE1); |
| 209 |
coloredString.append('2'); |
| 210 |
coloredString.append('O', STYLE2); |
| 211 |
|
| 212 |
String res= "H2O"; |
| 213 |
|
| 214 |
assertEquals(res.length(), coloredString.length()); |
| 215 |
assertEquals(res, coloredString.getString()); |
| 216 |
StyleRange[] styleRanges= coloredString.getStyleRanges(); |
| 217 |
assertEquals(2, styleRanges.length); |
| 218 |
assertEquals(styleRanges[0], STYLE1, 0, 1); |
| 219 |
assertEquals(styleRanges[1], STYLE2, 2, 1); |
| 220 |
} |
| 221 |
|
| 222 |
public void testAppendColoredString1() { |
| 223 |
ColoredString other= new ColoredString(); |
| 224 |
|
| 225 |
String str2= "You"; |
| 226 |
String str3= "Me"; |
| 227 |
other.append(str2, STYLE1); |
| 228 |
other.append(str3, STYLE2); |
| 229 |
|
| 230 |
String str1= "We"; |
| 231 |
|
| 232 |
ColoredString coloredString= new ColoredString(str1); |
| 233 |
coloredString.append(other); |
| 234 |
|
| 235 |
String res= str1 + str2 + str3; |
| 236 |
|
| 237 |
assertEquals(res.length(), coloredString.length()); |
| 238 |
assertEquals(res, coloredString.getString()); |
| 239 |
StyleRange[] styleRanges= coloredString.getStyleRanges(); |
| 240 |
assertEquals(2, styleRanges.length); |
| 241 |
assertEquals(styleRanges[0], STYLE1, str1.length(), str2.length()); |
| 242 |
assertEquals(styleRanges[1], STYLE2, str1.length() + str2.length(), str3.length()); |
| 243 |
} |
| 244 |
|
| 245 |
public void testAppendColoredString2() { |
| 246 |
ColoredString other= new ColoredString(); |
| 247 |
|
| 248 |
String str2= "You"; |
| 249 |
String str3= "Me"; |
| 250 |
other.append(str2, STYLE1); |
| 251 |
other.append(str3, STYLE2); |
| 252 |
|
| 253 |
String str1= "We"; |
| 254 |
|
| 255 |
ColoredString coloredString= new ColoredString(str1, STYLE1); |
| 256 |
coloredString.append(other); |
| 257 |
|
| 258 |
String res= str1 + str2 + str3; |
| 259 |
|
| 260 |
assertEquals(res.length(), coloredString.length()); |
| 261 |
assertEquals(res, coloredString.getString()); |
| 262 |
StyleRange[] styleRanges= coloredString.getStyleRanges(); |
| 263 |
assertEquals(2, styleRanges.length); |
| 264 |
assertEquals(styleRanges[0], STYLE1, 0, str1.length() + str2.length()); |
| 265 |
assertEquals(styleRanges[1], STYLE2, str1.length() + str2.length(), str3.length()); |
| 266 |
} |
| 267 |
|
| 268 |
public void testAppendColoredString3() { |
| 269 |
|
| 270 |
ColoredString other= new ColoredString(); |
| 271 |
|
| 272 |
String str2= "You"; |
| 273 |
String str3= "Me"; |
| 274 |
other.append(str2); |
| 275 |
other.append(str3, STYLE2); |
| 276 |
|
| 277 |
String str1= "We"; |
| 278 |
|
| 279 |
ColoredString coloredString= new ColoredString(str1, STYLE1); |
| 280 |
coloredString.append(other); |
| 281 |
|
| 282 |
String res= str1 + str2 + str3; |
| 283 |
|
| 284 |
assertEquals(res.length(), coloredString.length()); |
| 285 |
assertEquals(res, coloredString.getString()); |
| 286 |
StyleRange[] styleRanges= coloredString.getStyleRanges(); |
| 287 |
assertEquals(2, styleRanges.length); |
| 288 |
assertEquals(styleRanges[0], STYLE1, 0, str1.length()); |
| 289 |
assertEquals(styleRanges[1], STYLE2, str1.length() + str2.length(), str3.length()); |
| 290 |
} |
| 291 |
|
| 292 |
public void testAppendColoredString4() { |
| 293 |
|
| 294 |
ColoredString other= new ColoredString(); |
| 295 |
|
| 296 |
String str2= "You"; |
| 297 |
String str3= "Me"; |
| 298 |
other.append(str2, STYLE2); |
| 299 |
other.append(str3); |
| 300 |
|
| 301 |
String str1= "We"; |
| 302 |
|
| 303 |
ColoredString coloredString= new ColoredString(str1, STYLE1); |
| 304 |
coloredString.append(other); |
| 305 |
|
| 306 |
String res= str1 + str2 + str3; |
| 307 |
|
| 308 |
assertEquals(res.length(), coloredString.length()); |
| 309 |
assertEquals(res, coloredString.getString()); |
| 310 |
StyleRange[] styleRanges= coloredString.getStyleRanges(); |
| 311 |
assertEquals(2, styleRanges.length); |
| 312 |
assertEquals(styleRanges[0], STYLE1, 0, str1.length()); |
| 313 |
assertEquals(styleRanges[1], STYLE2, str1.length(), str2.length()); |
| 314 |
} |
| 315 |
|
| 316 |
public void testAppendColoredString5() { |
| 317 |
ColoredString other= new ColoredString(); |
| 318 |
|
| 319 |
String str2= "You"; |
| 320 |
String str3= "Me"; |
| 321 |
other.append(str2); |
| 322 |
other.append(str3, STYLE1); |
| 323 |
|
| 324 |
String str1= "We"; |
| 325 |
|
| 326 |
ColoredString coloredString= new ColoredString(str1); |
| 327 |
coloredString.append(other); |
| 328 |
|
| 329 |
String res= str1 + str2 + str3; |
| 330 |
|
| 331 |
assertEquals(res.length(), coloredString.length()); |
| 332 |
assertEquals(res, coloredString.getString()); |
| 333 |
StyleRange[] styleRanges= coloredString.getStyleRanges(); |
| 334 |
assertEquals(1, styleRanges.length); |
| 335 |
assertEquals(styleRanges[0], STYLE1, str1.length() + str2.length(), str3.length()); |
| 336 |
} |
| 337 |
|
| 338 |
public void testSetStyle1() { |
| 339 |
String str1= "One"; |
| 340 |
String str2= "Two"; |
| 341 |
String str3= "Three"; |
| 342 |
|
| 343 |
String res= str1 + str2 + str3; |
| 344 |
|
| 345 |
ColoredString coloredString= new ColoredString(); |
| 346 |
coloredString.append(res); |
| 347 |
|
| 348 |
coloredString.setStyle(0, str1.length(), STYLE1); |
| 349 |
|
| 350 |
assertEquals(res.length(), coloredString.length()); |
| 351 |
assertEquals(res, coloredString.getString()); |
| 352 |
StyleRange[] styleRanges= coloredString.getStyleRanges(); |
| 353 |
assertEquals(1, styleRanges.length); |
| 354 |
assertEquals(styleRanges[0], STYLE1, 0, str1.length()); |
| 355 |
} |
| 356 |
|
| 357 |
public void testSetStyle2() { |
| 358 |
|
| 359 |
String str1= "One"; |
| 360 |
String str2= "Two"; |
| 361 |
String str3= "Three"; |
| 362 |
|
| 363 |
String res= str1 + str2 + str3; |
| 364 |
|
| 365 |
ColoredString coloredString= new ColoredString(); |
| 366 |
coloredString.append(res); |
| 367 |
|
| 368 |
coloredString.setStyle(str1.length(), str2.length(), STYLE1); |
| 369 |
|
| 370 |
assertEquals(res.length(), coloredString.length()); |
| 371 |
assertEquals(res, coloredString.getString()); |
| 372 |
StyleRange[] styleRanges= coloredString.getStyleRanges(); |
| 373 |
assertEquals(1, styleRanges.length); |
| 374 |
assertEquals(styleRanges[0], STYLE1, str1.length(), str2.length()); |
| 375 |
} |
| 376 |
|
| 377 |
public void testSetStyle3() { |
| 378 |
|
| 379 |
String str1= "One"; |
| 380 |
String str2= "Two"; |
| 381 |
String str3= "Three"; |
| 382 |
|
| 383 |
String res= str1 + str2 + str3; |
| 384 |
|
| 385 |
ColoredString coloredString= new ColoredString(); |
| 386 |
coloredString.append(res); |
| 387 |
|
| 388 |
coloredString.setStyle(str1.length(), res.length() - str1.length(), STYLE1); |
| 389 |
|
| 390 |
assertEquals(res.length(), coloredString.length()); |
| 391 |
assertEquals(res, coloredString.getString()); |
| 392 |
StyleRange[] styleRanges= coloredString.getStyleRanges(); |
| 393 |
assertEquals(1, styleRanges.length); |
| 394 |
assertEquals(styleRanges[0], STYLE1, str1.length(), res.length() - str1.length()); |
| 395 |
} |
| 396 |
|
| 397 |
public void testSetStyle4() { |
| 398 |
|
| 399 |
String str1= "One"; |
| 400 |
String str2= "Two"; |
| 401 |
String str3= "Three"; |
| 402 |
|
| 403 |
String res= str1 + str2 + str3; |
| 404 |
|
| 405 |
ColoredString coloredString= new ColoredString(); |
| 406 |
coloredString.append(res); |
| 407 |
|
| 408 |
coloredString.setStyle(0, res.length(), STYLE1); |
| 409 |
|
| 410 |
assertEquals(res.length(), coloredString.length()); |
| 411 |
assertEquals(res, coloredString.getString()); |
| 412 |
StyleRange[] styleRanges= coloredString.getStyleRanges(); |
| 413 |
assertEquals(1, styleRanges.length); |
| 414 |
assertEquals(styleRanges[0], STYLE1, 0, res.length()); |
| 415 |
} |
| 416 |
|
| 417 |
public void testSetStyle5() { |
| 418 |
|
| 419 |
String str1= "One"; |
| 420 |
String str2= "Two"; |
| 421 |
String str3= "Three"; |
| 422 |
|
| 423 |
String res= str1 + str2 + str3; |
| 424 |
|
| 425 |
ColoredString coloredString= new ColoredString(); |
| 426 |
coloredString.append(res); |
| 427 |
|
| 428 |
coloredString.setStyle(0, res.length(), null); |
| 429 |
|
| 430 |
assertEquals(res.length(), coloredString.length()); |
| 431 |
assertEquals(res, coloredString.getString()); |
| 432 |
StyleRange[] styleRanges= coloredString.getStyleRanges(); |
| 433 |
assertEquals(0, styleRanges.length); |
| 434 |
} |
| 435 |
|
| 436 |
public void testSetStyle6() { |
| 437 |
|
| 438 |
String str1= "One"; |
| 439 |
String str2= "Two"; |
| 440 |
|
| 441 |
String res= str1 + str2; |
| 442 |
|
| 443 |
ColoredString coloredString= new ColoredString(str1, STYLE1); |
| 444 |
coloredString.append(str2); |
| 445 |
|
| 446 |
coloredString.setStyle(str1.length(), str2.length(), STYLE2); |
| 447 |
|
| 448 |
assertEquals(res.length(), coloredString.length()); |
| 449 |
assertEquals(res, coloredString.getString()); |
| 450 |
StyleRange[] styleRanges= coloredString.getStyleRanges(); |
| 451 |
assertEquals(2, styleRanges.length); |
| 452 |
assertEquals(styleRanges[0], STYLE1, 0, str1.length()); |
| 453 |
assertEquals(styleRanges[1], STYLE2, str1.length(), str2.length()); |
| 454 |
} |
| 455 |
|
| 456 |
public void testSetStyle7() { |
| 457 |
|
| 458 |
String str1= "One"; |
| 459 |
String str2= "Two"; |
| 460 |
|
| 461 |
String res= str1 + str2; |
| 462 |
|
| 463 |
ColoredString coloredString= new ColoredString(str1); |
| 464 |
coloredString.append(str2, STYLE1); |
| 465 |
|
| 466 |
coloredString.setStyle(0, str1.length(), STYLE2); |
| 467 |
|
| 468 |
assertEquals(res.length(), coloredString.length()); |
| 469 |
assertEquals(res, coloredString.getString()); |
| 470 |
StyleRange[] styleRanges= coloredString.getStyleRanges(); |
| 471 |
assertEquals(2, styleRanges.length); |
| 472 |
assertEquals(styleRanges[0], STYLE2, 0, str1.length()); |
| 473 |
assertEquals(styleRanges[1], STYLE1, str1.length(), str2.length()); |
| 474 |
} |
| 475 |
|
| 476 |
public void testSetStyle8() { |
| 477 |
|
| 478 |
String str1= "One"; |
| 479 |
String str2= "Two"; |
| 480 |
|
| 481 |
String res= str1 + str2; |
| 482 |
|
| 483 |
ColoredString coloredString= new ColoredString(); |
| 484 |
coloredString.append(str1, STYLE1); |
| 485 |
coloredString.append(str2, STYLE2); |
| 486 |
|
| 487 |
coloredString.setStyle(0, str1.length(), STYLE2); |
| 488 |
|
| 489 |
assertEquals(res.length(), coloredString.length()); |
| 490 |
assertEquals(res, coloredString.getString()); |
| 491 |
StyleRange[] styleRanges= coloredString.getStyleRanges(); |
| 492 |
assertEquals(1, styleRanges.length); |
| 493 |
assertEquals(styleRanges[0], STYLE2, 0, res.length()); |
| 494 |
} |
| 495 |
|
| 496 |
public void testSetStyle9() { |
| 497 |
|
| 498 |
String str1= "One"; |
| 499 |
String str2= "Two"; |
| 500 |
|
| 501 |
String res= str1 + str2; |
| 502 |
|
| 503 |
ColoredString coloredString= new ColoredString(); |
| 504 |
coloredString.append(str1, STYLE1); |
| 505 |
coloredString.append(str2, STYLE2); |
| 506 |
|
| 507 |
coloredString.setStyle(0, res.length(), null); |
| 508 |
|
| 509 |
assertEquals(res.length(), coloredString.length()); |
| 510 |
assertEquals(res, coloredString.getString()); |
| 511 |
StyleRange[] styleRanges= coloredString.getStyleRanges(); |
| 512 |
assertEquals(0, styleRanges.length); |
| 513 |
} |
| 514 |
|
| 515 |
public void testSetStyle10() { |
| 516 |
|
| 517 |
String str1= "One"; |
| 518 |
String str2= "Two"; |
| 519 |
|
| 520 |
String res= str1 + str2; |
| 521 |
|
| 522 |
ColoredString coloredString= new ColoredString(); |
| 523 |
coloredString.append(str1, STYLE1); |
| 524 |
coloredString.append(str2, STYLE2); |
| 525 |
|
| 526 |
coloredString.setStyle(1, res.length() - 2, null); |
| 527 |
|
| 528 |
assertEquals(res.length(), coloredString.length()); |
| 529 |
assertEquals(res, coloredString.getString()); |
| 530 |
StyleRange[] styleRanges= coloredString.getStyleRanges(); |
| 531 |
assertEquals(2, styleRanges.length); |
| 532 |
|
| 533 |
assertEquals(styleRanges[0], STYLE1, 0, 1); |
| 534 |
assertEquals(styleRanges[1], STYLE2, res.length() - 1, 1); |
| 535 |
} |
| 536 |
|
| 537 |
public void testSetStyle11() { |
| 538 |
|
| 539 |
String str1= "One"; |
| 540 |
String str2= "Two"; |
| 541 |
|
| 542 |
String res= str1 + str2; |
| 543 |
|
| 544 |
ColoredString coloredString= new ColoredString(); |
| 545 |
coloredString.append(str1, STYLE1); |
| 546 |
coloredString.append(str2, STYLE2); |
| 547 |
|
| 548 |
coloredString.setStyle(1, res.length() - 1, STYLE1); |
| 549 |
|
| 550 |
assertEquals(res.length(), coloredString.length()); |
| 551 |
assertEquals(res, coloredString.getString()); |
| 552 |
StyleRange[] styleRanges= coloredString.getStyleRanges(); |
| 553 |
assertEquals(1, styleRanges.length); |
| 554 |
|
| 555 |
assertEquals(styleRanges[0], STYLE1, 0, res.length()); |
| 556 |
} |
| 557 |
|
| 558 |
public void testSetStyle12() { |
| 559 |
|
| 560 |
String str1= "One"; |
| 561 |
String str2= "Two"; |
| 562 |
|
| 563 |
String res= str1 + str2; |
| 564 |
|
| 565 |
ColoredString coloredString= new ColoredString(); |
| 566 |
coloredString.append(str1, STYLE1); |
| 567 |
coloredString.append(str2, STYLE2); |
| 568 |
|
| 569 |
coloredString.setStyle(0, res.length() - 1, STYLE2); |
| 570 |
|
| 571 |
assertEquals(res.length(), coloredString.length()); |
| 572 |
assertEquals(res, coloredString.getString()); |
| 573 |
StyleRange[] styleRanges= coloredString.getStyleRanges(); |
| 574 |
assertEquals(1, styleRanges.length); |
| 575 |
|
| 576 |
assertEquals(styleRanges[0], STYLE2, 0, res.length()); |
| 577 |
} |
| 578 |
|
| 579 |
public void testSetStyle13() { |
| 580 |
|
| 581 |
String str1= "One"; |
| 582 |
String str2= "Two"; |
| 583 |
|
| 584 |
String res= str1 + str2; |
| 585 |
|
| 586 |
ColoredString coloredString= new ColoredString(); |
| 587 |
coloredString.append(str1, STYLE1); |
| 588 |
coloredString.append(str2, STYLE2); |
| 589 |
|
| 590 |
coloredString.setStyle(1, res.length() - 2, STYLE1); |
| 591 |
|
| 592 |
assertEquals(res.length(), coloredString.length()); |
| 593 |
assertEquals(res, coloredString.getString()); |
| 594 |
StyleRange[] styleRanges= coloredString.getStyleRanges(); |
| 595 |
assertEquals(2, styleRanges.length); |
| 596 |
|
| 597 |
assertEquals(styleRanges[0], STYLE1, 0, res.length() - 1); |
| 598 |
assertEquals(styleRanges[1], STYLE2, res.length() - 1, 1); |
| 599 |
} |
| 600 |
|
| 601 |
public void testSetStyle14() { |
| 602 |
|
| 603 |
String str1= "One"; |
| 604 |
String str2= "Two"; |
| 605 |
|
| 606 |
String res= str1 + str2; |
| 607 |
|
| 608 |
ColoredString coloredString= new ColoredString(); |
| 609 |
coloredString.append(str1, STYLE1); |
| 610 |
coloredString.append(str2, STYLE2); |
| 611 |
|
| 612 |
coloredString.setStyle(1, res.length() - 2, STYLE2); |
| 613 |
|
| 614 |
assertEquals(res.length(), coloredString.length()); |
| 615 |
assertEquals(res, coloredString.getString()); |
| 616 |
StyleRange[] styleRanges= coloredString.getStyleRanges(); |
| 617 |
assertEquals(2, styleRanges.length); |
| 618 |
|
| 619 |
assertEquals(styleRanges[0], STYLE1, 0, 1); |
| 620 |
assertEquals(styleRanges[1], STYLE2, 1, res.length() - 1); |
| 621 |
} |
| 622 |
|
| 623 |
public void testSetStyle15() { |
| 624 |
|
| 625 |
String str1= "One"; |
| 626 |
String str2= "Two"; |
| 627 |
|
| 628 |
String res= str1 + str2; |
| 629 |
|
| 630 |
ColoredString coloredString= new ColoredString(); |
| 631 |
coloredString.append(str1, null); |
| 632 |
coloredString.append(str2, STYLE2); |
| 633 |
|
| 634 |
coloredString.setStyle(0, 1, STYLE1); |
| 635 |
|
| 636 |
assertEquals(res.length(), coloredString.length()); |
| 637 |
assertEquals(res, coloredString.getString()); |
| 638 |
StyleRange[] styleRanges= coloredString.getStyleRanges(); |
| 639 |
assertEquals(2, styleRanges.length); |
| 640 |
|
| 641 |
assertEquals(styleRanges[0], STYLE1, 0, 1); |
| 642 |
assertEquals(styleRanges[1], STYLE2, str1.length(), str2.length()); |
| 643 |
} |
| 644 |
|
| 645 |
public void testSetStyle16() { |
| 646 |
|
| 647 |
String res= "H2O"; |
| 648 |
|
| 649 |
ColoredString coloredString= new ColoredString(); |
| 650 |
coloredString.append('H', null); |
| 651 |
coloredString.append('2', STYLE1); |
| 652 |
coloredString.append('O', STYLE2); |
| 653 |
|
| 654 |
coloredString.setStyle(0, res.length(), STYLE1); |
| 655 |
|
| 656 |
assertEquals(res.length(), coloredString.length()); |
| 657 |
assertEquals(res, coloredString.getString()); |
| 658 |
StyleRange[] styleRanges= coloredString.getStyleRanges(); |
| 659 |
assertEquals(1, styleRanges.length); |
| 660 |
|
| 661 |
assertEquals(styleRanges[0], STYLE1, 0, res.length()); |
| 662 |
} |
| 663 |
|
| 664 |
public void testSetStyle17() { |
| 665 |
|
| 666 |
String res= "H2O"; |
| 667 |
|
| 668 |
ColoredString coloredString= new ColoredString(); |
| 669 |
coloredString.append('H', null); |
| 670 |
coloredString.append('2', STYLE1); |
| 671 |
coloredString.append('O', STYLE2); |
| 672 |
|
| 673 |
coloredString.setStyle(0, res.length(), null); |
| 674 |
|
| 675 |
assertEquals(res.length(), coloredString.length()); |
| 676 |
assertEquals(res, coloredString.getString()); |
| 677 |
StyleRange[] styleRanges= coloredString.getStyleRanges(); |
| 678 |
assertEquals(0, styleRanges.length); |
| 679 |
} |
| 680 |
|
| 681 |
public void testSetStyle18() { |
| 682 |
String res= "H2OH2O"; |
| 683 |
|
| 684 |
ColoredString coloredString= new ColoredString(); |
| 685 |
coloredString.append('H', null); |
| 686 |
coloredString.append('2', STYLE1); |
| 687 |
coloredString.append('O', STYLE2); |
| 688 |
coloredString.append('H', null); |
| 689 |
coloredString.append('2', STYLE2); |
| 690 |
coloredString.append('O', STYLE1); |
| 691 |
|
| 692 |
coloredString.setStyle(1, res.length() - 2, STYLE1); |
| 693 |
|
| 694 |
assertEquals(res.length(), coloredString.length()); |
| 695 |
assertEquals(res, coloredString.getString()); |
| 696 |
StyleRange[] styleRanges= coloredString.getStyleRanges(); |
| 697 |
assertEquals(1, styleRanges.length); |
| 698 |
|
| 699 |
assertEquals(styleRanges[0], STYLE1, 1, res.length() - 1); |
| 700 |
} |
| 701 |
|
| 702 |
public void testSetStyle19() { |
| 703 |
String res= "O2O2O2O2O2O2"; |
| 704 |
|
| 705 |
ColoredString coloredString= new ColoredString(); |
| 706 |
coloredString.append("O2", null); |
| 707 |
coloredString.append("O2", STYLE1); |
| 708 |
coloredString.append("O2", STYLE2); |
| 709 |
coloredString.append("O2", STYLE1); |
| 710 |
coloredString.append("O2", STYLE2); |
| 711 |
coloredString.append("O2", null); |
| 712 |
|
| 713 |
coloredString.setStyle(1, res.length() - 2, STYLE1); |
| 714 |
|
| 715 |
assertEquals(res.length(), coloredString.length()); |
| 716 |
assertEquals(res, coloredString.getString()); |
| 717 |
StyleRange[] styleRanges= coloredString.getStyleRanges(); |
| 718 |
assertEquals(1, styleRanges.length); |
| 719 |
|
| 720 |
assertEquals(styleRanges[0], STYLE1, 1, res.length() - 2); |
| 721 |
} |
| 722 |
|
| 723 |
public void testSetStyle20() { |
| 724 |
String res= "O2O2O2O2O2O2"; |
| 725 |
|
| 726 |
ColoredString coloredString= new ColoredString(); |
| 727 |
coloredString.append("O2", null); |
| 728 |
coloredString.append("O2", STYLE1); |
| 729 |
coloredString.append("O2", STYLE2); |
| 730 |
coloredString.append("O2", STYLE1); |
| 731 |
coloredString.append("O2", STYLE2); |
| 732 |
coloredString.append("O2", null); |
| 733 |
|
| 734 |
coloredString.setStyle(3, 6, null); |
| 735 |
|
| 736 |
assertEquals(res.length(), coloredString.length()); |
| 737 |
assertEquals(res, coloredString.getString()); |
| 738 |
StyleRange[] styleRanges= coloredString.getStyleRanges(); |
| 739 |
assertEquals(2, styleRanges.length); |
| 740 |
|
| 741 |
assertEquals(styleRanges[0], STYLE1, 2, 1); |
| 742 |
assertEquals(styleRanges[1], STYLE2, 9, 1); |
| 743 |
} |
| 744 |
|
| 745 |
public void testSetStyle21() { |
| 746 |
String res= "O2O2O2O2O2O2"; |
| 747 |
|
| 748 |
ColoredString coloredString= new ColoredString(); |
| 749 |
coloredString.append("O2", null); |
| 750 |
coloredString.append("O2", STYLE1); |
| 751 |
coloredString.append("O2", STYLE2); |
| 752 |
coloredString.append("O2", STYLE1); |
| 753 |
coloredString.append("O2", STYLE2); |
| 754 |
coloredString.append("O2", null); |
| 755 |
|
| 756 |
coloredString.setStyle(3, 6, STYLE1); |
| 757 |
coloredString.setStyle(3, 6, null); |
| 758 |
|
| 759 |
assertEquals(res.length(), coloredString.length()); |
| 760 |
assertEquals(res, coloredString.getString()); |
| 761 |
StyleRange[] styleRanges= coloredString.getStyleRanges(); |
| 762 |
assertEquals(2, styleRanges.length); |
| 763 |
|
| 764 |
assertEquals(styleRanges[0], STYLE1, 2, 1); |
| 765 |
assertEquals(styleRanges[1], STYLE2, 9, 1); |
| 766 |
} |
| 767 |
|
| 768 |
public void testCombination1() { |
| 769 |
String str1= "One"; |
| 770 |
String str2= "Two"; |
| 771 |
|
| 772 |
String res= str1 + str2 + str1; |
| 773 |
|
| 774 |
ColoredString coloredString= new ColoredString(); |
| 775 |
coloredString.append(str1, null); |
| 776 |
coloredString.append(str2, STYLE2); |
| 777 |
|
| 778 |
coloredString.setStyle(str1.length(), str2.length(), STYLE1); |
| 779 |
|
| 780 |
coloredString.append(str1, STYLE1); |
| 781 |
|
| 782 |
assertEquals(res.length(), coloredString.length()); |
| 783 |
assertEquals(res, coloredString.getString()); |
| 784 |
StyleRange[] styleRanges= coloredString.getStyleRanges(); |
| 785 |
assertEquals(1, styleRanges.length); |
| 786 |
|
| 787 |
assertEquals(styleRanges[0], STYLE1, str1.length(), str2.length() + str1.length()); |
| 788 |
} |
| 789 |
|
| 790 |
|
| 791 |
private void assertEquals(StyleRange range, TestStyle style, int offset, int length) { |
| 792 |
assertEquals(offset, range.start); |
| 793 |
assertEquals(length, range.length); |
| 794 |
assertEquals(style.borderStyle, range.borderStyle); |
| 795 |
} |
| 796 |
|
| 797 |
|
| 798 |
} |