From 1ab1086e4af90081de1bf2810aa4fcf74356aca3 Mon Sep 17 00:00:00 2001 From: Christian Bandowski Date: Sat, 30 Jan 2021 15:01:05 +0100 Subject: [PATCH] Fix changing temperature via BasicUI (#2091) --- .../ui/internal/items/ItemUIRegistryImpl.java | 5 ++ .../items/ItemUIRegistryImplTest.java | 53 +++++++++++++++++-- 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/bundles/org.openhab.core.ui/src/main/java/org/openhab/core/ui/internal/items/ItemUIRegistryImpl.java b/bundles/org.openhab.core.ui/src/main/java/org/openhab/core/ui/internal/items/ItemUIRegistryImpl.java index f55fd012938..f2927bb702d 100644 --- a/bundles/org.openhab.core.ui/src/main/java/org/openhab/core/ui/internal/items/ItemUIRegistryImpl.java +++ b/bundles/org.openhab.core.ui/src/main/java/org/openhab/core/ui/internal/items/ItemUIRegistryImpl.java @@ -1263,6 +1263,11 @@ public void removeRegistryHook(RegistryHook 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; diff --git a/bundles/org.openhab.core.ui/src/test/java/org/openhab/core/ui/internal/items/ItemUIRegistryImplTest.java b/bundles/org.openhab.core.ui/src/test/java/org/openhab/core/ui/internal/items/ItemUIRegistryImplTest.java index 28174686129..da93dfdc77a 100644 --- a/bundles/org.openhab.core.ui/src/test/java/org/openhab/core/ui/internal/items/ItemUIRegistryImplTest.java +++ b/bundles/org.openhab.core.ui/src/test/java/org/openhab/core/ui/internal/items/ItemUIRegistryImplTest.java @@ -12,18 +12,26 @@ */ package org.openhab.core.ui.internal.items; -import static org.hamcrest.CoreMatchers.*; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.instanceOf; +import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.collection.IsCollectionWithSize.hasSize; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; import java.text.DecimalFormatSymbols; 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 +883,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"))); + } }