use of org.mockito.stubbing.Answer 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();
}
}
use of org.mockito.stubbing.Answer 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();
}
}
use of org.mockito.stubbing.Answer 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));
}
}
}
use of org.mockito.stubbing.Answer in project storio by pushtorefresh.
the class RxChangesObserverTest method contentObserverShouldReturnFalseOnDeliverSelfNotificationsOnAllSdkVersions.
@Test
public void contentObserverShouldReturnFalseOnDeliverSelfNotificationsOnAllSdkVersions() {
for (int sdkVersion = MIN_SDK_VERSION; sdkVersion < MAX_SDK_VERSION; sdkVersion++) {
ContentResolver contentResolver = mock(ContentResolver.class);
Uri uri = mock(Uri.class);
final AtomicReference<ContentObserver> contentObserver = new AtomicReference<ContentObserver>();
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
contentObserver.set((ContentObserver) invocation.getArguments()[2]);
return null;
}
}).when(contentResolver).registerContentObserver(same(uri), eq(true), any(ContentObserver.class));
Handler handler = mock(Handler.class);
Observable<Changes> observable = RxChangesObserver.observeChanges(contentResolver, singleton(uri), handler, sdkVersion);
Subscription subscription = observable.subscribe();
assertThat(contentObserver.get().deliverSelfNotifications()).isFalse();
subscription.unsubscribe();
}
}
use of org.mockito.stubbing.Answer in project byte-buddy by raphw.
the class AgentBuilderDefaultTest method testExecutingTransformerDoesNotRecurse.
@Test
@SuppressWarnings("unchecked")
public void testExecutingTransformerDoesNotRecurse() throws Exception {
final AgentBuilder.Default.ExecutingTransformer executingTransformer = new AgentBuilder.Default.ExecutingTransformer(byteBuddy, listener, poolStrategy, typeStrategy, locationStrategy, mock(AgentBuilder.Default.NativeMethodStrategy.class), initializationStrategy, mock(AgentBuilder.Default.BootstrapInjectionStrategy.class), AgentBuilder.LambdaInstrumentationStrategy.DISABLED, AgentBuilder.DescriptionStrategy.Default.HYBRID, mock(AgentBuilder.FallbackStrategy.class), mock(AgentBuilder.InstallationListener.class), mock(AgentBuilder.RawMatcher.class), mock(AgentBuilder.Default.Transformation.class), new AgentBuilder.Default.CircularityLock.Default());
final ClassLoader classLoader = mock(ClassLoader.class);
final ProtectionDomain protectionDomain = mock(ProtectionDomain.class);
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
assertThat(executingTransformer.transform(classLoader, FOO, Object.class, protectionDomain, new byte[0]), nullValue(byte[].class));
return null;
}
}).when(listener).onComplete(FOO, classLoader, JavaModule.UNSUPPORTED, true);
assertThat(executingTransformer.transform(classLoader, FOO, Object.class, protectionDomain, new byte[0]), nullValue(byte[].class));
verify(listener).onComplete(FOO, classLoader, JavaModule.UNSUPPORTED, true);
}
Aggregations