帆软报表设计器源代码。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

126 lines
4.0 KiB

package com.fr.design.utils.gui;
import com.fr.design.gui.core.UITextComponent;
import com.fr.design.gui.ibutton.UIButton;
import com.fr.design.gui.ilable.UILabel;
import com.fr.design.i18n.Toolkit;
import com.fr.general.GeneralContext;
import com.fr.stable.StringUtils;
import org.easymock.EasyMock;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import javax.swing.Icon;
import java.awt.Component;
import java.awt.Graphics;
import java.util.Locale;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
* Created by plough on 2019/1/11.
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({Toolkit.class})
@PowerMockIgnore("javax.swing.*")
@Ignore("涉及到UI")
public class UIComponentUtilsTest {
private static final String HTML_TAG_TPL = "<html><body style='width: %dpx'>";
private static final String HTML_TAG = "<html>";
private UIButton textButton;
private UIButton emptyTextButton;
private UIButton iconButton;
private UILabel textLabel;
private UILabel emptyTextLabel;
private UILabel iconLabel;
@Before
public void setUp() {
PowerMock.mockStatic(Toolkit.class);
EasyMock.expect(Toolkit.i18nText(EasyMock.anyString())).andReturn("test").anyTimes();
PowerMock.replayAll();
textButton = new UIButton("hello");
emptyTextButton = new UIButton(StringUtils.EMPTY);
iconButton = new UIButton(createMockIcon());
textLabel = new UILabel("hello");
emptyTextLabel = new UILabel(StringUtils.EMPTY);
iconLabel = new UILabel(createMockIcon());
GeneralContext.setLocale(Locale.US);
}
@Test
public void testSetLineWrap() {
UITextComponent[] noWrapComps = {emptyTextButton, emptyTextLabel, iconButton, iconLabel};
UITextComponent[] wrapComps = {textLabel, textButton};
for (UITextComponent comp : wrapComps) {
UIComponentUtils.setLineWrap(comp);
assertTrue(isLineWrapped(comp));
}
for (UITextComponent comp : noWrapComps) {
UIComponentUtils.setLineWrap(comp);
assertFalse(isLineWrapped(comp));
}
}
@Test
public void testSetLineWrapWithLineWidth() {
UILabel label1 = new UILabel("l1");
UILabel label2 = new UILabel("l2");
UILabel label3 = new UILabel("l3");
UIComponentUtils.setLineWrap(label1, 50);
assertTrue(isLineWrappedWithLineWidth(label1, 50));
UIComponentUtils.setLineWrap(label2, 0);
assertTrue(isLineWrappedWithLineWidth(label2, 10));
UIComponentUtils.setLineWrap(label3, -10);
assertTrue(isLineWrappedWithLineWidth(label3, 10));
}
@Test
public void testAddHtmlTwice() {
UIComponentUtils.setLineWrap(textLabel, 50);
UIComponentUtils.setLineWrap(textLabel, 20); // 第二次应该不生效
assertTrue(isLineWrappedWithLineWidth(textLabel, 50));
}
private boolean isLineWrapped(UITextComponent comp) {
String text = comp.getText();
return StringUtils.isNotEmpty(text) && text.startsWith(HTML_TAG);
}
private boolean isLineWrappedWithLineWidth(UITextComponent comp, int width) {
String text = comp.getText();
return StringUtils.isNotEmpty(text) && text.startsWith(String.format(HTML_TAG_TPL, width));
}
private Icon createMockIcon() {
return new Icon() {
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
// do nothing
}
@Override
public int getIconWidth() {
return 0;
}
@Override
public int getIconHeight() {
return 0;
}
};
}
}