From 26eb5fab4278d2b1bb1357abbeb569aa29409bf4 Mon Sep 17 00:00:00 2001 From: Shubham Goyal Date: Wed, 6 Nov 2024 12:19:20 +0530 Subject: [PATCH 1/3] Pass enable in method param for better control testing --- .../formplayer/mocks/FormPlayerPropertyManagerMock.java | 4 ++-- .../tests/AutoAdvanceMenuInNestedMultiSelectList.kt | 2 +- .../org/commcare/formplayer/tests/EndpointLaunchTest.java | 2 +- .../commcare/formplayer/tests/MultiSelectCaseClaimTest.java | 2 +- .../commcare/formplayer/tests/MultiSelectCaseListTest.java | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/test/java/org/commcare/formplayer/mocks/FormPlayerPropertyManagerMock.java b/src/test/java/org/commcare/formplayer/mocks/FormPlayerPropertyManagerMock.java index 0bbe69f7a..9316e5d3b 100644 --- a/src/test/java/org/commcare/formplayer/mocks/FormPlayerPropertyManagerMock.java +++ b/src/test/java/org/commcare/formplayer/mocks/FormPlayerPropertyManagerMock.java @@ -57,11 +57,11 @@ public boolean isPersistentMenuEnabled() { } // convenience method to set auto advance menu as true - public static void mockAutoAdvanceMenu(FormplayerStorageFactory storageFactoryMock) { + public static void mockAutoAdvanceMenu(FormplayerStorageFactory storageFactoryMock, boolean enable) { SQLiteDB db = storageFactoryMock.getSQLiteDB(); FormPlayerPropertyManagerMock propertyManagerMock = new FormPlayerPropertyManagerMock( new SqlStorage(db, Property.class, PropertyManager.STORAGE_KEY)); - propertyManagerMock.enableAutoAdvanceMenu(true); + propertyManagerMock.enableAutoAdvanceMenu(enable); when(storageFactoryMock.getPropertyManager()).thenReturn(propertyManagerMock); } } diff --git a/src/test/java/org/commcare/formplayer/tests/AutoAdvanceMenuInNestedMultiSelectList.kt b/src/test/java/org/commcare/formplayer/tests/AutoAdvanceMenuInNestedMultiSelectList.kt index 14ee3cbd4..39870b4b3 100644 --- a/src/test/java/org/commcare/formplayer/tests/AutoAdvanceMenuInNestedMultiSelectList.kt +++ b/src/test/java/org/commcare/formplayer/tests/AutoAdvanceMenuInNestedMultiSelectList.kt @@ -41,7 +41,7 @@ class AutoAdvanceMenuInNestedMultiSelectList : BaseTestClass() { @Test fun testAutoAdvanceMenuInNestedMultiSelectList() { - FormPlayerPropertyManagerMock.mockAutoAdvanceMenu(storageFactoryMock) + FormPlayerPropertyManagerMock.mockAutoAdvanceMenu(storageFactoryMock, true) mockRequest.mockQuery( "query_responses/case_search_multi_select_response.xml", 2 ).use { diff --git a/src/test/java/org/commcare/formplayer/tests/EndpointLaunchTest.java b/src/test/java/org/commcare/formplayer/tests/EndpointLaunchTest.java index 6d9b9b577..3d5d56caa 100644 --- a/src/test/java/org/commcare/formplayer/tests/EndpointLaunchTest.java +++ b/src/test/java/org/commcare/formplayer/tests/EndpointLaunchTest.java @@ -43,7 +43,7 @@ public void setUp() throws Exception { super.setUp(); configureRestoreFactory("endpointdomain", "endpointusername"); storageFactoryMock.configure("endpointusername", "endpointdomain", "app_id", "asUser"); - FormPlayerPropertyManagerMock.mockAutoAdvanceMenu(storageFactoryMock); + FormPlayerPropertyManagerMock.mockAutoAdvanceMenu(storageFactoryMock, true); mockRequest = new MockRequestUtils(webClientMock, restoreFactoryMock); } diff --git a/src/test/java/org/commcare/formplayer/tests/MultiSelectCaseClaimTest.java b/src/test/java/org/commcare/formplayer/tests/MultiSelectCaseClaimTest.java index 168cbc699..6d7ecd5b5 100644 --- a/src/test/java/org/commcare/formplayer/tests/MultiSelectCaseClaimTest.java +++ b/src/test/java/org/commcare/formplayer/tests/MultiSelectCaseClaimTest.java @@ -241,7 +241,7 @@ public void testAutoSelection_WithNoCases() throws Exception { @Test public void testAutoAdvanceMenuWithCaseSearch() throws Exception { - FormPlayerPropertyManagerMock.mockAutoAdvanceMenu(storageFactoryMock); + FormPlayerPropertyManagerMock.mockAutoAdvanceMenu(storageFactoryMock, true); try (MockRequestUtils.VerifiedMock ignore = mockRequest.mockQuery( "query_responses/case_search_multi_select_response.xml")) { EntityListResponse entityResp = sessionNavigateWithQuery(new String[]{"1"}, diff --git a/src/test/java/org/commcare/formplayer/tests/MultiSelectCaseListTest.java b/src/test/java/org/commcare/formplayer/tests/MultiSelectCaseListTest.java index 0498a5ed7..9ff0086ba 100644 --- a/src/test/java/org/commcare/formplayer/tests/MultiSelectCaseListTest.java +++ b/src/test/java/org/commcare/formplayer/tests/MultiSelectCaseListTest.java @@ -47,7 +47,7 @@ public void setUp() throws Exception { super.setUp(); configureRestoreFactory("caseclaimdomain", "caseclaimusername"); storageFactoryMock.configure("user", "domain", "app_id", "asUser"); - FormPlayerPropertyManagerMock.mockAutoAdvanceMenu(storageFactoryMock); + FormPlayerPropertyManagerMock.mockAutoAdvanceMenu(storageFactoryMock, true); } @Override From 62adf0bcfa26afcc6850283067613023ded3aa68 Mon Sep 17 00:00:00 2001 From: Shubham Goyal Date: Wed, 6 Nov 2024 14:13:49 +0530 Subject: [PATCH 2/3] More persistent menu tests --- .../tests/MultiSelectCaseClaimTest.java | 16 +++++++---- .../tests/MultiSelectCaseListTest.java | 28 ++++++++++++++----- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/src/test/java/org/commcare/formplayer/tests/MultiSelectCaseClaimTest.java b/src/test/java/org/commcare/formplayer/tests/MultiSelectCaseClaimTest.java index 6d7ecd5b5..4fcdc4d7a 100644 --- a/src/test/java/org/commcare/formplayer/tests/MultiSelectCaseClaimTest.java +++ b/src/test/java/org/commcare/formplayer/tests/MultiSelectCaseClaimTest.java @@ -9,6 +9,7 @@ import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import com.google.common.collect.ImmutableList; import com.google.common.collect.Multimap; import org.apache.commons.lang3.ArrayUtils; @@ -207,17 +208,22 @@ public void testAutoSelection() throws Exception { ArrayList subMenu = reponse.getPersistentMenu().get(2).getCommands(); assertEquals(1, subMenu.size()); assertEquals("Close", subMenu.get(0).getDisplayText()); // directly contains the form instead of entity selection - assertEquals(2, reponse.getBreadcrumbs().length); + assertEquals(ImmutableList.of("Case Claim", "Follow Up"), + Arrays.stream(reponse.getBreadcrumbs()).toList()); } - ArrayList updatedSelections = new ArrayList<>(); - updatedSelections.addAll(Arrays.asList(reponse.getSelections())); + ArrayList updatedSelections = new ArrayList<>(Arrays.asList(reponse.getSelections())); updatedSelections.add("0"); - sessionNavigateWithQuery(updatedSelections.toArray(new String[0]), + NewFormResponse formResponse = sessionNavigateWithQuery(updatedSelections.toArray(new String[0]), APP_NAME, null, - FormEntryResponseBean.class); + NewFormResponse.class); + ArrayList subMenu = formResponse.getPersistentMenu().get(2).getCommands(); + assertEquals(1, subMenu.size()); + assertEquals("Close", subMenu.get(0).getDisplayText()); + assertEquals(ImmutableList.of("Case Claim", "Follow Up", "Close"), + Arrays.stream(formResponse.getBreadcrumbs()).toList()); } @Test diff --git a/src/test/java/org/commcare/formplayer/tests/MultiSelectCaseListTest.java b/src/test/java/org/commcare/formplayer/tests/MultiSelectCaseListTest.java index 9ff0086ba..985a434fe 100644 --- a/src/test/java/org/commcare/formplayer/tests/MultiSelectCaseListTest.java +++ b/src/test/java/org/commcare/formplayer/tests/MultiSelectCaseListTest.java @@ -275,20 +275,34 @@ public void testPersistentMenuWithAutoSelect() throws Exception { expectedMenu.add(new PersistentCommand("1", "Case List", null, NavIconState.NEXT)); expectedMenu.add(new PersistentCommand("2", "Menu with Auto Submit Form", null, NavIconState.NEXT)); expectedMenu.add(new PersistentCommand("3", "Single Form Auto Select", null, NavIconState.NEXT)); + PersistentCommand firstMenu = expectedMenu.get(0); + firstMenu.addCommand(new PersistentCommand("0","Registration Form", null, NavIconState.JUMP)); + firstMenu.addCommand(new PersistentCommand("1","Followup Form", null, NavIconState.JUMP)); + firstMenu.addCommand(new PersistentCommand("2","Followup Form with AutoSelect Datum", "jr://file/commcare/image/m0f2customicon_en.png", NavIconState.NEXT)); + firstMenu.addCommand(new PersistentCommand("3","Followup Form with AutoSelect Datum", null, NavIconState.NEXT)); + String[] selections = new String[]{"0", "2"}; + NewFormResponse formResponse = sessionNavigate(selections, APP, NewFormResponse.class); + assertEquals(expectedMenu, formResponse.getPersistentMenu()); + } + + @Test + public void testPersistentMenuWithAutoAdvance() throws Exception { + ArrayList expectedMenu = new ArrayList<>(); + expectedMenu.add(new PersistentCommand("0", "Case List", "jr://file/commcare/image/m0customicon_en.png", NavIconState.NEXT)); + expectedMenu.add(new PersistentCommand("1", "Case List", null, NavIconState.NEXT)); + expectedMenu.add(new PersistentCommand("2", "Menu with Auto Submit Form", null, NavIconState.NEXT)); + expectedMenu.add(new PersistentCommand("3", "Single Form Auto Select", null, NavIconState.NEXT)); // Auto-Advance in a Auto Select Case List String[] selections = new String[]{"3"}; NewFormResponse formResponse = sessionNavigate(selections, APP, NewFormResponse.class); assertEquals(expectedMenu, formResponse.getPersistentMenu()); - - PersistentCommand firstMenu = expectedMenu.get(0); - firstMenu.addCommand(new PersistentCommand("0","Registration Form", null, NavIconState.JUMP)); - firstMenu.addCommand(new PersistentCommand("1","Followup Form", null, NavIconState.JUMP)); - firstMenu.addCommand(new PersistentCommand("2","Followup Form with AutoSelect Datum", "jr://file/commcare/image/m0f2customicon_en.png", NavIconState.NEXT)); - firstMenu.addCommand(new PersistentCommand("3","Followup Form with AutoSelect Datum", null, NavIconState.NEXT)); - selections = new String[]{"0", "2"}; + FormPlayerPropertyManagerMock.mockAutoAdvanceMenu(storageFactoryMock, false); + selections = new String[]{"3", "0"}; formResponse = sessionNavigate(selections, APP, NewFormResponse.class); + expectedMenu.get(3).addCommand(new PersistentCommand("0", "Followup Form with AutoSelect Datum", + "jr://file/commcare/image/m0f2customicon_en.png", NavIconState.JUMP)); assertEquals(expectedMenu, formResponse.getPersistentMenu()); } } From b3af85fd25530cc38b7235224aa6e86337ec1295 Mon Sep 17 00:00:00 2001 From: Shubham Goyal Date: Wed, 6 Nov 2024 14:28:00 +0530 Subject: [PATCH 3/3] Ensure unique additions based on index for persistent menu --- .../formplayer/session/PersistentMenuHelper.kt | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/commcare/formplayer/session/PersistentMenuHelper.kt b/src/main/java/org/commcare/formplayer/session/PersistentMenuHelper.kt index 7a27b5c54..0565b5e59 100644 --- a/src/main/java/org/commcare/formplayer/session/PersistentMenuHelper.kt +++ b/src/main/java/org/commcare/formplayer/session/PersistentMenuHelper.kt @@ -70,10 +70,17 @@ class PersistentMenuHelper(val isPersistentMenuEnabled: Boolean) { private fun addPersistentCommand(command: PersistentCommand) { // currentMenu!=null implies that we must have added items to persistent menu check(currentMenu == null || persistentMenu.size > 0) - if (currentMenu == null) { - persistentMenu.add(command) - } else { - currentMenu!!.addCommand(command) + if (isCommandNotPresent(command)) { + if (currentMenu == null) { + persistentMenu.add(command) + } else { + currentMenu!!.addCommand(command) + } } } + + private fun isCommandNotPresent(command: PersistentCommand): Boolean { + val currentCommands = if (currentMenu == null) persistentMenu else currentMenu!!.commands + return currentCommands.firstOrNull { persistentCommand -> persistentCommand.index == command.index } == null + } }