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 17package com.android.volley; 18 19import android.test.suitebuilder.annotation.MediumTest; 20 21import com.android.volley.mock.MockRequest; 22import com.android.volley.utils.CacheTestUtils; 23import com.android.volley.utils.ImmediateResponseDelivery; 24 25import junit.framework.TestCase; 26 27@MediumTest 28public class ResponseDeliveryTest extends TestCase { 29 30 private ExecutorDelivery mDelivery; 31 private MockRequest mRequest; 32 private Response<byte[]> mSuccessResponse; 33 34 @Override 35 protected void setUp() throws Exception { 36 super.setUp(); 37 38 // Make the delivery just run its posted responses immediately. 39 mDelivery = new ImmediateResponseDelivery(); 40 mRequest = new MockRequest(); 41 mRequest.setSequence(1); 42 byte[] data = new byte[16]; 43 Cache.Entry cacheEntry = CacheTestUtils.makeRandomCacheEntry(data); 44 mSuccessResponse = Response.success(data, cacheEntry); 45 } 46 47 public void testPostResponse_callsDeliverResponse() { 48 mDelivery.postResponse(mRequest, mSuccessResponse); 49 assertTrue(mRequest.deliverResponse_called); 50 assertFalse(mRequest.deliverError_called); 51 } 52 53 public void testPostResponse_suppressesCanceled() { 54 mRequest.cancel(); 55 mDelivery.postResponse(mRequest, mSuccessResponse); 56 assertFalse(mRequest.deliverResponse_called); 57 assertFalse(mRequest.deliverError_called); 58 } 59 60 public void testPostError_callsDeliverError() { 61 Response<byte[]> errorResponse = Response.error(new ServerError()); 62 63 mDelivery.postResponse(mRequest, errorResponse); 64 assertTrue(mRequest.deliverError_called); 65 assertFalse(mRequest.deliverResponse_called); 66 } 67} 68