Skip to content

Commit bb7cdbb

Browse files
authored
Merge pull request #1639 from dimagi/persistentMenuAutoSelectFix
Ensure Unique Menu Entries in Persistent Menu
2 parents 5d405bf + b3af85f commit bb7cdbb

File tree

6 files changed

+49
-22
lines changed

6 files changed

+49
-22
lines changed

src/main/java/org/commcare/formplayer/session/PersistentMenuHelper.kt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,17 @@ class PersistentMenuHelper(val isPersistentMenuEnabled: Boolean) {
7070
private fun addPersistentCommand(command: PersistentCommand) {
7171
// currentMenu!=null implies that we must have added items to persistent menu
7272
check(currentMenu == null || persistentMenu.size > 0)
73-
if (currentMenu == null) {
74-
persistentMenu.add(command)
75-
} else {
76-
currentMenu!!.addCommand(command)
73+
if (isCommandNotPresent(command)) {
74+
if (currentMenu == null) {
75+
persistentMenu.add(command)
76+
} else {
77+
currentMenu!!.addCommand(command)
78+
}
7779
}
7880
}
81+
82+
private fun isCommandNotPresent(command: PersistentCommand): Boolean {
83+
val currentCommands = if (currentMenu == null) persistentMenu else currentMenu!!.commands
84+
return currentCommands.firstOrNull { persistentCommand -> persistentCommand.index == command.index } == null
85+
}
7986
}

src/test/java/org/commcare/formplayer/mocks/FormPlayerPropertyManagerMock.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ public boolean isPersistentMenuEnabled() {
5757
}
5858

5959
// convenience method to set auto advance menu as true
60-
public static void mockAutoAdvanceMenu(FormplayerStorageFactory storageFactoryMock) {
60+
public static void mockAutoAdvanceMenu(FormplayerStorageFactory storageFactoryMock, boolean enable) {
6161
SQLiteDB db = storageFactoryMock.getSQLiteDB();
6262
FormPlayerPropertyManagerMock propertyManagerMock = new FormPlayerPropertyManagerMock(
6363
new SqlStorage(db, Property.class, PropertyManager.STORAGE_KEY));
64-
propertyManagerMock.enableAutoAdvanceMenu(true);
64+
propertyManagerMock.enableAutoAdvanceMenu(enable);
6565
when(storageFactoryMock.getPropertyManager()).thenReturn(propertyManagerMock);
6666
}
6767
}

src/test/java/org/commcare/formplayer/tests/AutoAdvanceMenuInNestedMultiSelectList.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class AutoAdvanceMenuInNestedMultiSelectList : BaseTestClass() {
4141

4242
@Test
4343
fun testAutoAdvanceMenuInNestedMultiSelectList() {
44-
FormPlayerPropertyManagerMock.mockAutoAdvanceMenu(storageFactoryMock)
44+
FormPlayerPropertyManagerMock.mockAutoAdvanceMenu(storageFactoryMock, true)
4545
mockRequest.mockQuery(
4646
"query_responses/case_search_multi_select_response.xml", 2
4747
).use {

src/test/java/org/commcare/formplayer/tests/EndpointLaunchTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public void setUp() throws Exception {
4343
super.setUp();
4444
configureRestoreFactory("endpointdomain", "endpointusername");
4545
storageFactoryMock.configure("endpointusername", "endpointdomain", "app_id", "asUser");
46-
FormPlayerPropertyManagerMock.mockAutoAdvanceMenu(storageFactoryMock);
46+
FormPlayerPropertyManagerMock.mockAutoAdvanceMenu(storageFactoryMock, true);
4747
mockRequest = new MockRequestUtils(webClientMock, restoreFactoryMock);
4848
}
4949

src/test/java/org/commcare/formplayer/tests/MultiSelectCaseClaimTest.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import static org.mockito.Mockito.times;
1010
import static org.mockito.Mockito.verify;
1111

12+
import com.google.common.collect.ImmutableList;
1213
import com.google.common.collect.Multimap;
1314

1415
import org.apache.commons.lang3.ArrayUtils;
@@ -207,17 +208,22 @@ public void testAutoSelection() throws Exception {
207208
ArrayList<PersistentCommand> subMenu = reponse.getPersistentMenu().get(2).getCommands();
208209
assertEquals(1, subMenu.size());
209210
assertEquals("Close", subMenu.get(0).getDisplayText()); // directly contains the form instead of entity selection
210-
assertEquals(2, reponse.getBreadcrumbs().length);
211+
assertEquals(ImmutableList.of("Case Claim", "Follow Up"),
212+
Arrays.stream(reponse.getBreadcrumbs()).toList());
211213
}
212214

213-
ArrayList<String> updatedSelections = new ArrayList<>();
214-
updatedSelections.addAll(Arrays.asList(reponse.getSelections()));
215+
ArrayList<String> updatedSelections = new ArrayList<>(Arrays.asList(reponse.getSelections()));
215216
updatedSelections.add("0");
216217

217-
sessionNavigateWithQuery(updatedSelections.toArray(new String[0]),
218+
NewFormResponse formResponse = sessionNavigateWithQuery(updatedSelections.toArray(new String[0]),
218219
APP_NAME,
219220
null,
220-
FormEntryResponseBean.class);
221+
NewFormResponse.class);
222+
ArrayList<PersistentCommand> subMenu = formResponse.getPersistentMenu().get(2).getCommands();
223+
assertEquals(1, subMenu.size());
224+
assertEquals("Close", subMenu.get(0).getDisplayText());
225+
assertEquals(ImmutableList.of("Case Claim", "Follow Up", "Close"),
226+
Arrays.stream(formResponse.getBreadcrumbs()).toList());
221227
}
222228

223229
@Test
@@ -241,7 +247,7 @@ public void testAutoSelection_WithNoCases() throws Exception {
241247

242248
@Test
243249
public void testAutoAdvanceMenuWithCaseSearch() throws Exception {
244-
FormPlayerPropertyManagerMock.mockAutoAdvanceMenu(storageFactoryMock);
250+
FormPlayerPropertyManagerMock.mockAutoAdvanceMenu(storageFactoryMock, true);
245251
try (MockRequestUtils.VerifiedMock ignore = mockRequest.mockQuery(
246252
"query_responses/case_search_multi_select_response.xml")) {
247253
EntityListResponse entityResp = sessionNavigateWithQuery(new String[]{"1"},

src/test/java/org/commcare/formplayer/tests/MultiSelectCaseListTest.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public void setUp() throws Exception {
4747
super.setUp();
4848
configureRestoreFactory("caseclaimdomain", "caseclaimusername");
4949
storageFactoryMock.configure("user", "domain", "app_id", "asUser");
50-
FormPlayerPropertyManagerMock.mockAutoAdvanceMenu(storageFactoryMock);
50+
FormPlayerPropertyManagerMock.mockAutoAdvanceMenu(storageFactoryMock, true);
5151
}
5252

5353
@Override
@@ -275,20 +275,34 @@ public void testPersistentMenuWithAutoSelect() throws Exception {
275275
expectedMenu.add(new PersistentCommand("1", "Case List", null, NavIconState.NEXT));
276276
expectedMenu.add(new PersistentCommand("2", "Menu with Auto Submit Form", null, NavIconState.NEXT));
277277
expectedMenu.add(new PersistentCommand("3", "Single Form Auto Select", null, NavIconState.NEXT));
278+
PersistentCommand firstMenu = expectedMenu.get(0);
279+
firstMenu.addCommand(new PersistentCommand("0","Registration Form", null, NavIconState.JUMP));
280+
firstMenu.addCommand(new PersistentCommand("1","Followup Form", null, NavIconState.JUMP));
281+
firstMenu.addCommand(new PersistentCommand("2","Followup Form with AutoSelect Datum", "jr://file/commcare/image/m0f2customicon_en.png", NavIconState.NEXT));
282+
firstMenu.addCommand(new PersistentCommand("3","Followup Form with AutoSelect Datum", null, NavIconState.NEXT));
283+
String[] selections = new String[]{"0", "2"};
284+
NewFormResponse formResponse = sessionNavigate(selections, APP, NewFormResponse.class);
285+
assertEquals(expectedMenu, formResponse.getPersistentMenu());
286+
}
287+
288+
@Test
289+
public void testPersistentMenuWithAutoAdvance() throws Exception {
290+
ArrayList<PersistentCommand> expectedMenu = new ArrayList<>();
291+
expectedMenu.add(new PersistentCommand("0", "Case List", "jr://file/commcare/image/m0customicon_en.png", NavIconState.NEXT));
292+
expectedMenu.add(new PersistentCommand("1", "Case List", null, NavIconState.NEXT));
293+
expectedMenu.add(new PersistentCommand("2", "Menu with Auto Submit Form", null, NavIconState.NEXT));
294+
expectedMenu.add(new PersistentCommand("3", "Single Form Auto Select", null, NavIconState.NEXT));
278295

279296
// Auto-Advance in a Auto Select Case List
280297
String[] selections = new String[]{"3"};
281298
NewFormResponse formResponse = sessionNavigate(selections, APP, NewFormResponse.class);
282299
assertEquals(expectedMenu, formResponse.getPersistentMenu());
283300

284-
285-
PersistentCommand firstMenu = expectedMenu.get(0);
286-
firstMenu.addCommand(new PersistentCommand("0","Registration Form", null, NavIconState.JUMP));
287-
firstMenu.addCommand(new PersistentCommand("1","Followup Form", null, NavIconState.JUMP));
288-
firstMenu.addCommand(new PersistentCommand("2","Followup Form with AutoSelect Datum", "jr://file/commcare/image/m0f2customicon_en.png", NavIconState.NEXT));
289-
firstMenu.addCommand(new PersistentCommand("3","Followup Form with AutoSelect Datum", null, NavIconState.NEXT));
290-
selections = new String[]{"0", "2"};
301+
FormPlayerPropertyManagerMock.mockAutoAdvanceMenu(storageFactoryMock, false);
302+
selections = new String[]{"3", "0"};
291303
formResponse = sessionNavigate(selections, APP, NewFormResponse.class);
304+
expectedMenu.get(3).addCommand(new PersistentCommand("0", "Followup Form with AutoSelect Datum",
305+
"jr://file/commcare/image/m0f2customicon_en.png", NavIconState.JUMP));
292306
assertEquals(expectedMenu, formResponse.getPersistentMenu());
293307
}
294308
}

0 commit comments

Comments
 (0)