1 /* 2 * Copyright (C) 2015 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.server.telecom.tests; 18 19 import android.telecom.CallerInfo; 20 import android.telecom.CallerInfoAsyncQuery; 21 import com.android.server.telecom.CallerInfoAsyncQueryFactory; 22 23 import android.content.Context; 24 import android.telecom.Log; 25 26 import org.mockito.Mockito; 27 28 import java.util.ArrayList; 29 import java.util.Collections; 30 import java.util.HashMap; 31 import java.util.List; 32 import java.util.Map; 33 34 /** 35 * Controls a test {@link CallerInfoAsyncQueryFactory} to abstract away the asynchronous retrieval 36 * of caller information from the Android contacts database. 37 */ 38 public class CallerInfoAsyncQueryFactoryFixture implements 39 TestFixture<CallerInfoAsyncQueryFactory> { 40 41 static class Request { 42 int mToken; 43 Object mCookie; 44 CallerInfoAsyncQuery.OnQueryCompleteListener mListener; reply()45 void reply() { 46 replyWithCallerInfo(new CallerInfo()); 47 } 48 replyWithCallerInfo(CallerInfo callerInfo)49 void replyWithCallerInfo(CallerInfo callerInfo) { 50 mListener.onQueryComplete(mToken, mCookie, callerInfo); 51 } 52 } 53 54 CallerInfoAsyncQueryFactory mCallerInfoAsyncQueryFactory = new CallerInfoAsyncQueryFactory() { 55 @Override 56 public CallerInfoAsyncQuery startQuery(int token, Context context, String number, 57 CallerInfoAsyncQuery.OnQueryCompleteListener listener, Object cookie) { 58 Request r = new Request(); 59 r.mToken = token; 60 r.mCookie = cookie; 61 r.mListener = listener; 62 mRequests.add(r); 63 if (mStoredResponse != null) { 64 listener.onQueryComplete(token, cookie, mStoredResponse); 65 } 66 return Mockito.mock(CallerInfoAsyncQuery.class); 67 } 68 }; 69 70 final List<Request> mRequests = Collections.synchronizedList(new ArrayList<Request>()); 71 private CallerInfo mStoredResponse; 72 CallerInfoAsyncQueryFactoryFixture()73 public CallerInfoAsyncQueryFactoryFixture() throws Exception { 74 Log.i(this, "Creating ..."); 75 } 76 setResponse(CallerInfo callerInfo)77 public void setResponse(CallerInfo callerInfo) { 78 mStoredResponse = callerInfo; 79 } 80 81 @Override getTestDouble()82 public CallerInfoAsyncQueryFactory getTestDouble() { 83 return mCallerInfoAsyncQueryFactory; 84 } 85 } 86