Search in sources :

Example 1 with DateField

use of com.haulmont.cuba.gui.components.DateField in project cuba by cuba-platform.

the class DateFieldDsTest method testValueChangeListener.

@Test
public void testValueChangeListener() {
    DateField dateField = (DateField) factory.createComponent(DateField.NAME);
    Datasource<User> userDs = getTestUserDatasource();
    User user = userDs.getItem();
    user.setUpdateTs(new Date());
    // listener before datasource
    Boolean[] valueWasChanged = { false };
    Component.ValueChangeListener listener = e -> valueWasChanged[0] = true;
    dateField.addValueChangeListener(listener);
    dateField.setDatasource(userDs, "updateTs");
    assertEquals(true, valueWasChanged[0]);
    // reset state
    dateField.setDatasource(null, null);
    dateField.removeValueChangeListener(listener);
    valueWasChanged[0] = false;
    // datasource before listener
    dateField.setDatasource(userDs, "updateTs");
    dateField.addValueChangeListener(listener);
    user.setUpdateTs(new Date());
    assertEquals(true, valueWasChanged[0]);
}
Also used : DateField(com.haulmont.cuba.gui.components.DateField) Datasource(com.haulmont.cuba.gui.data.Datasource) Ignore(org.junit.Ignore) Date(java.util.Date) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Component(com.haulmont.cuba.gui.components.Component) User(com.haulmont.cuba.security.entity.User) Assert.assertEquals(org.junit.Assert.assertEquals) User(com.haulmont.cuba.security.entity.User) DateField(com.haulmont.cuba.gui.components.DateField) Component(com.haulmont.cuba.gui.components.Component) Date(java.util.Date) Test(org.junit.Test)

Example 2 with DateField

use of com.haulmont.cuba.gui.components.DateField in project cuba by cuba-platform.

the class DateFieldDsTest method testUnsubscribeSubscribeDsListener.

@Test
public void testUnsubscribeSubscribeDsListener() {
    DateField dateField = (DateField) factory.createComponent(DateField.NAME);
    Datasource<User> userDs = getTestUserDatasource();
    User user = userDs.getItem();
    Date updateTs = new Date();
    user.setUpdateTs(updateTs);
    dateField.setDatasource(userDs, "updateTs");
    // unbind
    dateField.setDatasource(null, null);
    // setup
    boolean[] valueWasChanged = { false };
    Datasource.ItemPropertyChangeListener<User> listener = e -> valueWasChanged[0] = true;
    userDs.addItemPropertyChangeListener(listener);
    dateField.setDatasource(userDs, "updateTs");
    dateField.setResolution(DateField.Resolution.SEC);
    Date updateTs2 = new Date();
    dateField.setValue(updateTs2);
    assertEquals(true, valueWasChanged[0]);
    assertEquals(updateTs2, user.getUpdateTs());
}
Also used : Datasource(com.haulmont.cuba.gui.data.Datasource) DateField(com.haulmont.cuba.gui.components.DateField) Datasource(com.haulmont.cuba.gui.data.Datasource) Ignore(org.junit.Ignore) Date(java.util.Date) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Component(com.haulmont.cuba.gui.components.Component) User(com.haulmont.cuba.security.entity.User) Assert.assertEquals(org.junit.Assert.assertEquals) User(com.haulmont.cuba.security.entity.User) DateField(com.haulmont.cuba.gui.components.DateField) Date(java.util.Date) Test(org.junit.Test)

Example 3 with DateField

use of com.haulmont.cuba.gui.components.DateField in project cuba by cuba-platform.

the class DateFieldDsTest method testUnsubscribeSubscribeComponentListener.

