summaryrefslogtreecommitdiff
path: root/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/proxy/SdlProxyBaseTests.java
blob: 92670c7d48a59f59d2bc41006bea3dad4c6719f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package com.smartdevicelink.test.proxy;

import android.content.Context;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.smartdevicelink.exception.SdlException;
import com.smartdevicelink.exception.SdlExceptionCause;
import com.smartdevicelink.proxy.RPCMessage;
import com.smartdevicelink.proxy.RPCRequest;
import com.smartdevicelink.proxy.RPCResponse;
import com.smartdevicelink.proxy.SdlProxyALM;
import com.smartdevicelink.proxy.SdlProxyBase;
import com.smartdevicelink.proxy.SdlProxyBuilder;
import com.smartdevicelink.proxy.SdlProxyConfigurationResources;
import com.smartdevicelink.proxy.interfaces.IProxyListenerALM;
import com.smartdevicelink.proxy.rpc.GenericResponse;
import com.smartdevicelink.proxy.rpc.Show;
import com.smartdevicelink.proxy.rpc.ShowResponse;
import com.smartdevicelink.proxy.rpc.Speak;
import com.smartdevicelink.proxy.rpc.SpeakResponse;
import com.smartdevicelink.proxy.rpc.enums.Result;
import com.smartdevicelink.proxy.rpc.listeners.OnMultipleRequestListener;
import com.smartdevicelink.proxy.rpc.listeners.OnRPCResponseListener;
import com.smartdevicelink.streaming.video.SdlRemoteDisplay;
import com.smartdevicelink.streaming.video.VideoStreamingParameters;
import com.smartdevicelink.test.streaming.video.SdlRemoteDisplayTest;

import junit.framework.Assert;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertNotNull;
import static junit.framework.TestCase.assertTrue;
import static junit.framework.TestCase.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;

@RunWith(AndroidJUnit4.class)
public class SdlProxyBaseTests {
    public static final String TAG = "SdlProxyBaseTests";

    int onUpdateListenerCounter, onFinishedListenerCounter, onResponseListenerCounter, onErrorListenerCounter, remainingRequestsExpected;

    /**
     * Test SdlProxyBase for handling null SdlProxyConfigurationResources
     */
    @Test
    public void testNullSdlProxyConfigurationResources() {
        SdlProxyALM proxy = null;
        SdlProxyBuilder.Builder builder = new SdlProxyBuilder.Builder(mock(IProxyListenerALM.class), "appId", "appName", true, getInstrumentation().getTargetContext());
        SdlProxyConfigurationResources config = new SdlProxyConfigurationResources("path", (TelephonyManager) getInstrumentation().getTargetContext().getSystemService(Context.TELEPHONY_SERVICE));
        //Construct with a non-null SdlProxyConfigurationResources
        builder.setSdlProxyConfigurationResources(config);
        try {
            proxy = builder.build();
        } catch (Exception e) {
            Log.v(TAG, "Exception in testNullSdlProxyConfigurationResources, testing non null SdlProxyConfigurationResources");
            if (!(e instanceof SdlException) || !((SdlException) e).getSdlExceptionCause().equals(SdlExceptionCause.BLUETOOTH_ADAPTER_NULL)) {
                e.printStackTrace();
                Assert.fail("Exception in testNullSdlProxyConfigurationResources - \n" + e.toString());
            }
        }

        if (proxy != null) {
            try {
                proxy.dispose();
                proxy = null;
            }catch(SdlException e){
                e.printStackTrace();
            }
        }

        //Construct with a null SdlProxyConfigurationResources
        builder.setSdlProxyConfigurationResources(null);
        try {
            proxy = builder.build();
        } catch (Exception e) {
            Log.v(TAG, "Exception in testNullSdlProxyConfigurationResources, testing null SdlProxyConfigurationResources");
            if (!(e instanceof SdlException) || !((SdlException) e).getSdlExceptionCause().equals(SdlExceptionCause.BLUETOOTH_ADAPTER_NULL)) {
                e.printStackTrace();
                Assert.fail("Exception in testNullSdlProxyConfigurationResources, testing null SdlProxyConfigurationResources");
            }
        }
        if (proxy != null) {
            try {
                proxy.dispose();
                proxy = null;
            }catch(SdlException e){
                e.printStackTrace();
            }
        }

        //Construct with a non-null SdlProxyConfigurationResources and a null TelephonyManager
        config.setTelephonyManager(null);
        builder.setSdlProxyConfigurationResources(config);
        try {
            proxy = builder.build();
        } catch (Exception e) {
            Log.v(TAG, "Exception in testNullSdlProxyConfigurationResources, testing null TelephonyManager");
            if (!(e instanceof SdlException) || !((SdlException) e).getSdlExceptionCause().equals(SdlExceptionCause.BLUETOOTH_ADAPTER_NULL)) {
                Assert.fail("Exception in testNullSdlProxyConfigurationResources, testing null TelephonyManager");
            }
        }
        if (proxy != null) {
            try {
                proxy.dispose();
                proxy = null;
            }catch(SdlException e){
                e.printStackTrace();
            }
        }
    }

