Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix changing temperature via Basic UI #2165

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1263,6 +1263,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")));
}
}