001: /*******************************************************************************
002: * Copyright (c) 2006 Brad Reynolds 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: * Brad Reynolds - 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.Arrays;
015: import java.util.List;
016:
017: import junit.framework.Test;
018: import junit.framework.TestCase;
019:
020: import org.eclipse.core.databinding.observable.IObservable;
021: import org.eclipse.core.databinding.observable.IObservableCollection;
022: import org.eclipse.core.databinding.observable.ObservableTracker;
023: import org.eclipse.core.databinding.observable.Realm;
024: import org.eclipse.core.databinding.observable.list.AbstractObservableList;
025: import org.eclipse.core.databinding.observable.list.ListDiff;
026: import org.eclipse.jface.conformance.databinding.AbstractObservableCollectionContractDelegate;
027: import org.eclipse.jface.conformance.databinding.ObservableListContractTest;
028: import org.eclipse.jface.conformance.databinding.SuiteBuilder;
029: import org.eclipse.jface.tests.databinding.RealmTester;
030: import org.eclipse.jface.tests.databinding.RealmTester.CurrentRealm;
031:
032: /**
033: * @since 3.2
034: */
035: public class AbstractObservableListTest extends TestCase {
036: private AbstractObservableListStub list;
037:
038: protected void setUp() throws Exception {
039: RealmTester.setDefault(new CurrentRealm(true));
040: list = new AbstractObservableListStub();
041: }
042:
043: protected void tearDown() throws Exception {
044: RealmTester.setDefault(null);
045: }
046:
047: public void testFireChangeRealmChecks() throws Exception {
048: RealmTester.exerciseCurrent(new Runnable() {
049: public void run() {
050: list.fireChange();
051: }
052: });
053: }
054:
055: public void testFireStaleRealmChecks() throws Exception {
056: RealmTester.exerciseCurrent(new Runnable() {
057: public void run() {
058: list.fireStale();
059: }
060: });
061: }
062:
063: public void testFireListChangeRealmChecks() throws Exception {
064: RealmTester.exerciseCurrent(new Runnable() {
065: public void run() {
066: list.fireListChange(null);
067: }
068: });
069: }
070:
071: public static Test suite() {
072: return new SuiteBuilder().addTests(
073: AbstractObservableListTest.class)
074: .addObservableContractTest(
075: ObservableListContractTest.class,
076: new Delegate()).build();
077: }
078:
079: /* package */static class Delegate extends
080: AbstractObservableCollectionContractDelegate {
081:
082: public IObservableCollection createObservableCollection(
083: Realm realm, final int itemCount) {
084:
085: String[] items = new String[itemCount];
086: for (int i = 0; i < itemCount; i++) {
087: items[i] = String.valueOf(i);
088: }
089:
090: AbstractObservableListStub observable = new AbstractObservableListStub(
091: realm, Arrays.asList(items));
092: observable.elementType = String.class;
093: return observable;
094: }
095:
096: public Object getElementType(IObservableCollection collection) {
097: return String.class;
098: }
099:
100: public void change(IObservable observable) {
101: ((AbstractObservableListStub) observable).fireChange();
102: }
103: }
104:
105: static class AbstractObservableListStub extends
106: AbstractObservableList {
107: Object elementType;
108:
109: List wrappedList;
110:
111: public AbstractObservableListStub() {
112: super ();
113: wrappedList = new ArrayList();
114: }
115:
116: public AbstractObservableListStub(Realm realm, List list) {
117: super (realm);
118: this .wrappedList = list;
119: }
120:
121: protected int doGetSize() {
122: return wrappedList.size();
123: }
124:
125: public Object get(int index) {
126: ObservableTracker.getterCalled(this );
127: return wrappedList.get(index);
128: }
129:
130: public Object getElementType() {
131: return elementType;
132: }
133:
134: protected void fireChange() {
135: super .fireChange();
136: }
137:
138: protected void fireStale() {
139: super .fireStale();
140: }
141:
142: protected void fireListChange(ListDiff diff) {
143: super.fireListChange(diff);
144: }
145: }
146: }
|