MoodleViewModel extends AndroidViewModel { @VisibleForTesting List<MoodleAssignmentCourse> filterAssignmentCourses(List<MoodleAssignmentCourse> assignmentCourses) { List<MoodleAssignmentCourse> filteredCourses = new ArrayList<>(); for (MoodleAssignmentCourse course : assignmentCourses) { int nbAssignments = course.getAssignments().size(); if (nbAssignments != 0) { if (!isDisplayPastAssignments()) { for (MoodleAssignment assignment : course.getAssignments()) { Date dueDate = assignment.getDueDateObj(); Date currentDate = new Date(); if (dueDate.before(currentDate)) nbAssignments--; } } if (nbAssignments != 0) filteredCourses.add(course); } } return filteredCourses; } MoodleViewModel(Application application, MoodleRepository moodleRepository); boolean isShowCaseHasBeenDisplayed(); void setShowCaseHasBeenDisplayed(boolean display); LiveData<RemoteResource<List<MoodleCourse>>> getCourses(); LiveData<RemoteResource<List<MoodleAssignmentCourse>>> getAssignmentCourses(); LiveData<RemoteResource<MoodleAssignmentSubmission>> getAssignmentSubmission(int assignId); void setDisplayPastAssignments(boolean display); boolean isDisplayPastAssignments(); void setAssignmentsSortIndex(int index); int getAssignmentsSortIndex(); Comparator<MoodleAssignment> getAssignmentsSortComparator(); void selectAssignment(MoodleAssignment selectedAssignment); LiveData<MoodleAssignment> getSelectedAssignment(); static final int SORT_BY_DATE; static final int SORT_ALPHA; }
@Test public void filterAssignmentCoursesNoAssignments() { createMockSharedPreferences(); List<MoodleAssignmentCourse> courses = new ArrayList<>(); assertEquals(0, viewModel.filterAssignmentCourses(courses).size()); List<MoodleAssignment> assignments = new ArrayList<>(); MoodleAssignment assignment = new MoodleAssignment(); long unixTimeFuture = (TimeUnit.MILLISECONDS.toSeconds(new Date().getTime()) + TimeUnit.DAYS.toSeconds(1)); assignment.setDueDate(unixTimeFuture); courses.add(new MoodleAssignmentCourse(0, "", "", 1510358400, assignments)); assertEquals(0, viewModel.filterAssignmentCourses(courses).size()); assignments.add(assignment); assertEquals(1, viewModel.filterAssignmentCourses(courses).size()); }
@Test public void filterAssignmentsCoursesPastAssignments() { createMockSharedPreferences(); long unixTimeNowMs = (TimeUnit.MILLISECONDS.toSeconds(new Date().getTime())); long unixTimeFutureSec = unixTimeNowMs + TimeUnit.DAYS.toSeconds(1); long unixTimePastSec = unixTimeNowMs - TimeUnit.DAYS.toSeconds(1); List<MoodleAssignment> assignments = new ArrayList<>(); MoodleAssignment futureAssignment = new MoodleAssignment(); futureAssignment.setDueDate(unixTimeFutureSec); MoodleAssignment pastAssignment = new MoodleAssignment(); pastAssignment.setDueDate(unixTimePastSec); List<MoodleAssignmentCourse> courses = new ArrayList<>(); assertEquals(0, viewModel.filterAssignmentCourses(courses).size()); assignments.add(pastAssignment); courses.add(new MoodleAssignmentCourse(0, "", "", 1510358400, assignments)); assertEquals(0, viewModel.filterAssignmentCourses(courses).size()); assignments.add(futureAssignment); assertEquals(1, viewModel.filterAssignmentCourses(courses).size()); assignments.clear(); assignments.add(futureAssignment); courses.add(new MoodleAssignmentCourse(0, "", "", 1510358400, assignments)); assertEquals(2, viewModel.filterAssignmentCourses(courses).size()); }