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 org.mockito.Mockito;
20 
21 import android.net.Uri;
22 import android.telecom.Connection;
23 import android.telecom.Connection.VideoProvider;
24 import android.telecom.InCallService;
25 import android.telecom.InCallService.VideoCall;
26 import android.telecom.Log;
27 import android.telecom.VideoProfile;
28 import android.view.Surface;
29 
30 /**
31  * Provides a mock implementation of a video provider.
32  */
33 public class MockVideoProvider extends VideoProvider {
34     public static final String CAMERA_NONE = "none";
35     public static final String CAMERA_FRONT = "front";
36     public static final String CAMERA_BACK = "back";
37     public static final int CAMERA_FRONT_DIMENSIONS = 1024;
38     public static final int CAMERA_BACK_DIMENSIONS = 2048;
39     public static final long DATA_USAGE = 1024;
40     public static final int PEER_DIMENSIONS = 4096;
41     public static final int DEVICE_ORIENTATION_UNDEFINED = -1;
42     public static final float ZOOM_UNDEFINED = -1.0f;
43 
44     private Surface mPreviewSurface = null;
45     private Surface mDisplaySurface = null;
46     private int mDeviceOrientation = DEVICE_ORIENTATION_UNDEFINED;
47     private float mZoom = ZOOM_UNDEFINED;
48     private VideoProfile mSessionModifyResponse = null;
49     private Uri mPauseImage = null;
50 
51     /**
52      * Responds to a request to set the camera by reporting the new camera information via the
53      * {@link #changeCameraCapabilities(VideoProfile.CameraCapabilities)} API.
54      *
55      * @param cameraId The id of the camera (use ids as reported by
56      */
57     @Override
onSetCamera(String cameraId)58     public void onSetCamera(String cameraId) {
59         handleCameraChange(cameraId);
60     }
61 
62     /**
63      * Stores the preview surface set via the {@link VideoCall#setPreviewSurface(Surface)} API for
64      * retrieval using {@link #getPreviewSurface()}.
65      *
66      * @param surface The {@link Surface}.
67      */
68     @Override
onSetPreviewSurface(Surface surface)69     public void onSetPreviewSurface(Surface surface) {
70         mPreviewSurface = surface;
71     }
72 
73     /**
74      * Stores the display surface set via the {@link VideoCall#setDisplaySurface(Surface)} API for
75      * retrieval using {@link #getDisplaySurface()}.
76      *
77      * @param surface The {@link Surface}.
78      */
79     @Override
onSetDisplaySurface(Surface surface)80     public void onSetDisplaySurface(Surface surface) {
81         mDisplaySurface = surface;
82     }
83 
84     /**
85      * Stores the device orientation set via the {@link VideoCall#setDeviceOrientation(int)} API for
86      * retrieval using {@link #getDeviceOrientation()}.
87      *
88      * @param rotation The device orientation, in degrees.
89      */
90     @Override
onSetDeviceOrientation(int rotation)91     public void onSetDeviceOrientation(int rotation) {
92         mDeviceOrientation = rotation;
93     }
94 
95     /**
96      * Stores the zoom level set via the {@link VideoCall#setZoom(float)} API for retrieval using
97      * {@link #getZoom()}.
98      *
99      * @param value The camera zoom.
100      */
101     @Override
onSetZoom(float value)102     public void onSetZoom(float value) {
103         mZoom = value;
104     }
105 
106     /**
107      * Responds to any incoming session modify request by accepting the requested profile.
108      *
109      * @param fromProfile The video profile prior to the request.
110      * @param toProfile The video profile with the requested changes made.
111      */
112     @Override
onSendSessionModifyRequest(VideoProfile fromProfile, VideoProfile toProfile)113     public void onSendSessionModifyRequest(VideoProfile fromProfile, VideoProfile toProfile) {
114         super.receiveSessionModifyResponse(VideoProvider.SESSION_MODIFY_REQUEST_SUCCESS,
115                 fromProfile, toProfile);
116     }
117 
118     /**
119      * Stores session modify responses received via the
120      * {@link VideoCall#sendSessionModifyResponse(VideoProfile)} API for retrieval via
121      * {@link #getSessionModifyResponse()}.
122      *
123      * @param responseProfile The response video profile.
124      */
125     @Override
onSendSessionModifyResponse(VideoProfile responseProfile)126     public void onSendSessionModifyResponse(VideoProfile responseProfile) {
127         mSessionModifyResponse = responseProfile;
128     }
129 
130     /**
131      * Responds to requests for camera capabilities by reporting the front camera capabilities.
132      */
133     @Override
onRequestCameraCapabilities()134     public void onRequestCameraCapabilities() {
135         handleCameraChange(CAMERA_FRONT);
136     }
137 
138     /**
139      * Responds to all requests for data usage by reporting {@link #DATA_USAGE}.
140      */
141     @Override
onRequestConnectionDataUsage()142     public void onRequestConnectionDataUsage() {
143         super.setCallDataUsage(DATA_USAGE);
144     }
145 
146     /**
147      * Stores pause image URIs received via the {@link VideoCall#setPauseImage(Uri)} API for
148      * retrieval via {@link #getPauseImage()}.
149      *
150      * @param uri URI of image to display.
151      */
152     @Override
onSetPauseImage(Uri uri)153     public void onSetPauseImage(Uri uri) {
154         mPauseImage = uri;
155     }
156 
157     /**
158      * Handles a change to the current camera selection.  Responds by reporting the capabilities of
159      * the camera.
160      */
handleCameraChange(String cameraId)161     private void handleCameraChange(String cameraId) {
162         if (CAMERA_FRONT.equals(cameraId)) {
163             super.changeCameraCapabilities(new VideoProfile.CameraCapabilities(
164                     CAMERA_FRONT_DIMENSIONS, CAMERA_FRONT_DIMENSIONS));
165         } else if (CAMERA_BACK.equals(cameraId)) {
166             super.changeCameraCapabilities(new VideoProfile.CameraCapabilities(
167                     CAMERA_BACK_DIMENSIONS, CAMERA_BACK_DIMENSIONS));
168         } else {
169             // If the camera is nulled, we will send back a "camera ready" event so that the unit
170             // test has something to wait for.
171             super.handleCallSessionEvent(VideoProvider.SESSION_EVENT_CAMERA_READY);
172         }
173     }
174 
175     /**
176      * Retrieves the last preview surface sent to the provider.
177      *
178      * @return the surface.
179      */
getPreviewSurface()180     public Surface getPreviewSurface() {
181         return mPreviewSurface;
182     }
183 
184     /**
185      * Retrieves the last display surface sent to the provider.
186      *
187      * @return the surface.
188      */
getDisplaySurface()189     public Surface getDisplaySurface() {
190         return mDisplaySurface;
191     }
192 
193     /**
194      * Retrieves the last device orientation sent to the provider.
195      *
196      * @return the orientation.
197      */
getDeviceOrientation()198     public int getDeviceOrientation() {
199         return mDeviceOrientation;
200     }
201 
202     /**
203      * Retrieves the last zoom sent to the provider.
204      *
205      * @return the zoom.
206      */
getZoom()207     public float getZoom() {
208         return mZoom;
209     }
210 
211     /**
212      * Retrieves the last session modify response sent to the provider.
213      *
214      * @return the session modify response.
215      */
getSessionModifyResponse()216     public VideoProfile getSessionModifyResponse() {
217         return mSessionModifyResponse;
218     }
219 
220     /**
221      * Retrieves the last pause image sent to the provider.
222      *
223      * @return the pause image URI.
224      */
getPauseImage()225     public Uri getPauseImage() {
226         return mPauseImage;
227     }
228 
229     /**
230      * Sends a mock session modify request via the provider.
231      */
sendMockSessionModifyRequest()232     public void sendMockSessionModifyRequest() {
233         super.receiveSessionModifyRequest(new VideoProfile(VideoProfile.STATE_BIDIRECTIONAL));
234     }
235 
236     /**
237      * Sends a mock session event via the provider.
238      *
239      * @param event the event.
240      */
sendMockSessionEvent(int event)241     public void sendMockSessionEvent(int event) {
242         super.handleCallSessionEvent(event);
243     }
244 
245     /**
246      * Sends a mock peer dimension change via the provider.
247      *
248      * @param width The new width.
249      * @param height The new height.
250      */
sendMockPeerDimensions(int width, int height)251     public void sendMockPeerDimensions(int width, int height) {
252         super.changePeerDimensions(width, height);
253     }
254 
255     /**
256      * Sends a mock video quality change via the provider.
257      *
258      * @param videoQuality the video quality.
259      */
sendMockVideoQuality(int videoQuality)260     public void sendMockVideoQuality(int videoQuality) {
261         super.changeVideoQuality(videoQuality);
262     }
263 }
264