From 210fa0493bfe051ed58352ed3f328428b59bbbcd Mon Sep 17 00:00:00 2001 From: grzegorz Date: Tue, 21 May 2019 13:53:08 +0200 Subject: [PATCH] Validate number of columns, it should be between 1 and 10 --- .../utilities/WidgetAppearanceUtils.java | 6 ++++++ .../utilities/WidgetAppearanceUtilsTest.java | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/collect_app/src/main/java/org/odk/collect/android/utilities/WidgetAppearanceUtils.java b/collect_app/src/main/java/org/odk/collect/android/utilities/WidgetAppearanceUtils.java index 75b4b110612..ae30aad781a 100644 --- a/collect_app/src/main/java/org/odk/collect/android/utilities/WidgetAppearanceUtils.java +++ b/collect_app/src/main/java/org/odk/collect/android/utilities/WidgetAppearanceUtils.java @@ -122,6 +122,12 @@ public static int getNumberOfColumns(FormEntryPrompt formEntryPrompt, Context co numColumns = Integer.parseInt(substringFromNumColumns.substring(0, substringFromNumColumns.contains(" ") ? substringFromNumColumns.indexOf(' ') : substringFromNumColumns.length())); + + if (numColumns < 1) { + numColumns = 1; + } else if (numColumns > 10) { + numColumns = 10; + } } } catch (Exception e) { Timber.e(EXCEPTION_PARSING_COLUMNS); diff --git a/collect_app/src/test/java/org/odk/collect/android/utilities/WidgetAppearanceUtilsTest.java b/collect_app/src/test/java/org/odk/collect/android/utilities/WidgetAppearanceUtilsTest.java index 4fecb015d0b..7278dac6c1a 100644 --- a/collect_app/src/test/java/org/odk/collect/android/utilities/WidgetAppearanceUtilsTest.java +++ b/collect_app/src/test/java/org/odk/collect/android/utilities/WidgetAppearanceUtilsTest.java @@ -64,5 +64,23 @@ public void getNumberOfColumnsTest() { when(formEntryPrompt.getAppearanceHint()).thenReturn("compact-5"); assertEquals(5, WidgetAppearanceUtils.getNumberOfColumns(formEntryPrompt, null)); + + when(formEntryPrompt.getAppearanceHint()).thenReturn("compact-9"); + assertEquals(9, WidgetAppearanceUtils.getNumberOfColumns(formEntryPrompt, null)); + + when(formEntryPrompt.getAppearanceHint()).thenReturn("columns-9"); + assertEquals(9, WidgetAppearanceUtils.getNumberOfColumns(formEntryPrompt, null)); + + when(formEntryPrompt.getAppearanceHint()).thenReturn("compact-11"); + assertEquals(10, WidgetAppearanceUtils.getNumberOfColumns(formEntryPrompt, null)); + + when(formEntryPrompt.getAppearanceHint()).thenReturn("columns-11"); + assertEquals(10, WidgetAppearanceUtils.getNumberOfColumns(formEntryPrompt, null)); + + when(formEntryPrompt.getAppearanceHint()).thenReturn("columns--1"); + assertEquals(1, WidgetAppearanceUtils.getNumberOfColumns(formEntryPrompt, null)); + + when(formEntryPrompt.getAppearanceHint()).thenReturn("columns--10"); + assertEquals(1, WidgetAppearanceUtils.getNumberOfColumns(formEntryPrompt, null)); } }