summaryrefslogtreecommitdiff
path: root/android/sdl_android/src/androidTest/java/com/smartdevicelink/managers/screen/SoftButtonManagerTests.java
blob: becc01a749651a260c91da155ba7c246068cdcf4 (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
package com.smartdevicelink.managers.screen;


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

import com.livio.taskmaster.Taskmaster;
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.managers.lifecycle.OnSystemCapabilityListener;
import com.smartdevicelink.managers.lifecycle.SystemCapabilityManager;
import com.smartdevicelink.protocol.enums.FunctionID;
import com.smartdevicelink.proxy.rpc.DisplayCapability;
import com.smartdevicelink.proxy.rpc.Image;
import com.smartdevicelink.proxy.rpc.OnButtonEvent;
import com.smartdevicelink.proxy.rpc.OnButtonPress;
import com.smartdevicelink.proxy.rpc.OnHMIStatus;
import com.smartdevicelink.proxy.rpc.Show;
import com.smartdevicelink.proxy.rpc.ShowResponse;
import com.smartdevicelink.proxy.rpc.SoftButton;
import com.smartdevicelink.proxy.rpc.SoftButtonCapabilities;
import com.smartdevicelink.proxy.rpc.WindowCapability;
import com.smartdevicelink.proxy.rpc.enums.FileType;
import com.smartdevicelink.proxy.rpc.enums.HMILevel;
import com.smartdevicelink.proxy.rpc.enums.ImageType;
import com.smartdevicelink.proxy.rpc.enums.Result;
import com.smartdevicelink.proxy.rpc.enums.SoftButtonType;
import com.smartdevicelink.proxy.rpc.enums.StaticIconName;
import com.smartdevicelink.proxy.rpc.enums.SystemAction;
import com.smartdevicelink.proxy.rpc.enums.SystemCapabilityType;
import com.smartdevicelink.proxy.rpc.listeners.OnRPCNotificationListener;
import com.smartdevicelink.test.Validator;

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

import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertFalse;
import static junit.framework.TestCase.assertNull;
import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.assertNotEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
 * This is a unit test class for the SmartDeviceLink library manager class :
 * {@link SoftButtonManager}
 */
@RunWith(AndroidJUnit4.class)
public class SoftButtonManagerTests {

    private SoftButtonManager softButtonManager;
    private int fileManagerUploadArtworksListenerCalledCounter;
    private int internalInterfaceSendRPCListenerCalledCounter;
    private int softButtonObject1Id = 1000, softButtonObject2Id = 2000;
    private SoftButtonObject softButtonObject1, softButtonObject2;
    private SoftButtonState softButtonState1, softButtonState2, softButtonState3, softButtonState4;


    @Before
    public void setUp() throws Exception {

        // When internalInterface.addOnRPCNotificationListener(FunctionID.ON_HMI_STATUS, OnRPCNotificationListener) is called
        // inside SoftButtonManager, respond with a fake HMILevel.HMI_FULL response to let the SoftButtonManager continue working.
        ISdl internalInterface = mock(ISdl.class);
        Answer<Void> onHMIStatusAnswer = new Answer<Void>() {
            @Override
            public Void answer(InvocationOnMock invocation) {
                Object[] args = invocation.getArguments();
                OnRPCNotificationListener onHMIStatusListener = (OnRPCNotificationListener) args[1];
                OnHMIStatus onHMIStatusFakeNotification = new OnHMIStatus();
                onHMIStatusFakeNotification.setHmiLevel(HMILevel.HMI_FULL);
                onHMIStatusListener.onNotified(onHMIStatusFakeNotification);
                return null;
            }
        };
        doAnswer(onHMIStatusAnswer).when(internalInterface).addOnRPCNotificationListener(eq(FunctionID.ON_HMI_STATUS), any(OnRPCNotificationListener.class));


        // When internalInterface.getSystemCapabilityManager().addOnSystemCapabilityListener(SystemCapabilityType.DISPLAYS, onSystemCapabilityListener) is called
        // inside SoftButtonManager, respond with a fake response to let the SoftButtonManager continue working.
        Answer<Void> onSystemCapabilityAnswer = new Answer<Void>() {
            @Override
            public Void answer(InvocationOnMock invocation) {
                Object[] args = invocation.getArguments();
                OnSystemCapabilityListener onSystemCapabilityListener = (OnSystemCapabilityListener) args[1];
                SoftButtonCapabilities softButtonCapabilities = new SoftButtonCapabilities();
                softButtonCapabilities.setImageSupported(true);
                softButtonCapabilities.setTextSupported(true);
                WindowCapability windowCapability = new WindowCapability();
                windowCapability.setSoftButtonCapabilities(Collections.singletonList(softButtonCapabilities));
                DisplayCapability displayCapability = new DisplayCapability();
                displayCapability.setWindowCapabilities(Collections.singletonList(windowCapability));
                List<DisplayCapability> capabilities = Collections.singletonList(displayCapability);
                onSystemCapabilityListener.onCapabilityRetrieved(capabilities);
                return null;
            }
        };
        SystemCapabilityManager systemCapabilityManager = mock(SystemCapabilityManager.class);
        doAnswer(onSystemCapabilityAnswer).when(systemCapabilityManager).addOnSystemCapabilityListener(eq(SystemCapabilityType.DISPLAYS), any(OnSystemCapabilityListener.class));
        doReturn(systemCapabilityManager).when(internalInterface).getSystemCapabilityManager();

        // When fileManager.uploadArtworks() is called inside the SoftButtonManager, respond with
        // a fake onComplete() callback to let the SoftButtonManager continue working.
        FileManager fileManager = mock(FileManager.class);
        Answer<Void> onFileManagerUploadAnswer = new Answer<Void>() {
            @Override
            public Void answer(InvocationOnMock invocation) {
                fileManagerUploadArtworksListenerCalledCounter++;
                Object[] args = invocation.getArguments();
                MultipleFileCompletionListener multipleFileCompletionListener = (MultipleFileCompletionListener) args[1];
                multipleFileCompletionListener.onComplete(null);
                return null;
            }
        };
        doAnswer(onFileManagerUploadAnswer).when(fileManager).uploadArtworks(any(List.class), any(MultipleFileCompletionListener.class));


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


        // Create softButtonManager
        Taskmaster taskmaster = new Taskmaster.Builder().build();
        taskmaster.start();
        when(internalInterface.getTaskmaster()).thenReturn(taskmaster);
        softButtonManager = new SoftButtonManager(internalInterface, fileManager);


        // When internalInterface.sendRPC() is called inside SoftButtonManager:
        // 1) respond with a fake onResponse() callback to let the SoftButtonManager continue working
        // 2) assert that the Show RPC values (ie: MainField1 & SoftButtons) that are created by the SoftButtonManager, match the ones that are provided by the developer
        Answer<Void> onSendShowRPCAnswer = new Answer<Void>() {
            @Override
            public Void answer(InvocationOnMock invocation) {
                internalInterfaceSendRPCListenerCalledCounter++;
                Object[] args = invocation.getArguments();
                Show show = (Show) args[0];

                show.getOnRPCResponseListener().onResponse(0, new ShowResponse(true, Result.SUCCESS));

                assertEquals(show.getMainField1(), softButtonManager.getCurrentMainField1());
                assertEquals(show.getSoftButtons().size(), softButtonManager.getSoftButtonObjects().size());

                return null;
            }
        };
        doAnswer(onSendShowRPCAnswer).when(internalInterface).sendRPC(any(Show.class));


        // Create soft button objects
        softButtonState1 = new SoftButtonState("object1-state1", "o1s1", new SdlArtwork("image1", FileType.GRAPHIC_PNG, 1, true));
        softButtonState2 = new SoftButtonState("object1-state2", "o1s2", new SdlArtwork(StaticIconName.ALBUM));
        softButtonObject1 = new SoftButtonObject("object1", Arrays.asList(softButtonState1, softButtonState2), softButtonState1.getName(), null);
        softButtonObject1.setButtonId(softButtonObject1Id);
        softButtonState3 = new SoftButtonState("object2-state1", "o2s1", null);
        softButtonState4 = new SoftButtonState("object2-state2", "o2s2", new SdlArtwork("image3", FileType.GRAPHIC_PNG, 3, true));
        softButtonObject2 = new SoftButtonObject("object2", Arrays.asList(softButtonState3, softButtonState4), softButtonState3.getName(), null);
        softButtonObject2.setButtonId(softButtonObject2Id);
    }


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

    @Test
    public void testSoftButtonManagerUpdate() {
        // Reset the boolean variables
        fileManagerUploadArtworksListenerCalledCounter = 0;
        internalInterfaceSendRPCListenerCalledCounter = 0;


        // Test batch update
        softButtonManager.setBatchUpdates(true);
        List<SoftButtonObject> softButtonObjects = Arrays.asList(softButtonObject1, softButtonObject2);
        softButtonManager.setSoftButtonObjects(Arrays.asList(softButtonObject1, softButtonObject2));
        softButtonManager.setBatchUpdates(false);


        // Test single update, setCurrentMainField1, and transitionToNextState
        softButtonManager.setCurrentMainField1("It is Wednesday my dudes!");
        softButtonObject1.transitionToNextState();


        // Sleep to give time to Taskmaster to run the operations
        sleep();


        // Check that everything got called as expected
        assertEquals("FileManager.uploadArtworks() did not get called correctly", 1, fileManagerUploadArtworksListenerCalledCounter);
        assertEquals("InternalInterface.sendRPC() did not get called correctly", 2, internalInterfaceSendRPCListenerCalledCounter);


        // Test getSoftButtonObjects
        assertEquals("Returned softButtonObjects value doesn't match the expected value", softButtonObjects, softButtonManager.getSoftButtonObjects());
    }

    @Test
    public void testSoftButtonManagerGetSoftButtonObject() {
        softButtonManager.setSoftButtonObjects(Arrays.asList(softButtonObject1, softButtonObject2));


        // Test get by valid name
        assertEquals("Returned SoftButtonObject doesn't match the expected value", softButtonObject2, softButtonManager.getSoftButtonObjectByName("object2"));


        // Test get by invalid name
        assertNull("Returned SoftButtonObject doesn't match the expected value", softButtonManager.getSoftButtonObjectByName("object300"));


        // Test get by valid id
        assertEquals("Returned SoftButtonObject doesn't match the expected value", softButtonObject2, softButtonManager.getSoftButtonObjectById(softButtonObject2Id));


        // Test get by invalid id
        assertNull("Returned SoftButtonObject doesn't match the expected value", softButtonManager.getSoftButtonObjectById(5555));
    }

    @Test
    public void testSoftButtonState() {
        // Test SoftButtonState.getName()
        String nameExpectedValue = "object1-state1";
        assertEquals("Returned state name doesn't match the expected value", nameExpectedValue, softButtonState1.getName());


        // Test SoftButtonState.getArtwork()
        SdlArtwork artworkExpectedValue = new SdlArtwork("image1", FileType.GRAPHIC_PNG, 1, true);
        assertTrue("Returned SdlArtwork doesn't match the expected value", Validator.validateSdlFile(artworkExpectedValue, softButtonState1.getArtwork()));
        SdlArtwork artworkExpectedValue2 = new SdlArtwork(StaticIconName.ALBUM);
        assertTrue("Returned SdlArtwork doesn't match the expected value", Validator.validateSdlFile(artworkExpectedValue2, softButtonState2.getArtwork()));


        // Test SoftButtonState.getSoftButton()
        SoftButton softButtonExpectedValue = new SoftButton(SoftButtonType.SBT_BOTH, SoftButtonObject.SOFT_BUTTON_ID_NOT_SET_VALUE);
        softButtonExpectedValue.setText("o1s1");
        softButtonExpectedValue.setImage(new Image(artworkExpectedValue.getName(), ImageType.DYNAMIC));
        softButtonExpectedValue.setSystemAction(SystemAction.DEFAULT_ACTION);
        SoftButton actual = softButtonState1.getSoftButton();
        assertTrue("Returned SoftButton doesn't match the expected value", Validator.validateSoftButton(softButtonExpectedValue, actual));
    }

    @Test
    public void testSoftButtonObject() {
        // Test SoftButtonObject.getName()
        assertEquals("Returned object name doesn't match the expected value", "object1", softButtonObject1.getName());


        // Test SoftButtonObject.getCurrentState()
        assertEquals("Returned current state doesn't match the expected value", softButtonState1, softButtonObject1.getCurrentState());


        // Test SoftButtonObject.getCurrentStateName()
        assertEquals("Returned current state name doesn't match the expected value", softButtonState1.getName(), softButtonObject1.getCurrentStateName());


        // Test SoftButtonObject.getButtonId()
        assertEquals("Returned button Id doesn't match the expected value", softButtonObject1Id, softButtonObject1.getButtonId());


        // Test SoftButtonObject.getCurrentStateSoftButton()
        SoftButton softButtonExpectedValue = new SoftButton(SoftButtonType.SBT_TEXT, softButtonObject2Id);
        softButtonExpectedValue.setText("o2s1");
        softButtonExpectedValue.setSystemAction(SystemAction.DEFAULT_ACTION);
        assertTrue("Returned current state SoftButton doesn't match the expected value", Validator.validateSoftButton(softButtonExpectedValue, softButtonObject2.getCurrentStateSoftButton()));


        // Test SoftButtonObject.getStates()
        assertEquals("Returned object states doesn't match the expected value", Arrays.asList(softButtonState1, softButtonState2), softButtonObject1.getStates());


        // Test SoftButtonObject.transitionToNextState()
        assertEquals(softButtonState1, softButtonObject1.getCurrentState());
        softButtonObject1.transitionToNextState();
        assertEquals(softButtonState2, softButtonObject1.getCurrentState());


        // Test SoftButtonObject.transitionToStateByName() - transitioning to a none existing state
        boolean success = softButtonObject1.transitionToStateByName("none existing name");
        assertFalse(success);


        // Test SoftButtonObject.transitionToStateByName() - transitioning to an existing state
        success = softButtonObject1.transitionToStateByName("object1-state1");
        assertTrue(success);
        assertEquals(softButtonState1, softButtonObject1.getCurrentState());
    }

    /**
     * Test custom overridden softButtonObject equals method
     */
    @Test
    public void testSoftButtonObjectEquals() {
        SoftButtonObject softButtonObject1;
        SoftButtonObject softButtonObject2;

        SoftButtonObject.OnEventListener testOnEventList1 = new SoftButtonObject.OnEventListener() {
            @Override
            public void onPress(SoftButtonObject softButtonObject, OnButtonPress onButtonPress) {
            }

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

        SoftButtonObject.OnEventListener testOnEventList2 = new SoftButtonObject.OnEventListener() {
            @Override
            public void onPress(SoftButtonObject softButtonObject, OnButtonPress onButtonPress) {
            }

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

        // Case 1: object is null, assertFalse
        softButtonObject1 = new SoftButtonObject("test", softButtonState1, null);
        softButtonObject2 = null;
        assertNotEquals(softButtonObject1, softButtonObject2);

        // Case 2 SoftButtonObjects are the same, assertTrue
        assertEquals(softButtonObject1, softButtonObject1);

        // Case 3: object is not an instance of SoftButtonObject assertFalse
        SdlArtwork artwork = new SdlArtwork("image1", FileType.GRAPHIC_PNG, 1, true);
        assertNotEquals(softButtonObject1, artwork);

        // Case 4: SoftButtonObjectState List are not same size, assertFalse
        List<SoftButtonState> softButtonStateList = new ArrayList<>();
        List<SoftButtonState> softButtonStateList2 = new ArrayList<>();
        softButtonStateList.add(softButtonState1);
        softButtonStateList2.add(softButtonState1);
        softButtonStateList2.add(softButtonState2);
        softButtonObject1 = new SoftButtonObject("hi", softButtonStateList, "Hi", null);
        softButtonObject2 = new SoftButtonObject("hi", softButtonStateList2, "Hi", null);
        assertNotEquals(softButtonObject1, softButtonObject2);

        // Case 5: SoftButtonStates are not the same, assertFalse
        softButtonObject1 = new SoftButtonObject("test", softButtonState1, null);
        softButtonObject2 = new SoftButtonObject("test", softButtonState2, null);
        assertNotEquals(softButtonObject1, softButtonObject2);

        // Case 6: SoftButtonObject names are not same, assertFalse
        softButtonObject1 = new SoftButtonObject("test", softButtonState1, null);
        softButtonObject2 = new SoftButtonObject("test23123", softButtonState1, null);
        assertNotEquals(softButtonObject1, softButtonObject2);

        // Case 7: SoftButtonObject currentStateName not same, assertFalse
        softButtonObject1 = new SoftButtonObject("hi", softButtonStateList, "Hi", null);
        softButtonObject2 = new SoftButtonObject("hi", softButtonStateList, "Hi2", null);
        assertNotEquals(softButtonObject1, softButtonObject2);
    }

    /**
     * Test custom overridden softButtonState equals method
     */
    @Test
    public void testSoftButtonStateEquals() {
        assertNotEquals(softButtonState1, softButtonState2);
        SdlArtwork artwork1 = new SdlArtwork("image1", FileType.GRAPHIC_PNG, 1, true);
        SdlArtwork artwork2 = new SdlArtwork("image2", FileType.GRAPHIC_PNG, 1, true);

        // Case 1: object is null, assertFalse
        softButtonState1 = new SoftButtonState("object1-state1", "o1s1", artwork1);
        softButtonState2 = null;
        assertNotEquals(softButtonState1, softButtonState2);

        // Case 2 SoftButtonObjects are the same, assertTrue
        assertEquals(softButtonState1, softButtonState1);

        // Case 3: object is not an instance of SoftButtonState, assertFalse
        assertNotEquals(softButtonState1, artwork1);

        // Case 4: different artwork, assertFalse
        softButtonState2 = new SoftButtonState("object1-state1", "o1s1", artwork2);
        assertNotEquals(softButtonState1, softButtonState2);

        // Case 5: different name, assertFalse
        softButtonState2 = new SoftButtonState("object1-state1 different name", "o1s1", artwork1);
        assertNotEquals(softButtonState1, softButtonState2);

        // Case 6 they are equal, assertTrue
        softButtonState2 = new SoftButtonState("object1-state1", "o1s1", artwork1);
        assertEquals(softButtonState1, softButtonState2);
    }
}