Source:
private void checkTextBoxDimensions() { int boxOffset = TextTool.BOX_OFFSET; int textSizeMagnificationFactor = TextTool.TEXT_SIZE_MAGNIFICATION_FACTOR; float actualBoxWidth = getToolMemberBoxWidth(); float actualBoxHeight = getToolMemberBoxHeight(); boolean italic = italicToggleButton.isChecked(); String font = (String) fontSpinner.getSelectedItem(); String stringTextSize = ((String) textSizeSpinner.getSelectedItem()); String[] splitedTextSize = stringTextSize.split("p"); float textSize = Float.valueOf(splitedTextSize[0]) * textSizeMagnificationFactor; Paint textPaint = new Paint(); textPaint.setAntiAlias(true); textPaint.setTextSize(textSize); int style = italic ? Typeface.ITALIC : Typeface.NORMAL; switch (font) { case FONT_SANS_SERIF: textPaint.setTypeface(Typeface.create(Typeface.SANS_SERIF, style)); break; case FONT_SERIF: textPaint.setTypeface(Typeface.create(Typeface.SERIF, style)); break; case FONT_STC: textPaint.setTypeface(ResourcesCompat.getFont(launchActivityRule.getActivity(), R.font.stc_regular)); break; case FONT_DUBAI: textPaint.setTypeface(ResourcesCompat.getFont(launchActivityRule.getActivity(), R.font.dubai)); break; default: textPaint.setTypeface(Typeface.create(Typeface.MONOSPACE, style)); break; } float textDescent = textPaint.descent(); float textAscent = textPaint.ascent(); String[] multilineText = getToolMemberMultilineText(); float maxTextWidth = 0; for (String str : multilineText) { float textWidth = textPaint.measureText(str); if (textWidth > maxTextWidth) { maxTextWidth = textWidth; } } float expectedBoxWidth = maxTextWidth + 2 * boxOffset; float textHeight = textDescent - textAscent; float expectedBoxHeight = textHeight * multilineText.length + 2 * boxOffset; assertEquals(expectedBoxWidth, actualBoxWidth, EQUALS_DELTA); assertEquals(expectedBoxHeight, actualBoxHeight, EQUALS_DELTA); }

Target:
private void checkTextBoxDimensions() { int boxOffset = TextTool.BOX_OFFSET; int textSizeMagnificationFactor = TextTool.TEXT_SIZE_MAGNIFICATION_FACTOR; float actualBoxWidth = getToolMemberBoxWidth(); float actualBoxHeight = getToolMemberBoxHeight(); boolean italic = italicToggleButton.isChecked(); String font = (String) fontSpinner.getSelectedItem(); String stringTextSize = ((String) textSizeSpinner.getSelectedItem()); stringTextSize = stringTextSize.substring(0, stringTextSize.indexOf("px")); float textSize = Float.valueOf(stringTextSize) * textSizeMagnificationFactor; Paint textPaint = new Paint(); textPaint.setAntiAlias(true); textPaint.setTextSize(textSize); int style = italic ? Typeface.ITALIC : Typeface.NORMAL; switch (font) { case FONT_SANS_SERIF: textPaint.setTypeface(Typeface.create(Typeface.SANS_SERIF, style)); break; case FONT_SERIF: textPaint.setTypeface(Typeface.create(Typeface.SERIF, style)); break; case FONT_STC: textPaint.setTypeface(ResourcesCompat.getFont(launchActivityRule.getActivity(), R.font.stc_regular)); break; case FONT_DUBAI: textPaint.setTypeface(ResourcesCompat.getFont(launchActivityRule.getActivity(), R.font.dubai)); break; default: textPaint.setTypeface(Typeface.create(Typeface.MONOSPACE, style)); break; } float textDescent = textPaint.descent(); float textAscent = textPaint.ascent(); String[] multilineText = getToolMemberMultilineText(); float maxTextWidth = 0; for (String str : multilineText) { float textWidth = textPaint.measureText(str); if (textWidth > maxTextWidth) { maxTextWidth = textWidth; } } float expectedBoxWidth = maxTextWidth + 2 * boxOffset; float textHeight = textDescent - textAscent; float expectedBoxHeight = textHeight * multilineText.length + 2 * boxOffset; assertEquals(expectedBoxWidth, actualBoxWidth, EQUALS_DELTA); assertEquals(expectedBoxHeight, actualBoxHeight, EQUALS_DELTA); }