FileDocCategorySizeDatePackage
DynamicLayoutBlocksTest.javaAPI DocAndroid 5.1 API10591Thu Mar 12 22:22:12 GMT 2015android.text

DynamicLayoutBlocksTest

public class DynamicLayoutBlocksTest extends TestCase
Tests DynamciLayout updateBlocks method. Requires disabling access checks in the vm since this calls package-private APIs.
Suppress

Fields Summary
private android.text.DynamicLayout
dl
private static final int
___
private int[]
initialBlockEnds
private int[]
initialBlockIndices
Constructors Summary
Methods Summary
private voidassertState(int[] sizes, int[] indices)

        checkInvariants();

        assertEquals(sizes.length, dl.getNumberOfBlocks());
        assertEquals(indices.length, dl.getNumberOfBlocks());

        int[] ends = new int[sizes.length];
        for (int i = 0; i < ends.length; i++) {
            ends[i] = i == 0 ? (sizes[0] == 0 ? 0 : sizes[0] - 1) : ends[i - 1] + sizes[i];
        }

        for (int i = 0; i < dl.getNumberOfBlocks(); i++) {
            assertEquals(ends[i], dl.getBlockEndLines()[i]);
            assertEquals(indices[i], dl.getBlockIndices()[i]);
        }
    
private voidassertState(int[] sizes)

        int[] ids = new int[sizes.length];
        for (int i = 0; i < sizes.length; i++) {
            ids[i] = DynamicLayout.INVALID_BLOCK_INDEX;
        }
        assertState(sizes, ids);
    
public voidcheckInvariants()

        assertTrue(dl.getNumberOfBlocks() > 0);
        assertTrue(dl.getNumberOfBlocks() <= dl.getBlockEndLines().length);
        assertEquals(dl.getBlockEndLines().length, dl.getBlockIndices().length);

        for (int i = 1; i < dl.getNumberOfBlocks(); i++) {
            assertTrue(dl.getBlockEndLines()[i] > dl.getBlockEndLines()[i-1]);
        }
    
private voiddefineInitialState(int[] ends, int[] indices)


          
        initialBlockEnds = ends;
        initialBlockIndices = indices;
        assertEquals(initialBlockEnds.length, initialBlockIndices.length);
    
public voidprintBlocks(java.lang.String message)

        System.out.print(message);
        for (int i = 0; i < dl.getNumberOfBlocks(); i++) {
            System.out.print("  " + Integer.toString(dl.getBlockEndLines()[i]));
        }
        System.out.println();
    
public voidtestFrom0()

        defineInitialState( new int[] { 0 }, new int[] { 123 });

        update(0, 0, 0);
        assertState( new int[] { 0 } );

        update(0, 0, 1);
        assertState( new int[] { 0 } );

        update(0, 0, 10);
        assertState( new int[] { 10 } );
    
public voidtestFrom1ReplaceByEmpty()

        defineInitialState( new int[] { 100 }, new int[] { 123 });

        update(0, 0, 0);
        assertState( new int[] { 100 } );

        update(0, 10, 0);
        assertState( new int[] { 90 } );

        update(0, 100, 0);
        assertState( new int[] { 0 } );

        update(20, 30, 0);
        assertState( new int[] { 20, 70 } );

        update(20, 20, 0);
        assertState( new int[] { 20, 80 } );

        update(40, 100, 0);
        assertState( new int[] { 40 } );

        update(100, 100, 0);
        assertState( new int[] { 100 } );
    
public voidtestFrom1ReplaceFromCenter()

        defineInitialState( new int[] { 100 }, new int[] { 123 });

        update(20, 20, 1);
        assertState( new int[] { 20, 1, 80 } );

        update(20, 20, 10);
        assertState( new int[] { 20, 10, 80 } );

        update(20, 30, 50);
        assertState( new int[] { 20, 50, 70 } );

        update(20, 100, 50);
        assertState( new int[] { 20, 50 } );
    
public voidtestFrom1ReplaceFromEnd()

        defineInitialState( new int[] { 100 }, new int[] { 123 });

        update(100, 100, 0);
        assertState( new int[] { 100 } );

        update(100, 100, 1);
        assertState( new int[] { 100, 1 } );

        update(100, 100, 10);
        assertState( new int[] { 100, 10 } );
    
public voidtestFrom1ReplaceFromFirstLine()

        defineInitialState( new int[] { 100 }, new int[] { 123 });

        update(0, 0, 1);
        assertState( new int[] { 0, 100 } );

        update(0, 0, 10);
        assertState( new int[] { 10, 100 } );

        update(0, 30, 31);
        assertState( new int[] { 31, 70 } );

        update(0, 100, 20);
        assertState( new int[] { 20 } );
    
public voidtestFrom2RemoveFromFirst()

        defineInitialState( new int[] { 10, 20 }, new int[] { 123, 456 });

        update(0, 4, 0);
        assertState( new int[] { 10-4, 20-10 }, new int[] { ___, 456 } );

        update(0, 10, 0);
        assertState( new int[] { 20-10 }, new int[] { 456 } );

        update(0, 14, 0);
        assertState( new int[] { 20-14 }, new int[] { ___ } );

        update(0, 20, 0);
        assertState( new int[] { 0 }, new int[] { ___ } );
    
public voidtestFrom2RemoveFromFirstBlock()

        defineInitialState( new int[] { 10, 20 }, new int[] { 123, 456 });

        update(4, 7, 0);
        assertState( new int[] { 4, 10-7, 20-10 }, new int[] { ___, ___, 456 } );

        update(4, 10, 0);
        assertState( new int[] { 4, 20-10 }, new int[] { ___, 456 } );

        update(4, 14, 0);
        assertState( new int[] { 4, 20-14 }, new int[] { ___, ___ } );

        update(4, 20, 0);
        assertState( new int[] { 4 }, new int[] { ___ } );
    
