Home | History | Annotate | Download | only in cts
      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.Paint.FontMetricsInt;
     20 import android.test.AndroidTestCase;
     21 import android.text.DynamicLayout;
     22 import android.text.Layout;
     23 import android.text.TextPaint;
     24 import android.text.TextUtils;
     25 
     26 public class DynamicLayoutTest extends AndroidTestCase {
     27 
     28     protected static final float SPACING_MULT_NO_SCALE = 1.0f;
     29     protected static final float SPACING_ADD_NO_SCALE = 0.0f;
     30     protected static final int DEFAULT_OUTER_WIDTH = 150;
     31     protected static final CharSequence SINGLELINE_CHAR_SEQUENCE = "......";
     32     protected static final String[] TEXT = {"CharSequence\n", "Char\tSequence\n", "CharSequence"};
     33     protected static final CharSequence MULTLINE_CHAR_SEQUENCE = TEXT[0] + TEXT[1] + TEXT[2];
     34     protected static final Layout.Alignment DEFAULT_ALIGN = Layout.Alignment.ALIGN_CENTER;
     35     protected TextPaint mDefaultPaint;
     36 
     37     private static final int LINE0 = 0;
     38     private static final int LINE0_TOP = 0;
     39     private static final int LINE1 = 1;
     40     private static final int LINE2 = 2;
     41     private static final int LINE3 = 3;
     42 
     43     private static final int ELLIPSIS_UNDEFINED = 0x80000000;
     44 
     45     private DynamicLayout mDynamicLayout;
     46 
     47     @Override
     48     protected void setUp() throws Exception {
     49         super.setUp();
     50         mDefaultPaint = new TextPaint();
     51         mDynamicLayout = new DynamicLayout(MULTLINE_CHAR_SEQUENCE,
     52                 mDefaultPaint,
     53                 DEFAULT_OUTER_WIDTH,
     54                 DEFAULT_ALIGN,
     55                 SPACING_MULT_NO_SCALE,
     56                 SPACING_ADD_NO_SCALE,
     57                 true);
     58     }
     59 
     60     public void testConstructors() {
     61         new DynamicLayout(SINGLELINE_CHAR_SEQUENCE,
     62                 MULTLINE_CHAR_SEQUENCE,
     63                 mDefaultPaint,
     64                 DEFAULT_OUTER_WIDTH,
     65                 DEFAULT_ALIGN,
     66                 SPACING_MULT_NO_SCALE,
     67                 SPACING_ADD_NO_SCALE,
     68                 true);
     69         new DynamicLayout(SINGLELINE_CHAR_SEQUENCE,
     70                 MULTLINE_CHAR_SEQUENCE,
     71                 mDefaultPaint,
     72                 DEFAULT_OUTER_WIDTH,
     73                 DEFAULT_ALIGN,
     74                 SPACING_MULT_NO_SCALE,
     75                 SPACING_ADD_NO_SCALE,
     76                 true,
     77                 TextUtils.TruncateAt.START,
     78                 DEFAULT_OUTER_WIDTH);
     79         new DynamicLayout(MULTLINE_CHAR_SEQUENCE,
     80                 mDefaultPaint,
     81                 DEFAULT_OUTER_WIDTH,
     82                 DEFAULT_ALIGN,
     83                 SPACING_MULT_NO_SCALE,
     84                 SPACING_ADD_NO_SCALE,
     85                 true);
     86     }
     87 
     88     public void testEllipsis() {
     89         final DynamicLayout dynamicLayout = new DynamicLayout(SINGLELINE_CHAR_SEQUENCE,
     90                 MULTLINE_CHAR_SEQUENCE,
     91                 mDefaultPaint,
     92                 DEFAULT_OUTER_WIDTH,
     93                 DEFAULT_ALIGN,
     94                 SPACING_MULT_NO_SCALE,
     95                 SPACING_ADD_NO_SCALE,
     96                 true,
     97                 TextUtils.TruncateAt.START,
     98                 DEFAULT_OUTER_WIDTH);
     99         assertEquals(0, dynamicLayout.getEllipsisCount(LINE1));
    100         assertEquals(ELLIPSIS_UNDEFINED, dynamicLayout.getEllipsisStart(LINE1));
    101         assertEquals(DEFAULT_OUTER_WIDTH, dynamicLayout.getEllipsizedWidth());
    102     }
    103 
    104     /*
    105      * Test whether include the padding to calculate the layout.
    106      * 1. Include padding while calculate the layout.
    107      * 2. Don't include padding while calculate the layout.
    108      */
    109     public void testIncludePadding() {
    110         final FontMetricsInt fontMetricsInt = mDefaultPaint.getFontMetricsInt();
    111 
    112         DynamicLayout dynamicLayout = new DynamicLayout(SINGLELINE_CHAR_SEQUENCE,
    113                 mDefaultPaint,
    114                 DEFAULT_OUTER_WIDTH,
    115                 DEFAULT_ALIGN,
    116                 SPACING_MULT_NO_SCALE,
    117                 SPACING_ADD_NO_SCALE,
    118                 true);
    119         assertEquals(fontMetricsInt.top - fontMetricsInt.ascent, dynamicLayout.getTopPadding());
    120         assertEquals(fontMetricsInt.bottom - fontMetricsInt.descent,
    121                 dynamicLayout.getBottomPadding());
    122 
    123         dynamicLayout = new DynamicLayout(SINGLELINE_CHAR_SEQUENCE,
    124                 mDefaultPaint,
    125                 DEFAULT_OUTER_WIDTH,
    126                 DEFAULT_ALIGN,
    127                 SPACING_MULT_NO_SCALE,
    128                 SPACING_ADD_NO_SCALE,
    129                 false);
    130         assertEquals(0, dynamicLayout.getTopPadding());
    131         assertEquals(0, dynamicLayout.getBottomPadding());
    132     }
    133 
    134     /*
    135      * Test the line count and whether include the Tab the layout.
    136      * 1. Include Tab. 2. Don't include Tab Use the Y-coordinate to calculate the line number
    137      * Test the line top
    138      * 1. the Y-coordinate of line top.2. the Y-coordinate of baseline.
    139      */
    140     public void testLineLayout() {
    141         assertEquals(TEXT.length, mDynamicLayout.getLineCount());
    142         assertFalse(mDynamicLayout.getLineContainsTab(LINE0));
    143         assertTrue(mDynamicLayout.getLineContainsTab(LINE1));
    144 
    145         assertEquals(LINE0_TOP, mDynamicLayout.getLineTop(LINE0));
    146 
    147         assertEquals(mDynamicLayout.getLineBottom(LINE0), mDynamicLayout.getLineTop(LINE1));
    148         assertEquals(mDynamicLayout.getLineBottom(LINE1), mDynamicLayout.getLineTop(LINE2));
    149         assertEquals(mDynamicLayout.getLineBottom(LINE2), mDynamicLayout.getLineTop(LINE3));
    150 
    151         try {
    152             assertEquals(mDynamicLayout.getLineBottom(mDynamicLayout.getLineCount()),
    153                     mDynamicLayout.getLineTop(mDynamicLayout.getLineCount() + 1));
    154             fail("Test DynamicLayout fail, should throw IndexOutOfBoundsException.");
    155         } catch (IndexOutOfBoundsException e) {
    156             // expected
    157         }
    158 
    159         assertEquals(mDynamicLayout.getLineDescent(LINE0) - mDynamicLayout.getLineAscent(LINE0),
    160                 mDynamicLayout.getLineBottom(LINE0));
    161 
    162         assertEquals(mDynamicLayout.getLineDescent(LINE1) - mDynamicLayout.getLineAscent(LINE1),
    163                 mDynamicLayout.getLineBottom(LINE1) - mDynamicLayout.getLineBottom(LINE0));
    164 
    165         assertEquals(mDynamicLayout.getLineDescent(LINE2) - mDynamicLayout.getLineAscent(LINE2),
    166                 mDynamicLayout.getLineBottom(LINE2) - mDynamicLayout.getLineBottom(LINE1));
    167 
    168         assertEquals(LINE0, mDynamicLayout.getLineForVertical(mDynamicLayout.getLineTop(LINE0)));
    169 
    170         assertNotNull(mDynamicLayout.getLineDirections(LINE0));
    171         assertEquals(Layout.DIR_LEFT_TO_RIGHT, mDynamicLayout.getParagraphDirection(LINE0));
    172 
    173         assertEquals(0, mDynamicLayout.getLineStart(LINE0));
    174         assertEquals(TEXT[0].length(), mDynamicLayout.getLineStart(LINE1));
    175         assertEquals(TEXT[0].length() + TEXT[1].length(), mDynamicLayout.getLineStart(LINE2));
    176     }
    177 }
    178