blob: 340fcdb76856a723b74edf32e642394b9252525a [file] [log] [blame]
/**
* Copyright (c) 2012, 2015 - Lunifera GmbH (Austria), Loetz GmbH&Co.KG and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Florian Pirchner - initial API and implementation
*/
package org.eclipse.osbp.runtime.web.ecview.presentation.vaadin.tests.presentation;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.eclipse.osbp.ecview.core.common.context.ContextException;
import org.eclipse.osbp.ecview.core.common.context.IViewContext;
import org.eclipse.osbp.ecview.core.common.editpart.DelegatingEditPartManager;
import org.eclipse.osbp.ecview.core.common.editpart.IConverterEditpart;
import org.eclipse.osbp.ecview.core.common.editpart.IElementEditpart;
import org.eclipse.osbp.ecview.core.common.editpart.IEmbeddableEditpart;
import org.eclipse.osbp.ecview.core.common.editpart.IViewEditpart;
import org.eclipse.osbp.ecview.core.common.model.binding.YBeanValueBindingEndpoint;
import org.eclipse.osbp.ecview.core.common.model.binding.YBindingSet;
import org.eclipse.osbp.ecview.core.common.model.core.CoreModelFactory;
import org.eclipse.osbp.ecview.core.common.model.core.YDelegateConverter;
import org.eclipse.osbp.ecview.core.common.model.core.YElement;
import org.eclipse.osbp.ecview.core.common.model.core.YView;
import org.eclipse.osbp.ecview.core.common.presentation.DelegatingConverterFactory;
import org.eclipse.osbp.ecview.core.common.presentation.IConverterFactory;
import org.eclipse.osbp.ecview.core.common.presentation.IWidgetPresentation;
import org.eclipse.osbp.ecview.core.extension.model.datatypes.YDecimalDatatype;
import org.eclipse.osbp.ecview.core.extension.model.datatypes.YNumericDatatype;
import org.eclipse.osbp.ecview.core.extension.model.extension.YGridLayout;
import org.eclipse.osbp.ecview.core.extension.model.extension.YNumericField;
import org.eclipse.osbp.ecview.core.extension.model.extension.util.SimpleExtensionModelFactory;
import org.eclipse.osbp.ecview.core.ui.core.editparts.extension.INumericFieldEditpart;
import org.eclipse.osbp.runtime.common.i18n.II18nService;
import org.eclipse.osbp.runtime.web.ecview.presentation.vaadin.VaadinRenderer;
import org.eclipse.osbp.runtime.web.ecview.presentation.vaadin.common.AbstractFieldWidgetPresenter;
import org.eclipse.osbp.runtime.web.ecview.presentation.vaadin.common.AbstractVaadinWidgetPresenter;
import org.eclipse.osbp.runtime.web.ecview.presentation.vaadin.internal.TextFieldPresentation;
import org.eclipse.osbp.runtime.web.ecview.presentation.vaadin.tests.model.ValueBean;
import org.eclipse.osbp.runtime.web.vaadin.components.converter.NumberConverter;
import org.eclipse.osbp.runtime.web.vaadin.components.fields.NumericField;
import org.osgi.framework.BundleException;
import org.osgi.service.cm.ConfigurationException;
import com.vaadin.ui.Component;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.UI;
/**
* Tests the {@link TextFieldPresentation}.
*/
@SuppressWarnings("restriction")
public class NumericFieldPresentationTests {
private SimpleExtensionModelFactory factory = new SimpleExtensionModelFactory();
private CssLayout rootLayout = new CssLayout();
/**
* Setup tests.
*
* @throws ConfigurationException
* @throws BundleException
*/
@Before
public void setup() throws ConfigurationException, BundleException {
Locale.setDefault(Locale.GERMANY);
UI.setCurrent(new DefaultUI());
UI.getCurrent().setContent(rootLayout);
}
@After
public void after() {
DelegatingConverterFactory.getInstance().clear();
}
/**
* Tests rendering issues.
*
* @throws Exception
*/
@Test
// BEGIN SUPRESS CATCH EXCEPTION
public void test_isRendered_unrender_byModel() throws Exception {
// END SUPRESS CATCH EXCEPTION
// build the view model
// ...> yView
// ......> yGridLayout
// .........> yText
YView yView = factory.createView();
YGridLayout yGridlayout = factory.createGridLayout();
yView.setContent(yGridlayout);
YNumericField yText = factory.createNumericField();
yGridlayout.getElements().add(yText);
VaadinRenderer renderer = new VaadinRenderer();
IViewContext viewContext = renderer.render(rootLayout, yView, null);
INumericFieldEditpart textEditpart = DelegatingEditPartManager
.getInstance().getEditpart(viewContext, yText);
IWidgetPresentation<Component> presentation = textEditpart
.getPresentation();
assertTrue(presentation.isRendered());
assertFalse(presentation.isDisposed());
yGridlayout.getElements().remove(yText);
assertFalse(presentation.isRendered());
assertFalse(presentation.isDisposed());
}
/**
* Tests the internal structure.
*
* @throws Exception
*/
@Test
// BEGIN SUPRESS CATCH EXCEPTION
public void test_InternalStructure() throws Exception {
// END SUPRESS CATCH EXCEPTION
// build the view model
// ...> yView
// ......> yText
YView yView = factory.createView();
YNumericField yText = factory.createNumericField();
yView.setContent(yText);
VaadinRenderer renderer = new VaadinRenderer();
IViewContext viewContext = renderer.render(rootLayout, yView, null);
INumericFieldEditpart textEditpart = DelegatingEditPartManager
.getInstance().getEditpart(viewContext, yText);
IWidgetPresentation<Component> presentation = textEditpart
.getPresentation();
NumericField text = (NumericField) presentation.getWidget();
assertNotNull(text);
}
/**
* Test the internal structure based on CSS.
*
* @throws Exception
*/
@Test
// BEGIN SUPRESS CATCH EXCEPTION
public void test_InternalStructure__CSS() throws Exception {
// END SUPRESS CATCH EXCEPTION
// build the view model
// ...> yView
// ......> yText
YView yView = factory.createView();
YGridLayout yLayout = factory.createGridLayout();
yView.setContent(yLayout);
YNumericField yText1 = factory.createNumericField();
yText1.setCssID("ID_0815");
yText1.setCssClass("anyOtherClass");
yLayout.getElements().add(yText1);
YNumericField yText2 = factory.createNumericField();
yLayout.getElements().add(yText2);
VaadinRenderer renderer = new VaadinRenderer();
IViewContext viewContext = renderer.render(rootLayout, yView, null);
INumericFieldEditpart text1Editpart = DelegatingEditPartManager
.getInstance().getEditpart(viewContext, yText1);
INumericFieldEditpart text2Editpart = DelegatingEditPartManager
.getInstance().getEditpart(viewContext, yText2);
IWidgetPresentation<Component> text1Presentation = text1Editpart
.getPresentation();
IWidgetPresentation<Component> text2Presentation = text2Editpart
.getPresentation();
NumericField text1 = (NumericField) text1Presentation.getWidget();
NumericField text2 = (NumericField) text2Presentation.getWidget();
// assert css class
assertTrue(text1.getStyleName().contains("anyOtherClass"));
assertTrue(text2.getStyleName().contains(
AbstractVaadinWidgetPresenter.CSS_CLASS_CONTROL));
// assert css id
assertEquals("ID_0815", text1.getId());
assertEquals(text2Editpart.getId(), text2.getId());
}
/**
* Test the internal structure based on CSS.
*
* @throws Exception
*/
@Test
// BEGIN SUPRESS CATCH EXCEPTION
public void test_Bindings() throws Exception {
// END SUPRESS CATCH EXCEPTION
// build the view model
// ...> yView
// ......> yText
YView yView = factory.createView();
YGridLayout yLayout = factory.createGridLayout();
yView.setContent(yLayout);
YNumericField yText1 = factory.createNumericField();
yLayout.getElements().add(yText1);
YNumericField yText2 = factory.createNumericField();
yLayout.getElements().add(yText2);
VaadinRenderer renderer = new VaadinRenderer();
IViewContext viewContext = renderer.render(rootLayout, yView, null);
INumericFieldEditpart text1Editpart = DelegatingEditPartManager
.getInstance().getEditpart(viewContext, yText1);
INumericFieldEditpart text2Editpart = DelegatingEditPartManager
.getInstance().getEditpart(viewContext, yText2);
IWidgetPresentation<Component> text1Presentation = text1Editpart
.getPresentation();
IWidgetPresentation<Component> text2Presentation = text2Editpart
.getPresentation();
NumericField text1 = (NumericField) text1Presentation.getWidget();
NumericField text2 = (NumericField) text2Presentation.getWidget();
// start tests
//
assertTrue(text1.isVisible());
assertTrue(text1.isEnabled());
assertFalse(text1.isReadOnly());
assertTrue(text2.isVisible());
assertTrue(text2.isEnabled());
assertFalse(text2.isReadOnly());
yText1.setVisible(false);
assertFalse(text1.isVisible());
yText1.setEnabled(false);
assertFalse(text1.isEnabled());
yText1.setEditable(false);
assertTrue(text1.isReadOnly());
// target to model
text1.setReadOnly(false);
assertTrue(yText1.isEditable());
yText1.setValue(112233);
assertEquals("112.233", text1.getValue());
yText1.setValue(332211);
assertEquals("332.211", text1.getValue());
text1.setValue("998.877");
assertEquals(998877, yText1.getValue());
}
@Test
// BEGIN SUPRESS CATCH EXCEPTION
public void test_ValueBinding() throws Exception {
// END SUPRESS CATCH EXCEPTION
// build the view model
// ...> yView
// ......> yText
YView yView = factory.createView();
YGridLayout yLayout = factory.createGridLayout();
yView.setContent(yLayout);
YNumericField yField1 = factory.createNumericField();
yLayout.getElements().add(yField1);
VaadinRenderer renderer = new VaadinRenderer();
IViewContext viewContext = renderer.render(rootLayout, yView, null);
INumericFieldEditpart text1Editpart = DelegatingEditPartManager
.getInstance().getEditpart(viewContext, yField1);
IWidgetPresentation<Component> text1Presentation = text1Editpart
.getPresentation();
NumericField field1 = (NumericField) text1Presentation.getWidget();
// start tests
//
YBindingSet yBindingSet = yView.getOrCreateBindingSet();
YBeanValueBindingEndpoint beanBinding = factory
.createBeanBindingEndpoint();
ValueBean bean = new ValueBean(998877);
beanBinding.setPropertyPath("intValue");
beanBinding.setBean(bean);
yBindingSet.addBinding(yField1.createValueEndpoint(), beanBinding);
assertEquals("998.877", field1.getValue());
assertEquals(998877, yField1.getValue());
bean.setIntValue(223344);
assertEquals("223.344", field1.getValue());
assertEquals(223344, yField1.getValue());
field1.setValue("445.566");
assertEquals(445566, bean.getIntValue());
assertEquals(445566, yField1.getValue());
yField1.setValue(778899);
assertEquals(778899, bean.getIntValue());
assertEquals("778.899", field1.getValue());
}
/**
* Test the automatic disposal of bindings
*
* @throws ContextException
*/
@Test
public void testBindingIsDisposed() throws ContextException {
YView yView = factory.createView();
YGridLayout yGridlayout = factory.createGridLayout();
yView.setContent(yGridlayout);
YNumericField yText = factory.createNumericField();
yGridlayout.getElements().add(yText);
VaadinRenderer renderer = new VaadinRenderer();
IViewContext viewContext = renderer.render(rootLayout, yView, null);
INumericFieldEditpart textEditpart = DelegatingEditPartManager
.getInstance().getEditpart(viewContext, yText);
IWidgetPresentation<Component> presentation = textEditpart
.getPresentation();
assertTrue(presentation.isRendered());
assertFalse(presentation.isDisposed());
assertEquals(4, presentation.getUIBindings().size());
presentation.dispose();
assertFalse(presentation.isRendered());
assertTrue(presentation.isDisposed());
assertEquals(0, presentation.getUIBindings().size());
}
@Test
public void testMarkNegative() throws ContextException {
YView yView = factory.createView();
YGridLayout yGridlayout = factory.createGridLayout();
yView.setContent(yGridlayout);
YNumericField yText = factory.createNumericField();
yGridlayout.getElements().add(yText);
VaadinRenderer renderer = new VaadinRenderer();
IViewContext viewContext = renderer.render(rootLayout, yView, null);
INumericFieldEditpart textEditpart = DelegatingEditPartManager
.getInstance().getEditpart(viewContext, yText);
IWidgetPresentation<Component> presentation = textEditpart
.getPresentation();
NumericField field = (NumericField) presentation.getWidget();
yText.setValue(99);
assertEquals("99", field.getValue());
assertFalse(field.getStyleName().contains("lun-negative-value"));
yText.setValue(-99);
assertEquals("-99", field.getValue());
assertTrue(field.getStyleName().contains("lun-negative-value"));
}
/**
* Test grouping of decimals
*
* @throws ContextException
*/
@Test
public void testGrouping() throws ContextException {
YView yView = factory.createView();
YGridLayout yLayout = factory.createGridLayout();
yView.setContent(yLayout);
YNumericField yText1 = factory.createNumericField();
yLayout.getElements().add(yText1);
YDecimalDatatype dt1 = factory.createDecimalDatatype();
dt1.setGrouping(true);
yText1.setDatatype(dt1);
YNumericField yText2 = factory.createNumericField();
yLayout.getElements().add(yText2);
YDecimalDatatype dt2 = factory.createDecimalDatatype();
dt2.setGrouping(false);
yText2.setDatatype(dt2);
VaadinRenderer renderer = new VaadinRenderer();
IViewContext viewContext = renderer.render(rootLayout, yView, null);
INumericFieldEditpart text1Editpart = DelegatingEditPartManager
.getInstance().getEditpart(viewContext, yText1);
INumericFieldEditpart text2Editpart = DelegatingEditPartManager
.getInstance().getEditpart(viewContext, yText2);
IWidgetPresentation<Component> text1Presentation = text1Editpart
.getPresentation();
IWidgetPresentation<Component> text2Presentation = text2Editpart
.getPresentation();
NumericField text1 = (NumericField) text1Presentation.getWidget();
NumericField text2 = (NumericField) text2Presentation.getWidget();
yText1.setValue(112233);
assertEquals("112.233", text1.getValue());
yText2.setValue(332211);
assertEquals("332211", text2.getValue());
}
@Test
public void testGrouping_ByChangingDatatype() throws ContextException {
YView yView = factory.createView();
YGridLayout yLayout = factory.createGridLayout();
yView.setContent(yLayout);
YNumericField yField = factory.createNumericField();
yLayout.getElements().add(yField);
YDecimalDatatype dt1 = factory.createDecimalDatatype();
dt1.setGrouping(true);
YDecimalDatatype dt2 = factory.createDecimalDatatype();
dt2.setPrecision(1);
dt2.setGrouping(false);
VaadinRenderer renderer = new VaadinRenderer();
IViewContext viewContext = renderer.render(rootLayout, yView, null);
INumericFieldEditpart text1Editpart = DelegatingEditPartManager
.getInstance().getEditpart(viewContext, yField);
IWidgetPresentation<Component> text1Presentation = text1Editpart
.getPresentation();
NumericField field = (NumericField) text1Presentation.getWidget();
// start tests
yField.setDatatype(dt1);
yField.setValue(112233);
assertEquals("112.233", field.getValue());
assertEquals(112233, yField.getValue(), 0);
yField.setDatatype(dt2);
assertEquals("112233", field.getValue());
assertEquals(112233, yField.getValue(), 0);
yField.setValue(4567);
assertEquals("4567", field.getValue());
assertEquals(4567, yField.getValue(), 0);
}
@Test
public void testMarkNegative_ByChangingDatatype() throws ContextException {
YView yView = factory.createView();
YGridLayout yLayout = factory.createGridLayout();
yView.setContent(yLayout);
YNumericField yField = factory.createNumericField();
yLayout.getElements().add(yField);
YDecimalDatatype dt1 = factory.createDecimalDatatype();
dt1.setMarkNegative(true);
YDecimalDatatype dt2 = factory.createDecimalDatatype();
dt2.setPrecision(1);
dt2.setMarkNegative(false);
VaadinRenderer renderer = new VaadinRenderer();
IViewContext viewContext = renderer.render(rootLayout, yView, null);
INumericFieldEditpart text1Editpart = DelegatingEditPartManager
.getInstance().getEditpart(viewContext, yField);
IWidgetPresentation<Component> text1Presentation = text1Editpart
.getPresentation();
NumericField field = (NumericField) text1Presentation.getWidget();
// start tests
yField.setDatatype(dt1);
yField.setValue(112233);
assertEquals("112.233", field.getValue());
assertEquals(112233, yField.getValue(), 0);
assertFalse(field.getStyleName().contains("lun-negative-value"));
yField.setValue(-112233);
assertEquals("-112.233", field.getValue());
assertEquals(-112233, yField.getValue(), 0);
assertTrue(field.getStyleName().contains("lun-negative-value"));
yField.setDatatype(dt2);
assertEquals("-112.233", field.getValue());
assertEquals(-112233, yField.getValue(), 0);
assertFalse(field.getStyleName().contains("lun-negative-value"));
}
@Test
public void test_i18n() throws ContextException {
// switch the global locale to german
Locale.setDefault(Locale.GERMAN);
YView yView = factory.createView();
YGridLayout yGridlayout = factory.createGridLayout();
yView.setContent(yGridlayout);
YNumericField yText = factory.createNumericField();
yText.setValue(123456789);
yGridlayout.getElements().add(yText);
// set the i18n key
yText.setLabelI18nKey(I18nServiceForTests.KEY__AGE);
// prepare the I18nService and pass it to the renderer
Map<String, Object> parameter = new HashMap<String, Object>();
Map<String, Object> services = new HashMap<String, Object>();
parameter.put(IViewContext.PARAM_SERVICES, services);
services.put(II18nService.ID, new I18nServiceForTests());
VaadinRenderer renderer = new VaadinRenderer();
IViewContext viewContext = renderer.render(rootLayout, yView, parameter);
INumericFieldEditpart textEditpart = DelegatingEditPartManager
.getInstance().getEditpart(viewContext, yText);
AbstractFieldWidgetPresenter<Component> presentation = textEditpart
.getPresentation();
NumericField textField = (NumericField) presentation.getWidget();
assertEquals("Alter", presentation.getWidget().getCaption());
assertEquals("123.456.789", textField.getValue());
viewContext.setLocale(Locale.ENGLISH);
assertEquals("Age", presentation.getWidget().getCaption());
assertEquals("123,456,789", textField.getValue());
}
@Test
// BEGIN SUPRESS CATCH EXCEPTION
public void test_Readonly_Binding() throws Exception {
// END SUPRESS CATCH EXCEPTION
// build the view model
YView yView = factory.createView();
YGridLayout yLayout = factory.createGridLayout();
yView.setContent(yLayout);
YNumericField yText = factory.createNumericField();
yLayout.getElements().add(yText);
VaadinRenderer renderer = new VaadinRenderer();
IViewContext viewContext = renderer.render(rootLayout, yView, null);
INumericFieldEditpart editpart = DelegatingEditPartManager
.getInstance().getEditpart(viewContext, yText);
IWidgetPresentation<Component> presentation = editpart
.getPresentation();
NumericField textField = (NumericField) presentation.getWidget();
ValueBean bean = new ValueBean(false);
YBeanValueBindingEndpoint yBeanBinding = factory
.createBeanBindingEndpoint();
yBeanBinding.setBean(bean);
yBeanBinding.setPropertyPath("boolValue");
YBindingSet yBindingSet = yView.getOrCreateBindingSet();
yBindingSet.addBinding(yText.createEditableEndpoint(), yBeanBinding);
// test binding
assertFalse(yText.isEditable());
assertFalse(!textField.isReadOnly());
assertFalse(bean.isBoolValue());
bean.setBoolValue(true);
assertTrue(yText.isEditable());
assertTrue(!textField.isReadOnly());
assertTrue(bean.isBoolValue());
}
@Test
// BEGIN SUPRESS CATCH EXCEPTION
public void test_Visible_Binding() throws Exception {
// END SUPRESS CATCH EXCEPTION
// build the view model
YView yView = factory.createView();
YGridLayout yLayout = factory.createGridLayout();
yView.setContent(yLayout);
YNumericField yText = factory.createNumericField();
yLayout.getElements().add(yText);
VaadinRenderer renderer = new VaadinRenderer();
IViewContext viewContext = renderer.render(rootLayout, yView, null);
INumericFieldEditpart editpart = DelegatingEditPartManager
.getInstance().getEditpart(viewContext, yText);
IWidgetPresentation<Component> presentation = editpart
.getPresentation();
NumericField textField = (NumericField) presentation.getWidget();
ValueBean bean = new ValueBean(false);
YBeanValueBindingEndpoint yBeanBinding = factory
.createBeanBindingEndpoint();
yBeanBinding.setBean(bean);
yBeanBinding.setPropertyPath("boolValue");
YBindingSet yBindingSet = yView.getOrCreateBindingSet();
yBindingSet.addBinding(yText.createVisibleEndpoint(), yBeanBinding);
// test binding
assertFalse(yText.isVisible());
assertFalse(textField.isVisible());
assertFalse(bean.isBoolValue());
bean.setBoolValue(true);
assertTrue(yText.isVisible());
assertTrue(textField.isVisible());
assertTrue(bean.isBoolValue());
}
@Test
// BEGIN SUPRESS CATCH EXCEPTION
public void test_Enabled_Binding() throws Exception {
// END SUPRESS CATCH EXCEPTION
// build the view model
YView yView = factory.createView();
YGridLayout yLayout = factory.createGridLayout();
yView.setContent(yLayout);
YNumericField yText = factory.createNumericField();
yLayout.getElements().add(yText);
VaadinRenderer renderer = new VaadinRenderer();
IViewContext viewContext = renderer.render(rootLayout, yView, null);
INumericFieldEditpart editpart = DelegatingEditPartManager
.getInstance().getEditpart(viewContext, yText);
IWidgetPresentation<Component> presentation = editpart
.getPresentation();
NumericField textField = (NumericField) presentation.getWidget();
ValueBean bean = new ValueBean(false);
YBeanValueBindingEndpoint yBeanBinding = factory
.createBeanBindingEndpoint();
yBeanBinding.setBean(bean);
yBeanBinding.setPropertyPath("boolValue");
YBindingSet yBindingSet = yView.getOrCreateBindingSet();
yBindingSet.addBinding(yText.createEnabledEndpoint(), yBeanBinding);
// test binding
assertFalse(yText.isEnabled());
assertFalse(textField.isEnabled());
assertFalse(bean.isBoolValue());
bean.setBoolValue(true);
assertTrue(yText.isEnabled());
assertTrue(textField.isEnabled());
assertTrue(bean.isBoolValue());
}
/**
* Tests rendering issues.
*
* @throws Exception
*/
@Test
// BEGIN SUPRESS CATCH EXCEPTION
public void test_Converter() throws Exception {
// END SUPRESS CATCH EXCEPTION
// build the view model
// ...> yView
// ......> yGridLayout
// .........> yText
YView yView = factory.createView();
YGridLayout yGridlayout = factory.createGridLayout();
yView.setContent(yGridlayout);
YNumericField yDecimal = factory.createNumericField();
YDelegateConverter yConverter = CoreModelFactory.eINSTANCE
.createYDelegateConverter();
yConverter.setConverterId("text.test.numeric");
yDecimal.setConverter(yConverter);
yGridlayout.getElements().add(yDecimal);
DelegatingConverterFactory.getInstance().addDelegate(
new ConverterFactory());
VaadinRenderer renderer = new VaadinRenderer();
IViewContext viewContext = renderer.render(rootLayout, yView, null);
INumericFieldEditpart textEditpart = DelegatingEditPartManager
.getInstance().getEditpart(viewContext, yDecimal);
IWidgetPresentation<Component> presentation = textEditpart
.getPresentation();
NumericField text = (NumericField) presentation.getWidget();
yDecimal.setValue(123);
assertEquals(123, yDecimal.getValue(), 0);
assertEquals("123 EUR", text.getValue());
text.setValue("321 EUR");
assertEquals(321, yDecimal.getValue(), 0);
assertEquals("321 EUR", text.getValue());
}
@Test
// BEGIN SUPRESS CATCH EXCEPTION
public void test_ConverterAndDatatype() throws Exception {
// END SUPRESS CATCH EXCEPTION
// build the view model
// ...> yView
// ......> yGridLayout
// .........> yText
YView yView = factory.createView();
YGridLayout yGridlayout = factory.createGridLayout();
yView.setContent(yGridlayout);
YNumericField yDecimal = factory.createNumericField();
YDelegateConverter yConverter = CoreModelFactory.eINSTANCE
.createYDelegateConverter();
yConverter.setConverterId("text.test");
yDecimal.setConverter(yConverter);
YNumericDatatype dt = factory.createNumericDatatype();
dt.setGrouping(true);
yDecimal.setDatatype(dt);
yGridlayout.getElements().add(yDecimal);
DelegatingConverterFactory.getInstance().addDelegate(
new ConverterFactory());
VaadinRenderer renderer = new VaadinRenderer();
IViewContext viewContext = renderer.render(rootLayout, yView, null);
INumericFieldEditpart textEditpart = DelegatingEditPartManager
.getInstance().getEditpart(viewContext, yDecimal);
IWidgetPresentation<Component> presentation = textEditpart
.getPresentation();
NumericField text = (NumericField) presentation.getWidget();
yDecimal.setValue(1234);
assertEquals(1234, yDecimal.getValue(), 0);
assertEquals("1,234 EUR", text.getValue());
text.setValue("3,210 EUR");
assertEquals(3210, yDecimal.getValue(), 0);
assertEquals("3,210 EUR", text.getValue());
dt.setGrouping(false);
yDecimal.setValue(1234);
assertEquals(1234, yDecimal.getValue(), 0);
assertEquals("1234 EUR", text.getValue());
dt.setGrouping(true);
yDecimal.setValue(4321);
assertEquals(4321, yDecimal.getValue(), 0);
assertEquals("4,321 EUR", text.getValue());
}
private static class ConverterFactory implements IConverterFactory {
@Override
public boolean isFor(IViewContext uiContext, IConverterEditpart editpart) {
return true;
}
@Override
public Object createConverter(IViewContext uiContext,
IConverterEditpart editpart) throws IllegalArgumentException {
return new Converter();
}
}
/**
*/
@SuppressWarnings("serial")
private static class Converter extends NumberConverter {
@Override
protected NumberFormat getFormat(Locale locale) {
DecimalFormat result = new DecimalFormat(
super.getNumberFormatPattern() + " EUR",
DecimalFormatSymbols.getInstance(Locale.US));
result.setGroupingUsed(super.isUseGrouping());
return result;
}
}
}