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
|
/*
* 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.file.FileManager;
import com.smartdevicelink.managers.ISdl;
import com.smartdevicelink.proxy.rpc.KeyboardProperties;
import com.smartdevicelink.proxy.rpc.enums.HMILevel;
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.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertFalse;
import static junit.framework.TestCase.assertNotNull;
import static junit.framework.TestCase.assertNotSame;
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);
csm = new ChoiceSetManager(internalInterface, fileManager);
assertEquals(csm.getState(), BaseSubManager.SETTING_UP);
assertEquals(csm.currentSystemContext, SystemContext.SYSCTXT_MAIN);
assertEquals(csm.currentHMILevel, HMILevel.HMI_NONE);
assertEquals(csm.choiceCellIdMin, 1);
assertEquals(csm.nextChoiceId, 1);
assertFalse(csm.isVROptional);
assertNotNull(csm.fileManager);
assertNotNull(csm.preloadedChoices);
assertNotNull(csm.pendingPreloadChoices);
assertNotNull(csm.transactionQueue);
assertNotNull(csm.hmiListener);
assertNotNull(csm.onDisplayCapabilityListener);
assertNull(csm.pendingPresentOperation);
}
@After
public void tearDown() throws Exception {
csm.dispose();
assertNull(csm.currentHMILevel);
assertNull(csm.currentSystemContext);
assertNull(csm.defaultMainWindowCapability);
assertNull(csm.pendingPresentationSet);
assertNull(csm.pendingPresentOperation);
assertEquals(csm.transactionQueue.getTasksAsList().size(), 0);
assertEquals(csm.nextChoiceId, 1);
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 cant have duplicate text
ChoiceCell cell1 = new ChoiceCell("test");
ChoiceCell cell2 = new ChoiceCell("test");
ChoiceSet choiceSet2 = new ChoiceSet("test", Arrays.asList(cell1, cell2), choiceSetSelectionListener);
assertFalse(csm.setUpChoiceSet(choiceSet2));
// cells cannot mix and match VR / non-VR
ChoiceCell cell3 = new ChoiceCell("test", Collections.singletonList("Test"), null);
ChoiceCell cell4 = new ChoiceCell("test2");
ChoiceSet choiceSet3 = new ChoiceSet("test", Arrays.asList(cell3, cell4), choiceSetSelectionListener);
assertFalse(csm.setUpChoiceSet(choiceSet3));
// VR Commands must be unique
ChoiceCell cell5 = new ChoiceCell("test", Collections.singletonList("Test"), null);
ChoiceCell cell6 = new ChoiceCell("test2", Collections.singletonList("Test"), null);
ChoiceSet choiceSet4 = new ChoiceSet("test", Arrays.asList(cell5, cell6), choiceSetSelectionListener);
assertFalse(csm.setUpChoiceSet(choiceSet4));
// Passing Case
ChoiceCell cell7 = new ChoiceCell("test", Collections.singletonList("Test"), null);
ChoiceCell cell8 = new ChoiceCell("test2", Collections.singletonList("Test2"), null);
ChoiceSet choiceSet5 = new ChoiceSet("test", Arrays.asList(cell7, cell8), choiceSetSelectionListener);
assertTrue(csm.setUpChoiceSet(choiceSet5));
}
@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 testUpdateIdsOnChoices(){
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);
cellSet.add(cell3);
// Cells are initially set to MAX_ID
assertEquals(cell1.getChoiceId(), 2000000000);
assertEquals(cell2.getChoiceId(), 2000000000);
assertEquals(cell3.getChoiceId(), 2000000000);
csm.updateIdsOnChoices(cellSet);
// We are looking for unique IDs
assertNotSame(cell1.getChoiceId(), 2000000000);
assertNotSame(cell2.getChoiceId(), 2000000000);
assertNotSame(cell3.getChoiceId(), 2000000000);
}
@Test
public void testChoicesToBeRemovedFromPendingWithArray(){
ChoiceCell cell1 = new ChoiceCell("test");
ChoiceCell cell2 = new ChoiceCell("test2");
ChoiceCell cell3 = new ChoiceCell("test3");
HashSet<ChoiceCell> pendingPreloadSet = new HashSet<>();
pendingPreloadSet.add(cell1);
pendingPreloadSet.add(cell2);
pendingPreloadSet.add(cell3);
csm.pendingPreloadChoices.clear();
csm.pendingPreloadChoices = pendingPreloadSet;
List<ChoiceCell> choices = new ArrayList<>();
choices.add(cell2);
HashSet<ChoiceCell> returnedChoices = csm.choicesToBeRemovedFromPendingWithArray(choices);
assertEquals(returnedChoices.size(), 1);
for (ChoiceCell cell : returnedChoices){
assertEquals(cell.getText(), "test2");
}
}
@Test
public void testChoicesToBeUploadedWithArray(){
ChoiceCell cell1 = new ChoiceCell("test");
ChoiceCell cell2 = new ChoiceCell("test2");
ChoiceCell cell3 = new ChoiceCell("test3");
HashSet<ChoiceCell> pendingDeleteSet = new HashSet<>();
pendingDeleteSet.add(cell1);
pendingDeleteSet.add(cell2);
pendingDeleteSet.add(cell3);
csm.preloadedChoices.clear();
csm.preloadedChoices = pendingDeleteSet;
List<ChoiceCell> choices = new ArrayList<>();
choices.add(cell2);
HashSet<ChoiceCell> returnedChoices = csm.choicesToBeDeletedWithArray(choices);
assertEquals(returnedChoices.size(), 1);
for (ChoiceCell cell : returnedChoices){
assertEquals(cell.getText(), "test2");
}
}
@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 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();
}
}
|