CallerInfoAsyncQueryFactoryFixture.java revision 953e1af643b66df6f931d76c23bcc54147668cd4
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
17package com.android.server.telecom.tests;
18
19import com.android.internal.telephony.CallerInfo;
20import com.android.internal.telephony.CallerInfoAsyncQuery;
21import com.android.server.telecom.CallerInfoAsyncQueryFactory;
22
23import android.content.Context;
24import android.telecom.Log;
25
26import org.mockito.Mockito;
27
28import java.util.ArrayList;
29import java.util.Collections;
30import 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 */
36public 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