@Test
public void testUnsubscribeSubscribeComponentListener() {
    DateField dateField = (DateField) factory.createComponent(DateField.NAME);
    Datasource<User> userDs = getTestUserDatasource();
    User user = userDs.getItem();
    Date updateTs = new Date();
    user.setUpdateTs(updateTs);
    dateField.setDatasource(userDs, "updateTs");
    // unbind
    dateField.setDatasource(null, null);
    // datasource before listener
    dateField.setDatasource(userDs, "updateTs");
    dateField.setResolution(DateField.Resolution.SEC);
    assertEquals(updateTs, dateField.getValue());
    boolean[] valueWasChanged = { false };
    Component.ValueChangeListener listener = e -> valueWasChanged[0] = true;
    dateField.addValueChangeListener(listener);
    Date updateTs2 = new Date();
    user.setUpdateTs(updateTs2);
    assertEquals(true, valueWasChanged[0]);
    assertEquals(updateTs2, dateField.getValue());
    // reset state
    dateField.removeValueChangeListener(listener);
    dateField.setDatasource(null, null);
    valueWasChanged[0] = false;
    dateField.setValue(updateTs);
    // listener before datasource
    dateField.addValueChangeListener(listener);
    dateField.setDatasource(userDs, "updateTs");
    dateField.setResolution(DateField.Resolution.SEC);
    assertEquals(true, valueWasChanged[0]);
    assertEquals(updateTs2, dateField.getValue());
}
Also used : DateField(com.haulmont.cuba.gui.components.DateField) Datasource(com.haulmont.cuba.gui.data.Datasource) Ignore(org.junit.Ignore) Date(java.util.Date) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Component(com.haulmont.cuba.gui.components.Component) User(com.haulmont.cuba.security.entity.User) Assert.assertEquals(org.junit.Assert.assertEquals) User(com.haulmont.cuba.security.entity.User) DateField(com.haulmont.cuba.gui.components.DateField) Component(com.haulmont.cuba.gui.components.Component) Date(java.util.Date) Test(org.junit.Test)

Example 4 with DateField

use of com.haulmont.cuba.gui.components.DateField in project cuba by cuba-platform.

the class DateFieldDsTest method testUnsubscribeComponentListener.

@Test
public void testUnsubscribeComponentListener() {
    DateField dateField = (DateField) factory.createComponent(DateField.NAME);
    Datasource<User> userDs = getTestUserDatasource();
    User user = userDs.getItem();
    Date updateTs = new Date();
    user.setUpdateTs(updateTs);
    dateField.setDatasource(userDs, "updateTs");
    dateField.setResolution(DateField.Resolution.SEC);
    // unbind
    dateField.setDatasource(null, null);
    Component.ValueChangeListener valueChangeListener = e -> {
        throw new RuntimeException("Value was changed externally");
    };
    dateField.addValueChangeListener(valueChangeListener);
    user.setUpdateTs(new Date());
    assertEquals(updateTs, dateField.getValue());
}
Also used : DateField(com.haulmont.cuba.gui.components.DateField) Datasource(com.haulmont.cuba.gui.data.Datasource) Ignore(org.junit.Ignore) Date(java.util.Date) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Component(com.haulmont.cuba.gui.components.Component) User(com.haulmont.cuba.security.entity.User) Assert.assertEquals(org.junit.Assert.assertEquals) User(com.haulmont.cuba.security.entity.User) DateField(com.haulmont.cuba.gui.components.DateField) Component(com.haulmont.cuba.gui.components.Component) Date(java.util.Date) Test(org.junit.Test)

Example 5 with DateField

use of com.haulmont.cuba.gui.components.DateField in project cuba by cuba-platform.

the class DateFieldDsTest method testDatasourceRepeatableAssign.

@Test
public void testDatasourceRepeatableAssign() {
    DateField dateField = (DateField) factory.createComponent(DateField.NAME);
    dateField.setDatasource(null, null);
    dateField.setDatasource(null, null);
    Datasource<User> userDs1 = getTestUserDatasource();
    dateField.setDatasource(userDs1, "updateTs");
    dateField.setDatasource(userDs1, "updateTs");
    boolean exceptionWasThrown = false;
    try {
        dateField.setDatasource(userDs1, null);
    } catch (Exception e) {
        exceptionWasThrown = true;
    }
    assertTrue(exceptionWasThrown);
    exceptionWasThrown = false;
    try {
        dateField.setDatasource(null, "updateTs");
    } catch (Exception e) {
        exceptionWasThrown = true;
    }
    assertTrue(exceptionWasThrown);
    Date updateTs = new Date();
    userDs1.getItem().setUpdateTs(updateTs);
    dateField.setDatasource(userDs1, "updateTs");
    Datasource<User> userDs2 = getTestUserDatasource();
    dateField.setDatasource(userDs2, "updateTs");
    dateField.setValue(new Date());
    assertEquals(updateTs, userDs1.getItem().getUpdateTs());
}
Also used : User(com.haulmont.cuba.security.entity.User) DateField(com.haulmont.cuba.gui.components.DateField) Date(java.util.Date) Test(org.junit.Test)

Aggregations

DateField (com.haulmont.cuba.gui.components.DateField)6 User (com.haulmont.cuba.security.entity.User)6 Date (java.util.Date)6 Test (org.junit.Test)6 Component (com.haulmont.cuba.gui.components.Component)5 Datasource (com.haulmont.cuba.gui.data.Datasource)5 Assert.assertEquals (org.junit.Assert.assertEquals)5 Assert.assertTrue (org.junit.Assert.assertTrue)5 Ignore (org.junit.Ignore)5