summaryrefslogtreecommitdiff
path: root/android/sdl_android/src/androidTest/java/com/smartdevicelink/managers/screen/choiceset/ChoiceSetManagerTests.java
blob: f26a7a5c5bee954609f6db528fd5d002325f78ff (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
/*
 * Copyright (c)  2019 Livio, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following
 * disclaimer in the documentation and/or other materials provided with the
 * distribution.
 *
 * Neither the name of the Livio Inc. nor the names of its contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * Created by brettywhite on 6/11/19 9:58 AM
 *
 */

package com.smartdevicelink.managers.screen.choiceset;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import com.livio.taskmaster.Taskmaster;
import com.smartdevicelink.managers.BaseSubManager;
import com.smartdevicelink.managers.CompletionListener;
import com.smartdevicelink.managers.ISdl;
import com.smartdevicelink.managers.file.FileManager;
import com.smartdevicelink.proxy.rpc.KeyboardCapabilities;
import com.smartdevicelink.proxy.rpc.KeyboardLayoutCapability;
import com.smartdevicelink.proxy.rpc.KeyboardProperties;
import com.smartdevicelink.proxy.rpc.SdlMsgVersion;
import com.smartdevicelink.proxy.rpc.WindowCapability;
import com.smartdevicelink.proxy.rpc.enums.HMILevel;
import com.smartdevicelink.proxy.rpc.enums.KeyboardInputMask;
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.SystemContext;
import com.smartdevicelink.proxy.rpc.enums.TriggerSource;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;

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.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(AndroidJUnit4.class)
public class ChoiceSetManagerTests {

    private ChoiceSetManager csm;
    Taskmaster taskmaster;

    @Before
    public void setUp() throws Exception {

        ISdl internalInterface = mock(ISdl.class);
        FileManager fileManager = mock(FileManager.class);
        taskmaster = new Taskmaster.Builder().build();
        when(internalInterface.getTaskmaster()).thenReturn(taskmaster);
        when(internalInterface.getSdlMsgVersion()).thenReturn(new SdlMsgVersion(7, 0));
        csm = new ChoiceSetManager(internalInterface, fileManager);

        assertEquals(csm.getState(), BaseSubManager.SETTING_UP);
        assertEquals(csm.currentSystemContext, SystemContext.SYSCTXT_MAIN);
        assertEquals(csm.currentHMILevel, HMILevel.HMI_NONE);
        assertFalse(csm.isVROptional);
        assertNotNull(csm.fileManager);
        assertNotNull(csm.preloadedChoices);
        assertNotNull(csm.transactionQueue);
        assertNotNull(csm.hmiListener);
        assertNotNull(csm.onDisplayCapabilityListener);
    }

    @After
    public void tearDown() throws Exception {

        csm.dispose();

        assertNull(csm.currentHMILevel);
        assertNull(csm.currentSystemContext);
        assertNull(csm.defaultMainWindowCapability);

        assertEquals(csm.transactionQueue.getTasksAsList().size(), 0);

        assertFalse(csm.isVROptional);

        assertEquals(csm.getState(), BaseSubManager.SHUTDOWN);

    }

    @Test
    public void testDefaultKeyboardConfiguration() {
        KeyboardProperties properties = csm.defaultKeyboardConfiguration();
        assertEquals(properties.getLanguage(), Language.EN_US);
        assertEquals(properties.getKeyboardLayout(), KeyboardLayout.QWERTY);
        assertEquals(properties.getKeypressMode(), KeypressMode.RESEND_CURRENT_ENTRY);
    }

    @Test
    public void testSetupChoiceSet() {

        ChoiceSetSelectionListener choiceSetSelectionListener = new ChoiceSetSelectionListener() {
            @Override
            public void onChoiceSelected(ChoiceCell choiceCell, TriggerSource triggerSource, int rowIndex) {
            }

            @Override
            public void onError(String error) {
            }
        };

        // Cannot send choice set with empty or null choice list
        ChoiceSet choiceSet1 = new ChoiceSet("test", Collections.<ChoiceCell>emptyList(), choiceSetSelectionListener);
        assertFalse(csm.setUpChoiceSet(choiceSet1));

        // cells that have duplicate text will be allowed if there is another property to make them unique because a unique name will be assigned and used
        ChoiceCell cell3 = new ChoiceCell("test");
        cell3.setSecondaryText("text 1");
        ChoiceCell cell4 = new ChoiceCell("test");
        cell4.setSecondaryText("text 2");
        ChoiceSet choiceSet3 = new ChoiceSet("test", Arrays.asList(cell3, cell4), choiceSetSelectionListener);
        assertTrue(csm.setUpChoiceSet(choiceSet3));

        // cells cannot mix and match VR / non-VR
        ChoiceCell cell5 = new ChoiceCell("test", Collections.singletonList("Test"), null);
        ChoiceCell cell6 = new ChoiceCell("test2");
        ChoiceSet choiceSet4 = new ChoiceSet("test", Arrays.asList(cell5, cell6), choiceSetSelectionListener);
        assertFalse(csm.setUpChoiceSet(choiceSet4));

        // VR Commands must be unique
        ChoiceCell cell7 = new ChoiceCell("test", Collections.singletonList("Test"), null);
        ChoiceCell cell8 = new ChoiceCell("test2", Collections.singletonList("Test"), null);
        ChoiceSet choiceSet5 = new ChoiceSet("test", Arrays.asList(cell7, cell8), choiceSetSelectionListener);
        assertFalse(csm.setUpChoiceSet(choiceSet5));

        // Passing Case
        ChoiceCell cell9 = new ChoiceCell("test", Collections.singletonList("Test"), null);
        ChoiceCell cell10 = new ChoiceCell("test2", Collections.singletonList("Test2"), null);
        ChoiceSet choiceSet6 = new ChoiceSet("test", Arrays.asList(cell9, cell10), choiceSetSelectionListener);
        assertTrue(csm.setUpChoiceSet(choiceSet6));
    }

    @Test
    public void testFindIfPresent() {

        ChoiceCell cell1 = new ChoiceCell("test");
        ChoiceCell cell2 = new ChoiceCell("test2");
        ChoiceCell cell3 = new ChoiceCell("test3");
        HashSet<ChoiceCell> cellSet = new HashSet<>();
        cellSet.add(cell1);
        cellSet.add(cell2);

        assertNotNull(csm.findIfPresent(cell1, cellSet));
        assertNull(csm.findIfPresent(cell3, cellSet));
    }

    @Test
    public void preloadChoicesAddsToQueue() {
        ChoiceCell cell1 = new ChoiceCell("test");
        ChoiceCell cell2 = new ChoiceCell("test2");
        ChoiceCell cell3 = new ChoiceCell("test3");
        ArrayList<ChoiceCell> cellSet = new ArrayList<>();
        cellSet.add(cell1);
        cellSet.add(cell2);
        cellSet.add(cell3);
        csm.preloadChoices(cellSet, new CompletionListener() {
            @Override
            public void onComplete(boolean success) {

            }
        });
        assertEquals(csm.transactionQueue.getTasksAsList().size(), 1);
    }

    @Test
    public void preloadChoicesQueueEmptyWhenNoChoiceCells() {
        ArrayList<ChoiceCell> cellSet = new ArrayList<>();
        csm.preloadChoices(cellSet, new CompletionListener() {
            @Override
            public void onComplete(boolean success) {

            }
        });
        assertEquals(csm.transactionQueue.getTasksAsList().size(), 0);
    }

    @Test
    public void testPreloadChoicesQueueEmptyIfFileManagerNull() {
        ChoiceCell cell1 = new ChoiceCell("test");
        ChoiceCell cell2 = new ChoiceCell("test2");
        ChoiceCell cell3 = new ChoiceCell("test3");
        ArrayList<ChoiceCell> cellSet = new ArrayList<>();
        cellSet.add(cell1);
        cellSet.add(cell2);
        cellSet.add(cell3);

        ISdl internalInterface = mock(ISdl.class);
        when(internalInterface.getTaskmaster()).thenReturn(taskmaster);
        FileManager fileManager = null;
        ChoiceSetManager newCSM = new ChoiceSetManager(internalInterface, fileManager);
        newCSM.preloadChoices(cellSet, new CompletionListener() {
            @Override
            public void onComplete(boolean success) {

            }
        });
        assertEquals(csm.transactionQueue.getTasksAsList().size(), 0);
    }

    @Test
    public void testPresentingKeyboardShouldReturnCancelIDIfKeyboardCanBeSent() {
        ISdl internalInterface = mock(ISdl.class);
        when(internalInterface.getTaskmaster()).thenReturn(taskmaster);
        FileManager fileManager = mock(FileManager.class);

        ChoiceSetManager newCSM = new ChoiceSetManager(internalInterface, fileManager);
        ChoiceSetManager partialMockCSM = spy(newCSM);
        when(partialMockCSM.getState()).thenReturn(BaseSubManager.READY);

        Integer cancelId = partialMockCSM.presentKeyboard("initial text", mock(KeyboardProperties.class), mock(KeyboardListener.class));
        assertNotNull(cancelId);
    }

    @Test
    public void testPresentingKeyboardShouldNotReturnCancelIDIfKeyboardCannotBeSent() {
        ISdl internalInterface = mock(ISdl.class);
        when(internalInterface.getTaskmaster()).thenReturn(taskmaster);
        FileManager fileManager = mock(FileManager.class);

        ChoiceSetManager newCSM = new ChoiceSetManager(internalInterface, fileManager);
        ChoiceSetManager partialMockCSM = spy(newCSM);
        when(partialMockCSM.getState()).thenReturn(BaseSubManager.ERROR);

        Integer cancelId = partialMockCSM.presentKeyboard("initial text", mock(KeyboardProperties.class), mock(KeyboardListener.class));
        assertNull(cancelId);
    }

    @Test
    public void testDefaultWindowCapabilityNotSet() throws NoSuchFieldException, IllegalAccessException {
        ISdl internalInterface = mock(ISdl.class);
        when(internalInterface.getTaskmaster()).thenReturn(taskmaster);
        FileManager fileManager = mock(FileManager.class);

        // Test direct set
        ChoiceSetManager newCSM = new ChoiceSetManager(internalInterface, fileManager);
        newCSM.setKeyboardConfiguration(newCSM.defaultKeyboardConfiguration());
        Field field = BaseChoiceSetManager.class.getDeclaredField("keyboardConfiguration");
        field.setAccessible(true);
        KeyboardProperties properties = (KeyboardProperties)field.get(newCSM);
        assertEquals(properties, csm.defaultKeyboardConfiguration());

        // Test presentKeyboard
        newCSM = new ChoiceSetManager(internalInterface, fileManager);
        newCSM.presentKeyboard("qwerty", newCSM.defaultKeyboardConfiguration(), null);
        field = BaseChoiceSetManager.class.getDeclaredField("keyboardConfiguration");
        field.setAccessible(true);
        properties = (KeyboardProperties)field.get(newCSM);
        assertEquals(properties, csm.defaultKeyboardConfiguration());
    }

    @Test
    public void testDefaultWindowCapabilityTooManyKeys() throws NoSuchFieldException, IllegalAccessException {
        ISdl internalInterface = mock(ISdl.class);
        when(internalInterface.getTaskmaster()).thenReturn(taskmaster);
        FileManager fileManager = mock(FileManager.class);

        ChoiceSetManager newCSM = new ChoiceSetManager(internalInterface, fileManager);
        WindowCapability smallKeysAmountCapability = new WindowCapability();
        KeyboardCapabilities capabilities = new KeyboardCapabilities();
        capabilities.setMaskInputCharactersSupported(true);
        KeyboardLayout layout = KeyboardLayout.QWERTY;
        capabilities.setSupportedKeyboards(Collections.singletonList(new KeyboardLayoutCapability(layout, 1)));
        smallKeysAmountCapability.setKeyboardCapabilities(capabilities);
        newCSM.defaultMainWindowCapability = smallKeysAmountCapability;

        KeyboardProperties setProperties = new KeyboardProperties();
        setProperties.setKeyboardLayout(layout);
        setProperties.setCustomKeys(Arrays.asList("1", "2"));

        newCSM.setKeyboardConfiguration(setProperties);
        Field field = BaseChoiceSetManager.class.getDeclaredField("keyboardConfiguration");
        field.setAccessible(true);

        KeyboardProperties getProperties = (KeyboardProperties)field.get(newCSM);

        assertEquals(getProperties.getCustomKeys().size(), 1);
    }

    @Test
    public void testCustomKeysNull() throws NoSuchFieldException, IllegalAccessException {
        ISdl internalInterface = mock(ISdl.class);
        when(internalInterface.getTaskmaster()).thenReturn(taskmaster);
        FileManager fileManager = mock(FileManager.class);

        ChoiceSetManager newCSM = new ChoiceSetManager(internalInterface, fileManager);
        WindowCapability smallKeysAmountCapability = new WindowCapability();
        KeyboardCapabilities capabilities = new KeyboardCapabilities();
        capabilities.setMaskInputCharactersSupported(true);
        KeyboardLayout layout = KeyboardLayout.QWERTY;
        capabilities.setSupportedKeyboards(Collections.singletonList(new KeyboardLayoutCapability(layout, 0)));
        smallKeysAmountCapability.setKeyboardCapabilities(capabilities);
        newCSM.defaultMainWindowCapability = smallKeysAmountCapability;

        KeyboardProperties setProperties = new KeyboardProperties();
        setProperties.setKeyboardLayout(layout);
        setProperties.setCustomKeys(new ArrayList<String>());

        newCSM.setKeyboardConfiguration(setProperties);
        Field field = BaseChoiceSetManager.class.getDeclaredField("keyboardConfiguration");
        field.setAccessible(true);

        KeyboardProperties getProperties = (KeyboardProperties)field.get(newCSM);

        assertNull(getProperties.getCustomKeys());
    }

    @Test
    public void testMaskInputCharactersNotSupported() throws NoSuchFieldException, IllegalAccessException {
        ISdl internalInterface = mock(ISdl.class);
        when(internalInterface.getTaskmaster()).thenReturn(taskmaster);
        FileManager fileManager = mock(FileManager.class);

        ChoiceSetManager newCSM = new ChoiceSetManager(internalInterface, fileManager);
        WindowCapability maskInputNotSupportedCapability = new WindowCapability();
        KeyboardCapabilities capabilities = new KeyboardCapabilities();

        capabilities.setMaskInputCharactersSupported(false);
        KeyboardLayout layout = KeyboardLayout.QWERTY;
        capabilities.setSupportedKeyboards(Collections.singletonList(new KeyboardLayoutCapability(layout, 0)));

        maskInputNotSupportedCapability.setKeyboardCapabilities(capabilities);

        newCSM.defaultMainWindowCapability = maskInputNotSupportedCapability;

        KeyboardProperties setProperties = new KeyboardProperties();
        setProperties.setKeyboardLayout(layout);
        setProperties.setMaskInputCharacters(KeyboardInputMask.DISABLE_INPUT_KEY_MASK);

        newCSM.setKeyboardConfiguration(setProperties);
        Field field = BaseChoiceSetManager.class.getDeclaredField("keyboardConfiguration");
        field.setAccessible(true);

        KeyboardProperties getProperties = (KeyboardProperties)field.get(newCSM);

        assertNull(getProperties.getMaskInputCharacters());
    }

    @Test
    public void testDismissingExecutingKeyboard() {
        Integer testCancelID = 42;
        PresentKeyboardOperation testKeyboardOp = mock(PresentKeyboardOperation.class);
        doReturn(testCancelID).when(testKeyboardOp).getCancelID();
        csm.currentlyPresentedKeyboardOperation = testKeyboardOp;
        csm.dismissKeyboard(testCancelID);
        verify(testKeyboardOp, times(1)).dismissKeyboard();
    }

    @Test
    public void testDismissingQueuedKeyboard() {
        Integer testCancelID = 42;

        // Currently executing operation
        PresentKeyboardOperation testKeyboardOp = mock(PresentKeyboardOperation.class);
        doReturn(96).when(testKeyboardOp).getCancelID();
        csm.currentlyPresentedKeyboardOperation = testKeyboardOp;

        // Queued operations
        PresentKeyboardOperation testKeyboardOp2 = mock(PresentKeyboardOperation.class);
        doReturn(testCancelID).when(testKeyboardOp2).getCancelID();
        csm.currentlyPresentedKeyboardOperation = testKeyboardOp2;

        // Queued operation should be canceled
        csm.dismissKeyboard(testCancelID);
        verify(testKeyboardOp, times(0)).dismissKeyboard();
        verify(testKeyboardOp2, times(1)).dismissKeyboard();
    }
}