FileDocCategorySizeDatePackage
TestStringItemSizing.javaAPI DocphoneME MR2 API (J2ME)16472Wed May 02 18:00:20 BST 2007javax.microedition.lcdui

TestStringItemSizing

public class TestStringItemSizing extends TestCase

Fields Summary
private final int
MAX_WIDTH
Maximum width available for items.
private final int
ITEM_LEFT_PAD
private final int
ITEM_RIGHT_PAD
private final int
ITEM_TOP_PAD
private final int
ITEM_BOTTOM_PAD
private final int
CONTENT_LEFT_PAD
private final int
CONTENT_RIGHT_PAD
private final int
CONTENT_TOP_PAD
private final int
CONTENT_BOTTOM_PAD
private final int
LABEL_BODY_HORIZ_PAD
private final int
LABEL_BODY_VERTICAL_PAD
private final int
LABEL_I_WIDTH
private final int
LABEL_FONT_HEIGHT
private final int
TEXT_I_WIDTH
private final int
TEXT_FONT_HEIGHT
private final int[]
PLAIN_PRMS
private final int[]
HYPERLINK_PRMS
private final int[]
BUTTON_PRMS
private final int
EMPTY
private final int
EMPTY_LABEL
private final int
EMPTY_TEXT
private final int
SAME_LINE
private final int
MULT_LINE
private final String[]
appearanceModeNames
StringItemInfo[]
itemInfos
int[]
modes
boolean[]
addCommands
StringItemInfo[]
itemInfosWidthDependent
boolean[]
addCommandsToWidthDepInfos
Constructors Summary
Methods Summary
intcalculateHeight(StringItem strItem, int type)


	int params[] = getParameters(strItem);

	switch (type) {
	case EMPTY:
	    return 0;

	case EMPTY_LABEL:
	    return calculateHeightEmptyLabel(params, 
					     getNumOfIsPerLine(strItem.str).length);

	case EMPTY_TEXT:
	    return calculateHeightEmptyText(params, 
					    getNumOfIsPerLine(strItem.label).length);

	case SAME_LINE:
	    return calculateHeightSameLine(params);


	case MULT_LINE:
	    return calculateHeightMultLines(params,
					    getNumOfIsPerLine(strItem.label).length,
					    getNumOfIsPerLine(strItem.str).length);
	    
	default:
	    System.err.println("Incorrect type passing");
	    return 0;
	}
    
intcalculateHeightEmptyLabel(int[] params, int numOfLines)

	return params[ITEM_TOP_PAD] + 
	       params[CONTENT_LEFT_PAD] +
	       params[TEXT_FONT_HEIGHT]*numOfLines +
	       params[CONTENT_RIGHT_PAD] +
	       params[ITEM_BOTTOM_PAD];
    
intcalculateHeightEmptyText(int[] params, int numOfLines)

	return params[ITEM_TOP_PAD] + 
	       params[LABEL_FONT_HEIGHT]*numOfLines +
	       params[ITEM_BOTTOM_PAD];
    
intcalculateHeightMultLines(int[] params, int labelNumLines, int textNumLines)

	return params[ITEM_TOP_PAD] + 
	       labelNumLines*params[LABEL_FONT_HEIGHT] +
	       params[LABEL_BODY_VERTICAL_PAD] +
	       params[CONTENT_TOP_PAD] +
	       labelNumLines*params[TEXT_FONT_HEIGHT] +
	       params[CONTENT_BOTTOM_PAD] +
	       params[ITEM_BOTTOM_PAD];
    
intcalculateHeightSameLine(int[] params)

	int contentHeight = params[CONTENT_LEFT_PAD] + 
	                    params[TEXT_FONT_HEIGHT] +
	                    params[CONTENT_RIGHT_PAD];
	return params[ITEM_TOP_PAD] + params[ITEM_BOTTOM_PAD] +
	       (params[LABEL_FONT_HEIGHT] > contentHeight ? 
                params[LABEL_FONT_HEIGHT] : contentHeight);
    
intcalculateWidth(StringItem strItem, int type)


	int params[] = getParameters(strItem);

	switch (type) {
	case EMPTY:
	    return 0;

	case EMPTY_LABEL:
	    return calculateWidthEmptyLabel(params, 
					    getNumOfIsPerLine(strItem.str));

	case EMPTY_TEXT:
	    return calculateWidthEmptyText(params, 
					   getNumOfIsPerLine(strItem.label));

	case SAME_LINE:
	    // we rely on the fact that both label and text are present
	    // and not empty
	    return calculateWidthSameLine(params,
					  getNumOfIsPerLine(strItem.label)[0],
					  getNumOfIsPerLine(strItem.str)[0]);

	case MULT_LINE:
	    return calculateWidthMultLines(params,
					   getNumOfIsPerLine(strItem.label),
					   getNumOfIsPerLine(strItem.str));
	    
	default:
	    System.err.println("Incorrect type passing");
	    return 0;
	}
    
intcalculateWidthEmptyLabel(int[] params, int[] numOfIperLine)

	int maxIs = 0;
	for (int i=0; i<numOfIperLine.length; i++) {
	    if (numOfIperLine[i] > maxIs) {
		maxIs = numOfIperLine[i];
	    }
	}
	return params[ITEM_LEFT_PAD] + 
	       params[CONTENT_LEFT_PAD] +
	       maxIs*params[TEXT_I_WIDTH]+
	       params[CONTENT_RIGHT_PAD] +
	       params[ITEM_RIGHT_PAD];
    
intcalculateWidthEmptyText(int[] params, int[] numOfIperLine)

	int maxIs = 0;
	for (int i=0; i<numOfIperLine.length; i++) {
	    if (numOfIperLine[i] > maxIs) {
		maxIs = numOfIperLine[i];
	    }
	}
	return params[ITEM_LEFT_PAD] + 
	       maxIs*params[LABEL_I_WIDTH]+
	       params[ITEM_RIGHT_PAD];
    