public voidtestFrom2RemoveFromSecondBlock()

        defineInitialState( new int[] { 10, 20 }, new int[] { 123, 456 });

        update(14, 17, 0);
        assertState( new int[] { 11, 14-11, 20-17 }, new int[] { 123, ___, ___ } );

        update(14, 20, 0);
        assertState( new int[] { 11, 14-11 }, new int[] { 123, ___ } );
    
public voidtestFrom2ReplaceFromBottomBoundary()

        defineInitialState( new int[] { 10, 20 }, new int[] { 123, 456 });

        update(10, 10, 50);
        assertState( new int[] { 10, 50, 20-10 }, new int[] { ___, ___, 456 } );

        update(10, 14, 50);
        assertState( new int[] { 10, 50, 20-14 }, new int[] { ___, ___, ___ } );

        update(10, 20, 50);
        assertState( new int[] { 10, 50 }, new int[] { ___, ___ } );
    
public voidtestFrom2ReplaceFromFirstBlock()

        defineInitialState( new int[] { 10, 20 }, new int[] { 123, 456 });

        update(3, 7, 50);
        assertState( new int[] { 3, 50, 10-7, 20-10 }, new int[] { ___, ___, ___, 456 } );

        update(3, 10, 50);
        assertState( new int[] { 3, 50, 20-10 }, new int[] { ___, ___, 456 } );

        update(3, 14, 50);
        assertState( new int[] { 3, 50, 20-14 }, new int[] { ___, ___, ___ } );

        update(3, 20, 50);
        assertState( new int[] { 3, 50 }, new int[] { ___, ___ } );
    
public voidtestFrom2ReplaceFromFirstLine()

        defineInitialState( new int[] { 10, 20 }, new int[] { 123, 456 });

        update(0, 4, 50);
        assertState( new int[] { 50, 10-4, 20-10 }, new int[] { ___, ___, 456 } );

        update(0, 10, 50);
        assertState( new int[] { 50, 20-10 }, new int[] { ___, 456 } );

        update(0, 15, 50);
        assertState( new int[] { 50, 20-15 }, new int[] { ___, ___ } );

        update(0, 20, 50);
        assertState( new int[] { 50 }, new int[] { ___ } );
    
public voidtestFrom2ReplaceFromSecondBlock()

        defineInitialState( new int[] { 10, 20 }, new int[] { 123, 456 });

        update(14, 14, 50);
        assertState( new int[] { 11, 14-11, 50, 20-14 }, new int[] { 123, ___, ___, ___ } );

        update(14, 17, 50);
        assertState( new int[] { 11, 14-11, 50, 20-17 }, new int[] { 123, ___, ___, ___ } );

        update(14, 20, 50);
        assertState( new int[] { 11, 14-11, 50 }, new int[] { 123, ___, ___ } );
    
public voidtestFrom2ReplaceFromTopBoundary()

        defineInitialState( new int[] { 10, 20 }, new int[] { 123, 456 });

        update(11, 11, 50);
        assertState( new int[] { 11, 50, 20-11 }, new int[] { 123, ___, ___ } );

        update(11, 14, 50);
        assertState( new int[] { 11, 50, 20-14 }, new int[] { 123, ___, ___ } );

        update(11, 20, 50);
        assertState( new int[] { 11, 50 }, new int[] { 123, ___ } );
    
public voidtestFrom3ReplaceFromFirstBlock()

        defineInitialState( new int[] { 10, 30, 60 }, new int[] { 123, 456, 789 });

        update(3, 7, 50);
        assertState( new int[] { 3, 50, 10-7, 30-10, 60-30 }, new int[] { ___, ___, ___, 456, 789 } );

        update(3, 10, 50);
        assertState( new int[] { 3, 50, 30-10, 60-30 }, new int[] { ___, ___, 456, 789 } );

        update(3, 17, 50);
        assertState( new int[] { 3, 50, 30-17, 60-30 }, new int[] { ___, ___, ___, 789 } );

        update(3, 30, 50);
        assertState( new int[] { 3, 50, 60-30 }, new int[] { ___, ___, 789 } );

        update(3, 40, 50);
        assertState( new int[] { 3, 50, 60-40 }, new int[] { ___, ___, ___ } );

        update(3, 60, 50);
        assertState( new int[] { 3, 50 }, new int[] { ___, ___ } );
    
public voidtestFrom3ReplaceFromSecondBlock()

        defineInitialState( new int[] { 10, 30, 60 }, new int[] { 123, 456, 789 });

        update(13, 17, 50);
        assertState( new int[] { 11, 2, 50, 30-17, 60-30 }, new int[] { 123, ___, ___, ___, 789 } );

        update(13, 30, 50);
        assertState( new int[] { 11, 2, 50, 60-30 }, new int[] { 123, ___, ___, 789 } );

        update(13, 40, 50);
        assertState( new int[] { 11, 2, 50, 60-40 }, new int[] { 123, ___, ___, ___ } );

        update(13, 60, 50);
        assertState( new int[] { 11, 2, 50 }, new int[] { 123, ___, ___ } );
    
private voidupdate(int startLine, int endLine, int newLineCount)

        dl.setBlocksDataForTest(initialBlockEnds, initialBlockIndices, initialBlockEnds.length);
        checkInvariants();
        dl.updateBlocks(startLine, endLine, newLineCount);