1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.text.cts; 18 19 import android.graphics.Bitmap; 20 import android.graphics.Canvas; 21 import android.graphics.Paint; 22 import android.graphics.Path; 23 import android.graphics.Rect; 24 import android.graphics.Bitmap.Config; 25 import android.test.AndroidTestCase; 26 import android.text.Layout; 27 import android.text.Spannable; 28 import android.text.SpannableString; 29 import android.text.TextPaint; 30 import android.text.Layout.Alignment; 31 import android.text.style.StrikethroughSpan; 32 33 import dalvik.annotation.BrokenTest; 34 import dalvik.annotation.TestTargets; 35 import dalvik.annotation.TestLevel; 36 import dalvik.annotation.TestTargetClass; 37 import dalvik.annotation.TestTargetNew; 38 import dalvik.annotation.ToBeFixed; 39 40 @TestTargetClass(Layout.class) 41 public class LayoutTest extends AndroidTestCase { 42 private final static int LINE_COUNT = 5; 43 private final static int LINE_HEIGHT = 12; 44 private final static int LINE_DESCENT = 4; 45 private final static CharSequence LAYOUT_TEXT = "alwei\t;sdfs\ndf @"; 46 47 private int mWidth; 48 private Layout.Alignment mAlign; 49 private float mSpacingmult; 50 private float mSpacingadd; 51 private SpannableString mSpannedText; 52 53 private TextPaint mTextPaint; 54 55 @Override setUp()56 protected void setUp() throws Exception { 57 super.setUp(); 58 mTextPaint = new TextPaint(); 59 mSpannedText = new SpannableString(LAYOUT_TEXT); 60 mSpannedText.setSpan(new StrikethroughSpan(), 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); 61 mWidth = 11; 62 mAlign = Alignment.ALIGN_CENTER; 63 mSpacingmult = 1; 64 mSpacingadd = 2; 65 } 66 67 @TestTargetNew( 68 level = TestLevel.COMPLETE, 69 method = "Layout", 70 args = {java.lang.CharSequence.class, android.text.TextPaint.class, int.class, 71 android.text.Layout.Alignment.class, float.class, float.class} 72 ) 73 @ToBeFixed(bug = "1417734", explanation = "should add @throws clause into javadoc " + 74 " of Layout constructor when the width is smaller than 0") testConstructor()75 public void testConstructor() { 76 new MockLayout(LAYOUT_TEXT, mTextPaint, mWidth, mAlign, mSpacingmult, mSpacingadd); 77 78 try { 79 new MockLayout(null, null, -1, null, 0, 0); 80 fail("should throw IllegalArgumentException here"); 81 } catch (IllegalArgumentException e) { 82 } 83 } 84 85 @TestTargetNew( 86 level = TestLevel.COMPLETE, 87 method = "draw", 88 args = {android.graphics.Canvas.class} 89 ) 90 @ToBeFixed(bug = "1386429", explanation = "can not get the" + 91 " package protected class Directions") testDraw1()92 public void testDraw1() { 93 Layout layout = new MockLayout(LAYOUT_TEXT, mTextPaint, mWidth, 94 mAlign, mSpacingmult, mSpacingadd); 95 layout.draw(new Canvas()); 96 97 try { 98 layout.draw(new Canvas(Bitmap.createBitmap(200, 200, Config.ARGB_4444))); 99 fail("should throw NullPointerException here"); 100 } catch (NullPointerException e) { 101 } 102 } 103 104 @TestTargetNew( 105 level = TestLevel.COMPLETE, 106 method = "draw", 107 args = {android.graphics.Canvas.class, android.graphics.Path.class, 108 android.graphics.Paint.class, int.class} 109 ) 110 @ToBeFixed(bug = "1386429", explanation = "can not get the" + 111 " package protected class Directions") testDraw2()112 public void testDraw2() { 113 Layout layout = new MockLayout(LAYOUT_TEXT, mTextPaint, mWidth, 114 mAlign, mSpacingmult, mSpacingadd); 115 layout.draw(new Canvas(), null, null, 0); 116 117 try { 118 Bitmap bitmap = Bitmap.createBitmap(200, 200,Config.ARGB_4444); 119 layout.draw(new Canvas(bitmap), null, null, 0); 120 fail("should throw NullPointerException here"); 121 } catch (NullPointerException e) { 122 } 123 124 try { 125 Bitmap bitmap = Bitmap.createBitmap(200, 200, null); 126 layout.draw(new Canvas(bitmap), new Path(), new Paint(), 2); 127 fail("should throw NullPointerException here"); 128 } catch (NullPointerException e) { 129 } 130 } 131 132 @TestTargetNew( 133 level = TestLevel.COMPLETE, 134 method = "getText", 135 args = {} 136 ) testGetText()137 public void testGetText() { 138 CharSequence text = "test case 1"; 139 Layout layout = new MockLayout(text, mTextPaint, mWidth, 140 mAlign, mSpacingmult, mSpacingadd); 141 assertEquals(text, layout.getText()); 142 143 layout = new MockLayout(null, mTextPaint, mWidth, mAlign, mSpacingmult, mSpacingadd); 144 assertNull(layout.getText()); 145 } 146 147 @TestTargetNew( 148 level = TestLevel.COMPLETE, 149 method = "getPaint", 150 args = {} 151 ) testGetPaint()152 public void testGetPaint() { 153 Layout layout = new MockLayout(LAYOUT_TEXT, mTextPaint, mWidth, 154 mAlign, mSpacingmult, mSpacingadd); 155 156 assertSame(mTextPaint, layout.getPaint()); 157 158 layout = new MockLayout(LAYOUT_TEXT, null, mWidth, mAlign, mSpacingmult, mSpacingadd); 159 assertNull(layout.getPaint()); 160 } 161 162 @TestTargetNew( 163 level = TestLevel.COMPLETE, 164 method = "getWidth", 165 args = {} 166 ) testGetWidth()167 public void testGetWidth() { 168 Layout layout = new MockLayout(LAYOUT_TEXT, mTextPaint, 10, 169 mAlign, mSpacingmult, mSpacingadd); 170 assertEquals(10, layout.getWidth()); 171 172 layout = new MockLayout(LAYOUT_TEXT, mTextPaint, 0, mAlign, mSpacingmult, mSpacingadd); 173 assertEquals(0, layout.getWidth()); 174 } 175 176 @TestTargetNew( 177 level = TestLevel.COMPLETE, 178 method = "getEllipsizedWidth", 179 args = {} 180 ) testGetEllipsizedWidth()181 public void testGetEllipsizedWidth() { 182 Layout layout = new MockLayout(LAYOUT_TEXT, mTextPaint, 15, 183 mAlign, mSpacingmult, mSpacingadd); 184 assertEquals(15, layout.getEllipsizedWidth()); 185 186 layout = new MockLayout(LAYOUT_TEXT, mTextPaint, 0, mAlign, mSpacingmult, mSpacingadd); 187 assertEquals(0, layout.getEllipsizedWidth()); 188 } 189 190 @TestTargetNew( 191 level = TestLevel.COMPLETE, 192 method = "increaseWidthTo", 193 args = {int.class} 194 ) 195 @ToBeFixed(bug = "1417734", explanation = "should add @throws clause into javadoc " + 196 " of Layout#increaseWidthTo(int) when the new width is smaller than old one") testIncreaseWidthTo()197 public void testIncreaseWidthTo() { 198 Layout layout = new MockLayout(LAYOUT_TEXT, mTextPaint, mWidth, 199 mAlign, mSpacingmult, mSpacingadd); 200 int oldWidth = layout.getWidth(); 201 202 layout.increaseWidthTo(oldWidth); 203 assertEquals(oldWidth, layout.getWidth()); 204 205 try { 206 layout.increaseWidthTo(oldWidth - 1); 207 fail("should throw runtime exception attempted to reduce Layout width"); 208 } catch (RuntimeException e) { 209 } 210 211 layout.increaseWidthTo(oldWidth + 1); 212 assertEquals(oldWidth + 1, layout.getWidth()); 213 } 214 215 @TestTargetNew( 216 level = TestLevel.COMPLETE, 217 method = "getHeight", 218 args = {} 219 ) testGetHeight()220 public void testGetHeight() { 221 Layout layout = new MockLayout(LAYOUT_TEXT, mTextPaint, mWidth, 222 mAlign, mSpacingmult, mSpacingadd); 223 assertEquals(60, layout.getHeight()); 224 } 225 226 @TestTargetNew( 227 level = TestLevel.COMPLETE, 228 method = "getAlignment", 229 args = {} 230 ) testGetAlignment()231 public void testGetAlignment() { 232 Layout layout = new MockLayout(LAYOUT_TEXT, mTextPaint, mWidth, 233 mAlign, mSpacingmult, mSpacingadd); 234 assertSame(mAlign, layout.getAlignment()); 235 236 layout = new MockLayout(LAYOUT_TEXT, mTextPaint, mWidth, null, mSpacingmult, mSpacingadd); 237 assertNull(layout.getAlignment()); 238 } 239 240 @TestTargetNew( 241 level = TestLevel.COMPLETE, 242 method = "getSpacingMultiplier", 243 args = {} 244 ) testGetSpacingMultiplier()245 public void testGetSpacingMultiplier() { 246 Layout layout = new MockLayout(LAYOUT_TEXT, mTextPaint, mWidth, mAlign, -1, mSpacingadd); 247 assertEquals(-1.0f, layout.getSpacingMultiplier()); 248 249 layout = new MockLayout(LAYOUT_TEXT, mTextPaint, mWidth, mAlign, 5, mSpacingadd); 250 assertEquals(5.0f, layout.getSpacingMultiplier()); 251 } 252 253 @TestTargetNew( 254 level = TestLevel.COMPLETE, 255 method = "getSpacingAdd", 256 args = {} 257 ) testGetSpacingAdd()258 public void testGetSpacingAdd() { 259 Layout layout = new MockLayout(LAYOUT_TEXT, mTextPaint, mWidth, mAlign, mSpacingmult, -1); 260 assertEquals(-1.0f, layout.getSpacingAdd()); 261 262 layout = new MockLayout(LAYOUT_TEXT, mTextPaint, mWidth, mAlign, mSpacingmult, 20); 263 assertEquals(20.0f, layout.getSpacingAdd()); 264 } 265 266 @TestTargetNew( 267 level = TestLevel.COMPLETE, 268 method = "getLineBounds", 269 args = {int.class, android.graphics.Rect.class} 270 ) testGetLineBounds()271 public void testGetLineBounds() { 272 Layout layout = new MockLayout(LAYOUT_TEXT, mTextPaint, mWidth, 273 mAlign, mSpacingmult, mSpacingadd); 274 Rect bounds = new Rect(); 275 276 assertEquals(32, layout.getLineBounds(2, bounds)); 277 assertEquals(0, bounds.left); 278 assertEquals(mWidth, bounds.right); 279 assertEquals(24, bounds.top); 280 assertEquals(36, bounds.bottom); 281 } 282 283 @TestTargetNew( 284 level = TestLevel.COMPLETE, 285 method = "getPrimaryHorizontal", 286 args = {int.class} 287 ) 288 @ToBeFixed(bug = "1386429", explanation = "can not get the" + 289 " package protected class Directions") testGetPrimaryHorizontal()290 public void testGetPrimaryHorizontal() { 291 Layout layout = new MockLayout(LAYOUT_TEXT, mTextPaint, mWidth, 292 mAlign, mSpacingmult, mSpacingadd); 293 try { 294 layout.getPrimaryHorizontal(0); 295 fail("should throw NullPointerException here"); 296 } catch (NullPointerException e) { 297 } 298 } 299 300 @TestTargetNew( 301 level = TestLevel.COMPLETE, 302 method = "getSecondaryHorizontal", 303 args = {int.class} 304 ) 305 @ToBeFixed(bug = "1386429", explanation = "can not get the" + 306 " package protected class Directions") testGetSecondaryHorizontal()307 public void testGetSecondaryHorizontal() { 308 Layout layout = new MockLayout(LAYOUT_TEXT, mTextPaint, mWidth, 309 mAlign, mSpacingmult, mSpacingadd); 310 try { 311 layout.getSecondaryHorizontal(0); 312 fail("should throw NullPointerException here"); 313 } catch (NullPointerException e) { 314 } 315 } 316 317 @TestTargetNew( 318 level = TestLevel.COMPLETE, 319 method = "getLineLeft", 320 args = {int.class} 321 ) testGetLineLeft()322 public void testGetLineLeft() { 323 Layout layout = new MockLayout(LAYOUT_TEXT, mTextPaint, mWidth, 324 mAlign, mSpacingmult, mSpacingadd); 325 assertEquals(2.0f, layout.getLineLeft(0)); 326 assertEquals(4.0f, layout.getLineLeft(1)); 327 assertEquals(1.0f, layout.getLineLeft(2)); 328 } 329 330 @TestTargetNew( 331 level = TestLevel.COMPLETE, 332 method = "getLineRight", 333 args = {int.class} 334 ) testGetLineRight()335 public void testGetLineRight() { 336 Layout layout = new MockLayout(LAYOUT_TEXT, mTextPaint, mWidth, 337 mAlign, mSpacingmult, mSpacingadd); 338 assertEquals(9.0f, layout.getLineRight(0)); 339 assertEquals(7.0f, layout.getLineRight(1)); 340 assertEquals(10.0f, layout.getLineRight(2)); 341 } 342 343 @TestTargetNew( 344 level = TestLevel.COMPLETE, 345 method = "getLineMax", 346 args = {int.class} 347 ) 348 @BrokenTest("unsure if asserted widths are correct") testGetLineMax()349 public void testGetLineMax() { 350 Layout layout = new MockLayout(LAYOUT_TEXT, mTextPaint, mWidth, 351 mAlign, mSpacingmult, mSpacingadd); 352 assertEquals(6.0f, layout.getLineMax(0)); 353 assertEquals(3.0f, layout.getLineMax(1)); 354 assertEquals(9.0f, layout.getLineMax(2)); 355 } 356 357 @TestTargetNew( 358 level = TestLevel.COMPLETE, 359 method = "getLineWidth", 360 args = {int.class} 361 ) 362 @BrokenTest("unsure if asserted widths are correct") testGetLineWidth()363 public void testGetLineWidth() { 364 Layout layout = new MockLayout(LAYOUT_TEXT, mTextPaint, mWidth, 365 mAlign, mSpacingmult, mSpacingadd); 366 assertEquals(6.0f, layout.getLineWidth(0)); 367 assertEquals(3.0f, layout.getLineWidth(1)); 368 assertEquals(9.0f, layout.getLineWidth(2)); 369 } 370 371 @TestTargetNew( 372 level = TestLevel.COMPLETE, 373 method = "getLineForVertical", 374 args = {int.class} 375 ) testGetLineForVertical()376 public void testGetLineForVertical() { 377 Layout layout = new MockLayout(LAYOUT_TEXT, mTextPaint, mWidth, 378 mAlign, mSpacingmult, mSpacingadd); 379 assertEquals(0, layout.getLineForVertical(-1)); 380 assertEquals(0, layout.getLineForVertical(0)); 381 assertEquals(0, layout.getLineForVertical(LINE_COUNT)); 382 assertEquals(LINE_COUNT - 1, layout.getLineForVertical(1000)); 383 } 384 385 @TestTargetNew( 386 level = TestLevel.COMPLETE, 387 method = "getLineForOffset", 388 args = {int.class} 389 ) testGetLineForOffset()390 public void testGetLineForOffset() { 391 Layout layout = new MockLayout(LAYOUT_TEXT, mTextPaint, mWidth, 392 mAlign, mSpacingmult, mSpacingadd); 393 assertEquals(0, layout.getLineForOffset(-1)); 394 assertEquals(1, layout.getLineForOffset(1)); 395 assertEquals(LINE_COUNT - 1, layout.getLineForOffset(LINE_COUNT - 1)); 396 assertEquals(LINE_COUNT - 1, layout.getLineForOffset(1000)); 397 } 398 399 @TestTargetNew( 400 level = TestLevel.COMPLETE, 401 method = "getOffsetForHorizontal", 402 args = {int.class, float.class} 403 ) 404 @ToBeFixed(bug = "1386429", explanation = "can not get the" + 405 " package protected class Directions") testGetOffsetForHorizontal()406 public void testGetOffsetForHorizontal() { 407 Layout layout = new MockLayout(LAYOUT_TEXT, mTextPaint, mWidth, 408 mAlign, mSpacingmult, mSpacingadd); 409 try { 410 layout.getOffsetForHorizontal(0, 0); 411 fail("should throw NullPointerException here"); 412 } catch (NullPointerException e) { 413 } 414 } 415 416 @TestTargetNew( 417 level = TestLevel.COMPLETE, 418 method = "getLineEnd", 419 args = {int.class} 420 ) testGetLineEnd()421 public void testGetLineEnd() { 422 Layout layout = new MockLayout(LAYOUT_TEXT, mTextPaint, mWidth, 423 mAlign, mSpacingmult, mSpacingadd); 424 assertEquals(2, layout.getLineEnd(1)); 425 } 426 427 @TestTargetNew( 428 level = TestLevel.COMPLETE, 429 method = "getLineVisibleEnd", 430 args = {int.class} 431 ) 432 @ToBeFixed(bug = "1417734", explanation = "should add @throws clause into javadoc " + 433 " of Layout#getLineVisibleEnd(int) when the line is out of bound") testGetLineVisibleEnd()434 public void testGetLineVisibleEnd() { 435 Layout layout = new MockLayout(LAYOUT_TEXT, mTextPaint, mWidth, 436 mAlign, mSpacingmult, mSpacingadd); 437 438 assertEquals(2, layout.getLineVisibleEnd(1)); 439 assertEquals(LINE_COUNT, layout.getLineVisibleEnd(LINE_COUNT - 1)); 440 assertEquals(LAYOUT_TEXT.length(), layout.getLineVisibleEnd(LAYOUT_TEXT.length() - 1)); 441 try { 442 layout.getLineVisibleEnd(LAYOUT_TEXT.length()); 443 fail("should throw .StringIndexOutOfBoundsException here"); 444 } catch (StringIndexOutOfBoundsException e) { 445 } 446 } 447 448 @TestTargetNew( 449 level = TestLevel.COMPLETE, 450 method = "getLineBottom", 451 args = {int.class} 452 ) testGetLineBottom()453 public void testGetLineBottom() { 454 Layout layout = new MockLayout(LAYOUT_TEXT, mTextPaint, mWidth, 455 mAlign, mSpacingmult, mSpacingadd); 456 assertEquals(LINE_HEIGHT, layout.getLineBottom(0)); 457 } 458 459 @TestTargetNew( 460 level = TestLevel.COMPLETE, 461 method = "getLineBaseline", 462 args = {int.class} 463 ) testGetLineBaseline()464 public void testGetLineBaseline() { 465 Layout layout = new MockLayout(LAYOUT_TEXT, mTextPaint, mWidth, 466 mAlign, mSpacingmult, mSpacingadd); 467 assertEquals(8, layout.getLineBaseline(0)); 468 } 469 470 @TestTargetNew( 471 level = TestLevel.COMPLETE, 472 method = "getLineAscent", 473 args = {int.class} 474 ) testGetLineAscent()475 public void testGetLineAscent() { 476 Layout layout = new MockLayout(LAYOUT_TEXT, mTextPaint, mWidth, 477 mAlign, mSpacingmult, mSpacingadd); 478 assertEquals(-8, layout.getLineAscent(0)); 479 } 480 481 @TestTargetNew( 482 level = TestLevel.COMPLETE, 483 method = "getOffsetToLeftOf", 484 args = {int.class} 485 ) 486 @ToBeFixed(bug = "1386429", explanation = "can not get the" + 487 " package protected class Directions") testGetOffsetToLeftOf()488 public void testGetOffsetToLeftOf() { 489 Layout layout = new MockLayout(LAYOUT_TEXT, mTextPaint, mWidth, 490 mAlign, mSpacingmult, mSpacingadd); 491 try { 492 layout.getOffsetToLeftOf(0); 493 fail("should throw NullPointerException here"); 494 } catch (NullPointerException e) { 495 } 496 } 497 498 @TestTargetNew( 499 level = TestLevel.COMPLETE, 500 method = "getOffsetToRightOf", 501 args = {int.class} 502 ) 503 @ToBeFixed(bug = "1386429", explanation = "can not get the" + 504 " package protected class Directions") testGetOffsetToRightOf()505 public void testGetOffsetToRightOf() { 506 Layout layout = new MockLayout(LAYOUT_TEXT, mTextPaint, mWidth, 507 mAlign, mSpacingmult, mSpacingadd); 508 try { 509 layout.getOffsetToRightOf(0); 510 fail("should throw NullPointerException here"); 511 } catch (NullPointerException e) { 512 } 513 } 514 515 @TestTargetNew( 516 level = TestLevel.COMPLETE, 517 method = "getCursorPath", 518 args = {int.class, android.graphics.Path.class, java.lang.CharSequence.class} 519 ) 520 @ToBeFixed(bug = "1386429", explanation = "can not get the" + 521 " package protected class Directions") testGetCursorPath()522 public void testGetCursorPath() { 523 Layout layout = new MockLayout(LAYOUT_TEXT, mTextPaint, mWidth, 524 mAlign, mSpacingmult, mSpacingadd); 525 try { 526 layout.getCursorPath(0, new Path(), "test"); 527 fail("should throw NullPointerException here"); 528 } catch (NullPointerException e) { 529 } 530 } 531 532 @TestTargetNew( 533 level = TestLevel.COMPLETE, 534 method = "getSelectionPath", 535 args = {int.class, int.class, android.graphics.Path.class} 536 ) 537 @ToBeFixed(bug = "1386429", explanation = "can not get the" + 538 " package protected class Directions") testGetSelectionPath()539 public void testGetSelectionPath() { 540 Layout layout = new MockLayout(LAYOUT_TEXT, mTextPaint, mWidth, 541 mAlign, mSpacingmult, mSpacingadd); 542 Path path = new Path(); 543 544 layout.getSelectionPath(0, 0, path); 545 546 try { 547 layout.getSelectionPath(1, 0, path); 548 fail("should throw NullPointerException here"); 549 } catch (NullPointerException e) { 550 } 551 552 try { 553 layout.getSelectionPath(0, 1, path); 554 fail("should throw NullPointerException here"); 555 } catch (NullPointerException e) { 556 } 557 } 558 559 @TestTargetNew( 560 level = TestLevel.COMPLETE, 561 method = "getParagraphAlignment", 562 args = {int.class} 563 ) testGetParagraphAlignment()564 public void testGetParagraphAlignment() { 565 Layout layout = new MockLayout(LAYOUT_TEXT, mTextPaint, mWidth, 566 mAlign, mSpacingmult, mSpacingadd); 567 assertSame(mAlign, layout.getParagraphAlignment(0)); 568 569 layout = new MockLayout(mSpannedText, mTextPaint, mWidth, 570 mAlign, mSpacingmult, mSpacingadd); 571 assertSame(mAlign, layout.getParagraphAlignment(0)); 572 assertSame(mAlign, layout.getParagraphAlignment(1)); 573 } 574 575 @TestTargetNew( 576 level = TestLevel.COMPLETE, 577 method = "getParagraphLeft", 578 args = {int.class} 579 ) testGetParagraphLeft()580 public void testGetParagraphLeft() { 581 Layout layout = new MockLayout(LAYOUT_TEXT, mTextPaint, mWidth, 582 mAlign, mSpacingmult, mSpacingadd); 583 assertEquals(0, layout.getParagraphLeft(0)); 584 } 585 586 @TestTargetNew( 587 level = TestLevel.COMPLETE, 588 method = "getParagraphRight", 589 args = {int.class} 590 ) testGetParagraphRight()591 public void testGetParagraphRight() { 592 Layout layout = new MockLayout(LAYOUT_TEXT, mTextPaint, mWidth, 593 mAlign, mSpacingmult, mSpacingadd); 594 assertEquals(mWidth, layout.getParagraphRight(0)); 595 } 596 597 @TestTargetNew( 598 level = TestLevel.COMPLETE, 599 method = "isSpanned", 600 args = {} 601 ) testIsSpanned()602 public void testIsSpanned() { 603 MockLayout layout = new MockLayout(LAYOUT_TEXT, mTextPaint, mWidth, 604 mAlign, mSpacingmult, mSpacingadd); 605 // default is not spanned text 606 assertFalse(layout.mockIsSpanned()); 607 608 // try to create a spanned text 609 layout = new MockLayout(mSpannedText, mTextPaint, mWidth, 610 mAlign, mSpacingmult, mSpacingadd); 611 assertTrue(layout.mockIsSpanned()); 612 } 613 614 @TestTargetNew( 615 level = TestLevel.COMPLETE, 616 method = "getDesiredWidth", 617 args = {java.lang.CharSequence.class, int.class, int.class, android.text.TextPaint.class} 618 ) testGetDesiredWidthRange()619 public void testGetDesiredWidthRange() { 620 CharSequence textShort = "test"; 621 CharSequence textLonger = "test\ngetDesiredWidth"; 622 CharSequence textLongest = "test getDesiredWidth"; 623 TextPaint paint = new TextPaint(); 624 float widthShort = Layout.getDesiredWidth(textShort, 0, textShort.length(), paint); 625 float widthLonger = Layout.getDesiredWidth(textLonger, 0, textLonger.length(), paint); 626 float widthLongest = Layout.getDesiredWidth(textLongest, 0, textLongest.length(), paint); 627 float widthPartShort = Layout.getDesiredWidth(textShort, 2, textShort.length(), paint); 628 float widthZero = Layout.getDesiredWidth(textLonger, 5, textShort.length() - 3, paint); 629 assertTrue(widthLonger > widthShort); 630 assertTrue(widthLongest > widthLonger); 631 assertEquals(0f, widthZero); 632 assertTrue(widthShort > widthPartShort); 633 } 634 635 @TestTargetNew( 636 level = TestLevel.COMPLETE, 637 method = "getDesiredWidth", 638 args = {java.lang.CharSequence.class, android.text.TextPaint.class} 639 ) testGetDesiredWidth()640 public void testGetDesiredWidth() { 641 CharSequence textShort = "test"; 642 CharSequence textLonger = "test\ngetDesiredWidth"; 643 CharSequence textLongest = "test getDesiredWidth"; 644 TextPaint paint = new TextPaint(); 645 float widthShort = Layout.getDesiredWidth(textShort, paint); 646 float widthLonger = Layout.getDesiredWidth(textLonger, paint); 647 float widthLongest = Layout.getDesiredWidth(textLongest, paint); 648 assertTrue(widthLonger > widthShort); 649 assertTrue(widthLongest > widthLonger); 650 } 651 652 private final class MockLayout extends Layout { MockLayout(CharSequence text, TextPaint paint, int width, Alignment align, float spacingmult, float spacingadd)653 public MockLayout(CharSequence text, TextPaint paint, int width, 654 Alignment align, float spacingmult, float spacingadd) { 655 super(text, paint, width, align, spacingmult, spacingadd); 656 } 657 mockIsSpanned()658 protected boolean mockIsSpanned() { 659 return super.isSpanned(); 660 } 661 662 @Override getBottomPadding()663 public int getBottomPadding() { 664 return 0; 665 } 666 667 @Override getEllipsisCount(int line)668 public int getEllipsisCount(int line) { 669 return 0; 670 } 671 672 @Override getEllipsisStart(int line)673 public int getEllipsisStart(int line) { 674 return 0; 675 } 676 677 @Override getLineContainsTab(int line)678 public boolean getLineContainsTab(int line) { 679 return false; 680 } 681 682 @Override getLineCount()683 public int getLineCount() { 684 return LINE_COUNT; 685 } 686 687 @Override getLineDescent(int line)688 public int getLineDescent(int line) { 689 return LINE_DESCENT; 690 } 691 692 @Override getLineDirections(int line)693 public Directions getLineDirections(int line) { 694 return null; 695 } 696 697 @Override getLineStart(int line)698 public int getLineStart(int line) { 699 if (line < 0) { 700 return 0; 701 } 702 return line; 703 } 704 705 @Override getLineTop(int line)706 public int getLineTop(int line) { 707 if (line < 0) { 708 return 0; 709 } 710 return LINE_HEIGHT * (line); 711 } 712 713 @Override getParagraphDirection(int line)714 public int getParagraphDirection(int line) { 715 return 0; 716 } 717 718 @Override getTopPadding()719 public int getTopPadding() { 720 return 0; 721 } 722 } 723 } 724