Fix changing temperature via BasicUI (#2091) (#2165)

Signed-off-by: Christian Bandowski <christian@myvm.de>
This commit is contained in:
Christian Bandowski
2021-01-30 22:50:58 +01:00
committed by GitHub
parent de61ce1ead
commit d48646d8c6
2 changed files with 46 additions and 0 deletions
@@ -1264,6 +1264,11 @@ public class ItemUIRegistryImpl implements ItemUIRegistry {
// we require the item to define a dimension, otherwise no unit will be reported to the UIs.
if (item instanceof NumberItem && ((NumberItem) item).getDimension() != null) {
if (w.getLabel() == null) {
// if no Label was assigned to the Widget we fallback to the items unit
return ((NumberItem) item).getUnitSymbol();
}
String unit = getUnitFromLabel(w.getLabel());
if (!UnitUtils.UNIT_PLACEHOLDER.equals(unit)) {
return unit;
@@ -24,6 +24,8 @@ import java.util.ArrayList;
import java.util.List;
import java.util.TimeZone;
import javax.measure.quantity.Temperature;
import org.eclipse.emf.common.util.BasicEList;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -875,4 +877,43 @@ public class ItemUIRegistryImplTest {
defaultWidget = uiRegistry.getDefaultWidget(StringItem.class, ITEM_NAME);
assertThat(defaultWidget, is(instanceOf(Text.class)));
}
@Test
public void getUnitForWidgetForNonNumberItem() throws Exception {
String unit = uiRegistry.getUnitForWidget(widget);
assertThat(unit, is(""));
}
@Test
public void getUnitForWidgetWithWidgetLabel() throws Exception {
// a NumberItem having a Dimension must be returned
NumberItem item = mock(NumberItem.class);
when(registry.getItem(ITEM_NAME)).thenReturn(item);
doReturn(Temperature.class).when(item).getDimension();
// we set the Label on the widget itself
when(widget.getLabel()).thenReturn("Label [%.1f °C]");
String unit = uiRegistry.getUnitForWidget(widget);
assertThat(unit, is(equalTo("°C")));
}
@Test
public void getUnitForWidgetWithItemLabelAndWithoutWidgetLabel() throws Exception {
// a NumberItem having a Dimension must be returned
NumberItem item = mock(NumberItem.class);
when(registry.getItem(ITEM_NAME)).thenReturn(item);
doReturn(Temperature.class).when(item).getDimension();
// we set the UnitSymbol on the item, this must be used as a fallback if no Widget label was used
when(item.getUnitSymbol()).thenReturn("°C");
String unit = uiRegistry.getUnitForWidget(widget);
assertThat(unit, is(equalTo("°C")));
}
}