MoodleViewModel extends AndroidViewModel { public LiveData<RemoteResource<List<MoodleAssignmentCourse>>> getAssignmentCourses() { if (filteredAssignmentCourses == null || filteredAssignmentCourses.getValue() == null || filteredAssignmentCourses.getValue().data == null) { filteredAssignmentCourses = new MediatorLiveData<>(); filteredAssignmentCourses.addSource(repository.getAssignmentCourses(), assignmentCoursesRes -> { assignmentCourses.setValue(assignmentCoursesRes); if (assignmentCoursesRes != null && assignmentCoursesRes.data != null) { setFilteredAssignmentCourses(assignmentCoursesRes); } else { filteredAssignmentCourses.setValue(assignmentCoursesRes); } }); } return filteredAssignmentCourses; } 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 getAssignmentCourses() { MutableLiveData<RemoteResource<List<MoodleAssignmentCourse>>> liveData = new MutableLiveData<>(); when(repository.getAssignmentCourses()).thenReturn(liveData); Observer<RemoteResource<List<MoodleAssignmentCourse>>> observer = mock(Observer.class); viewModel.getAssignmentCourses().observeForever(observer); verify(observer, never()).onChanged(any(RemoteResource.class)); RemoteResource<List<MoodleAssignmentCourse>> remoteRes = RemoteResource.loading(null); liveData.setValue(remoteRes); verify(observer).onChanged(remoteRes); reset(observer); List<MoodleAssignmentCourse> list = new ArrayList<>(); remoteRes = RemoteResource.success(list); liveData.setValue(remoteRes); verify(observer).onChanged(any(RemoteResource.class)); }