Search in sources :

Example 31 with Observable

use of java.util.Observable 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 32 with Observable

use of java.util.Observable in project AutoRefactor by JnRouvignac.

the class SetRatherThanListSample method refactorWithMethod.

public boolean refactorWithMethod() {
    // Keep this comment
    java.util.HashSet<Observable> collection = new java.util.HashSet<Observable>();
    // Keep this comment too
    collection.add(new Observable());
    return collection.contains(new Observable());
}
Also used : Observable(java.util.Observable)

Example 33 with Observable

use of java.util.Observable 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)

Example 34 with Observable

use of java.util.Observable in project osumer by mob41.

the class Queue method start.

public void start() {
    runBeforeActions();
    setStartTime(System.nanoTime());
    downloader.download();
    downloader.deleteObservers();
    downloader.addObserver(new Observer() {

        @Override
        public void update(Observable arg0, Object arg1) {
            if (downloader.getStatus() == Downloader.COMPLETED) {
                runAfterActions();
            }
        }
    });
}
Also used : Observer(java.util.Observer) Observable(java.util.Observable)

Example 35 with Observable

use of java.util.Observable in project opentest by mcdcorp.

the class SeleniumTestAction method initialize.

@Override
public void initialize() {
    super.initialize();
    if (!SeleniumTestAction.initialized) {
        SeleniumTestAction.initialized = true;
        SeleniumTestAction.config = SeleniumHelper.getConfig();
        this.getActor().addObserver(new Observer() {

            @Override
            public void update(Observable eventSource, Object eventData) {
                if (eventSource instanceof ITestActor) {
                    if (eventData == TestActorEvents.TEST_COMPLETED) {
                        SeleniumTestAction.discardActionsObject();
                        if (!SeleniumHelper.getConfig().getBoolean("selenium.reuseDriver", false)) {
                            SeleniumHelper.discardDriver();
                        }
                    }
                }
            }
        });
    }
    this.explicitWaitSec = this.readIntArgument("explicitWaitSec", SeleniumHelper.getExplicitWaitSec());
    this.driver = SeleniumHelper.getDriver();
}
Also used : Observer(java.util.Observer) ITestActor(org.getopentest.contracts.ITestActor) Observable(java.util.Observable)

Aggregations

Observable (java.util.Observable)52 Observer (java.util.Observer)30 Test (org.junit.Test)8 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 LinkedList (java.util.LinkedList)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 MessagePanel (edu.ucsf.rbvi.clusterMaker2.internal.treeview.MessagePanel)2 IOException (java.io.IOException)2 HashSet (java.util.HashSet)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 KeyStoreAlias (org.jcryptool.crypto.keystore.backend.KeyStoreAlias)2 WithDumpException (com.github.mob41.osumer.debug.WithDumpException)1 OsuDownloader (com.github.mob41.osumer.io.OsuDownloader)1