summaryrefslogtreecommitdiff
path: root/android/sdl_android/src/androidTest/java/com/smartdevicelink/managers/screen/PresentAlertOperationTest.java
blob: 5f69010f18a652a5164abd09905f6ded8955260a (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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package com.smartdevicelink.managers.screen;

import android.content.Context;
import android.net.Uri;

import androidx.test.ext.junit.runners.AndroidJUnit4;

import com.livio.taskmaster.Task;
import com.smartdevicelink.managers.AlertCompletionListener;
import com.smartdevicelink.managers.ISdl;
import com.smartdevicelink.managers.file.FileManager;
import com.smartdevicelink.managers.file.MultipleFileCompletionListener;
import com.smartdevicelink.managers.file.filetypes.SdlArtwork;
import com.smartdevicelink.managers.file.filetypes.SdlFile;
import com.smartdevicelink.proxy.RPCRequest;
import com.smartdevicelink.proxy.rpc.Alert;
import com.smartdevicelink.proxy.rpc.AlertResponse;
import com.smartdevicelink.proxy.rpc.CancelInteraction;
import com.smartdevicelink.proxy.rpc.CancelInteractionResponse;
import com.smartdevicelink.proxy.rpc.ImageField;
import com.smartdevicelink.proxy.rpc.OnButtonEvent;
import com.smartdevicelink.proxy.rpc.OnButtonPress;
import com.smartdevicelink.proxy.rpc.SdlMsgVersion;
import com.smartdevicelink.proxy.rpc.SoftButtonCapabilities;
import com.smartdevicelink.proxy.rpc.TextField;
import com.smartdevicelink.proxy.rpc.WindowCapability;
import com.smartdevicelink.proxy.rpc.enums.FileType;
import com.smartdevicelink.proxy.rpc.enums.ImageFieldName;
import com.smartdevicelink.proxy.rpc.enums.SpeechCapabilities;
import com.smartdevicelink.proxy.rpc.enums.TextFieldName;
import com.smartdevicelink.test.TestValues;

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

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(AndroidJUnit4.class)
public class PresentAlertOperationTest {

    private PresentAlertOperation presentAlertOperation;
    private WindowCapability defaultMainWindowCapability;
    private AlertView alertView;
    private AlertAudioData alertAudioData;
    SdlArtwork testAlertArtwork, testSoftButtonArtwork;
    ISdl internalInterface;
    FileManager fileManager;
    SoftButtonState alertSoftButtonState;
    SoftButtonObject alertSoftButtonObject;
    private List<SpeechCapabilities> speechCapabilities;
    SdlFile testAudio;
    AlertCompletionListener alertCompletionListener;
    BaseAlertManager.AlertSoftButtonClearListener alertSoftButtonClearListener;

    private Answer<Void> onArtworkUploadSuccess = new Answer<Void>() {
        @Override
        public Void answer(InvocationOnMock invocation) {
            Object[] args = invocation.getArguments();
            MultipleFileCompletionListener listener = (MultipleFileCompletionListener) args[1];
            listener.onComplete(null);
            return null;
        }
    };

    private Answer<Void> onAlertSuccess = new Answer<Void>() {
        @Override
        public Void answer(InvocationOnMock invocation) {
            Object[] args = invocation.getArguments();
            RPCRequest message = (RPCRequest) args[0];
            if (message instanceof Alert) {
                int correlationId = message.getCorrelationID();
                AlertResponse alertResponse = new AlertResponse();
                alertResponse.setSuccess(true);
                message.getOnRPCResponseListener().onResponse(correlationId, alertResponse);
            }
            return null;
        }
    };

    private Answer<Void> onCancelAlert = new Answer<Void>() {
        @Override
        public Void answer(InvocationOnMock invocation) {
            Object[] args = invocation.getArguments();
            RPCRequest message = (RPCRequest) args[0];
            if (message instanceof CancelInteraction) {
                int correlationId = message.getCorrelationID();
                CancelInteractionResponse cancelInteraction = new CancelInteractionResponse();
                cancelInteraction.setSuccess(true);
                message.getOnRPCResponseListener().onResponse(correlationId, cancelInteraction);
            }
            return null;
        }
    };

    Task task;
    @Before
    public void setUp() throws Exception {
        Context mTestContext = getInstrumentation().getContext();
        // mock things
        internalInterface = mock(ISdl.class);
        fileManager = mock(FileManager.class);
        task = mock(Task.class);

        alertSoftButtonClearListener = new BaseAlertManager.AlertSoftButtonClearListener() {
            @Override
            public void onButtonClear(List<SoftButtonObject> softButtonObjects) {

            }
        };
        testAlertArtwork = new SdlArtwork();
        testAlertArtwork.setName("testArtwork1");
        Uri uri1 = Uri.parse("android.resource://" + mTestContext.getPackageName() + "/drawable/ic_sdl");
        testAlertArtwork.setUri(uri1);
        testAlertArtwork.setType(FileType.GRAPHIC_PNG);

        testSoftButtonArtwork = new SdlArtwork();
        Uri uri2 = Uri.parse("android.resource://" + mTestContext.getPackageName() + "drawable-hdpi/sdl_lockscreen_icon.png");
        testSoftButtonArtwork.setName("testArtwork2");
        testSoftButtonArtwork.setUri(uri2);
        testSoftButtonArtwork.setType(FileType.GRAPHIC_PNG);

        Uri uri3 = Uri.parse("android.resource://" + mTestContext.getPackageName() + "/raw/test_audio_square_250hz_80amp_1s.mp3");
        testAudio = new SdlFile("TestAudioFile", FileType.AUDIO_MP3, uri3, false);

        alertAudioData = new AlertAudioData("Spoken Sting");
        alertAudioData.setPlayTone(true);
        alertAudioData.addAudioFiles(Collections.singletonList(testAudio));

        alertSoftButtonState = new SoftButtonState("state1", "State 1", testSoftButtonArtwork);
        SoftButtonObject.OnEventListener onEventListener = new SoftButtonObject.OnEventListener() {
            @Override
            public void onPress(SoftButtonObject softButtonObject, OnButtonPress onButtonPress) {

            }

            @Override
            public void onEvent(SoftButtonObject softButtonObject, OnButtonEvent onButtonEvent) {

            }
        };
        alertSoftButtonObject = new SoftButtonObject("Soft button 1", alertSoftButtonState, onEventListener);

        AlertView.Builder builder = new AlertView.Builder();
        builder.setText("test");
        builder.setSecondaryText("secondaryText");
        builder.setTertiaryText("tertiaryText");
        builder.setAudio(alertAudioData);
        builder.setIcon(testAlertArtwork);
        builder.setDefaultTimeOut(10);
        builder.setTimeout(5);
        builder.setSoftButtons(Collections.singletonList(alertSoftButtonObject));
        builder.setShowWaitIndicator(true);
        alertView = builder.build();

        defaultMainWindowCapability = getWindowCapability(3);
        speechCapabilities = new ArrayList<SpeechCapabilities>();
        speechCapabilities.add(SpeechCapabilities.FILE);
        alertCompletionListener = new AlertCompletionListener() {
            @Override
            public void onComplete(boolean success, Integer tryAgainTime) {

            }
        };
        presentAlertOperation = new PresentAlertOperation(internalInterface, alertView, defaultMainWindowCapability, speechCapabilities, fileManager, 1, alertCompletionListener, alertSoftButtonClearListener);
        when(fileManager.fileNeedsUpload(any(SdlFile.class))).thenReturn(true);
    }

    @Test
    public void testPresentAlertTruncatedText() {
        doAnswer(onAlertSuccess).when(internalInterface).sendRPC(any(Alert.class));
        // Same response works for uploading artworks as it does for files

        when(internalInterface.getSdlMsgVersion()).thenReturn(new SdlMsgVersion(6, 0));
        WindowCapability windowCapability = getWindowCapability(1);
        PresentAlertOperation presentAlertOperation = new PresentAlertOperation(internalInterface, alertView, windowCapability, speechCapabilities, fileManager, 1, alertCompletionListener, alertSoftButtonClearListener);
        Alert alert = presentAlertOperation.alertRpc();

        assertEquals(alert.getAlertText1(), alertView.getText() + " - " + alertView.getSecondaryText() + " - " + alertView.getTertiaryText());

        windowCapability = getWindowCapability(2);

        presentAlertOperation = new PresentAlertOperation(internalInterface, alertView, windowCapability, speechCapabilities, fileManager, 1, alertCompletionListener, alertSoftButtonClearListener);
        alert = presentAlertOperation.alertRpc();
        assertEquals(alert.getAlertText1(), alertView.getText());
        assertEquals(alert.getAlertText2(), alertView.getSecondaryText() + " - " + alertView.getTertiaryText());
    }

    @Test
    public void testPresentAlertHappyPath() {
        doAnswer(onAlertSuccess).when(internalInterface).sendRPC(any(Alert.class));
        // Same response works for uploading artworks as it does for files
        doAnswer(onArtworkUploadSuccess).when(fileManager).uploadArtworks(any(List.class), any(MultipleFileCompletionListener.class));
        doAnswer(onArtworkUploadSuccess).when(fileManager).uploadFiles(any(List.class), any(MultipleFileCompletionListener.class));
        when(internalInterface.getSdlMsgVersion()).thenReturn(new SdlMsgVersion(6, 0));

        // Test Images need to be uploaded, sending text and uploading images
        presentAlertOperation.onExecute();

        // Verifies that uploadArtworks gets called only with the fist presentAlertOperation.onExecute call
        verify(fileManager, times(1)).uploadArtworks(any(List.class), any(MultipleFileCompletionListener.class));

        verify(fileManager, times(1)).uploadFiles(any(List.class), any(MultipleFileCompletionListener.class));

        verify(internalInterface, times(1)).sendRPC(any(Alert.class));
    }

    @Test
    public void testPresentAlertNoAudioAndArtwork() {
        doAnswer(onAlertSuccess).when(internalInterface).sendRPC(any(Alert.class));

        AlertView.Builder builder = new AlertView.Builder();
        builder.setText("Hi");
        builder.build();
        AlertView alertView1 = builder.build();

        presentAlertOperation = new PresentAlertOperation(internalInterface, alertView1, defaultMainWindowCapability, speechCapabilities, fileManager, 2, alertCompletionListener, alertSoftButtonClearListener);

        when(internalInterface.getSdlMsgVersion()).thenReturn(new SdlMsgVersion(6, 0));

        // Test Images need to be uploaded, sending text and uploading images
        presentAlertOperation.onExecute();

        // Verifies that uploadArtworks gets called only with the fist presentAlertOperation.onExecute call
        verify(fileManager, times(0)).uploadArtworks(any(List.class), any(MultipleFileCompletionListener.class));
        verify(fileManager, times(0)).uploadFiles(any(List.class), any(MultipleFileCompletionListener.class));

        verify(internalInterface, times(1)).sendRPC(any(Alert.class));
    }

    @Test
    public void testPresentAlertNoImages() {
        doAnswer(onAlertSuccess).when(internalInterface).sendRPC(any(Alert.class));
        // Same response works for uploading artworks as it does for files
        doAnswer(onArtworkUploadSuccess).when(fileManager).uploadArtworks(any(List.class), any(MultipleFileCompletionListener.class));
        doAnswer(onArtworkUploadSuccess).when(fileManager).uploadFiles(any(List.class), any(MultipleFileCompletionListener.class));

        when(internalInterface.getSdlMsgVersion()).thenReturn(new SdlMsgVersion(6, 0));

        // Test Images need to be uploaded, sending text and uploading images
        presentAlertOperation.onExecute();

        // Verifies that uploadArtworks gets called only with the fist presentAlertOperation.onExecute call
        verify(fileManager, times(1)).uploadArtworks(any(List.class), any(MultipleFileCompletionListener.class));
        verify(internalInterface, times(1)).sendRPC(any(Alert.class));
    }

    @Test
    public void testCancelOperation() {
        //Cancel right away
        presentAlertOperation.cancelTask();
        presentAlertOperation.onExecute();
        verify(internalInterface, times(0)).sendRPC(any(Alert.class));
    }

    private WindowCapability getWindowCapability(int numberOfAlertFields) {
        TextField alertText1 = new TextField();
        alertText1.setName(TextFieldName.alertText1);
        TextField alertText2 = new TextField();
        alertText2.setName(TextFieldName.alertText2);
        TextField alertText3 = new TextField();
        alertText3.setName(TextFieldName.alertText3);
        TextField mainField4 = new TextField();
        mainField4.setName(TextFieldName.mainField4);

        List<TextField> textFieldList = new ArrayList<>();

        textFieldList.add(alertText1);
        textFieldList.add(alertText2);
        textFieldList.add(alertText3);

        List<TextField> returnList = new ArrayList<>();

        if (numberOfAlertFields > 0) {
            for (int i = 0; i < numberOfAlertFields; i++) {
                returnList.add(textFieldList.get(i));
            }
        }

        WindowCapability windowCapability = new WindowCapability();
        windowCapability.setTextFields(returnList);

        ImageField imageField = new ImageField();
        imageField.setName(ImageFieldName.alertIcon);
        List<ImageField> imageFieldList = new ArrayList<>();
        imageFieldList.add(imageField);
        windowCapability.setImageFields(imageFieldList);

        windowCapability.setImageFields(imageFieldList);

        SoftButtonCapabilities softButtonCapabilities = new SoftButtonCapabilities();
        softButtonCapabilities.setImageSupported(TestValues.GENERAL_BOOLEAN);
        softButtonCapabilities.setShortPressAvailable(TestValues.GENERAL_BOOLEAN);
        softButtonCapabilities.setLongPressAvailable(TestValues.GENERAL_BOOLEAN);
        softButtonCapabilities.setUpDownAvailable(TestValues.GENERAL_BOOLEAN);
        softButtonCapabilities.setTextSupported(TestValues.GENERAL_BOOLEAN);

        windowCapability.setSoftButtonCapabilities(Collections.singletonList(softButtonCapabilities));
        return windowCapability;
    }
}