diff --git a/todoapp/app/src/main/java/com/example/android/architecture/blueprints/todoapp/addedittask/AddEditTaskPresenter.java b/todoapp/app/src/main/java/com/example/android/architecture/blueprints/todoapp/addedittask/AddEditTaskPresenter.java index 689797a06..261ea2536 100644 --- a/todoapp/app/src/main/java/com/example/android/architecture/blueprints/todoapp/addedittask/AddEditTaskPresenter.java +++ b/todoapp/app/src/main/java/com/example/android/architecture/blueprints/todoapp/addedittask/AddEditTaskPresenter.java @@ -150,7 +150,13 @@ private void updateTask(String title, String description) { if (isNewTask()) { throw new RuntimeException("updateTask() was called but task is new."); } - mTasksRepository.saveTask(new Task(title, description, mTaskId)); - mAddTaskView.showTasksList(); // After an edit, go back to the list. + + Task newTask = new Task(title, description, mTaskId); + if (newTask.isEmpty()) { + mAddTaskView.showEmptyTaskError(); + } else { + mTasksRepository.saveTask(newTask); + mAddTaskView.showTasksList(); // After an edit, go back to the list. + } } } diff --git a/todoapp/app/src/test/java/com/example/android/architecture/blueprints/todoapp/addedittask/AddEditTaskPresenterTest.java b/todoapp/app/src/test/java/com/example/android/architecture/blueprints/todoapp/addedittask/AddEditTaskPresenterTest.java index c3314aa00..96adaead1 100644 --- a/todoapp/app/src/test/java/com/example/android/architecture/blueprints/todoapp/addedittask/AddEditTaskPresenterTest.java +++ b/todoapp/app/src/test/java/com/example/android/architecture/blueprints/todoapp/addedittask/AddEditTaskPresenterTest.java @@ -101,6 +101,19 @@ public void saveTask_emptyTaskShowsErrorUi() { verify(mAddEditTaskView).showEmptyTaskError(); } + @Test + public void saveExistingTaskWithEmptyNameToRepository_emptyTaskShowsErrorUi() { + // Get a reference to the class under test + mAddEditTaskPresenter = new AddEditTaskPresenter( + "1", mTasksRepository, mAddEditTaskView, true, mSchedulerProvider); + + // When the presenter is asked to save an empty task + mAddEditTaskPresenter.saveTask("", ""); + + // Then an empty not error is shown in the UI + verify(mAddEditTaskView).showEmptyTaskError(); + } + @Test public void saveExistingTaskToRepository_showsSuccessMessageUi() { // Get a reference to the class under test