Home | History | Annotate | Download | only in volley
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.volley;
     18 
     19 import com.android.volley.mock.MockRequest;
     20 import com.android.volley.utils.CacheTestUtils;
     21 import com.android.volley.utils.ImmediateResponseDelivery;
     22 
     23 import org.junit.After;
     24 import org.junit.Before;
     25 import org.junit.Test;
     26 import org.junit.runner.RunWith;
     27 import org.robolectric.RobolectricTestRunner;
     28 
     29 import static org.junit.Assert.*;
     30 
     31 @RunWith(RobolectricTestRunner.class)
     32 public class ResponseDeliveryTest {
     33 
     34     private ExecutorDelivery mDelivery;
     35     private MockRequest mRequest;
     36     private Response<byte[]> mSuccessResponse;
     37 
     38     @Before public void setUp() throws Exception {
     39         // Make the delivery just run its posted responses immediately.
     40         mDelivery = new ImmediateResponseDelivery();
     41         mRequest = new MockRequest();
     42         mRequest.setSequence(1);
     43         byte[] data = new byte[16];
     44         Cache.Entry cacheEntry = CacheTestUtils.makeRandomCacheEntry(data);
     45         mSuccessResponse = Response.success(data, cacheEntry);
     46     }
     47 
     48     @Test public void postResponseCallsDeliverResponse() {
     49         mDelivery.postResponse(mRequest, mSuccessResponse);
     50         assertTrue(mRequest.deliverResponse_called);
     51         assertFalse(mRequest.deliverError_called);
     52     }
     53 
     54     @Test public void postResponseSuppressesCanceled() {
     55         mRequest.cancel();
     56         mDelivery.postResponse(mRequest, mSuccessResponse);
     57         assertFalse(mRequest.deliverResponse_called);
     58         assertFalse(mRequest.deliverError_called);
     59     }
     60 
     61     @Test public void postErrorCallsDeliverError() {
     62         Response<byte[]> errorResponse = Response.error(new ServerError());
     63 
     64         mDelivery.postResponse(mRequest, errorResponse);
     65         assertTrue(mRequest.deliverError_called);
     66         assertFalse(mRequest.deliverResponse_called);
     67     }
     68 }
     69