summaryrefslogtreecommitdiff
path: root/android/sdl_android/src/androidTest/java/com/smartdevicelink/managers/screen/menu/MenuReplaceUtilitiesTests.java
blob: 61587dbdcfeccf3696355b0c9e65009b9aafc286 (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
/*
 * Copyright (c) 2021 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.
 */

package com.smartdevicelink.managers.screen.menu;

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

import com.smartdevicelink.managers.file.FileManager;
import com.smartdevicelink.managers.file.filetypes.SdlArtwork;
import com.smartdevicelink.proxy.rpc.ImageField;
import com.smartdevicelink.proxy.rpc.WindowCapability;
import com.smartdevicelink.proxy.rpc.enums.ImageFieldName;
import com.smartdevicelink.proxy.rpc.enums.MenuLayout;
import com.smartdevicelink.test.TestValues;

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

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static com.smartdevicelink.managers.screen.menu.BaseMenuManager.parentIdNotFound;
import static com.smartdevicelink.managers.screen.menu.MenuReplaceUtilities.addIdsToMenuCells;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertFalse;
import static junit.framework.TestCase.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
 * Created by Bilal Alsharifi on 2/4/21.
 */
@RunWith(AndroidJUnit4.class)
public class MenuReplaceUtilitiesTests {
    @Before
    public void setUp() throws Exception {
    }

    @Test
    public void testRemoveMenuCellFromList() {
        MenuCell menuCellToDelete;
        boolean cellRemoved;
        List<MenuCell> actualMenuCellList = createMenuCellList();
        List<MenuCell> expectedMenuCellList = createMenuCellList();

        // Delete cell c4
        menuCellToDelete = actualMenuCellList.get(3);
        cellRemoved = MenuReplaceUtilities.removeCellFromList(actualMenuCellList, menuCellToDelete.getCellId());
        assertTrue(cellRemoved);
        expectedMenuCellList.remove(3);
        assertEquals(expectedMenuCellList, actualMenuCellList);
        assertEquals(3, actualMenuCellList.size());

        // Delete cell c4 again - removal should fail and list should not change
        cellRemoved = MenuReplaceUtilities.removeCellFromList(actualMenuCellList, menuCellToDelete.getCellId());
        assertFalse(cellRemoved);
        assertEquals(expectedMenuCellList, actualMenuCellList);
        assertEquals(3, actualMenuCellList.size());

        // Delete cell c3
        menuCellToDelete = actualMenuCellList.get(2);
        cellRemoved = MenuReplaceUtilities.removeCellFromList(actualMenuCellList, menuCellToDelete.getCellId());
        assertTrue(cellRemoved);
        expectedMenuCellList.remove(2);
        assertEquals(expectedMenuCellList, actualMenuCellList);
        assertEquals(2, actualMenuCellList.size());

        // Delete cell c2-2-2
        menuCellToDelete = actualMenuCellList.get(1).getSubCells().get(1).getSubCells().get(1);
        cellRemoved = MenuReplaceUtilities.removeCellFromList(actualMenuCellList, menuCellToDelete.getCellId());
        assertTrue(cellRemoved);
        expectedMenuCellList.get(1).getSubCells().get(1).getSubCells().remove(1);
        assertEquals(expectedMenuCellList, actualMenuCellList);
        assertEquals(2, actualMenuCellList.size());
        assertEquals(1, actualMenuCellList.get(1).getSubCells().get(1).getSubCells().size());

        // Delete cell c2-2-1
        menuCellToDelete = actualMenuCellList.get(1).getSubCells().get(1).getSubCells().get(0);
        cellRemoved = MenuReplaceUtilities.removeCellFromList(actualMenuCellList, menuCellToDelete.getCellId());
        assertTrue(cellRemoved);
        expectedMenuCellList.get(1).getSubCells().get(1).getSubCells().remove(0);
        assertEquals(expectedMenuCellList, actualMenuCellList);
        assertEquals(2, actualMenuCellList.size());
        assertEquals(0, actualMenuCellList.get(1).getSubCells().get(1).getSubCells().size());

        // Delete cell c2-2
        menuCellToDelete = actualMenuCellList.get(1).getSubCells().get(1);
        cellRemoved = MenuReplaceUtilities.removeCellFromList(actualMenuCellList, menuCellToDelete.getCellId());
        assertTrue(cellRemoved);
        expectedMenuCellList.get(1).getSubCells().remove(1);
        assertEquals(expectedMenuCellList, actualMenuCellList);
        assertEquals(2, actualMenuCellList.size());
        assertEquals(1, actualMenuCellList.get(1).getSubCells().size());

        // Delete cell c2-1
        menuCellToDelete = actualMenuCellList.get(1).getSubCells().get(0);
        cellRemoved = MenuReplaceUtilities.removeCellFromList(actualMenuCellList, menuCellToDelete.getCellId());
        assertTrue(cellRemoved);
        expectedMenuCellList.get(1).getSubCells().remove(0);
        assertEquals(expectedMenuCellList, actualMenuCellList);
        assertEquals(2, actualMenuCellList.size());
        assertEquals(0, actualMenuCellList.get(1).getSubCells().size());

        // Delete cell c2
        menuCellToDelete = actualMenuCellList.get(1);
        cellRemoved = MenuReplaceUtilities.removeCellFromList(actualMenuCellList, menuCellToDelete.getCellId());
        assertTrue(cellRemoved);
        expectedMenuCellList.remove(1);
        assertEquals(expectedMenuCellList, actualMenuCellList);
        assertEquals(1, actualMenuCellList.size());

        // Delete cell c1
        menuCellToDelete = actualMenuCellList.get(0);
        cellRemoved = MenuReplaceUtilities.removeCellFromList(actualMenuCellList, menuCellToDelete.getCellId());
        assertTrue(cellRemoved);
        expectedMenuCellList.remove(0);
        assertEquals(expectedMenuCellList, actualMenuCellList);
        assertEquals(0, actualMenuCellList.size());
    }

    @Test
    public void testAddMenuRequestWithCommandId() {
        MenuCell menuCellToAdd;
        boolean cellAdded;
        List<MenuCell> actualMenuCellList = createMenuCellList();
        List<MenuCell> expectedMenuCellList = createMenuCellList();
        List<MenuCell> newMenuList = createNewMenuList();

        // Add cell c5
        menuCellToAdd = newMenuList.get(0);
        cellAdded = MenuReplaceUtilities.addCellWithCellId(menuCellToAdd.getCellId(), 4, newMenuList, actualMenuCellList);
        assertTrue(cellAdded);
        expectedMenuCellList.add(4, cloneMenuCellAndRemoveSubCells(menuCellToAdd));
        assertEquals(expectedMenuCellList, actualMenuCellList);
        assertEquals(5, actualMenuCellList.size());
        assertEquals(0, actualMenuCellList.get(4).getSubCells().size());

        // Add cell c5-1
        menuCellToAdd = newMenuList.get(0).getSubCells().get(0);
        cellAdded = MenuReplaceUtilities.addCellWithCellId(menuCellToAdd.getCellId(), 0, newMenuList, actualMenuCellList);
        assertTrue(cellAdded);
        expectedMenuCellList.get(4).getSubCells().add(0, cloneMenuCellAndRemoveSubCells(menuCellToAdd));
        assertEquals(expectedMenuCellList, actualMenuCellList);
        assertEquals(5, actualMenuCellList.size());
        assertEquals(1, actualMenuCellList.get(4).getSubCells().size());

        // Add cell c5-1-1
        menuCellToAdd = newMenuList.get(0).getSubCells().get(0).getSubCells().get(0);
        cellAdded = MenuReplaceUtilities.addCellWithCellId(menuCellToAdd.getCellId(), 0, newMenuList, actualMenuCellList);
        assertTrue(cellAdded);
        expectedMenuCellList.get(4).getSubCells().get(0).getSubCells().add(0, cloneMenuCellAndRemoveSubCells(menuCellToAdd));
        assertEquals(expectedMenuCellList, actualMenuCellList);
        assertEquals(5, actualMenuCellList.size());
        assertEquals(1, actualMenuCellList.get(4).getSubCells().size());
        assertEquals(1, actualMenuCellList.get(4).getSubCells().get(0).getSubCells().size());

        // Add cell c5-2
        menuCellToAdd = newMenuList.get(0).getSubCells().get(1);
        cellAdded = MenuReplaceUtilities.addCellWithCellId(menuCellToAdd.getCellId(), 1, newMenuList, actualMenuCellList);
        assertTrue(cellAdded);
        expectedMenuCellList.get(4).getSubCells().add(1, cloneMenuCellAndRemoveSubCells(menuCellToAdd));
        assertEquals(expectedMenuCellList, actualMenuCellList);
        assertEquals(5, actualMenuCellList.size());
        assertEquals(2, actualMenuCellList.get(4).getSubCells().size());
        assertEquals(1, actualMenuCellList.get(4).getSubCells().get(0).getSubCells().size());
        assertEquals(0, actualMenuCellList.get(4).getSubCells().get(1).getSubCells().size());
        
        // Add cell c5-2-1
        menuCellToAdd = newMenuList.get(0).getSubCells().get(1).getSubCells().get(0);
        cellAdded = MenuReplaceUtilities.addCellWithCellId(menuCellToAdd.getCellId(), 0, newMenuList, actualMenuCellList);
        assertTrue(cellAdded);
        expectedMenuCellList.get(4).getSubCells().get(1).getSubCells().add(0, cloneMenuCellAndRemoveSubCells(menuCellToAdd));
        assertEquals(expectedMenuCellList, actualMenuCellList);
        assertEquals(5, actualMenuCellList.size());
        assertEquals(2, actualMenuCellList.get(4).getSubCells().size());
        assertEquals(1, actualMenuCellList.get(4).getSubCells().get(0).getSubCells().size());
        assertEquals(1, actualMenuCellList.get(4).getSubCells().get(1).getSubCells().size());
    }

    @Test
    public void testShouldCellIncludeImage() {
        MenuCell menuCell;
        WindowCapability windowCapability;
        FileManager fileManager;
        List<String> voiceCommands = null;

        // Case 1
        menuCell = new MenuCell(TestValues.GENERAL_STRING, TestValues.GENERAL_ARTWORK, voiceCommands, null);
        windowCapability = createWindowCapability(true, true);
        fileManager = createMockFileManager(true);
        assertTrue(MenuReplaceUtilities.shouldCellIncludePrimaryImageFromCell(menuCell, fileManager, windowCapability));

        // Case 2 - Image are not supported
        menuCell = new MenuCell(TestValues.GENERAL_STRING, TestValues.GENERAL_ARTWORK, voiceCommands, null);
        windowCapability = createWindowCapability(false, false);
        fileManager = createMockFileManager(true);
        assertFalse(MenuReplaceUtilities.shouldCellIncludePrimaryImageFromCell(menuCell, fileManager, windowCapability));

        // Case 3 - Artwork is null
        menuCell = new MenuCell(TestValues.GENERAL_STRING, null, voiceCommands, null);
        windowCapability = createWindowCapability(true, true);
        fileManager = createMockFileManager(true);
        assertFalse(MenuReplaceUtilities.shouldCellIncludePrimaryImageFromCell(menuCell, fileManager, windowCapability));

        // Case 4 - Artwork has not been uploaded
        menuCell = new MenuCell(TestValues.GENERAL_STRING, TestValues.GENERAL_ARTWORK, voiceCommands, null);
        windowCapability = createWindowCapability(true, true);
        fileManager = createMockFileManager(false);
        assertFalse(MenuReplaceUtilities.shouldCellIncludePrimaryImageFromCell(menuCell, fileManager, windowCapability));

        // Case 5 - Artwork is static icon
        menuCell = new MenuCell(TestValues.GENERAL_STRING, TestValues.GENERAL_ARTWORK_STATIC, voiceCommands, null);
        windowCapability = createWindowCapability(true, true);
        fileManager = createMockFileManager(false);
        assertTrue(MenuReplaceUtilities.shouldCellIncludePrimaryImageFromCell(menuCell, fileManager, windowCapability));
    }

    private WindowCapability createWindowCapability (boolean supportsCmdIcon, boolean supportsSubMenuIcon) {
        WindowCapability windowCapability = new WindowCapability();
        windowCapability.setImageFields(new ArrayList<ImageField>());
        if (supportsCmdIcon) {
            windowCapability.getImageFields().add(new ImageField(ImageFieldName.cmdIcon, null));
        }
        if (supportsSubMenuIcon) {
            windowCapability.getImageFields().add(new ImageField(ImageFieldName.subMenuIcon, null));
        }
        return windowCapability;
    }

    private FileManager createMockFileManager (boolean hasUploadedFile) {
        FileManager fileManager = mock(FileManager.class);
        when(fileManager.hasUploadedFile(any(SdlArtwork.class))).thenReturn(hasUploadedFile);
        return fileManager;
    }

    private MenuCell cloneMenuCellAndRemoveSubCells(MenuCell menuCell) {
        MenuCell clonedCell = menuCell.clone();
        if (clonedCell.getSubCells() != null) {
            clonedCell.getSubCells().clear();
        }
        return clonedCell;
    }

    private List<MenuCell> createMenuCellList() {
        /*

        c1            c2            c3            c4
                     /  \                        /  \
                    /    \                      /    \
                 c2-1   c2-2                  c4-1  c4-2
                        /  \
                       /    \
                   c2-2-1   c2-2-2

        */

        SdlArtwork sdlArtwork = null;
        List<String> voiceCommands = null;
        MenuSelectionListener listener = null;
        MenuLayout subMenuLayout = null;

        MenuCell menuCell1 = new MenuCell("c1", sdlArtwork, voiceCommands, listener);

        MenuCell menuCell2_1 = new MenuCell("c2_1", sdlArtwork, voiceCommands, listener);
        MenuCell menuCell2_2_1 = new MenuCell("c2_2_1", sdlArtwork, voiceCommands, listener);
        MenuCell menuCell2_2_2 = new MenuCell("c2_2_2", sdlArtwork, voiceCommands, listener);
        MenuCell menuCell2_2 = new MenuCell("c2_2", subMenuLayout, sdlArtwork, new ArrayList<>(Arrays.asList(menuCell2_2_1, menuCell2_2_2)));
        MenuCell menuCell2 = new MenuCell("c2", subMenuLayout, sdlArtwork, new ArrayList<>(Arrays.asList(menuCell2_1, menuCell2_2)));

        MenuCell menuCell3 = new MenuCell("c3", sdlArtwork, voiceCommands, listener);

        MenuCell menuCell4_1 = new MenuCell("c4_1", sdlArtwork, voiceCommands, listener);
        MenuCell menuCell4_2 = new MenuCell("c4_2", sdlArtwork, voiceCommands, listener);
        MenuCell menuCell4 = new MenuCell("c4", subMenuLayout, sdlArtwork, new ArrayList<>(Arrays.asList(menuCell4_1, menuCell4_2)));

        List<MenuCell> menuCellList = new ArrayList<>(Arrays.asList(menuCell1, menuCell2, menuCell3, menuCell4));
        addIdsToMenuCells(menuCellList, parentIdNotFound);

        return menuCellList ;
    }

    private List<MenuCell> createNewMenuList() {
        /*

                 c5
                / \
               /   \
            c5-1   c5-2
            /       /
           /       /
       c5-1-1   c5-2-1

         */

        SdlArtwork sdlArtwork = null;
        List<String> voiceCommands = null;
        MenuSelectionListener listener = null;
        MenuLayout subMenuLayout = null;

        MenuCell menuCell5_1_1 = new MenuCell("c5_1_1", sdlArtwork, voiceCommands, listener);
        MenuCell menuCell5_1 = new MenuCell("c5_1", subMenuLayout, sdlArtwork, new ArrayList<>(Arrays.asList(menuCell5_1_1)));
        MenuCell menuCell5_2_1 = new MenuCell("c5_2_1", sdlArtwork, voiceCommands, listener);
        MenuCell menuCell5_2 = new MenuCell("c5_2", subMenuLayout, sdlArtwork, new ArrayList<>(Arrays.asList(menuCell5_2_1)));
        MenuCell menuCell5 = new MenuCell("c5", subMenuLayout, sdlArtwork, new ArrayList<>(Arrays.asList(menuCell5_1, menuCell5_2)));

        List<MenuCell> newMenuList = new ArrayList<>(Arrays.asList(menuCell5));
        addIdsToMenuCells(newMenuList, parentIdNotFound);

        return newMenuList ;
    }
}