001: /*******************************************************************************
002: * Copyright (c) 2006, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: * Brad Reynolds - bug 167204
011: *******************************************************************************/package org.eclipse.core.tests.databinding.observable.list;
012:
013: import java.util.ArrayList;
014: import java.util.List;
015:
016: import junit.framework.Test;
017: import junit.framework.TestCase;
018:
019: import org.eclipse.core.databinding.observable.Diffs;
020: import org.eclipse.core.databinding.observable.IObservable;
021: import org.eclipse.core.databinding.observable.IObservableCollection;
022: import org.eclipse.core.databinding.observable.Realm;
023: import org.eclipse.core.databinding.observable.list.ListDiff;
024: import org.eclipse.core.databinding.observable.list.ObservableList;
025: import org.eclipse.jface.conformance.databinding.AbstractObservableCollectionContractDelegate;
026: import org.eclipse.jface.conformance.databinding.ObservableListContractTest;
027: import org.eclipse.jface.conformance.databinding.SuiteBuilder;
028: import org.eclipse.jface.tests.databinding.RealmTester;
029: import org.eclipse.jface.tests.databinding.RealmTester.CurrentRealm;
030:
031: /**
032: * @since 3.2
033: */
034: public class ObservableListTest extends TestCase {
035: private ObservableListStub list;
036:
037: protected void setUp() throws Exception {
038: RealmTester.setDefault(new CurrentRealm(true));
039:
040: list = new ObservableListStub(new ArrayList(0), Object.class);
041: }
042:
043: /*
044: * (non-Javadoc)
045: *
046: * @see junit.framework.TestCase#tearDown()
047: */
048: protected void tearDown() throws Exception {
049: RealmTester.setDefault(null);
050: }
051:
052: public void testIsStaleRealmChecks() throws Exception {
053: RealmTester.exerciseCurrent(new Runnable() {
054: public void run() {
055: list.isStale();
056: }
057: });
058: }
059:
060: public void testSetStaleRealmChecks() throws Exception {
061: RealmTester.exerciseCurrent(new Runnable() {
062: public void run() {
063: list.setStale(false);
064: }
065: });
066: }
067:
068: public static Test suite() {
069: return new SuiteBuilder().addTests(ObservableListTest.class)
070: .addObservableContractTest(
071: ObservableListContractTest.class,
072: new Delegate()).build();
073: }
074:
075: /* package */static class Delegate extends
076: AbstractObservableCollectionContractDelegate {
077: public IObservableCollection createObservableCollection(
078: Realm realm, final int elementCount) {
079: List wrappedList = new ArrayList();
080: for (int i = 0; i < elementCount; i++) {
081: wrappedList.add(String.valueOf(i));
082: }
083:
084: return new ObservableListStub(realm, wrappedList,
085: String.class);
086: }
087:
088: public void change(IObservable observable) {
089: ObservableListStub list = (ObservableListStub) observable;
090: Object element = "element";
091: list.wrappedList.add(element);
092: list.fireListChange(Diffs.createListDiff(Diffs
093: .createListDiffEntry(list.size(), true, element)));
094: }
095:
096: public Object getElementType(IObservableCollection collection) {
097: return String.class;
098: }
099: }
100:
101: /* package */static class ObservableListStub extends
102: ObservableList {
103: List wrappedList;
104:
105: ObservableListStub(Realm realm, List wrappedList,
106: Object elementType) {
107: super (realm, wrappedList, elementType);
108: this .wrappedList = wrappedList;
109: }
110:
111: ObservableListStub(List wrappedList, Object elementType) {
112: super (wrappedList, elementType);
113: this .wrappedList = wrappedList;
114: }
115:
116: protected void fireListChange(ListDiff diff) {
117: super.fireListChange(diff);
118: }
119: }
120: }
|