Search in sources :

Example 16 with Observer

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);
}
Also used : ContentValues(android.content.ContentValues) ContentQueryMap(android.content.ContentQueryMap) Observer(java.util.Observer) Handler(android.os.Handler) Cursor(android.database.Cursor) Observable(java.util.Observable) ContentResolver(android.content.ContentResolver) MediumTest(android.test.suitebuilder.annotation.MediumTest)

Example 17 with Observer

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);
}
Also used : ContentValues(android.content.ContentValues) ContentQueryMap(android.content.ContentQueryMap) Observer(java.util.Observer) Handler(android.os.Handler) Cursor(android.database.Cursor) Observable(java.util.Observable) ContentResolver(android.content.ContentResolver) MediumTest(android.test.suitebuilder.annotation.MediumTest)

Example 18 with Observer

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);
}
Also used : ContentValues(android.content.ContentValues) ContentQueryMap(android.content.ContentQueryMap) Observer(java.util.Observer) Handler(android.os.Handler) Cursor(android.database.Cursor) Observable(java.util.Observable) ContentResolver(android.content.ContentResolver) MediumTest(android.test.suitebuilder.annotation.MediumTest)

Example 19 with Observer

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();
        }
    });
}
Also used : IAction(org.eclipse.jface.action.IAction) Action(org.eclipse.jface.action.Action) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) Observable(java.util.Observable) AlignmentService(eu.esdihumboldt.hale.ui.service.align.AlignmentService) Observer(java.util.Observer) InstanceSampleService(eu.esdihumboldt.hale.ui.service.instance.sample.InstanceSampleService) Collection(java.util.Collection) AlignmentServiceAdapter(eu.esdihumboldt.hale.ui.service.align.AlignmentServiceAdapter) Map(java.util.Map) Cell(eu.esdihumboldt.hale.common.align.model.Cell) IActionBars(org.eclipse.ui.IActionBars) Display(org.eclipse.swt.widgets.Display) Pair(eu.esdihumboldt.util.Pair) HashSet(java.util.HashSet)

Example 20 with Observer

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());
}
Also used : Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) Observable(java.util.Observable) ServiceException(org.osgi.framework.ServiceException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Observer(java.util.Observer)

Aggregations

Observer (java.util.Observer)36 Observable (java.util.Observable)29 Test (org.junit.Test)12 ContentQueryMap (android.content.ContentQueryMap)6 ContentResolver (android.content.ContentResolver)6 ContentValues (android.content.ContentValues)6 Cursor (android.database.Cursor)6 Handler (android.os.Handler)6 MediumTest (android.test.suitebuilder.annotation.MediumTest)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 Map (java.util.Map)2 Properties (java.util.Properties)2 IJobChangeEvent (org.eclipse.core.runtime.jobs.IJobChangeEvent)2 IJobChangeListener (org.eclipse.core.runtime.jobs.IJobChangeListener)2 Job (org.eclipse.core.runtime.jobs.Job)2 ITestActor (org.getopentest.contracts.ITestActor)2 IDataObject (org.jcryptool.core.operations.dataobject.IDataObject)2 KeyStoreAlias (org.jcryptool.crypto.keystore.backend.KeyStoreAlias)2 ParameterizedConstructorInstantiator (org.mockito.internal.util.reflection.FieldInitializer.ParameterizedConstructorInstantiator)2 CvsBundle (com.intellij.CvsBundle)1