Browse Source

添加单元测试

feature/big-screen
kerry 4 years ago
parent
commit
878d65de01
  1. 39
      designer-base/src/main/java/com/fr/design/mainframe/JFormSliderPane.java
  2. 51
      designer-base/src/test/java/com/fr/design/mainframe/JFormSliderPaneTest.java

39
designer-base/src/main/java/com/fr/design/mainframe/JFormSliderPane.java

@ -131,7 +131,7 @@ public class JFormSliderPane extends JPanel {
int code = evt.getKeyCode();
if (code == KeyEvent.VK_ENTER) {
showValue = parseInputValue();
showValue = parseInputValue(showValField.getText());
showValField.setText(showValue + SUFFIX);
showValFieldChange();
}
@ -151,22 +151,26 @@ public class JFormSliderPane extends JPanel {
private void showValFieldChange() {
isButtonOrIsTxt = true;
if (showValue > FOUR_HUNDRED) {
showValue = FOUR_HUNDRED;
}
if (showValue < TEN) {
showValue = TEN;
}
showValue = getPreferredValue(showValue);
refreshShowValueFieldText();
refreshSlider();
}
private int getPreferredValue(int value){
if (value > FOUR_HUNDRED) {
value = FOUR_HUNDRED;
}
if (value < TEN) {
value = TEN;
}
return value;
}
private void refreshShowValueFieldText(){
showValField.setText(showValue + SUFFIX);
}
private int parseInputValue(){
String text = showValField.getText();
private int parseInputValue(String text){
if (text.endsWith(SUFFIX)){
text = text.substring(0, text.length() -1);
}
@ -175,7 +179,6 @@ public class JFormSliderPane extends JPanel {
}catch (NumberFormatException e){
return HUNDRED;
}
}
@ -201,13 +204,19 @@ public class JFormSliderPane extends JPanel {
private void refreshSlider() {
if (showValue > HUNDRED) {
slider.setValue((int) (showValue + TWO_HUNDRED) / SIX);
} else if (showValue < HUNDRED) {
slider.setValue((int) ((showValue - TEN) / ONEPOINTEIGHT));
slider.setValue(calSliderValue(showValue));
}
private int calSliderValue(int value) {
int result;
if (value > HUNDRED) {
result = (value + TWO_HUNDRED) / SIX;
} else if (value < HUNDRED) {
result = (int) ((value - TEN) / ONEPOINTEIGHT);
} else {
slider.setValue(HALF_HUNDRED);
result = HALF_HUNDRED;
}
return result;
}

51
designer-base/src/test/java/com/fr/design/mainframe/JFormSliderPaneTest.java

@ -0,0 +1,51 @@
package com.fr.design.mainframe;
import com.fr.invoke.Reflect;
import org.junit.Assert;
import org.junit.Test;
/**
* Created by kerry on 2020-07-28
*/
public class JFormSliderPaneTest {
@Test
public void testParseInputValue() {
JFormSliderPane sliderPane = new JFormSliderPane();
int result = Reflect.on(sliderPane).call("parseInputValue", "100%").get();
Assert.assertEquals(100, result);
result = Reflect.on(sliderPane).call("parseInputValue", "50%").get();
Assert.assertEquals(50, result);
result = Reflect.on(sliderPane).call("parseInputValue", "50").get();
Assert.assertEquals(50, result);
result = Reflect.on(sliderPane).call("parseInputValue", "50abc").get();
Assert.assertEquals(100, result);
}
@Test
public void testGetPreferredValue() {
JFormSliderPane sliderPane = new JFormSliderPane();
int result = Reflect.on(sliderPane).call("getPreferredValue", 100).get();
Assert.assertEquals(100, result);
result = Reflect.on(sliderPane).call("getPreferredValue", 0).get();
Assert.assertEquals(10, result);
result = Reflect.on(sliderPane).call("getPreferredValue", 1000).get();
Assert.assertEquals(400, result);
}
@Test
public void testCalSliderValue() {
JFormSliderPane sliderPane = new JFormSliderPane();
int result = Reflect.on(sliderPane).call("calSliderValue", 10).get();
Assert.assertEquals(0, result);
result = Reflect.on(sliderPane).call("calSliderValue", 90).get();
Assert.assertEquals(44, result);
result = Reflect.on(sliderPane).call("calSliderValue", 100).get();
Assert.assertEquals(50, result);
result = Reflect.on(sliderPane).call("calSliderValue", 200).get();
Assert.assertEquals(66, result);
result = Reflect.on(sliderPane).call("calSliderValue", 400).get();
Assert.assertEquals(100, result);
}
}
Loading…
Cancel
Save