use of java.util.Observer in project platform_frameworks_base by android.
the class ContentQueryMapTest method testContentQueryMap.
@MediumTest
public void testContentQueryMap() throws Throwable {
LooperThread thread = new LooperThread() {
void go() {
ContentResolver r = getContext().getContentResolver();
Settings.System.putString(r, "test", "Value");
Cursor cursor = r.query(Settings.System.CONTENT_URI, new String[] { Settings.System.NAME, Settings.System.VALUE }, null, null, null);
final ContentQueryMap cqm = new ContentQueryMap(cursor, Settings.System.NAME, true, null);
// Get the current state of the CQM. This forces a requery and means that the
// call to getValues() below won't do a requery().
cqm.getRows();
// The cache won't notice changes until the loop runs.
Settings.System.putString(r, "test", "New Value");
ContentValues v = cqm.getValues("test");
String value = v.getAsString(Settings.System.VALUE);
assertEquals("Value", value);
// Use an Observer to find out when the cache does update.
cqm.addObserver(new Observer() {
public void update(Observable o, Object arg) {
// Should have the new values by now.
ContentValues v = cqm.getValues("test");
String value = v.getAsString(Settings.System.VALUE);
assertEquals("New Value", value);
Looper.myLooper().quit();
cqm.close();
mSuccess = true;
}
});
// Give up after a few seconds, if it doesn't.
new Handler().postDelayed(new Runnable() {
public void run() {
fail("Timed out");
}
}, 5000);
}
};
thread.start();
thread.join();
if (thread.mError != null)
throw thread.mError;
assertTrue(thread.mSuccess);
}
use of java.util.Observer in project android_frameworks_base by AOSPA.
the class ContentQueryMapTest method testContentQueryMap.
@MediumTest
public void testContentQueryMap() throws Throwable {
LooperThread thread = new LooperThread() {
void go() {
ContentResolver r = getContext().getContentResolver();
Settings.System.putString(r, "test", "Value");
Cursor cursor = r.query(Settings.System.CONTENT_URI, new String[] { Settings.System.NAME, Settings.System.VALUE }, null, null, null);
final ContentQueryMap cqm = new ContentQueryMap(cursor, Settings.System.NAME, true, null);
// Get the current state of the CQM. This forces a requery and means that the
// call to getValues() below won't do a requery().
cqm.getRows();
// The cache won't notice changes until the loop runs.
Settings.System.putString(r, "test", "New Value");
ContentValues v = cqm.getValues("test");
String value = v.getAsString(Settings.System.VALUE);
assertEquals("Value", value);
// Use an Observer to find out when the cache does update.
cqm.addObserver(new Observer() {
public void update(Observable o, Object arg) {
// Should have the new values by now.
ContentValues v = cqm.getValues("test");
String value = v.getAsString(Settings.System.VALUE);
assertEquals("New Value", value);
Looper.myLooper().quit();
cqm.close();
mSuccess = true;
}
});
// Give up after a few seconds, if it doesn't.
new Handler().postDelayed(new Runnable() {
public void run() {
fail("Timed out");
}
}, 5000);
}
};
thread.start();
thread.join();
if (thread.mError != null)
throw thread.mError;
assertTrue(thread.mSuccess);
}
use of java.util.Observer in project android_frameworks_base by ResurrectionRemix.
the class ContentQueryMapTest method testContentQueryMap.
@MediumTest
public void testContentQueryMap() throws Throwable {
LooperThread thread = new LooperThread() {
void go() {
ContentResolver r = getContext().getContentResolver();
Settings.System.putString(r, "test", "Value");
Cursor cursor = r.query(Settings.System.CONTENT_URI, new String[] { Settings.System.NAME, Settings.System.VALUE }, null, null, null);
final ContentQueryMap cqm = new ContentQueryMap(cursor, Settings.System.NAME, true, null);
// Get the current state of the CQM. This forces a requery and means that the
// call to getValues() below won't do a requery().
cqm.getRows();
// The cache won't notice changes until the loop runs.
Settings.System.putString(r, "test", "New Value");
ContentValues v = cqm.getValues("test");
String value = v.getAsString(Settings.System.VALUE);
assertEquals("Value", value);
// Use an Observer to find out when the cache does update.
cqm.addObserver(new Observer() {
public void update(Observable o, Object arg) {
// Should have the new values by now.
ContentValues v = cqm.getValues("test");
String value = v.getAsString(Settings.System.VALUE);
assertEquals("New Value", value);
Looper.myLooper().quit();
cqm.close();
mSuccess = true;
}
});
// Give up after a few seconds, if it doesn't.
new Handler().postDelayed(new Runnable() {
public void run() {
fail("Timed out");
}
}, 5000);
}
};
thread.start();
thread.join();
if (thread.mError != null)
throw thread.mError;
assertTrue(thread.mSuccess);
}
use of java.util.Observer in project hale by halestudio.
the class TransformationView method createViewControl.
/**
* @see eu.esdihumboldt.hale.ui.views.mapping.AbstractMappingView#createViewControl(org.eclipse.swt.widgets.Composite)
*/
@Override
public void createViewControl(Composite parent) {
super.createViewControl(parent);
IActionBars bars = getViewSite().getActionBars();
bars.getToolBarManager().add(instanceAction = new Action("Apply sample instances", IAction.AS_CHECK_BOX) {
@Override
public void run() {
update();
}
});
instanceAction.setImageDescriptor(TransformationViewPlugin.getImageDescriptor("icons/samples.gif"));
instanceAction.setChecked(initInstanceAction);
update();
AlignmentService as = PlatformUI.getWorkbench().getService(AlignmentService.class);
as.addListener(alignmentListener = new AlignmentServiceAdapter() {
@Override
public void alignmentCleared() {
update();
}
@Override
public void cellsRemoved(Iterable<Cell> cells) {
update();
}
@Override
public void cellsReplaced(Map<? extends Cell, ? extends Cell> cells) {
update();
}
@Override
public void cellsAdded(Iterable<Cell> cells) {
update();
}
@Override
public void alignmentChanged() {
update();
}
@Override
public void customFunctionsChanged() {
update();
}
@Override
public void cellsPropertyChanged(Iterable<Cell> cells, String propertyName) {
if (Cell.PROPERTY_DISABLE_FOR.equals(propertyName) || Cell.PROPERTY_ENABLE_FOR.equals(propertyName))
// Could add/remove cells from transformation tree
update();
else {
final Display display = PlatformUI.getWorkbench().getDisplay();
display.syncExec(new Runnable() {
@Override
public void run() {
// refresh view
getViewer().refresh();
}
});
}
}
});
final InstanceSampleService iss = PlatformUI.getWorkbench().getService(InstanceSampleService.class);
iss.addObserver(instanceSampleObserver = new Observer() {
@SuppressWarnings("unchecked")
@Override
public void update(Observable o, Object arg) {
if (!instanceAction.isChecked()) {
return;
}
Object input = getViewer().getInput();
Collection<Instance> oldInstances = null;
int sampleCount = 0;
if (input instanceof Pair<?, ?>) {
Object second = ((Pair<?, ?>) input).getSecond();
if (second instanceof Collection<?>) {
oldInstances = (Collection<Instance>) second;
sampleCount = oldInstances.size();
}
}
Collection<Instance> newSamples = iss.getReferenceInstances();
if (sampleCount == newSamples.size()) {
// still to decide if update is necessary
if (sampleCount == 0) {
return;
}
// check if instances equal?
Set<Instance> test = new HashSet<Instance>(oldInstances);
test.removeAll(newSamples);
if (test.isEmpty()) {
return;
}
}
TransformationView.this.update();
}
});
}
use of java.util.Observer in project felix by apache.
the class ServiceRegistryTest method testGetUngetServiceFactory.
public void testGetUngetServiceFactory() throws Exception {
final ServiceRegistry sr = new ServiceRegistry(null, null);
final Bundle regBundle = Mockito.mock(Bundle.class);
final ServiceRegistration<?> reg = sr.registerService(regBundle, new String[] { Observer.class.getName() }, new ServiceFactory<Observer>() {
final class ObserverImpl implements Observer {
private final AtomicInteger counter = new AtomicInteger();
public volatile boolean active = true;
@Override
public void update(Observable o, Object arg) {
counter.incrementAndGet();
if (!active) {
throw new IllegalArgumentException("Iteration:" + counter.get());
}
}
}
@Override
public Observer getService(Bundle bundle, ServiceRegistration<Observer> registration) {
return new ObserverImpl();
}
@Override
public void ungetService(Bundle bundle, ServiceRegistration<Observer> registration, Observer service) {
((ObserverImpl) service).active = false;
}
}, null);
final Bundle clientBundle = Mockito.mock(Bundle.class);
Mockito.when(clientBundle.getBundleId()).thenReturn(42L);
// check simple get/unget
final Object obj = sr.getService(clientBundle, reg.getReference(), false);
assertNotNull(obj);
assertTrue(obj instanceof Observer);
((Observer) obj).update(null, null);
sr.ungetService(clientBundle, reg.getReference(), null);
try {
((Observer) obj).update(null, null);
fail();
} catch (final IllegalArgumentException iae) {
// expected
}
// start three threads
final int MAX_THREADS = 3;
final int MAX_LOOPS = 50000;
final CountDownLatch latch = new CountDownLatch(MAX_THREADS);
final Thread[] threads = new Thread[MAX_THREADS];
final List<Exception> exceptions = Collections.synchronizedList(new ArrayList<Exception>());
for (int i = 0; i < MAX_THREADS; i++) {
threads[i] = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.currentThread().sleep(50);
} catch (InterruptedException e1) {
// ignore
}
for (int i = 0; i < MAX_LOOPS; i++) {
try {
final Object obj = sr.getService(clientBundle, reg.getReference(), false);
((Observer) obj).update(null, null);
sr.ungetService(clientBundle, reg.getReference(), null);
} catch (final Exception e) {
exceptions.add(e);
}
}
latch.countDown();
}
});
}
for (int i = 0; i < MAX_THREADS; i++) {
threads[i].start();
}
latch.await();
List<String> counterValues = new ArrayList<String>();
for (Exception ex : exceptions) {
counterValues.add(ex.getMessage());
}
assertTrue("" + counterValues, exceptions.isEmpty());
}
Aggregations