blob: 68db09c0cc4afce9f9203ea05304b06ea96ec698 [file] [log] [blame]
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.server.telecom.tests;
import android.telecom.CallerInfo;
import android.telecom.CallerInfoAsyncQuery;
import com.android.server.telecom.CallerInfoAsyncQueryFactory;
import android.content.Context;
import android.telecom.Log;
import org.mockito.Mockito;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Controls a test {@link CallerInfoAsyncQueryFactory} to abstract away the asynchronous retrieval
* of caller information from the Android contacts database.
*/
public class CallerInfoAsyncQueryFactoryFixture implements
TestFixture<CallerInfoAsyncQueryFactory> {
static class Request {
int mToken;
Object mCookie;
CallerInfoAsyncQuery.OnQueryCompleteListener mListener;
void reply() {
replyWithCallerInfo(new CallerInfo());
}
void replyWithCallerInfo(CallerInfo callerInfo) {
mListener.onQueryComplete(mToken, mCookie, callerInfo);
}
}
CallerInfoAsyncQueryFactory mCallerInfoAsyncQueryFactory = new CallerInfoAsyncQueryFactory() {
@Override
public CallerInfoAsyncQuery startQuery(int token, Context context, String number,
CallerInfoAsyncQuery.OnQueryCompleteListener listener, Object cookie) {
Request r = new Request();
r.mToken = token;
r.mCookie = cookie;
r.mListener = listener;
mRequests.add(r);
if (mStoredResponse != null) {
listener.onQueryComplete(token, cookie, mStoredResponse);
}
return Mockito.mock(CallerInfoAsyncQuery.class);
}
};
final List<Request> mRequests = Collections.synchronizedList(new ArrayList<Request>());
private CallerInfo mStoredResponse;
public CallerInfoAsyncQueryFactoryFixture() throws Exception {
Log.i(this, "Creating ...");
}
public void setResponse(CallerInfo callerInfo) {
mStoredResponse = callerInfo;
}
@Override
public CallerInfoAsyncQueryFactory getTestDouble() {
return mCallerInfoAsyncQueryFactory;
}
}