Search in sources :

Example 6 with ContentObserver

use of android.database.ContentObserver in project storio by pushtorefresh.

the class RxChangesObserverTest method shouldEmitChangesOnSdkVersionLowerThan16.

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Test
public void shouldEmitChangesOnSdkVersionLowerThan16() {
    for (int sdkVersion = MIN_SDK_VERSION; sdkVersion < 16; sdkVersion++) {
        ContentResolver contentResolver = mock(ContentResolver.class);
        final Map<Uri, ContentObserver> contentObservers = new HashMap<Uri, ContentObserver>(3);
        doAnswer(new Answer() {

            @Override
            public Object answer(InvocationOnMock invocation) throws Throwable {
                contentObservers.put((Uri) invocation.getArguments()[0], (ContentObserver) invocation.getArguments()[2]);
                return null;
            }
        }).when(contentResolver).registerContentObserver(any(Uri.class), eq(true), any(ContentObserver.class));
        TestSubscriber<Changes> testSubscriber = new TestSubscriber<Changes>();
        Uri uri1 = mock(Uri.class);
        Uri uri2 = mock(Uri.class);
        Set<Uri> uris = new HashSet<Uri>(2);
        uris.add(uri1);
        uris.add(uri2);
        RxChangesObserver.observeChanges(contentResolver, uris, mock(Handler.class), sdkVersion).subscribe(testSubscriber);
        testSubscriber.assertNoTerminalEvent();
        testSubscriber.assertNoValues();
        // Emulate change of Uris, Observable should react and emit Changes objects
        contentObservers.get(uri1).onChange(false);
        contentObservers.get(uri2).onChange(false);
        testSubscriber.assertValues(Changes.newInstance(uri1), Changes.newInstance(uri2));
        testSubscriber.unsubscribe();
        testSubscriber.assertNoErrors();
    }
}
Also used : Changes(com.pushtorefresh.storio.contentresolver.Changes) HashMap(java.util.HashMap) Uri(android.net.Uri) ContentResolver(android.content.ContentResolver) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) TestSubscriber(rx.observers.TestSubscriber) ContentObserver(android.database.ContentObserver) HashSet(java.util.HashSet) Test(org.junit.Test) TargetApi(android.annotation.TargetApi)

Example 7 with ContentObserver

use of android.database.ContentObserver in project storio by pushtorefresh.

the class RxChangesObserverTest method shouldRegisterObserverForEachPassedUriAfterSubscribingToObservableOnSdkVersionLowerThan15.

@Test
public void shouldRegisterObserverForEachPassedUriAfterSubscribingToObservableOnSdkVersionLowerThan15() {
    for (int sdkVersion = MIN_SDK_VERSION; sdkVersion < 16; sdkVersion++) {
        ContentResolver contentResolver = mock(ContentResolver.class);
        final Map<Uri, ContentObserver> contentObservers = new HashMap<Uri, ContentObserver>(3);
        doAnswer(new Answer() {

            @Override
            public Object answer(InvocationOnMock invocation) throws Throwable {
                contentObservers.put((Uri) invocation.getArguments()[0], (ContentObserver) invocation.getArguments()[2]);
                return null;
            }
        }).when(contentResolver).registerContentObserver(any(Uri.class), eq(true), any(ContentObserver.class));
        Set<Uri> uris = new HashSet<Uri>(3);
        uris.add(mock(Uri.class));
        uris.add(mock(Uri.class));
        uris.add(mock(Uri.class));
        Observable<Changes> observable = RxChangesObserver.observeChanges(contentResolver, uris, mock(Handler.class), sdkVersion);
        // Should not register ContentObserver before subscribing to Observable
        verify(contentResolver, times(0)).registerContentObserver(any(Uri.class), anyBoolean(), any(ContentObserver.class));
        Subscription subscription = observable.subscribe();
        for (Uri uri : uris) {
            // Assert that new ContentObserver was registered for each uri
            verify(contentResolver).registerContentObserver(same(uri), eq(true), same(contentObservers.get(uri)));
        }
        assertThat(contentObservers).hasSameSizeAs(uris);
        subscription.unsubscribe();
    }
}
Also used : Changes(com.pushtorefresh.storio.contentresolver.Changes) HashMap(java.util.HashMap) Handler(android.os.Handler) Uri(android.net.Uri) ContentResolver(android.content.ContentResolver) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Subscription(rx.Subscription) ContentObserver(android.database.ContentObserver) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 8 with ContentObserver

use of android.database.ContentObserver in project storio by pushtorefresh.

