如何为自定义 SWT-Column-Ratio-Layout 编写 JUnit 测试?
How do I write a JUnit test for a custom SWT-Column-Ratio-Layout?
我在 Internet 上找到了一个自定义的 SWT-Column-Ratio 布局,它将 composite/control 的子项置于用户定义的比例中。不幸的是,我找不到 Column-Ratio Layout 实现的来源,但代码如下所示:
public class ColumnRatioLayout extends Layout {
int[] percentages;
public ColumnRatioLayout(int... percentages) {
this.percentages = percentages;
}
@Override
protected Point computeSize(Composite composite, int wHint, int hHint, boolean flushCache) {
Control[] children = composite.getChildren();
int height = hHint;
int width = wHint;
int consumedPercent = 0;
for (int i = 0; i < children.length; i++) {
int percent = 0;
calculatePercentAndConsumedPercent(percent, consumedPercent, children, i);
Point childSize = children[i].computeSize(wHint == -1 ? -1 : wHint * percent / 100, hHint);
if (wHint == SWT.DEFAULT) {
width = Math.max(width, childSize.x * (100 - percent) / 100);
}
if (hHint == SWT.DEFAULT) {
height = Math.max(height, childSize.y);
}
}
return new Point(width, Math.max(height, 0));
}
protected void calculatePercentAndConsumedPercent(int percent, int consumedPercent, Control[] children, int i) {
if (i >= percentages.length) {
percent = (100 - consumedPercent) / (children.length - percentages.length);
} else {
percent = percentages[i];
consumedPercent += percent;
}
}
@Override
protected void layout(Composite composite, boolean flushCache) {
Control[] children = composite.getChildren();
Rectangle available = composite.getClientArea();
int x = available.x;
int consumedPercent = 0;
for (int i = 0; i < children.length - 1; i++) {
int percent;
if (i >= percentages.length) {
percent = (100 - consumedPercent) / (children.length - percentages.length);
} else {
percent = percentages[i];
consumedPercent += percent;
}
int w = available.width * percent / 100;
children[i].setBounds(x, available.y, w, available.height);
x += w;
}
if (children.length > 0) {
children[children.length - 1].setBounds(x, available.y,
available.width - (x - available.x), available.height);
}
}
}
我想测试这个布局。我正在编写一个 JUnit 测试来测试使用此布局时比率是否为真。我已经这样做了,但它没有给我任何有用的输出 - Point {0, 0}:
public class ColumnRatioLayoutTest {
private static Display _display;
private static Shell _shell;
private static Composite _comp;
@BeforeAll
public static void setUpAll() {
_display = new Display();
_shell = new Shell(_display);
_comp = new Composite(_shell, SWT.NONE);
}
@Test
public void setLayoutTest() {
int[] colRatio = {20, 80};
ColumnRatioLayout colLayout = new ColumnRatioLayout(colRatio);
_comp.setLayout(colLayout);
_comp.setSize(_comp.computeSize(SWT.DEFAULT, SWT.DEFAULT));
Composite comp1 = new Composite(_comp, SWT.NONE);
comp1.setLayout(new FillLayout());
comp1.setSize(comp1.computeSize(SWT.DEFAULT, SWT.DEFAULT));
Composite comp2 = new Composite(_comp, SWT.NONE);
comp2.setLayout(new FillLayout());
comp2.setSize(comp2.computeSize(SWT.DEFAULT, SWT.DEFAULT));
System.out.println("Comp1 size: " + _comp.getSize());
}
}
我基本上想比较两个复合材料的大小,看看一个是另一个的 4 倍。这将完成我的测试。我怎么做?提前致谢。
您可以这样测试布局:
public class ColumnRatioLayoutTest {
private Display display;
private Shell shell;
@BeforeEach
public void setUp() {
display = new Display();
shell = new Shell(display);
}
@AfterEach
public void tearDown() {
display.dispose();
}
@Test
public void testLayout() {
shell.setSize(shell.computeSize(100, SWT.DEFAULT));
Control control20 = new Label(shell, SWT.NONE);
Control control80 = new Label(shell, SWT.NONE);
shell.setLayout(new ColumnRatioLayout(20, 80));
shell.layout();
assertEquals(100, shell.getSize().x);
assertEquals(20, control20.getSize().x);
assertEquals(80, control80.getSize().x);
}
}
测试创建了一个shell,其客户区宽度为 100 像素,然后确保应该占据 20% 和 80% 宽度的两个控件实际上是 20 和 80 像素宽。
无需声明静态 Display
和 Shell
,为每个测试重新创建它们可确保测试保持隔离。
顺便说一句,由布局管理的小部件不得调用 setSize
或以其他方式修改它们的边界,即您的代码不得调用 comp1.setSize(...);
而且,请遵循 Java Naming conventions,不要在变量前加上下划线
我在 Internet 上找到了一个自定义的 SWT-Column-Ratio 布局,它将 composite/control 的子项置于用户定义的比例中。不幸的是,我找不到 Column-Ratio Layout 实现的来源,但代码如下所示:
public class ColumnRatioLayout extends Layout {
int[] percentages;
public ColumnRatioLayout(int... percentages) {
this.percentages = percentages;
}
@Override
protected Point computeSize(Composite composite, int wHint, int hHint, boolean flushCache) {
Control[] children = composite.getChildren();
int height = hHint;
int width = wHint;
int consumedPercent = 0;
for (int i = 0; i < children.length; i++) {
int percent = 0;
calculatePercentAndConsumedPercent(percent, consumedPercent, children, i);
Point childSize = children[i].computeSize(wHint == -1 ? -1 : wHint * percent / 100, hHint);
if (wHint == SWT.DEFAULT) {
width = Math.max(width, childSize.x * (100 - percent) / 100);
}
if (hHint == SWT.DEFAULT) {
height = Math.max(height, childSize.y);
}
}
return new Point(width, Math.max(height, 0));
}
protected void calculatePercentAndConsumedPercent(int percent, int consumedPercent, Control[] children, int i) {
if (i >= percentages.length) {
percent = (100 - consumedPercent) / (children.length - percentages.length);
} else {
percent = percentages[i];
consumedPercent += percent;
}
}
@Override
protected void layout(Composite composite, boolean flushCache) {
Control[] children = composite.getChildren();
Rectangle available = composite.getClientArea();
int x = available.x;
int consumedPercent = 0;
for (int i = 0; i < children.length - 1; i++) {
int percent;
if (i >= percentages.length) {
percent = (100 - consumedPercent) / (children.length - percentages.length);
} else {
percent = percentages[i];
consumedPercent += percent;
}
int w = available.width * percent / 100;
children[i].setBounds(x, available.y, w, available.height);
x += w;
}
if (children.length > 0) {
children[children.length - 1].setBounds(x, available.y,
available.width - (x - available.x), available.height);
}
}
}
我想测试这个布局。我正在编写一个 JUnit 测试来测试使用此布局时比率是否为真。我已经这样做了,但它没有给我任何有用的输出 - Point {0, 0}:
public class ColumnRatioLayoutTest {
private static Display _display;
private static Shell _shell;
private static Composite _comp;
@BeforeAll
public static void setUpAll() {
_display = new Display();
_shell = new Shell(_display);
_comp = new Composite(_shell, SWT.NONE);
}
@Test
public void setLayoutTest() {
int[] colRatio = {20, 80};
ColumnRatioLayout colLayout = new ColumnRatioLayout(colRatio);
_comp.setLayout(colLayout);
_comp.setSize(_comp.computeSize(SWT.DEFAULT, SWT.DEFAULT));
Composite comp1 = new Composite(_comp, SWT.NONE);
comp1.setLayout(new FillLayout());
comp1.setSize(comp1.computeSize(SWT.DEFAULT, SWT.DEFAULT));
Composite comp2 = new Composite(_comp, SWT.NONE);
comp2.setLayout(new FillLayout());
comp2.setSize(comp2.computeSize(SWT.DEFAULT, SWT.DEFAULT));
System.out.println("Comp1 size: " + _comp.getSize());
}
}
我基本上想比较两个复合材料的大小,看看一个是另一个的 4 倍。这将完成我的测试。我怎么做?提前致谢。
您可以这样测试布局:
public class ColumnRatioLayoutTest {
private Display display;
private Shell shell;
@BeforeEach
public void setUp() {
display = new Display();
shell = new Shell(display);
}
@AfterEach
public void tearDown() {
display.dispose();
}
@Test
public void testLayout() {
shell.setSize(shell.computeSize(100, SWT.DEFAULT));
Control control20 = new Label(shell, SWT.NONE);
Control control80 = new Label(shell, SWT.NONE);
shell.setLayout(new ColumnRatioLayout(20, 80));
shell.layout();
assertEquals(100, shell.getSize().x);
assertEquals(20, control20.getSize().x);
assertEquals(80, control80.getSize().x);
}
}
测试创建了一个shell,其客户区宽度为 100 像素,然后确保应该占据 20% 和 80% 宽度的两个控件实际上是 20 和 80 像素宽。
无需声明静态 Display
和 Shell
,为每个测试重新创建它们可确保测试保持隔离。
顺便说一句,由布局管理的小部件不得调用 setSize
或以其他方式修改它们的边界,即您的代码不得调用 comp1.setSize(...);
而且,请遵循 Java Naming conventions,不要在变量前加上下划线