    @Test
    public void testRemoteDisplayStreaming(){
        SdlProxyALM proxy = null;
        SdlProxyBuilder.Builder builder = new SdlProxyBuilder.Builder(mock(IProxyListenerALM.class), "appId", "appName", true, getInstrumentation().getTargetContext());
        try{
            proxy = builder.build();
            //	public void startRemoteDisplayStream(Context context, final Class<? extends SdlRemoteDisplay> remoteDisplay, final VideoStreamingParameters parameters, final boolean encrypted){
            Method m = SdlProxyALM.class.getDeclaredMethod("startRemoteDisplayStream", Context.class, SdlRemoteDisplay.class, VideoStreamingParameters.class, boolean.class);
            assertNotNull(m);
            m.setAccessible(true);
            m.invoke(proxy,getInstrumentation().getTargetContext(), SdlRemoteDisplayTest.MockRemoteDisplay.class, (VideoStreamingParameters)null, false);
            assert true;

        }catch (Exception e){
            assert false;
        }
    }

    @Test
    public void testSendRPCsAllSucceed(){
        testSendMultipleRPCs(false, 1);
    }

    @Test
    public void testSendRPCsSomeFail(){
        testSendMultipleRPCs(false, 2);
    }

    @Test
    public void testSendSequentialRPCsAllSucceed(){
        testSendMultipleRPCs(true, 1);
    }

    @Test
    public void testSendSequentialRPCsSomeFail(){
        testSendMultipleRPCs(true, 2);
    }

