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.mock; 18 19 import com.android.volley.NetworkResponse; 20 import com.android.volley.Request; 21 import com.android.volley.Response; 22 import com.android.volley.Response.ErrorListener; 23 import com.android.volley.VolleyError; 24 import com.android.volley.utils.CacheTestUtils; 25 import java.util.HashMap; 26 import java.util.Map; 27 28 public class MockRequest extends Request<byte[]> { MockRequest()29 public MockRequest() { 30 super(Request.Method.GET, "http://foo.com", null); 31 } 32 MockRequest(String url, ErrorListener listener)33 public MockRequest(String url, ErrorListener listener) { 34 super(Request.Method.GET, url, listener); 35 } 36 37 private Map<String, String> mPostParams = new HashMap<String, String>(); 38 setPostParams(Map<String, String> postParams)39 public void setPostParams(Map<String, String> postParams) { 40 mPostParams = postParams; 41 } 42 43 @Override getPostParams()44 public Map<String, String> getPostParams() { 45 return mPostParams; 46 } 47 48 private String mCacheKey = super.getCacheKey(); 49 setCacheKey(String cacheKey)50 public void setCacheKey(String cacheKey) { 51 mCacheKey = cacheKey; 52 } 53 54 @Override getCacheKey()55 public String getCacheKey() { 56 return mCacheKey; 57 } 58 59 public boolean deliverResponse_called = false; 60 public boolean parseResponse_called = false; 61 62 @Override deliverResponse(byte[] response)63 protected void deliverResponse(byte[] response) { 64 deliverResponse_called = true; 65 } 66 67 public boolean deliverError_called = false; 68 69 @Override deliverError(VolleyError error)70 public void deliverError(VolleyError error) { 71 super.deliverError(error); 72 deliverError_called = true; 73 } 74 75 public boolean cancel_called = false; 76 77 @Override cancel()78 public void cancel() { 79 cancel_called = true; 80 super.cancel(); 81 } 82 83 private Priority mPriority = super.getPriority(); 84 setPriority(Priority priority)85 public void setPriority(Priority priority) { 86 mPriority = priority; 87 } 88 89 @Override getPriority()90 public Priority getPriority() { 91 return mPriority; 92 } 93 94 @Override parseNetworkResponse(NetworkResponse response)95 protected Response<byte[]> parseNetworkResponse(NetworkResponse response) { 96 parseResponse_called = true; 97 return Response.success(response.data, CacheTestUtils.makeRandomCacheEntry(response.data)); 98 } 99 } 100