Search in sources :

Example 6 with Datasource

use of com.haulmont.cuba.gui.data.Datasource in project cuba by cuba-platform.

the class LookupPickerFieldTest method testValueChangeListener.

@Test
public void testValueChangeListener() {
    LookupPickerField component = factory.createComponent(LookupPickerField.class);
    final AtomicInteger counter = new AtomicInteger(0);
    // noinspection unchecked
    Datasource<User> testDs = new DsBuilder().setId("testDs").setJavaClass(User.class).setView(viewRepository.getView(User.class, View.LOCAL)).buildDatasource();
    testDs.setItem(new User());
    ((DatasourceImpl) testDs).valid();
    assertNull(component.getValue());
    final Group g = new Group();
    testDs.getItem().setGroup(g);
    // noinspection unchecked
    CollectionDatasource<Group, UUID> groupsDs = new DsBuilder().setId("testDs").setJavaClass(Group.class).setView(viewRepository.getView(Group.class, View.LOCAL)).setRefreshMode(CollectionDatasource.RefreshMode.NEVER).setAllowCommit(false).buildCollectionDatasource();
    groupsDs.includeItem(g);
    Group g1 = new Group();
    groupsDs.includeItem(g1);
    Group g2 = new Group();
    groupsDs.includeItem(g2);
    component.setOptionsDatasource(groupsDs);
    Component.ValueChangeListener listener1 = e -> {
        assertNull(e.getPrevValue());
        assertEquals(g2, e.getValue());
        counter.addAndGet(1);
    };
    component.addValueChangeListener(listener1);
    component.setValue(g2);
    component.removeValueChangeListener(listener1);
    assertEquals(1, counter.get());
    Component.ValueChangeListener listener2 = e -> {
        assertEquals(g2, e.getPrevValue());
        assertEquals(g, e.getValue());
        counter.addAndGet(1);
    };
    component.addValueChangeListener(listener2);
    component.setDatasource(testDs, "group");
    assertEquals(g, component.getValue());
    assertEquals(2, counter.get());
    component.removeValueChangeListener(listener2);
    component.setValue(g1);
    assertEquals(g1, testDs.getItem().getGroup());
    assertEquals(2, counter.get());
    Component.ValueChangeListener listener3 = e -> {
        assertEquals(g1, e.getPrevValue());
        assertEquals(g2, e.getValue());
        counter.addAndGet(1);
    };
    component.addValueChangeListener(listener3);
    testDs.getItem().setGroup(g2);
    assertEquals(g2, component.getValue());
    assertEquals(3, counter.get());
}
Also used : PersistenceManagerClient(com.haulmont.cuba.client.sys.PersistenceManagerClient) Arrays(java.util.Arrays) Datasource(com.haulmont.cuba.gui.data.Datasource) NonStrictExpectations(mockit.NonStrictExpectations) DsBuilder(com.haulmont.cuba.gui.data.DsBuilder) Test(org.junit.Test) PersistenceManagerService(com.haulmont.cuba.core.app.PersistenceManagerService) UUID(java.util.UUID) AppBeans(com.haulmont.cuba.core.global.AppBeans) User(com.haulmont.cuba.security.entity.User) ArrayList(java.util.ArrayList) Group(com.haulmont.cuba.security.entity.Group) View(com.haulmont.cuba.core.global.View) Ignore(org.junit.Ignore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DatasourceImpl(com.haulmont.cuba.gui.data.impl.DatasourceImpl) Assert(org.junit.Assert) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) Mocked(mockit.Mocked) Group(com.haulmont.cuba.security.entity.Group) User(com.haulmont.cuba.security.entity.User) DatasourceImpl(com.haulmont.cuba.gui.data.impl.DatasourceImpl) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) UUID(java.util.UUID) DsBuilder(com.haulmont.cuba.gui.data.DsBuilder) Test(org.junit.Test)

Example 7 with Datasource

use of com.haulmont.cuba.gui.data.Datasource in project cuba by cuba-platform.

the class PickerFieldTest method testValueChangeListener.