    private void testSendMultipleRPCs(boolean sequentialSend, int caseNumber){
        final List<RPCRequest> rpcsList = new ArrayList<>();
        final List<RPCRequest> rpcsTempList = new ArrayList<>();
        final HashMap<RPCRequest, OnRPCResponseListener> requestsMap = new HashMap<>();
        onUpdateListenerCounter = 0;
        onFinishedListenerCounter = 0;
        onResponseListenerCounter = 0;
        onErrorListenerCounter = 0;


        // We extend the SdlProxyBase to be able to override getIsConnected() &  sendRPCMessagePrivate() methods so they don't cause issues when trying to send RPCs
        // Because otherwise, they will throw exception cause there not actual connection to head unit
        SdlProxyBase proxy = new SdlProxyBase() {

            @Override
            public Boolean getIsConnected() {
                return true;
            }

            @Override
            protected void sendRPCMessagePrivate(RPCMessage message) {
                // Do nothing
            }
        };


        // We need to get list of all OnRPCResponseListeners so we can trigger onResponse/onError for each RPC to fake a response from Core
        final Answer<Void> answer = new Answer<Void>() {
            @Override
            public Void answer(InvocationOnMock invocation) {
                Object[] args = invocation.getArguments();
                RPCRequest request = (RPCRequest) invocation.getMock();
                OnRPCResponseListener listener = (OnRPCResponseListener) args[0];
                requestsMap.put(request, listener);
                rpcsTempList.add(request);
                return null;
            }
        };


        // Prepare RPCs to send
        Speak speak = mock(Speak.class);
        doReturn(RPCMessage.KEY_REQUEST).when(speak).getMessageType();
        doAnswer(answer).when(speak).setOnRPCResponseListener(any(OnRPCResponseListener.class));
        rpcsList.add(speak);

        Show show = mock(Show.class);
        doReturn(RPCMessage.KEY_REQUEST).when(show).getMessageType();
        doAnswer(answer).when(show).setOnRPCResponseListener(any(OnRPCResponseListener.class));
        rpcsList.add(show);


        // Send RPCs
        remainingRequestsExpected = rpcsList.size();
        OnMultipleRequestListener onMultipleRequestListener = new OnMultipleRequestListener() {
            @Override
            public void onUpdate(int remainingRequests) {
                onUpdateListenerCounter++;
                assertEquals(remainingRequestsExpected, remainingRequests);
            }

            @Override
            public void onFinished() {
                onFinishedListenerCounter++;
            }

            @Override
            public void onResponse(int correlationId, RPCResponse response) {
                if (response.getSuccess()) {
                    onResponseListenerCounter++;
                    remainingRequestsExpected--;
                } else {
                    onErrorListenerCounter++;
                    remainingRequestsExpected--;
                }
            }
        };
        try {
            if (sequentialSend) {
                proxy.sendSequentialRequests(rpcsList, onMultipleRequestListener);
            } else {
                proxy.sendRequests(rpcsList, onMultipleRequestListener);
            }
            assertTrue(true);
        } catch (SdlException e) {
            e.printStackTrace();
            fail();
        }


        // Trigger fake RPC responses
        int onUpdateListenerCounterExpected = 0, onFinishedListenerCounterExpected = 0, onResponseListenerCounterExpected = 0, onErrorListenerCounterExpected = 0;
        switch (caseNumber){
            case 1: // All RPCs succeed
                onUpdateListenerCounterExpected = 2;
                onFinishedListenerCounterExpected = 1;
                onResponseListenerCounterExpected = 2;
                onErrorListenerCounterExpected = 0;

                while (rpcsTempList.size() != 0){
                    RPCRequest request = rpcsTempList.remove(0);
                    if (request instanceof Speak) {
                        requestsMap.get(request).onResponse(request.getCorrelationID(), new SpeakResponse(true, Result.SUCCESS));
                    } else if (request instanceof Show) {
                        requestsMap.get(request).onResponse(request.getCorrelationID(), new ShowResponse(true, Result.SUCCESS));
                    }
                }
                break;
            case 2: // Some RPCs fail
                onUpdateListenerCounterExpected = 2;
                onFinishedListenerCounterExpected = 1;
                onResponseListenerCounterExpected = 1;
                onErrorListenerCounterExpected = 1;

                while (rpcsTempList.size() != 0){
                    RPCRequest request = rpcsTempList.remove(0);
                    if (request instanceof Speak) {
                        requestsMap.get(request).onResponse(request.getCorrelationID(), new GenericResponse(false, Result.DISALLOWED));
                    } else if (request instanceof Show) {
                        requestsMap.get(request).onResponse(request.getCorrelationID(), new ShowResponse(true, Result.SUCCESS));
                    }
                }
                break;
        }


        // Make sure the listener is called correctly
        assertEquals("onUpdate Listener was not called or called more/less frequently than expected", onUpdateListenerCounterExpected, onUpdateListenerCounter);
        assertEquals("onFinished Listener was not called or called more/less frequently than expected", onFinishedListenerCounterExpected, onFinishedListenerCounter);
        assertEquals("onResponse Listener was not called or called more/less frequently than expected", onResponseListenerCounterExpected, onResponseListenerCounter);
        assertEquals("onError Listener was not called or called more/less frequently than expected", onErrorListenerCounterExpected, onErrorListenerCounter);
    }
}