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