@Test
public void testValueChangeListener() {
    PickerField component = factory.createComponent(PickerField.class);
    final AtomicInteger counter = new AtomicInteger(0);
    // noinspection unchecked
    Datasource<User> testDs = new DsBuilder().setId("testDs").setJavaClass(User.class).setView(viewRepository.getView(User.class, View.LOCAL)).buildDatasource();
    testDs.setItem(new User());
    ((DatasourceImpl) testDs).valid();
    assertNull(component.getValue());
    final Group g = new Group();
    testDs.getItem().setGroup(g);
    final Group g1 = new Group();
    final Group g2 = new Group();
    Component.ValueChangeListener listener1 = e -> {
        assertNull(e.getPrevValue());
        assertEquals(g2, e.getValue());
        counter.addAndGet(1);
    };
    component.addValueChangeListener(listener1);
    component.setMetaClass(metadata.getClass(Group.class));
    component.setValue(g2);
    component.removeValueChangeListener(listener1);
    assertEquals(1, counter.get());
    Component.ValueChangeListener listener2 = e -> {
        assertEquals(g2, e.getPrevValue());
        assertEquals(g, e.getValue());
        counter.addAndGet(1);
    };
    component.addValueChangeListener(listener2);
    component.setDatasource(testDs, "group");
    assertEquals(g, component.getValue());
    assertEquals(2, counter.get());
    component.removeValueChangeListener(listener2);
    component.setValue(g1);
    assertEquals(g1, testDs.getItem().getGroup());
    assertEquals(2, counter.get());
    Component.ValueChangeListener listener3 = e -> {
        assertEquals(g1, e.getPrevValue());
        assertEquals(g2, e.getValue());
        counter.addAndGet(1);
    };
    component.addValueChangeListener(listener3);
    testDs.getItem().setGroup(g2);
    assertEquals(g2, component.getValue());
    assertEquals(3, counter.get());
    component.removeValueChangeListener(listener3);
    component.setValue(g);
    Group gCopy = (Group) SerializationSupport.deserialize(SerializationSupport.serialize(g));
    Component.ValueChangeListener listener4 = e -> {
        assertEquals(g, e.getPrevValue());
        assertEquals(gCopy, e.getValue());
        counter.addAndGet(1);
    };
    component.addValueChangeListener(listener4);
    component.setValue(gCopy);
    assertEquals(4, counter.get());
    component.removeValueChangeListener(listener4);
    Component.ValueChangeListener listener5 = e -> {
        assertEquals(g, e.getPrevValue());
        assertEquals(gCopy, e.getValue());
        counter.addAndGet(1);
    };
    component.addValueChangeListener(listener5);
    testDs.getItem().setGroup(g);
    assertEquals(5, counter.get());
    component.removeValueChangeListener(listener5);
}
Also used : Group(com.haulmont.cuba.security.entity.Group) View(com.haulmont.cuba.core.global.View) Datasource(com.haulmont.cuba.gui.data.Datasource) NonStrictExpectations(mockit.NonStrictExpectations) Ignore(org.junit.Ignore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DsBuilder(com.haulmont.cuba.gui.data.DsBuilder) DatasourceImpl(com.haulmont.cuba.gui.data.impl.DatasourceImpl) SerializationSupport(com.haulmont.cuba.core.sys.serialization.SerializationSupport) Test(org.junit.Test) Assert(org.junit.Assert) User(com.haulmont.cuba.security.entity.User) Group(com.haulmont.cuba.security.entity.Group) User(com.haulmont.cuba.security.entity.User) DatasourceImpl(com.haulmont.cuba.gui.data.impl.DatasourceImpl) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DsBuilder(com.haulmont.cuba.gui.data.DsBuilder) Test(org.junit.Test)

Example 8 with Datasource

use of com.haulmont.cuba.gui.data.Datasource in project cuba by cuba-platform.

the class TextFieldTest method testValueChangeListener.

@Test
public void testValueChangeListener() {
    TextField component = factory.createComponent(TextField.class);
    AtomicInteger counter = new AtomicInteger(0);
    Component.ValueChangeListener okListener = e -> {
        assertNull(e.getPrevValue());
        assertEquals("OK", e.getValue());
        counter.addAndGet(1);
    };
    component.addValueChangeListener(okListener);
    component.setValue("OK");
    assertEquals(1, counter.get());
    component.removeValueChangeListener(okListener);
    component.setValue("Test");
    assertEquals(1, counter.get());
    // noinspection unchecked
    Datasource<User> testDs = new DsBuilder().setId("testDs").setJavaClass(User.class).setView(viewRepository.getView(User.class, View.LOCAL)).buildDatasource();
    testDs.setItem(new User());
    ((DatasourceImpl) testDs).valid();
    Component.ValueChangeListener dsLoadListener = e -> {
        assertEquals("Test", e.getPrevValue());
        assertNull(e.getValue());
        counter.addAndGet(1);
    };
    component.addValueChangeListener(dsLoadListener);
    component.setDatasource(testDs, "login");
    assertEquals(2, counter.get());
    component.removeValueChangeListener(dsLoadListener);
    Component.ValueChangeListener dsListener = e -> {
        assertNull(e.getPrevValue());
        assertEquals("dsValue", e.getValue());
        counter.addAndGet(1);
    };
    component.addValueChangeListener(dsListener);
    testDs.getItem().setLogin("dsValue");
    assertEquals(3, counter.get());
}
Also used : View(com.haulmont.cuba.core.global.View) Datasource(com.haulmont.cuba.gui.data.Datasource) Ignore(org.junit.Ignore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DsBuilder(com.haulmont.cuba.gui.data.DsBuilder) DatasourceImpl(com.haulmont.cuba.gui.data.impl.DatasourceImpl) Datatypes(com.haulmont.chile.core.datatypes.Datatypes) Test(org.junit.Test) Assert(org.junit.Assert) User(com.haulmont.cuba.security.entity.User) User(com.haulmont.cuba.security.entity.User) DatasourceImpl(com.haulmont.cuba.gui.data.impl.DatasourceImpl) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DsBuilder(com.haulmont.cuba.gui.data.DsBuilder) Test(org.junit.Test)

Example 9 with Datasource

use of com.haulmont.cuba.gui.data.Datasource in project cuba by cuba-platform.

the class CheckBoxDsTest method testUnsubscribeSubscribeComponentListener.

@Test
public void testUnsubscribeSubscribeComponentListener() {
    CheckBox checkBox = (CheckBox) factory.createComponent(CheckBox.NAME);
    Datasource<User> userDs = getTestUserDatasource();
    User user = userDs.getItem();
    user.setActive(true);
    checkBox.setDatasource(userDs, "active");
    // unbind
    checkBox.setDatasource(null, null);
    // datasource before listener
    checkBox.setDatasource(userDs, "active");
    assertEquals(true, checkBox.getValue());
    boolean[] valueWasChanged = { false };
    Component.ValueChangeListener listener = e -> valueWasChanged[0] = true;
    checkBox.addValueChangeListener(listener);
    user.setActive(false);
    assertEquals(true, valueWasChanged[0]);
    assertEquals(false, checkBox.getValue());
    // reset state
    checkBox.removeValueChangeListener(listener);
    checkBox.setDatasource(null, null);
    valueWasChanged[0] = false;
    checkBox.setValue(true);
    // listener before datasource
    checkBox.addValueChangeListener(listener);
    checkBox.setDatasource(userDs, "active");
    assertEquals(true, valueWasChanged[0]);
    assertEquals(false, checkBox.getValue());
}
Also used : Datasource(com.haulmont.cuba.gui.data.Datasource) Ignore(org.junit.Ignore) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) CheckBox(com.haulmont.cuba.gui.components.CheckBox) 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) CheckBox(com.haulmont.cuba.gui.components.CheckBox) Component(com.haulmont.cuba.gui.components.Component) Test(org.junit.Test)