intcalculateWidthMultLines(int[] params, int[] labelNumOfIperLine, int[] textNumOfIperLine)

	int labelMaxI = 0;
	for(int i=0; i<labelNumOfIperLine.length; i++) {
	    if (labelNumOfIperLine[i] > labelMaxI) {
		labelMaxI = labelNumOfIperLine[i];
	    }
	}
	labelMaxI *= params[LABEL_I_WIDTH];

	int textMaxI = 0;
	for(int i=0; i<textNumOfIperLine.length; i++) {
	    if (textNumOfIperLine[i] > textMaxI) {
		textMaxI = textNumOfIperLine[i];
	    }
	}
	textMaxI *= params[TEXT_I_WIDTH];
	textMaxI += params[CONTENT_LEFT_PAD] + params[CONTENT_RIGHT_PAD];

	return params[ITEM_LEFT_PAD] + params[ITEM_RIGHT_PAD] +
	    (labelMaxI > textMaxI ? labelMaxI : textMaxI); 
    
intcalculateWidthSameLine(int[] params, int labelNumOfI, int textNumOfI)

	return params[ITEM_LEFT_PAD] + 
	       labelNumOfI*params[LABEL_I_WIDTH] +
	       params[LABEL_BODY_HORIZ_PAD] +
	       params[CONTENT_LEFT_PAD] +
	       textNumOfI*params[TEXT_I_WIDTH]+
	       params[CONTENT_RIGHT_PAD] +
	       params[ITEM_RIGHT_PAD];
    
javax.microedition.lcdui.TestStringItemSizing$StringItemInfo[]createTestArray()


    // -----------------------------------------------------------------------
    // utilities

      

	StringItemInfo newItemInfos[] = 
	    new StringItemInfo[itemInfos.length*modes.length +
			       itemInfosWidthDependent.length];

	Command c = new Command("", Command.ITEM, 1);

	for (int j, mode = 0; 
	     mode < modes.length; 
	     mode++) {

	    j = itemInfos.length * mode;

	    for (int i = 0; i < itemInfos.length; i++, j++) {
		newItemInfos[j] = new StringItemInfo(
				    new StringItem(itemInfos[i].strItem.label,
						   itemInfos[i].strItem.str,
						   modes[mode]),
				    itemInfos[i].type,
				    itemInfos[i].description + ", " +
				    appearanceModeNames[modes[mode]] +")" +
				    (addCommands[mode] ? " command added" : "")); 

		if (addCommands[mode]) {
		    newItemInfos[j].strItem.addCommand(c);
		}
	    }
	}

	// Copy elements from itemInfosWidthDependent and add commands 
	// if needed
	System.arraycopy(itemInfosWidthDependent, 0, 
			 newItemInfos, itemInfos.length * modes.length, 
			 itemInfosWidthDependent.length);

	for (int i=itemInfos.length*modes.length, j=0; 
	     i < newItemInfos.length; i++, j++) {
	    if (addCommandsToWidthDepInfos[j]) {
		newItemInfos[i].strItem.addCommand(c);
	    }
	}

	return newItemInfos;
    
int[]getNumOfIsPerLine(java.lang.String str)

	if (str == null || str.length() == 0) {
	    return new int[]{0};
	}

	int lines = 1;
	for(int i=0; i < str.length(); i++) {
	    if (str.charAt(i) == '\n") {
		lines++;
	    }
	} 
	
	int return_array[] = new int[lines];
	int j = 0, numOfIs = 0;
	for (int i = 0; i < str.length(); i++) {
	    if (str.charAt(i) == '\n") {
		return_array[j] = numOfIs;
		numOfIs = 0;
		j++;
	    } else {
		numOfIs++;
	    } 
	}

	return_array[j] = numOfIs;


	return return_array;
    
int[]getParameters(StringItem strItem)

	// Hyperlink and Button appearance settings are ignored 
	// if there are no commands added

	// If a command is added to a StringItem with PLAIN appearance mode
	// it is changed to be HYPERLINK


	switch(strItem.getAppearanceMode()) { 
	    case Item.PLAIN:
		return (strItem.numCommands == 0 ? 
			PLAIN_PRMS : HYPERLINK_PRMS);

	    case Item.HYPERLINK:
		return (strItem.numCommands == 0 ? 
			PLAIN_PRMS : HYPERLINK_PRMS);

	    case Item.BUTTON:
		return (strItem.numCommands == 0 ?
			PLAIN_PRMS : BUTTON_PRMS); 
	}
	return null;
    
public voidrunTests()


	StringItemInfo newItemInfos[] = createTestArray();

	for (int i = 0 ; i < newItemInfos.length; i++) {
	    declare("testPreferredWidth StringItem(" +
		    newItemInfos[i].description);
	    testDefaultPreferredWidth(newItemInfos[i]); 

	    declare("testPreferredHeight StringItem(" +
		     newItemInfos[i].description);
	    testDefaultPreferredHeight(newItemInfos[i]);
	}
    
public voidtestDefaultPreferredHeight(javax.microedition.lcdui.TestStringItemSizing$StringItemInfo strItemInfo)

	assertEquals(calculateHeight(strItemInfo.strItem, strItemInfo.type),
		     strItemInfo.strItem.stringItemLF.lGetPreferredHeight(-1));
    
public voidtestDefaultPreferredWidth(javax.microedition.lcdui.TestStringItemSizing$StringItemInfo strItemInfo)

	assertEquals(calculateWidth(strItemInfo.strItem, strItemInfo.type),
		     strItemInfo.strItem.stringItemLF.lGetPreferredWidth(-1));