Add test for FontInfo

This commit is contained in:
Sergey Malenkov
2015-10-19 16:02:27 +03:00
parent b35ef472e4
commit 5456cf08db
4 changed files with 69 additions and 4 deletions
@@ -127,7 +127,7 @@ public final class FontComboBox extends ComboBox {
private FontInfo getInfo(Object item) {
for (FontInfo info : myMonospacedOnly ? myMonoFonts : myAllFonts) {
if (item instanceof String ? info.toString().equals(item) : info.equals(item)) {
if (item instanceof String ? info.toString().equalsIgnoreCase((String)item) : info.equals(item)) {
return info;
}
}
@@ -33,6 +33,7 @@ public final class FontInfo {
private static final FontInfoComparator COMPARATOR = new FontInfoComparator();
private static final FontRenderContext DEFAULT_CONTEXT = new FontRenderContext(null, false, false);
private static final String[] WRONG_SUFFIX = {".plain", ".bold", ".italic", ".bolditalic"};
private static final String[] DEFAULT = {Font.DIALOG, Font.DIALOG_INPUT, Font.MONOSPACED, Font.SANS_SERIF, Font.SERIF};
private static final int DEFAULT_SIZE = 12;
private final String myName;
@@ -121,7 +122,7 @@ public final class FontInfo {
private static FontInfo find(List<FontInfo> list, String name) {
for (FontInfo info : list) {
if (info.toString().equals(name)) {
if (info.toString().equalsIgnoreCase(name)) {
return info;
}
}
@@ -139,6 +140,12 @@ public final class FontInfo {
FontInfo info = byName(name);
if (info != null) list.add(info);
}
for (String name : DEFAULT) {
if (find(list, name) == null) {
FontInfo info = byName(name);
if (info != null) list.add(info);
}
}
Collections.sort(list, COMPARATOR);
return Collections.unmodifiableList(list);
}
@@ -159,15 +166,21 @@ public final class FontInfo {
}
private static FontInfo create(String name, Font font) {
boolean plainOnly = name == null;
try {
if (font == null) {
font = new Font(name, Font.PLAIN, DEFAULT_SIZE);
// Java uses Dialog family for nonexistent fonts
if (!Font.DIALOG.equals(name) && Font.DIALOG.equals(font.getFamily(ENGLISH))) {
throw new IllegalArgumentException("not supported " + font);
}
}
else if (DEFAULT_SIZE != font.getSize()) {
font = font.deriveFont((float)DEFAULT_SIZE);
name = font.getFontName(ENGLISH);
}
int width = getFontWidth(font, Font.PLAIN);
if (name != null) {
if (!plainOnly) {
if (width != getFontWidth(font, Font.BOLD)) width = 0;
if (width != getFontWidth(font, Font.ITALIC)) width = 0;
if (width != getFontWidth(font, Font.BOLD | Font.ITALIC)) width = 0;
@@ -0,0 +1,52 @@
package com.intellij.util.ui;
import junit.framework.TestCase;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.util.Locale;
/**
* @author Sergey.Malenkov
*/
public final class FontInfoTest extends TestCase {
private static void check(String name, boolean exist) {
if (GraphicsEnvironment.isHeadless()) return;
FontInfo info = FontInfo.get(name);
assertEquals(exist, null != info);
if (name != null) {
assertEquals(info, FontInfo.get(name.toUpperCase(Locale.ENGLISH)));
assertEquals(info, FontInfo.get(name.toLowerCase(Locale.ENGLISH)));
}
exist = false; // because predefined names are not real font names
assertEquals(exist, null != FontInfo.get(new Font(name, Font.PLAIN, 12)));
}
public void testDialog() {
check(Font.DIALOG, true);
}
public void testDialogInput() {
check(Font.DIALOG_INPUT, true);
}
public void testMonospaced() {
check(Font.MONOSPACED, true);
}
public void testSansSerif() {
check(Font.SANS_SERIF, true);
}
public void testSerif() {
check(Font.SERIF, true);
}
public void testNonExistent() {
check("Strange (very strange) nonexistent font", false);
}
public void testNull() {
check(null, false);
}
}
@@ -246,7 +246,7 @@ public class FontEditorDialog extends DialogWrapper {
public FontInfo findElement(String name) {
for (FontInfo info : myList) {
if (info.toString().equals(name)) {
if (info.toString().equalsIgnoreCase(name)) {
return info;
}
}