Example 10 with Datasource

use of com.haulmont.cuba.gui.data.Datasource in project cuba by cuba-platform.

the class CheckBoxDsTest method testUnsubscribeDsListener.

@Test
public void testUnsubscribeDsListener() {
    CheckBox checkBox = (CheckBox) factory.createComponent(CheckBox.NAME);
    Datasource<User> userDs = getTestUserDatasource();
    User user = userDs.getItem();
    user.setActive(true);
    checkBox.setDatasource(userDs, "active");
    // unbind
    checkBox.setDatasource(null, null);
    Datasource.ItemPropertyChangeListener<User> propertyChangeListener = e -> {
        throw new RuntimeException("Value was changed externally");
    };
    userDs.addItemPropertyChangeListener(propertyChangeListener);
    checkBox.setValue(false);
    assertEquals(true, user.getActive());
}
Also used : Datasource(com.haulmont.cuba.gui.data.Datasource) Datasource(com.haulmont.cuba.gui.data.Datasource) Ignore(org.junit.Ignore) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) CheckBox(com.haulmont.cuba.gui.components.CheckBox) 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) CheckBox(com.haulmont.cuba.gui.components.CheckBox) Test(org.junit.Test)

Aggregations

Datasource (com.haulmont.cuba.gui.data.Datasource)111 Ignore (org.junit.Ignore)66 Test (org.junit.Test)66 Component (com.haulmont.cuba.gui.components.Component)61 User (com.haulmont.cuba.security.entity.User)56 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)53 Assert.assertTrue (org.junit.Assert.assertTrue)49 Assert.assertEquals (org.junit.Assert.assertEquals)44 Group (com.haulmont.cuba.security.entity.Group)29 UUID (java.util.UUID)24 ArrayList (java.util.ArrayList)23 Assert (org.junit.Assert)22 List (java.util.List)20 DsBuilder (com.haulmont.cuba.gui.data.DsBuilder)13 Assert.assertNotNull (org.junit.Assert.assertNotNull)12 Date (java.util.Date)11 Entity (com.haulmont.cuba.core.entity.Entity)10 Role (com.haulmont.cuba.security.entity.Role)10 MetaClass (com.haulmont.chile.core.model.MetaClass)9 LookupField (com.haulmont.cuba.gui.components.LookupField)9