Skip to content

Commit

Permalink
Fix changing temperature via BasicUI (openhab#2091) (openhab#2165)
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Bandowski <christian@myvm.de>
  • Loading branch information
chris922 authored Jan 30, 2021
1 parent de61ce1 commit d48646d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1264,6 +1264,11 @@ public void removeRegistryHook(RegistryHook<Item> hook) {

// 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
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;
Expand Down Expand Up @@ -875,4 +877,43 @@ public void getDefaultWidgetsForStringItem() {
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")));
}
}

0 comments on commit d48646d

Please sign in to comment.