FileDocCategorySizeDatePackage
StaticLayoutDirectionsTest.javaAPI DocAndroid 5.1 API8171Thu Mar 12 22:22:12 GMT 2015android.text

StaticLayoutDirectionsTest

public class StaticLayoutDirectionsTest extends TestCase

Fields Summary
private static final char
ALEF
private static final int
RUN_LENGTH_MASK
private static final int
RUN_LEVEL_SHIFT
private static final int
RUN_LEVEL_MASK
private static final int
RUN_RTL_FLAG
private static final android.text.Layout.Directions
DIRS_ALL_LEFT_TO_RIGHT
private static final android.text.Layout.Directions
DIRS_ALL_RIGHT_TO_LEFT
private static final int
LVL1_1
private static final int
LVL2_1
private static final int
LVL2_2
private static String[]
texts
private static android.text.Layout.Directions[]
expected
Constructors Summary
Methods Summary
private voidcheckDirections(Layout l, int i, java.lang.String text, android.text.Layout.Directions[] expectedDirs, java.util.Formatter f)

        Directions expected = expectedDirs[i];
        Directions result = l.getLineDirections(0);
        if (!Arrays.equals(expected.mDirections, result.mDirections)) {
            f.format("%n[%2d] '%s', %s != %s", i, text,
                    hexArray(expected.mDirections),
                    hexArray(result.mDirections));
        }
    
private static android.text.Layout.Directionsdirs(int dirs)


          
        return new Directions(dirs);
    
private voidexpectDirections(java.lang.String msg, android.text.Layout.Directions expected, android.text.Layout.Directions result)

        if (!Arrays.equals(expected.mDirections, result.mDirections)) {
            fail("expected: " + hexArray(expected.mDirections) +
                    " got: " + hexArray(result.mDirections));
        }
    
private static java.lang.StringhexArray(int[] array)

        StringBuilder sb = new StringBuilder();
        sb.append('{");
        for (int i : array) {
            if (sb.length() > 1) {
                sb.append(", ");
            }
            sb.append(Integer.toHexString(i));
        }
        sb.append('}");
        return sb.toString();
    
private static java.lang.StringpseudoBidiToReal(java.lang.String src)


         
        char[] chars = src.toCharArray();
        for (int j = 0; j < chars.length; ++j) {
            char c = chars[j];
            if (c >= 'A" && c <= 'D") {
                chars[j] = (char)(ALEF + c - 'A");
            }
        }

        return new String(chars, 0, chars.length);
    
public voidtestDirections()

        StringBuilder buf = new StringBuilder("\n");
        Formatter f = new Formatter(buf);

        LayoutBuilder b = StaticLayoutTest.builder();
        for (int i = 0; i < texts.length; ++i) {
            b.setText(pseudoBidiToReal(texts[i]));
            checkDirections(b.build(), i, b.text, expected, f);
        }
        if (buf.length() > 1) {
            fail(buf.toString());
        }
    
public voidtestNextToLeftOf()

        LayoutBuilder b = StaticLayoutTest.builder();
        b.setText(pseudoBidiToReal("aA1B2"));
        int[] expected = { 0, 1, 4, 3, 2, 5 };
        Layout l = b.build();
        int n = 5;
        for (int i = expected.length - 1; --i >= 0;) {
            int t = l.getOffsetToLeftOf(n);
            if (t != expected[i]) {
                fail("offset[" + i + "] to left of: " + n + " expected: " +
                        expected[i] + " got: " + t);
            }
            n = t;
        }
    
public voidtestNextToRightOf()

        LayoutBuilder b = StaticLayoutTest.builder();
        b.setText(pseudoBidiToReal("aA1B2"));
        // visual a2B1A positions 04321
        // 0: |a2B1A, strong is sol, after -> 0
        // 1: a|2B1A, strong is a, after ->, 1
        // 2: a2|B1A, strong is B, after -> 4
        // 3: a2B|1A, strong is B, before -> 3
        // 4: a2B1|A, strong is A, after -> 2
        // 5: a2B1A|, strong is eol, before -> 5
        int[] expected = { 0, 1, 4, 3, 2, 5 };
        Layout l = b.build();
        int n = 0;
        for (int i = 1; i < expected.length; ++i) {
            int t = l.getOffsetToRightOf(n);
            if (t != expected[i]) {
                fail("offset[" + i + "] to right of: " + n + " expected: " +
                        expected[i] + " got: " + t);
            }
            n = t;
        }
    
public voidtestTrailingWhitespace()

        LayoutBuilder b = StaticLayoutTest.builder();
        b.setText(pseudoBidiToReal("Ab   c"));
        float width = b.paint.measureText(b.text, 0, 5);  // exclude 'c'
        b.setWidth(Math.round(width));
        Layout l = b.build();
        if (l.getLineCount() != 2) {
            throw new RuntimeException("expected 2 lines, got: " + l.getLineCount());
        }
        Directions result = l.getLineDirections(0);
        Directions expected = dirs(0, LVL1_1, 1, LVL2_1, 2, 3 | (1 << Layout.RUN_LEVEL_SHIFT));
        expectDirections("split line", expected, result);