use of com.codename1.rad.models.BaseEntity in project CodeRAD by shannah.
the class EntityListTest method testGenericList.
private void testGenericList() throws Exception {
EntityList list = new EntityList();
assertEqual(0, list.size());
list.add(new BaseEntity());
list.add(new BaseEntity());
list.add(new BaseEntity());
assertEqual(3, list.size());
Entity firstEntity = list.get(0);
assertNotNull(firstEntity);
list.remove(firstEntity);
assertEqual(2, list.size());
class Stats {
int counter = 0;
Entity lastAdd;
Entity lastRemove;
int added;
int removed;
}
Stats stats = new Stats();
Observer observer = (o, arg) -> {
stats.counter++;
};
list.addObserver(observer);
list.notifyObservers();
assertEqual(stats.counter, 1);
list.notifyObservers();
assertEqual(1, stats.counter);
list.add(new BaseEntity());
assertEqual(1, stats.counter);
list.notifyObservers();
assertEqual(2, stats.counter);
list.addActionListener(evt -> {
if (evt instanceof EntityAddedEvent) {
EntityAddedEvent eae = (EntityAddedEvent) evt;
stats.added++;
stats.lastAdd = eae.getEntity();
} else if (evt instanceof EntityRemovedEvent) {
EntityRemovedEvent ere = (EntityRemovedEvent) evt;
stats.removed++;
stats.lastRemove = ere.getEntity();
}
});
list.add(firstEntity);
assertEqual(1, stats.added);
assertEqual(firstEntity, stats.lastAdd);
list.remove(firstEntity);
assertEqual(1, stats.removed);
assertEqual(firstEntity, stats.lastRemove);
}
use of com.codename1.rad.models.BaseEntity in project CodeRAD by shannah.
the class EntityListViewTest method testSortedListTransactions.
private void testSortedListTransactions() {
SortedList<Entity> internal = new SortedList<Entity>(new Comparator<Entity>() {
@Override
public int compare(Entity a, Entity b) {
String name1 = a.getText(Thing.name);
if (name1 == null)
name1 = "";
String name2 = b.getText(Thing.name);
if (name2 == null)
name2 = "";
return name1.compareTo(name2);
}
});
EntityList el = new EntityList() {
@Override
protected List createInternalList() {
return internal;
}
};
class Person extends BaseEntity {
}
entityTypeBuilder(Person.class).string(Thing.name).factory(cls -> {
return new Person();
}).build();
Person a = new Person();
a.set(Thing.name, "A");
Person b = new Person();
b.set(Thing.name, "B");
Person c = new Person();
c.set(Thing.name, "C");
Person d = new Person();
d.set(Thing.name, "D");
ProfileListView view = new ProfileListView(el);
view.bind();
assertEqual(0, view.getScrollWrapper().getComponentCount());
el.add(b);
assertEqual(1, view.getScrollWrapper().getComponentCount());
assertNotNull(view.getRowViewForEntity(b));
assertNull(view.getRowViewForEntity(a));
el.add(a);
assertEqual(0, el.indexOf(a));
assertEqual(1, el.indexOf(b));
assertEqual(2, view.getScrollWrapper().getComponentCount());
assertEqual(0, view.getScrollWrapper().getComponentIndex((Component) view.getRowViewForEntity(a)));
assertEqual(1, view.getScrollWrapper().getComponentIndex((Component) view.getRowViewForEntity(b)));
view.unbind();
}
Aggregations