the class RxChangesObserverTest method shouldEmitChangesOnSdkVersionGreaterThan15.

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Test
public void shouldEmitChangesOnSdkVersionGreaterThan15() {
    for (int sdkVersion = 16; sdkVersion < MAX_SDK_VERSION; sdkVersion++) {
        ContentResolver contentResolver = mock(ContentResolver.class);
        final AtomicReference<ContentObserver> contentObserver = new AtomicReference<ContentObserver>();
        doAnswer(new Answer() {

            @Override
            public Object answer(InvocationOnMock invocation) throws Throwable {
                // Save reference to ContentObserver only once to assert that it was created once
                if (contentObserver.get() == null) {
                    contentObserver.set((ContentObserver) invocation.getArguments()[2]);
                } else if (contentObserver.get() != invocation.getArguments()[2]) {
                    throw new AssertionError("More than one ContentObserver was created");
                }
                return null;
            }
        }).when(contentResolver).registerContentObserver(any(Uri.class), eq(true), any(ContentObserver.class));
        TestSubscriber<Changes> testSubscriber = new TestSubscriber<Changes>();
        Uri uri1 = mock(Uri.class);
        Uri uri2 = mock(Uri.class);
        Set<Uri> uris = new HashSet<Uri>(2);
        uris.add(uri1);
        uris.add(uri2);
        RxChangesObserver.observeChanges(contentResolver, uris, mock(Handler.class), sdkVersion).subscribe(testSubscriber);
        testSubscriber.assertNoTerminalEvent();
        testSubscriber.assertNoValues();
        // RxChangesObserver should ignore call to onChange() without Uri on sdkVersion >= 16
        contentObserver.get().onChange(false);
        testSubscriber.assertNoValues();
        // Emulate change of Uris, Observable should react and emit Changes objects
        contentObserver.get().onChange(false, uri1);
        contentObserver.get().onChange(false, uri2);
        testSubscriber.assertValues(Changes.newInstance(uri1), Changes.newInstance(uri2));
        testSubscriber.unsubscribe();
        testSubscriber.assertNoErrors();
    }
}
Also used : Changes(com.pushtorefresh.storio.contentresolver.Changes) AtomicReference(java.util.concurrent.atomic.AtomicReference) Uri(android.net.Uri) ContentResolver(android.content.ContentResolver) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) TestSubscriber(rx.observers.TestSubscriber) ContentObserver(android.database.ContentObserver) HashSet(java.util.HashSet) Test(org.junit.Test) TargetApi(android.annotation.TargetApi)

Example 9 with ContentObserver

use of android.database.ContentObserver in project storio by pushtorefresh.

the class RxChangesObserverTest method shouldUnregisterContentObserverAfterUnsubscribingFromObservableOnSdkVersionGreaterThan15.

@Test
public void shouldUnregisterContentObserverAfterUnsubscribingFromObservableOnSdkVersionGreaterThan15() {
    for (int sdkVersion = 16; sdkVersion < MAX_SDK_VERSION; sdkVersion++) {
        ContentResolver contentResolver = mock(ContentResolver.class);
        Set<Uri> uris = new HashSet<Uri>(3);
        uris.add(mock(Uri.class));
        uris.add(mock(Uri.class));
        uris.add(mock(Uri.class));
        Subscription subscription = RxChangesObserver.observeChanges(contentResolver, uris, mock(Handler.class), sdkVersion).subscribe();
        // Should not unregister before unsubscribe from Subscription
        verify(contentResolver, times(0)).unregisterContentObserver(any(ContentObserver.class));
        subscription.unsubscribe();
        // Should unregister ContentObserver after unsubscribing from Subscription
        verify(contentResolver).unregisterContentObserver(any(ContentObserver.class));
    }
}
Also used : Subscription(rx.Subscription) Uri(android.net.Uri) ContentResolver(android.content.ContentResolver) HashSet(java.util.HashSet) ContentObserver(android.database.ContentObserver) Test(org.junit.Test)

Example 10 with ContentObserver

use of android.database.ContentObserver in project storio by pushtorefresh.

the class RxChangesObserverTest method shouldUnregisterContentObserversForEachUriAfterUnsubscribingFromObservableOnSdkVersionLowerThan16.

@Test
public void shouldUnregisterContentObserversForEachUriAfterUnsubscribingFromObservableOnSdkVersionLowerThan16() {
    for (int sdkVersion = MIN_SDK_VERSION; sdkVersion < 16; sdkVersion++) {
        ContentResolver contentResolver = mock(ContentResolver.class);
        Set<Uri> uris = new HashSet<Uri>(3);
        uris.add(mock(Uri.class));
        uris.add(mock(Uri.class));
        uris.add(mock(Uri.class));
        final Map<Uri, ContentObserver> contentObservers = new HashMap<Uri, ContentObserver>(3);
        doAnswer(new Answer() {

            @Override
            public Object answer(InvocationOnMock invocation) throws Throwable {
                contentObservers.put((Uri) invocation.getArguments()[0], (ContentObserver) invocation.getArguments()[2]);
                return null;
            }
        }).when(contentResolver).registerContentObserver(any(Uri.class), eq(true), any(ContentObserver.class));
        Subscription subscription = RxChangesObserver.observeChanges(contentResolver, uris, mock(Handler.class), sdkVersion).subscribe();
        // Should not unregister before unsubscribe from Subscription
        verify(contentResolver, times(0)).unregisterContentObserver(any(ContentObserver.class));
        subscription.unsubscribe();
        for (Uri uri : uris) {
            // Assert that ContentObserver for each uri was unregistered
            verify(contentResolver).unregisterContentObserver(contentObservers.get(uri));
        }
    }
}
Also used : HashMap(java.util.HashMap) Uri(android.net.Uri) ContentResolver(android.content.ContentResolver) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Subscription(rx.Subscription) HashSet(java.util.HashSet) ContentObserver(android.database.ContentObserver) Test(org.junit.Test)

Aggregations

ContentObserver (android.database.ContentObserver)94 Handler (android.os.Handler)40 Uri (android.net.Uri)33 ContentResolver (android.content.ContentResolver)28 Intent (android.content.Intent)15 IntentFilter (android.content.IntentFilter)15 BroadcastReceiver (android.content.BroadcastReceiver)8 Context (android.content.Context)8 PendingIntent (android.app.PendingIntent)7 RemoteException (android.os.RemoteException)7 Test (org.junit.Test)7 AppOpsManager (android.app.AppOpsManager)6 GeofenceManager (com.android.server.location.GeofenceManager)6 LocationBlacklist (com.android.server.location.LocationBlacklist)6 LocationFudger (com.android.server.location.LocationFudger)6 HashSet (java.util.HashSet)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 Mockito.doAnswer (org.mockito.Mockito.doAnswer)6 InvocationOnMock (org.mockito.invocation.InvocationOnMock)6 Answer (org.mockito.stubbing.Answer)6