From 179906be922bb8737764a9ffacccf5172023755c Mon Sep 17 00:00:00 2001 From: "S. Kozyr" Date: Wed, 9 Feb 2022 10:38:35 +0200 Subject: [PATCH] Added function PlaceOpeningHoursAdapter.buildWeekByFirstDay(int firstDayOfWeek) to build array of week days. Covered it with tests. Signed-off-by: S. Kozyr --- .../placepage/PlaceOpeningHoursAdapter.java | 33 +++++++++------- .../PlaceOpeningHoursAdapterTest.java | 38 +++++++++++++++++++ 2 files changed, 57 insertions(+), 14 deletions(-) diff --git a/android/src/com/mapswithme/maps/widget/placepage/PlaceOpeningHoursAdapter.java b/android/src/com/mapswithme/maps/widget/placepage/PlaceOpeningHoursAdapter.java index b693639d4b..a3b56a6808 100644 --- a/android/src/com/mapswithme/maps/widget/placepage/PlaceOpeningHoursAdapter.java +++ b/android/src/com/mapswithme/maps/widget/placepage/PlaceOpeningHoursAdapter.java @@ -1,6 +1,5 @@ package com.mapswithme.maps.widget.placepage; -import android.util.ArraySet; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -9,21 +8,16 @@ import android.widget.TextView; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import com.mapswithme.maps.R; -import static com.mapswithme.maps.editor.data.TimeFormatUtils.formatWeekdays; -import static com.mapswithme.maps.editor.data.TimeFormatUtils.formatWeekdaysRange; -import static com.mapswithme.maps.editor.data.TimeFormatUtils.formatNonBusinessTime; - import com.mapswithme.maps.editor.data.Timespan; import com.mapswithme.maps.editor.data.Timetable; import com.mapswithme.util.UiUtils; import java.util.ArrayList; -import java.util.Arrays; -import java.util.Calendar; import java.util.Collections; -import java.util.HashSet; import java.util.List; -import java.util.Set; + +import static com.mapswithme.maps.editor.data.TimeFormatUtils.formatNonBusinessTime; +import static com.mapswithme.maps.editor.data.TimeFormatUtils.formatWeekdaysRange; public class PlaceOpeningHoursAdapter extends RecyclerView.Adapter { @@ -38,11 +32,7 @@ public class PlaceOpeningHoursAdapter extends RecyclerView.Adapter scheduleData = new ArrayList<>(); @@ -82,6 +72,21 @@ public class PlaceOpeningHoursAdapter extends RecyclerView.Adapter 7) + throw new IllegalArgumentException("First day of week "+firstDayOfWeek+" is out of range [1..7]"); + + int[] weekDays = new int[7]; + for (int i=0; i<7; i++) + { + weekDays[i] = (i + firstDayOfWeek); + if (weekDays[i] > 7) + weekDays[i] -= 7; + } + return weekDays; + } + public static Timetable findScheduleForWeekDay(Timetable[] tables, int weekDay) { for(Timetable tt : tables) diff --git a/android/tests/java/com/mapswithme/maps/widget/placepage/PlaceOpeningHoursAdapterTest.java b/android/tests/java/com/mapswithme/maps/widget/placepage/PlaceOpeningHoursAdapterTest.java index be583fe05a..36ce763f54 100644 --- a/android/tests/java/com/mapswithme/maps/widget/placepage/PlaceOpeningHoursAdapterTest.java +++ b/android/tests/java/com/mapswithme/maps/widget/placepage/PlaceOpeningHoursAdapterTest.java @@ -8,9 +8,11 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.spy; @@ -35,6 +37,42 @@ public class PlaceOpeningHoursAdapterTest { mWeekScheduleField.setAccessible(true); } + @Test + public void test_build_week_from_sunday() + { + int[] weekDays = PlaceOpeningHoursAdapter.buildWeekByFirstDay(Calendar.SUNDAY); + assertArrayEquals(weekDays, new int[]{1, 2, 3, 4, 5, 6, 7}); + } + + @Test + public void test_build_week_from_monday() + { + int[] weekDays = PlaceOpeningHoursAdapter.buildWeekByFirstDay(Calendar.MONDAY); + assertArrayEquals(weekDays, new int[]{2, 3, 4, 5, 6, 7, 1}); + } + + @Test + public void test_build_week_from_saturday() + { + int[] weekDays = PlaceOpeningHoursAdapter.buildWeekByFirstDay(Calendar.SATURDAY); + assertArrayEquals(weekDays, new int[]{7, 1, 2, 3, 4, 5, 6}); + } + + @Test + public void test_build_week_from_friday() + { + int[] weekDays = PlaceOpeningHoursAdapter.buildWeekByFirstDay(Calendar.FRIDAY); + assertArrayEquals(weekDays, new int[]{6, 7, 1, 2, 3, 4, 5}); + } + + @Test + public void test_build_week_errors() + { + assertThrows(IllegalArgumentException.class, () -> PlaceOpeningHoursAdapter.buildWeekByFirstDay(-1)); + assertThrows(IllegalArgumentException.class, () -> PlaceOpeningHoursAdapter.buildWeekByFirstDay(0)); + assertThrows(IllegalArgumentException.class, () -> PlaceOpeningHoursAdapter.buildWeekByFirstDay(8)); + } + @Test public void test_single_closed_day_sunday() throws IllegalAccessException {