MoodleViewModel extends AndroidViewModel { public LiveData<RemoteResource<MoodleAssignmentSubmission>> getAssignmentSubmission(int assignId) { if (assignmentSubmission == null || assignmentSubmission.getValue() == null || assignmentSubmission.getValue().data == null || assignmentSubmission.getValue().data.getFeedback() == null || assignmentSubmission.getValue().data.getFeedback().getGrade() == null || assignmentSubmission.getValue().data.getFeedback().getGrade().getAssignment() != assignId) { this.assignmentSubmission = repository.getAssignmentSubmission(assignId); } return assignmentSubmission; } 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 getAssignmentSubmission() { MutableLiveData<RemoteResource<MoodleAssignmentSubmission>> liveData = new MutableLiveData<>(); when(repository.getAssignmentSubmission(anyInt())).thenReturn(liveData); Observer<RemoteResource<MoodleAssignmentSubmission>> observer = mock(Observer.class); viewModel.getAssignmentSubmission(anyInt()).observeForever(observer); verify(observer, never()).onChanged(any(RemoteResource.class)); RemoteResource<MoodleAssignmentSubmission> remoteRes = RemoteResource.loading(null); liveData.setValue(remoteRes); verify(observer).onChanged(remoteRes); reset(observer); remoteRes = RemoteResource.success(new MoodleAssignmentSubmission()); liveData.setValue(remoteRes); verify(observer).onChanged(remoteRes); }