Search in sources :

Example 1 with SpiceServiceListener

use of com.octo.android.robospice.request.listener.SpiceServiceListener in project robospice by stephanenicolas.

the class RequestProcessorTest method test_2_spiceservicelisteners_should_be_notified_of_all_events_from_request_processor_when_request_are_aggregated.

public void test_2_spiceservicelisteners_should_be_notified_of_all_events_from_request_processor_when_request_are_aggregated() throws Exception {
    // TDD for issue 182
    // given
    PausableThreadPoolExecutor executorService = PriorityThreadPoolExecutor.getPriorityExecutor(1);
    networkStateChecker = new MockNetworkStateChecker();
    mockRequestRunner = new DefaultRequestRunner(getContext(), mockCacheManager, executorService, mockRequestProgressManager, networkStateChecker);
    requestProcessorUnderTest = new RequestProcessor(mockCacheManager, mockRequestProgressManager, mockRequestRunner);
    EasyMock.expect(mockCacheManager.loadDataFromCache(EasyMock.eq(TEST_CLASS), EasyMock.eq(TEST_CACHE_KEY), EasyMock.eq(TEST_DURATION))).andReturn(null);
    EasyMock.expectLastCall().anyTimes();
    EasyMock.expect(mockCacheManager.saveDataToCacheAndReturnData(EasyMock.eq(TEST_RETURNED_DATA), EasyMock.eq(TEST_CACHE_KEY))).andReturn(TEST_RETURNED_DATA);
    EasyMock.expectLastCall().anyTimes();
    EasyMock.replay(mockCacheManager);
    CachedSpiceRequestStub<String> spiceRequestStub = createSuccessfulRequest(TEST_CLASS, TEST_CACHE_KEY, TEST_DURATION, TEST_RETURNED_DATA, WAIT_BEFORE_REQUEST_EXECUTION);
    spiceRequestStub.setPriority(SpiceRequest.PRIORITY_LOW);
    spiceRequestStub.setRetryPolicy(null);
    CachedSpiceRequestStub<String> spiceRequestStub2 = createSuccessfulRequest(TEST_CLASS, TEST_CACHE_KEY, TEST_DURATION, TEST_RETURNED_DATA, WAIT_BEFORE_REQUEST_EXECUTION);
    spiceRequestStub2.setPriority(SpiceRequest.PRIORITY_HIGH);
    spiceRequestStub2.setRetryPolicy(null);
    RequestListenerWithProgressStub<String> mockRequestListener = new RequestListenerWithProgressStub<String>();
    Set<RequestListener<?>> requestListenerSet = new HashSet<RequestListener<?>>();
    requestListenerSet.add(mockRequestListener);
    RequestListenerWithProgressStub<String> mockRequestListener2 = new RequestListenerWithProgressStub<String>();
    Set<RequestListener<?>> requestListenerSet2 = new HashSet<RequestListener<?>>();
    requestListenerSet2.add(mockRequestListener2);
    SpiceServiceListener listener1 = EasyMock.createMock(SpiceServiceListener.class);
    listener1.onRequestAdded((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
    listener1.onRequestAggregated((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
    listener1.onRequestProgressUpdated((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
    EasyMock.expectLastCall().anyTimes();
    listener1.onRequestProcessed((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
    EasyMock.expectLastCall().anyTimes();
    listener1.onRequestSucceeded((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
    EasyMock.replay(listener1);
    SpiceServiceListener listener2 = EasyMock.createMock(SpiceServiceListener.class);
    listener2.onRequestAdded((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
    listener2.onRequestAggregated((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
    listener2.onRequestProgressUpdated((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
    EasyMock.expectLastCall().anyTimes();
    listener2.onRequestProcessed((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
    EasyMock.expectLastCall().anyTimes();
    listener2.onRequestSucceeded((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
    EasyMock.replay(listener2);
    requestProcessorUnderTest.addSpiceServiceListener(listener1);
    requestProcessorUnderTest.addSpiceServiceListener(listener2);
    // when
    requestProcessorUnderTest.addRequest(spiceRequestStub, requestListenerSet);
    requestProcessorUnderTest.addRequest(spiceRequestStub2, requestListenerSet2);
    mockRequestListener.awaitComplete(REQUEST_COMPLETION_TIME_OUT);
    mockRequestListener.await(REQUEST_COMPLETION_TIME_OUT);
    mockRequestListener2.awaitComplete(REQUEST_COMPLETION_TIME_OUT);
    mockRequestListener2.await(REQUEST_COMPLETION_TIME_OUT);
    // test
    assertTrue(mockRequestListener.isComplete());
    assertTrue(mockRequestListener2.isComplete());
    assertTrue(mockRequestListener.isSuccessful());
    assertTrue(mockRequestListener2.isSuccessful());
    EasyMock.verify(listener1);
    EasyMock.verify(listener2);
}
Also used : PausableThreadPoolExecutor(com.octo.android.robospice.priority.PausableThreadPoolExecutor) RequestListener(com.octo.android.robospice.request.listener.RequestListener) SpiceServiceListener(com.octo.android.robospice.request.listener.SpiceServiceListener) RequestListenerWithProgressStub(com.octo.android.robospice.stub.RequestListenerWithProgressStub) PendingRequestListenerWithProgressStub(com.octo.android.robospice.stub.PendingRequestListenerWithProgressStub) HashSet(java.util.HashSet)

Example 2 with SpiceServiceListener

use of com.octo.android.robospice.request.listener.SpiceServiceListener in project robospice by stephanenicolas.

the class RequestProcessorTest method testAddRequest_should_process_offline_request_even_if_network_is_down.

public void testAddRequest_should_process_offline_request_even_if_network_is_down() throws CacheLoadingException, CacheSavingException, InterruptedException, CacheCreationException {
    // given
    CachedSpiceRequestStub<String> stubRequest = createSuccessfulRequest(TEST_CLASS, TEST_CACHE_KEY, TEST_DURATION, TEST_RETURNED_DATA);
    stubRequest.setOffline(true);
    RequestListenerStub<String> mockRequestListener = new RequestListenerStub<String>();
    Set<RequestListener<?>> requestListenerSet = new HashSet<RequestListener<?>>();
    requestListenerSet.add(mockRequestListener);
    EasyMock.expect(mockCacheManager.loadDataFromCache(EasyMock.eq(TEST_CLASS), EasyMock.eq(TEST_CACHE_KEY), EasyMock.eq(TEST_DURATION))).andReturn(null);
    EasyMock.expect(mockCacheManager.saveDataToCacheAndReturnData(EasyMock.eq(TEST_RETURNED_DATA), EasyMock.eq(TEST_CACHE_KEY))).andReturn(TEST_RETURNED_DATA);
    EasyMock.replay(mockCacheManager);
    SpiceServiceListener mockSpiceServiceListener = EasyMock.createMock(SpiceServiceListener.class);
    mockSpiceServiceListener.onRequestAdded((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
    mockSpiceServiceListener.onRequestProgressUpdated((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
    EasyMock.expectLastCall().anyTimes();
    mockSpiceServiceListener.onRequestSucceeded((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
    mockSpiceServiceListener.onRequestProcessed((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
    EasyMock.expectLastCall().anyTimes();
    EasyMock.replay(mockSpiceServiceListener);
    requestProcessorUnderTest.addSpiceServiceListener(mockSpiceServiceListener);
    // when
    requestProcessorUnderTest.setFailOnCacheError(true);
    networkStateChecker.setNetworkAvailable(false);
    requestProcessorUnderTest.addRequest(stubRequest, requestListenerSet);
    mockRequestListener.await(REQUEST_COMPLETION_TIME_OUT);
    // then
    EasyMock.verify(mockCacheManager);
    assertTrue(stubRequest.isLoadDataFromNetworkCalled());
    assertTrue(mockRequestListener.isExecutedInUIThread());
    assertTrue(mockRequestListener.isSuccessful());
    EasyMock.verify(mockSpiceServiceListener);
}
Also used : RequestListener(com.octo.android.robospice.request.listener.RequestListener) SpiceServiceListener(com.octo.android.robospice.request.listener.SpiceServiceListener) RequestListenerStub(com.octo.android.robospice.stub.RequestListenerStub) HashSet(java.util.HashSet)

Example 3 with SpiceServiceListener

use of com.octo.android.robospice.request.listener.SpiceServiceListener in project robospice by stephanenicolas.

the class RequestProcessorTest method test_2_spiceservicelisteners_should_be_notified_of_all_events_from_request_processor.

// ============================================================================================
// TDD
// ============================================================================================
public void test_2_spiceservicelisteners_should_be_notified_of_all_events_from_request_processor() throws Exception {
    // TDD for issue 182
    // given
    PausableThreadPoolExecutor executorService = PriorityThreadPoolExecutor.getPriorityExecutor(1);
    networkStateChecker = new MockNetworkStateChecker();
    mockRequestRunner = new DefaultRequestRunner(getContext(), mockCacheManager, executorService, mockRequestProgressManager, networkStateChecker);
    requestProcessorUnderTest = new RequestProcessor(mockCacheManager, mockRequestProgressManager, mockRequestRunner);
    EasyMock.expect(mockCacheManager.loadDataFromCache(EasyMock.eq(TEST_CLASS), EasyMock.eq(TEST_CACHE_KEY), EasyMock.eq(TEST_DURATION))).andReturn(null);
    EasyMock.expect(mockCacheManager.saveDataToCacheAndReturnData(EasyMock.eq(TEST_RETURNED_DATA), EasyMock.eq(TEST_CACHE_KEY))).andReturn(TEST_RETURNED_DATA);
    EasyMock.expect(mockCacheManager.loadDataFromCache(EasyMock.eq(TEST_CLASS), EasyMock.eq(TEST_CACHE_KEY2), EasyMock.eq(TEST_DURATION))).andReturn(null);
    EasyMock.expect(mockCacheManager.saveDataToCacheAndReturnData(EasyMock.eq(TEST_RETURNED_DATA2), EasyMock.eq(TEST_CACHE_KEY2))).andReturn(TEST_RETURNED_DATA2);
    EasyMock.replay(mockCacheManager);
    CachedSpiceRequestStub<String> spiceRequestStub = createSuccessfulRequest(TEST_CLASS, TEST_CACHE_KEY, TEST_DURATION, TEST_RETURNED_DATA);
    spiceRequestStub.setPriority(SpiceRequest.PRIORITY_LOW);
    spiceRequestStub.setRetryPolicy(null);
    CachedSpiceRequestStub<String> spiceRequestStub2 = createSuccessfulRequest(TEST_CLASS, TEST_CACHE_KEY2, TEST_DURATION, TEST_RETURNED_DATA2);
    spiceRequestStub2.setPriority(SpiceRequest.PRIORITY_HIGH);
    spiceRequestStub2.setRetryPolicy(null);
    RequestListenerWithProgressStub<String> mockRequestListener = new RequestListenerWithProgressStub<String>();
    Set<RequestListener<?>> requestListenerSet = new HashSet<RequestListener<?>>();
    requestListenerSet.add(mockRequestListener);
    RequestListenerWithProgressStub<String> mockRequestListener2 = new RequestListenerWithProgressStub<String>();
    Set<RequestListener<?>> requestListenerSet2 = new HashSet<RequestListener<?>>();
    requestListenerSet2.add(mockRequestListener2);
    SpiceServiceListener listener1 = EasyMock.createMock(SpiceServiceListener.class);
    listener1.onRequestAdded((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
    EasyMock.expectLastCall().times(2);
    listener1.onRequestProgressUpdated((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
    EasyMock.expectLastCall().anyTimes();
    listener1.onRequestProcessed((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
    EasyMock.expectLastCall().anyTimes();
    listener1.onRequestSucceeded((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
    EasyMock.expectLastCall().times(2);
    EasyMock.replay(listener1);
    SpiceServiceListener listener2 = EasyMock.createMock(SpiceServiceListener.class);
    listener2.onRequestAdded((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
    EasyMock.expectLastCall().times(2);
    listener2.onRequestProgressUpdated((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
    EasyMock.expectLastCall().anyTimes();
    listener2.onRequestProcessed((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
    EasyMock.expectLastCall().anyTimes();
    listener2.onRequestSucceeded((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
    EasyMock.expectLastCall().times(2);
    EasyMock.replay(listener2);
    requestProcessorUnderTest.addSpiceServiceListener(listener1);
    requestProcessorUnderTest.addSpiceServiceListener(listener2);
    // when
    requestProcessorUnderTest.addRequest(spiceRequestStub, requestListenerSet);
    requestProcessorUnderTest.addRequest(spiceRequestStub2, requestListenerSet2);
    mockRequestListener.awaitComplete(REQUEST_COMPLETION_TIME_OUT);
    mockRequestListener.await(REQUEST_COMPLETION_TIME_OUT);
    mockRequestListener2.awaitComplete(REQUEST_COMPLETION_TIME_OUT);
    mockRequestListener2.await(REQUEST_COMPLETION_TIME_OUT);
    // test
    assertTrue(mockRequestListener.isComplete());
    assertTrue(mockRequestListener2.isComplete());
    assertTrue(mockRequestListener.isSuccessful());
    assertTrue(mockRequestListener2.isSuccessful());
    EasyMock.verify(listener1);
    EasyMock.verify(listener2);
}
Also used : PausableThreadPoolExecutor(com.octo.android.robospice.priority.PausableThreadPoolExecutor) RequestListener(com.octo.android.robospice.request.listener.RequestListener) SpiceServiceListener(com.octo.android.robospice.request.listener.SpiceServiceListener) RequestListenerWithProgressStub(com.octo.android.robospice.stub.RequestListenerWithProgressStub) PendingRequestListenerWithProgressStub(com.octo.android.robospice.stub.PendingRequestListenerWithProgressStub) HashSet(java.util.HashSet)

Example 4 with SpiceServiceListener

use of com.octo.android.robospice.request.listener.SpiceServiceListener in project robospice by stephanenicolas.

the class RequestProcessorTest method testAddRequestWhenNetworkIsDown.

// ============================================================================================
// TESTING NETWORK MANAGER DEPENDENCY & SPICE LISTENER
// ============================================================================================
public void testAddRequestWhenNetworkIsDown() throws CacheLoadingException, CacheSavingException, InterruptedException, CacheCreationException {
    // given
    CachedSpiceRequestStub<String> stubRequest = createSuccessfulRequest(TEST_CLASS, TEST_CACHE_KEY, TEST_DURATION, TEST_RETURNED_DATA);
    stubRequest.setRetryPolicy(null);
    RequestListenerStub<String> mockRequestListener = new RequestListenerStub<String>();
    Set<RequestListener<?>> requestListenerSet = new HashSet<RequestListener<?>>();
    requestListenerSet.add(mockRequestListener);
    EasyMock.expect(mockCacheManager.loadDataFromCache(EasyMock.eq(TEST_CLASS), EasyMock.eq(TEST_CACHE_KEY), EasyMock.eq(TEST_DURATION))).andReturn(null);
    EasyMock.replay(mockCacheManager);
    SpiceServiceListener mockSpiceServiceListener = EasyMock.createMock(SpiceServiceListener.class);
    mockSpiceServiceListener.onRequestAdded((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
    mockSpiceServiceListener.onRequestProgressUpdated((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
    EasyMock.expectLastCall().anyTimes();
    mockSpiceServiceListener.onRequestFailed((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
    mockSpiceServiceListener.onRequestProcessed((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
    EasyMock.expectLastCall().anyTimes();
    EasyMock.replay(mockSpiceServiceListener);
    requestProcessorUnderTest.addSpiceServiceListener(mockSpiceServiceListener);
    // when
    requestProcessorUnderTest.setFailOnCacheError(true);
    networkStateChecker.setNetworkAvailable(false);
    requestProcessorUnderTest.addRequest(stubRequest, requestListenerSet);
    mockRequestListener.await(REQUEST_COMPLETION_TIME_OUT);
    // then
    EasyMock.verify(mockCacheManager);
    assertFalse(stubRequest.isLoadDataFromNetworkCalled());
    assertTrue(mockRequestListener.isExecutedInUIThread());
    assertFalse(mockRequestListener.isSuccessful());
    assertTrue(mockRequestListener.getReceivedException() instanceof NoNetworkException);
    EasyMock.verify(mockSpiceServiceListener);
}
Also used : RequestListener(com.octo.android.robospice.request.listener.RequestListener) NoNetworkException(com.octo.android.robospice.exception.NoNetworkException) SpiceServiceListener(com.octo.android.robospice.request.listener.SpiceServiceListener) RequestListenerStub(com.octo.android.robospice.stub.RequestListenerStub) HashSet(java.util.HashSet)

Aggregations

RequestListener (com.octo.android.robospice.request.listener.RequestListener)4 SpiceServiceListener (com.octo.android.robospice.request.listener.SpiceServiceListener)4 HashSet (java.util.HashSet)4 PausableThreadPoolExecutor (com.octo.android.robospice.priority.PausableThreadPoolExecutor)2 PendingRequestListenerWithProgressStub (com.octo.android.robospice.stub.PendingRequestListenerWithProgressStub)2 RequestListenerStub (com.octo.android.robospice.stub.RequestListenerStub)2 RequestListenerWithProgressStub (com.octo.android.robospice.stub.RequestListenerWithProgressStub)2 NoNetworkException (com.octo.android.robospice.exception.NoNetworkException)1