blob: fba1d32ea9d56d718574f52c8a47a8830ee164ce [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.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import javax.print.attribute.standard.DateTimeAtProcessing;
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.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.YElement;
import org.eclipse.osbp.ecview.core.common.model.core.YView;
import org.eclipse.osbp.ecview.core.common.model.datatypes.DatatypesFactory;
import org.eclipse.osbp.ecview.core.common.presentation.IWidgetPresentation;
import org.eclipse.osbp.ecview.core.extension.model.datatypes.ExtDatatypesFactory;
import org.eclipse.osbp.ecview.core.extension.model.datatypes.YDateTimeDatatype;
import org.eclipse.osbp.ecview.core.extension.model.datatypes.YDateTimeFormat;
import org.eclipse.osbp.ecview.core.extension.model.datatypes.YDateTimeResolution;
import org.eclipse.osbp.ecview.core.extension.model.extension.ExtensionModelFactory;
import org.eclipse.osbp.ecview.core.extension.model.extension.YDateTime;
import org.eclipse.osbp.ecview.core.extension.model.extension.YGridLayout;
import org.eclipse.osbp.ecview.core.extension.model.extension.util.SimpleExtensionModelFactory;
import org.eclipse.osbp.ecview.core.ui.core.editparts.extension.IDateTimeEditpart;
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.AbstractVaadinWidgetPresenter;
import org.eclipse.osbp.runtime.web.ecview.presentation.vaadin.internal.DateTimePresentation;
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.osgi.framework.BundleException;
import org.osgi.service.cm.ConfigurationException;
import com.vaadin.shared.ui.datefield.Resolution;
import com.vaadin.ui.Component;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.DateField;
import com.vaadin.ui.UI;
/**
* Tests the {@link TextFieldPresentation}.
*/
@SuppressWarnings("restriction")
public class DateTimePresentationTests {
private SimpleExtensionModelFactory factory = new SimpleExtensionModelFactory();
private CssLayout rootLayout = new CssLayout();
/**
* Setup tests.
*
* @throws ConfigurationException
* @throws BundleException
*/
@Before
public void setup() throws ConfigurationException, BundleException {
UI.setCurrent(new DefaultUI());
UI.getCurrent().setContent(rootLayout);
}
/**
* 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);
YDateTime yText = factory.createDateTime();
yGridlayout.getElements().add(yText);
VaadinRenderer renderer = new VaadinRenderer();
IViewContext viewContext = renderer.render(rootLayout, yView, null);
IDateTimeEditpart 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();
YDateTime yText = factory.createDateTime();
yView.setContent(yText);
VaadinRenderer renderer = new VaadinRenderer();
IViewContext viewContext = renderer.render(rootLayout, yView, null);
IDateTimeEditpart textEditpart = DelegatingEditPartManager
.getInstance().getEditpart(viewContext, yText);
IWidgetPresentation<Component> presentation = textEditpart
.getPresentation();
DateField text = (DateField) 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);
YDateTime yText1 = factory.createDateTime();
yText1.setCssID("ID_0815");
yText1.setCssClass("anyOtherClass");
yLayout.getElements().add(yText1);
YDateTime yText2 = factory.createDateTime();
yLayout.getElements().add(yText2);
VaadinRenderer renderer = new VaadinRenderer();
IViewContext viewContext = renderer.render(rootLayout, yView, null);
IDateTimeEditpart text1Editpart = DelegatingEditPartManager
.getInstance().getEditpart(viewContext, yText1);
IDateTimeEditpart text2Editpart = DelegatingEditPartManager
.getInstance().getEditpart(viewContext, yText2);
IWidgetPresentation<Component> text1Presentation = text1Editpart
.getPresentation();
IWidgetPresentation<Component> text2Presentation = text2Editpart
.getPresentation();
DateField text1 = (DateField) text1Presentation.getWidget();
DateField text2 = (DateField) 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);
YDateTime yText1 = factory.createDateTime();
yLayout.getElements().add(yText1);
YDateTime yText2 = factory.createDateTime();
yLayout.getElements().add(yText2);
VaadinRenderer renderer = new VaadinRenderer();
IViewContext viewContext = renderer.render(rootLayout, yView, null);
IDateTimeEditpart text1Editpart = DelegatingEditPartManager
.getInstance().getEditpart(viewContext, yText1);
IDateTimeEditpart text2Editpart = DelegatingEditPartManager
.getInstance().getEditpart(viewContext, yText2);
IWidgetPresentation<Component> text1Presentation = text1Editpart
.getPresentation();
IWidgetPresentation<Component> text2Presentation = text2Editpart
.getPresentation();
DateField text1 = (DateField) text1Presentation.getWidget();
DateField text2 = (DateField) 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());
Date date = new Date();
yText1.setValue(date);
assertEquals(date, text1.getValue());
date = new Date();
text1.setValue(date);
assertEquals(date, 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);
YDateTime yText1 = factory.createDateTime();
yLayout.getElements().add(yText1);
VaadinRenderer renderer = new VaadinRenderer();
IViewContext viewContext = renderer.render(rootLayout, yView, null);
IDateTimeEditpart text1Editpart = DelegatingEditPartManager
.getInstance().getEditpart(viewContext, yText1);
IWidgetPresentation<Component> text1Presentation = text1Editpart
.getPresentation();
DateField text1 = (DateField) text1Presentation.getWidget();
// start tests
//
YBindingSet yBindingSet = yView.getOrCreateBindingSet();
YBeanValueBindingEndpoint beanBinding = factory
.createBeanBindingEndpoint();
ValueBean bean = new ValueBean(new Date());
beanBinding.setPropertyPath("dateValue");
beanBinding.setBean(bean);
yBindingSet.addBinding(yText1.createValueEndpoint(), beanBinding);
bean.setDateValue(new Date(100000));
assertEquals(bean.getDateValue(), text1.getValue());
assertEquals(bean.getDateValue(), yText1.getValue());
bean.setDateValue(new Date(100001));
assertEquals(bean.getDateValue(), text1.getValue());
assertEquals(bean.getDateValue().getTime(), yText1.getValue().getTime());
Date date = new Date(100002);
text1.setValue(date);
assertEquals(date, bean.getDateValue());
assertEquals(date, yText1.getValue());
date = new Date(100003);
yText1.setValue(date);
assertEquals(date, bean.getDateValue());
assertEquals(date, text1.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);
YDateTime yText = factory.createDateTime();
yGridlayout.getElements().add(yText);
VaadinRenderer renderer = new VaadinRenderer();
IViewContext viewContext = renderer.render(rootLayout, yView, null);
IDateTimeEditpart 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 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);
YDateTime yText = factory.createDateTime();
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);
IDateTimeEditpart editpart = DelegatingEditPartManager.getInstance()
.getEditpart(viewContext, yText);
DateTimePresentation presentation = editpart.getPresentation();
assertEquals("Alter", presentation.getWidget().getCaption());
viewContext.setLocale(Locale.ENGLISH);
assertEquals("Age", presentation.getWidget().getCaption());
}
@Test
public void test_Format() throws ContextException {
// switch the global locale to german
Locale.setDefault(Locale.GERMAN);
YView yView = factory.createView();
YGridLayout yGridlayout = factory.createGridLayout();
yView.setContent(yGridlayout);
YDateTime yText = factory.createDateTime();
YDateTimeDatatype yDt = ExtDatatypesFactory.eINSTANCE.createYDateTimeDatatype();
// a not allowed setting
yDt.setFormat(YDateTimeFormat.DATE);
yDt.setResolution(YDateTimeResolution.SECOND);
yText.setDatatype(yDt);
yGridlayout.getElements().add(yText);
// 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);
IDateTimeEditpart editpart = DelegatingEditPartManager.getInstance()
.getEditpart(viewContext, yText);
DateTimePresentation presentation = editpart.getPresentation();
DateField widget = (DateField) presentation.getWidget();
assertEquals("dd.MM.yyyy", widget.getDateFormat());
assertEquals(Resolution.DAY, widget.getResolution());
}
@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);
YDateTime yText = factory.createDateTime();
yLayout.getElements().add(yText);
VaadinRenderer renderer = new VaadinRenderer();
IViewContext viewContext = renderer.render(rootLayout, yView, null);
IDateTimeEditpart editpart = DelegatingEditPartManager.getInstance()
.getEditpart(viewContext, yText);
IWidgetPresentation<Component> presentation = editpart
.getPresentation();
DateField datetime = (DateField) 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(!datetime.isReadOnly());
assertFalse(bean.isBoolValue());
bean.setBoolValue(true);
assertTrue(yText.isEditable());
assertTrue(!datetime.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);
YDateTime yText = factory.createDateTime();
yLayout.getElements().add(yText);
VaadinRenderer renderer = new VaadinRenderer();
IViewContext viewContext = renderer.render(rootLayout, yView, null);
IDateTimeEditpart editpart = DelegatingEditPartManager.getInstance()
.getEditpart(viewContext, yText);
IWidgetPresentation<Component> presentation = editpart
.getPresentation();
DateField datetime = (DateField) 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(datetime.isVisible());
assertFalse(bean.isBoolValue());
bean.setBoolValue(true);
assertTrue(yText.isVisible());
assertTrue(datetime.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);
YDateTime yText = factory.createDateTime();
yLayout.getElements().add(yText);
VaadinRenderer renderer = new VaadinRenderer();
IViewContext viewContext = renderer.render(rootLayout, yView, null);
IDateTimeEditpart editpart = DelegatingEditPartManager.getInstance()
.getEditpart(viewContext, yText);
IWidgetPresentation<Component> presentation = editpart
.getPresentation();
DateField datetime = (DateField) 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(datetime.isEnabled());
assertFalse(bean.isBoolValue());
bean.setBoolValue(true);
assertTrue(yText.isEnabled());
assertTrue(datetime.isEnabled());
assertTrue(bean.isBoolValue());
}
}