summaryrefslogtreecommitdiff
path: root/android/sdl_android/src/androidTest/java/com/smartdevicelink/managers/screen/choiceset/PreloadPresentChoicesOperationTests.java
blob: 0f3725f5397fce6a86cf9ac5bc5237ae8f003dde (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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
package com.smartdevicelink.managers.screen.choiceset;

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

import com.livio.taskmaster.Queue;
import com.livio.taskmaster.Task;
import com.livio.taskmaster.Taskmaster;
import com.smartdevicelink.managers.ISdl;
import com.smartdevicelink.managers.file.FileManager;
import com.smartdevicelink.managers.file.filetypes.SdlArtwork;
import com.smartdevicelink.managers.file.filetypes.SdlFile;
import com.smartdevicelink.protocol.enums.FunctionID;
import com.smartdevicelink.proxy.RPCResponse;
import com.smartdevicelink.proxy.rpc.CancelInteraction;
import com.smartdevicelink.proxy.rpc.ImageField;
import com.smartdevicelink.proxy.rpc.KeyboardProperties;
import com.smartdevicelink.proxy.rpc.PerformInteraction;
import com.smartdevicelink.proxy.rpc.SdlMsgVersion;
import com.smartdevicelink.proxy.rpc.TextField;
import com.smartdevicelink.proxy.rpc.WindowCapability;
import com.smartdevicelink.proxy.rpc.enums.CharacterSet;
import com.smartdevicelink.proxy.rpc.enums.FileType;
import com.smartdevicelink.proxy.rpc.enums.ImageFieldName;
import com.smartdevicelink.proxy.rpc.enums.ImageType;
import com.smartdevicelink.proxy.rpc.enums.InteractionMode;
import com.smartdevicelink.proxy.rpc.enums.KeyboardLayout;
import com.smartdevicelink.proxy.rpc.enums.KeypressMode;
import com.smartdevicelink.proxy.rpc.enums.Language;
import com.smartdevicelink.proxy.rpc.enums.LayoutMode;
import com.smartdevicelink.proxy.rpc.enums.TextFieldName;
import com.smartdevicelink.test.TestValues;
import com.smartdevicelink.util.Version;

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.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;

import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertFalse;
import static junit.framework.TestCase.assertNotNull;
import static junit.framework.TestCase.assertNull;
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.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(AndroidJUnit4.class)
public class PreloadPresentChoicesOperationTests {

    private PreloadPresentChoicesOperation preloadChoicesOperation;
    private PreloadPresentChoicesOperation preloadChoicesOperationNullCapability;
    private PreloadPresentChoicesOperation preloadChoicesOperationEmptyCapability;

    private PreloadPresentChoicesOperation presentChoicesOperation;
    private ChoiceSet choiceSet;
    private ISdl internalInterface;
    private FileManager fileManager;
    private KeyboardListener keyboardListener;
    private ChoiceSetSelectionListener choiceSetSelectionListener;

    private Taskmaster taskmaster;
    private Queue queue;

    @Before
    public void setUp() throws Exception {
        ChoiceCell cell1 = new ChoiceCell("cell 1");
        ChoiceCell cell2 = new ChoiceCell("cell 2", null, TestValues.GENERAL_ARTWORK);
        LinkedHashSet<ChoiceCell> cellsToPreload = new LinkedHashSet<>();
        cellsToPreload.add(cell1);
        cellsToPreload.add(cell2);

        ImageField imageField = new ImageField(ImageFieldName.choiceImage, Arrays.asList(FileType.GRAPHIC_PNG, FileType.GRAPHIC_JPEG));
        ImageField imageField2 = new ImageField();
        imageField2.setName(ImageFieldName.choiceSecondaryImage);
        TextField textField = new TextField(TextFieldName.menuName, CharacterSet.CID1SET, 2, 2);

        TextField textField2 = new TextField();
        TextField textField3 = new TextField();

        textField2.setName(TextFieldName.secondaryText);
        textField3.setName(TextFieldName.tertiaryText);


        WindowCapability windowCapability = new WindowCapability();
        windowCapability.setImageFields(Arrays.asList(imageField, imageField2));
        windowCapability.setImageTypeSupported(Arrays.asList(ImageType.STATIC, ImageType.DYNAMIC));
        windowCapability.setTextFields(Arrays.asList(textField, textField2, textField3));

        internalInterface = mock(ISdl.class);
        fileManager = mock(FileManager.class);

        // We still want the mock fileManager to use the real implementation for fileNeedsUpload()
        when(fileManager.fileNeedsUpload(any(SdlFile.class))).thenCallRealMethod();

        preloadChoicesOperation = new PreloadPresentChoicesOperation(internalInterface, fileManager, null, windowCapability, true, cellsToPreload, null, null);

        keyboardListener = mock(KeyboardListener.class);
        choiceSetSelectionListener = mock(ChoiceSetSelectionListener.class);

        ChoiceCell cell = new ChoiceCell("Cell1");
        cell.setChoiceId(0);
        choiceSet = new ChoiceSet("Test", Collections.singletonList(cell), choiceSetSelectionListener);

        taskmaster = new Taskmaster.Builder().build();
        queue = taskmaster.createQueue("test", 100, false);
        taskmaster.start();
    }

    private KeyboardProperties getKeyBoardProperties() {
        KeyboardProperties properties = new KeyboardProperties();
        properties.setLanguage(Language.EN_US);
        properties.setKeyboardLayout(KeyboardLayout.QWERTZ);
        properties.setKeypressMode(KeypressMode.RESEND_CURRENT_ENTRY);
        return properties;
    }

    /**
     * Sets up PreloadChoicesOperation with WindowCapability being null
     */
    public void setUpNullWindowCapability() {

        ChoiceCell cell1 = new ChoiceCell("cell 1");
        ChoiceCell cell2 = new ChoiceCell("cell 2", null, TestValues.GENERAL_ARTWORK);
        LinkedHashSet<ChoiceCell> cellsToPreload = new LinkedHashSet<>();
        cellsToPreload.add(cell1);
        cellsToPreload.add(cell2);

        ISdl internalInterface = mock(ISdl.class);
        preloadChoicesOperationNullCapability = new PreloadPresentChoicesOperation(internalInterface, fileManager, null, null, true, cellsToPreload, null, null);
    }

    /**
     * Sets up PreloadChoicesOperation with an Capability not being set
     * certain imageFields and TextFields
     */
    public void setUpEmptyWindowCapability() {

        ChoiceCell cell1 = new ChoiceCell("cell 1");
        ChoiceCell cell2 = new ChoiceCell("cell 2", null, TestValues.GENERAL_ARTWORK);
        LinkedHashSet<ChoiceCell> cellsToPreload = new LinkedHashSet<>();
        cellsToPreload.add(cell1);
        cellsToPreload.add(cell2);

        ImageField imageField = new ImageField();
        imageField.setName(ImageFieldName.alertIcon);

        TextField textField = new TextField();
        textField.setName(TextFieldName.mainField1);

        WindowCapability windowCapability = new WindowCapability();
        windowCapability.setImageFields(Collections.singletonList(imageField));
        windowCapability.setTextFields(Collections.singletonList(textField));

        ISdl internalInterface = mock(ISdl.class);
        preloadChoicesOperationEmptyCapability = new PreloadPresentChoicesOperation(internalInterface, fileManager, null, windowCapability, true, cellsToPreload, null, null);
    }

    @Test
    public void testArtworksToUpload() {
        HashSet<SdlArtwork> artworksToUpload = preloadChoicesOperation.artworksToUpload();
        assertNotNull(artworksToUpload);
        assertEquals(artworksToUpload.size(), 1);
    }

    /**
     * Testing shouldSend method's with varying WindowCapability set.
     */
    @Test
    public void testShouldSendText() {

        setUpNullWindowCapability();
        assertTrue(preloadChoicesOperationNullCapability.shouldSendChoicePrimaryImage());
        assertTrue(preloadChoicesOperationNullCapability.shouldSendChoiceSecondaryImage());
        assertTrue(preloadChoicesOperationNullCapability.shouldSendChoiceSecondaryText());
        assertTrue(preloadChoicesOperationNullCapability.shouldSendChoiceTertiaryText());
        assertTrue(preloadChoicesOperationNullCapability.shouldSendChoiceText());


        assertTrue(preloadChoicesOperation.shouldSendChoicePrimaryImage());
        assertTrue(preloadChoicesOperation.shouldSendChoiceSecondaryImage());
        assertTrue(preloadChoicesOperation.shouldSendChoiceSecondaryText());
        assertTrue(preloadChoicesOperation.shouldSendChoiceTertiaryText());
        assertTrue(preloadChoicesOperation.shouldSendChoiceText());

        setUpEmptyWindowCapability();
        assertFalse(preloadChoicesOperationEmptyCapability.shouldSendChoicePrimaryImage());
        assertFalse(preloadChoicesOperationEmptyCapability.shouldSendChoiceSecondaryImage());
        assertFalse(preloadChoicesOperationEmptyCapability.shouldSendChoiceSecondaryText());
        assertFalse(preloadChoicesOperationEmptyCapability.shouldSendChoiceTertiaryText());
        assertFalse(preloadChoicesOperationEmptyCapability.shouldSendChoiceText());
    }


    @Test
    public void testGetLayoutMode() {
        when(internalInterface.getSdlMsgVersion()).thenReturn(new SdlMsgVersion(7, 1));
        // First we will check knowing our keyboard listener is NOT NULL
        WindowCapability windowCapability = new WindowCapability();
        HashSet<ChoiceCell> loadedCells = new HashSet<>();
        presentChoicesOperation = new PreloadPresentChoicesOperation(internalInterface, fileManager, choiceSet, InteractionMode.MANUAL_ONLY, getKeyBoardProperties(), keyboardListener, TestValues.GENERAL_INTEGER, null, windowCapability, true, loadedCells, null, null);

        assertEquals(presentChoicesOperation.getLayoutMode(), LayoutMode.LIST_WITH_SEARCH);
        presentChoicesOperation.keyboardListener = null;
        assertEquals(presentChoicesOperation.getLayoutMode(), LayoutMode.LIST_ONLY);
    }

    @Test
    public void testGetPerformInteraction() {
        when(internalInterface.getSdlMsgVersion()).thenReturn(new SdlMsgVersion(7, 1));
        WindowCapability windowCapability = new WindowCapability();
        HashSet<ChoiceCell> loadedCells = new HashSet<>();
        presentChoicesOperation = new PreloadPresentChoicesOperation(internalInterface, fileManager, choiceSet, InteractionMode.MANUAL_ONLY, getKeyBoardProperties(), keyboardListener, TestValues.GENERAL_INTEGER, null, windowCapability, true, loadedCells, null, null);

        PerformInteraction pi = presentChoicesOperation.getPerformInteraction();
        assertEquals(pi.getInitialText(), "Test");
        assertNull(pi.getHelpPrompt());
        assertNull(pi.getTimeoutPrompt());
        assertNull(pi.getVrHelp());
        assertEquals(pi.getTimeout(), Integer.valueOf(10000));
        assertEquals(pi.getCancelID(), TestValues.GENERAL_INTEGER);
        assertEquals(presentChoicesOperation.getLayoutMode(), LayoutMode.LIST_WITH_SEARCH);
    }

    @Test
    public void testSetSelectedCellWithId() {
        when(internalInterface.getSdlMsgVersion()).thenReturn(new SdlMsgVersion(7, 1));
        WindowCapability windowCapability = new WindowCapability();
        HashSet<ChoiceCell> loadedCells = new HashSet<>();
        presentChoicesOperation = new PreloadPresentChoicesOperation(internalInterface, fileManager, choiceSet, InteractionMode.MANUAL_ONLY, getKeyBoardProperties(), keyboardListener, TestValues.GENERAL_INTEGER, null, windowCapability, true, loadedCells, null, null);

        assertNull(presentChoicesOperation.selectedCellRow);
        presentChoicesOperation.setSelectedCellWithId(0);
        assertEquals(presentChoicesOperation.selectedCellRow, Integer.valueOf(0));
    }

    private void sleep() {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testCancelingChoiceSetSuccessfullyIfThreadIsRunning() {
        when(internalInterface.getSdlMsgVersion()).thenReturn(new SdlMsgVersion(6, 0));
        WindowCapability windowCapability = new WindowCapability();
        HashSet<ChoiceCell> loadedCells = new HashSet<>();
        BaseChoiceSetManager.ChoicesOperationCompletionListener listener = new BaseChoiceSetManager.ChoicesOperationCompletionListener() {
            @Override
            public void onComplete(boolean success, HashSet<ChoiceCell> loadedChoiceCells) {
                choiceSet.cancel();
                sleep();

                verify(internalInterface, times(1)).sendRPC(any(CancelInteraction.class));
                verify(internalInterface, times(1)).sendRPC(any(PerformInteraction.class));

                assertEquals(Task.IN_PROGRESS, presentChoicesOperation.getState());
            }
        };
        presentChoicesOperation = new PreloadPresentChoicesOperation(internalInterface, fileManager, choiceSet, InteractionMode.MANUAL_ONLY, null, null, TestValues.GENERAL_INTEGER, null, windowCapability, true, loadedCells, listener, null);
        presentChoicesOperation.setLoadedCells(new HashSet<ChoiceCell>());
        queue.add(presentChoicesOperation, false);

        sleep();

        assertEquals(Task.IN_PROGRESS, presentChoicesOperation.getState());

        Answer<Void> cancelInteractionAnswer = new Answer<Void>() {
            @Override
            public Void answer(InvocationOnMock invocation) {
                Object[] args = invocation.getArguments();
                CancelInteraction cancelInteraction = (CancelInteraction) args[0];

                assertEquals(cancelInteraction.getCancelID(), TestValues.GENERAL_INTEGER);
                assertEquals(cancelInteraction.getInteractionFunctionID().intValue(), FunctionID.PERFORM_INTERACTION.getId());

                RPCResponse response = new RPCResponse(FunctionID.CANCEL_INTERACTION.toString());
                response.setSuccess(true);
                cancelInteraction.getOnRPCResponseListener().onResponse(0, response);

                return null;
            }
        };
        doAnswer(cancelInteractionAnswer).when(internalInterface).sendRPC(any(CancelInteraction.class));
    }

    @Test
    public void testCancelingChoiceSetUnsuccessfullyIfThreadIsRunning() {
        when(internalInterface.getSdlMsgVersion()).thenReturn(new SdlMsgVersion(6, 0));
        WindowCapability windowCapability = new WindowCapability();
        HashSet<ChoiceCell> loadedCells = new HashSet<>();
        BaseChoiceSetManager.ChoicesOperationCompletionListener listener = new BaseChoiceSetManager.ChoicesOperationCompletionListener() {
            @Override
            public void onComplete(boolean success, HashSet<ChoiceCell> loadedChoiceCells) {
                choiceSet.cancel();
                sleep();

                verify(internalInterface, times(1)).sendRPC(any(CancelInteraction.class));
                verify(internalInterface, times(1)).sendRPC(any(PerformInteraction.class));

                assertEquals(Task.IN_PROGRESS, presentChoicesOperation.getState());
            }
        };
        presentChoicesOperation = new PreloadPresentChoicesOperation(internalInterface, fileManager, choiceSet, InteractionMode.MANUAL_ONLY, null, null, TestValues.GENERAL_INTEGER, null, windowCapability, true, loadedCells, listener, null);
        presentChoicesOperation.setLoadedCells(new HashSet<ChoiceCell>());
        queue.add(presentChoicesOperation, false);
        sleep();

        assertEquals(Task.IN_PROGRESS, presentChoicesOperation.getState());

        Answer<Void> cancelInteractionAnswer = new Answer<Void>() {
            @Override
            public Void answer(InvocationOnMock invocation) {
                Object[] args = invocation.getArguments();
                CancelInteraction cancelInteraction = (CancelInteraction) args[0];

                assertEquals(cancelInteraction.getCancelID(), TestValues.GENERAL_INTEGER);
                assertEquals(cancelInteraction.getInteractionFunctionID().intValue(), FunctionID.PERFORM_INTERACTION.getId());

                RPCResponse response = new RPCResponse(FunctionID.CANCEL_INTERACTION.toString());
                response.setSuccess(false);
                cancelInteraction.getOnRPCResponseListener().onResponse(0, response);

                return null;
            }
        };
        doAnswer(cancelInteractionAnswer).when(internalInterface).sendRPC(any(CancelInteraction.class));
    }

    @Test
    public void testCancelingChoiceSetIfThreadHasFinished() {
        when(internalInterface.getSdlMsgVersion()).thenReturn(new SdlMsgVersion(6, 0));
        WindowCapability windowCapability = new WindowCapability();
        HashSet<ChoiceCell> loadedCells = new HashSet<>();
        presentChoicesOperation = new PreloadPresentChoicesOperation(internalInterface, fileManager, choiceSet, InteractionMode.MANUAL_ONLY, null, null, TestValues.GENERAL_INTEGER,null, windowCapability, true, loadedCells, null, null);
        presentChoicesOperation.finishOperation(false);

        assertEquals(Task.FINISHED, presentChoicesOperation.getState());

        choiceSet.cancel();
        verify(internalInterface, never()).sendRPC(any(CancelInteraction.class));

        assertEquals(Task.FINISHED, presentChoicesOperation.getState());
    }

    @Test
    public void testCancelingChoiceSetIfThreadHasCanceled() {
        when(internalInterface.getSdlMsgVersion()).thenReturn(new SdlMsgVersion(6, 0));
        WindowCapability windowCapability = new WindowCapability();
        HashSet<ChoiceCell> loadedCells = new HashSet<>();
        presentChoicesOperation = new PreloadPresentChoicesOperation(internalInterface, fileManager, choiceSet, InteractionMode.MANUAL_ONLY, null, null, TestValues.GENERAL_INTEGER,null, windowCapability, true, loadedCells, null, null);
        presentChoicesOperation.cancelTask();

        assertEquals(Task.CANCELED, presentChoicesOperation.getState());

        choiceSet.cancel();
        verify(internalInterface, never()).sendRPC(any(CancelInteraction.class));

        assertEquals(Task.CANCELED, presentChoicesOperation.getState());
    }

    @Test
    public void testCancelingChoiceSetIfThreadHasNotYetRun() {
        when(internalInterface.getSdlMsgVersion()).thenReturn(new SdlMsgVersion(6, 0));
        WindowCapability windowCapability = new WindowCapability();
        HashSet<ChoiceCell> loadedCells = new HashSet<>();
        presentChoicesOperation = new PreloadPresentChoicesOperation(internalInterface, fileManager, choiceSet, InteractionMode.MANUAL_ONLY, null, null, TestValues.GENERAL_INTEGER, null, windowCapability, true, loadedCells, null, null);

        assertEquals(Task.BLOCKED, presentChoicesOperation.getState());

        choiceSet.cancel();

        // Once the operation has started
        queue.add(presentChoicesOperation, false);
        sleep();

        assertEquals(Task.CANCELED, presentChoicesOperation.getState());

        // Make sure neither a `CancelInteraction` or `PerformInteraction` RPC is ever sent
        verify(internalInterface, never()).sendRPC(any(CancelInteraction.class));
        verify(internalInterface, never()).sendRPC(any(PerformInteraction.class));
    }

    @Test
    public void testCancelingChoiceSetIfHeadUnitDoesNotSupportFeature() {
        // Cancel Interaction is only supported on RPC specs v.6.0.0+
        when(internalInterface.getSdlMsgVersion()).thenReturn(new SdlMsgVersion(5, 3));
        WindowCapability windowCapability = new WindowCapability();
        HashSet<ChoiceCell> loadedCells = new HashSet<>();
        BaseChoiceSetManager.ChoicesOperationCompletionListener listener = new BaseChoiceSetManager.ChoicesOperationCompletionListener() {
            @Override
            public void onComplete(boolean success, HashSet<ChoiceCell> loadedChoiceCells) {
                choiceSet.cancel();
                sleep();

                assertEquals(Task.IN_PROGRESS, presentChoicesOperation.getState());

                verify(internalInterface, never()).sendRPC(any(CancelInteraction.class));
                verify(internalInterface, times(1)).sendRPC(any(PerformInteraction.class));
            }
        };
        presentChoicesOperation = new PreloadPresentChoicesOperation(internalInterface, fileManager, choiceSet, InteractionMode.MANUAL_ONLY, null, null, TestValues.GENERAL_INTEGER, null, windowCapability, true, loadedCells, listener, null);
        presentChoicesOperation.setLoadedCells(new HashSet<ChoiceCell>());
        queue.add(presentChoicesOperation, false);
    }

    @Test
    public void testCancelingChoiceSetIfHeadUnitDoesNotSupportFeatureButThreadIsNotRunning() {
        // Cancel Interaction is only supported on RPC specs v.6.0.0+
        when(internalInterface.getSdlMsgVersion()).thenReturn(new SdlMsgVersion(5, 3));
        WindowCapability windowCapability = new WindowCapability();
        HashSet<ChoiceCell> loadedCells = new HashSet<>();
        presentChoicesOperation = new PreloadPresentChoicesOperation(internalInterface, fileManager, choiceSet, InteractionMode.MANUAL_ONLY, null, null, TestValues.GENERAL_INTEGER, null, windowCapability, true, loadedCells, null, null);

        assertEquals(Task.BLOCKED, presentChoicesOperation.getState());

        choiceSet.cancel();

        verify(internalInterface, never()).sendRPC(any(CancelInteraction.class));

        // Once the operation has started
        queue.add(presentChoicesOperation, false);
        sleep();

        assertEquals(Task.CANCELED, presentChoicesOperation.getState());

        // Make sure neither a `CancelInteraction` or `PerformInteraction` RPC is ever sent
        verify(internalInterface, never()).sendRPC(any(CancelInteraction.class));
        verify(internalInterface, never()).sendRPC(any(PerformInteraction.class));
    }
}