Search in sources :

Example 11 with Task

use of com.example.android.architecture.blueprints.todoapp.data.Task in project todo-mvp-rxjava by albertizzy.

the class TasksRepositoryTest method deleteCompletedTasks_deleteCompletedTasksToServiceAPIUpdatesCache.

@Test
public void deleteCompletedTasks_deleteCompletedTasksToServiceAPIUpdatesCache() {
    // Given 2 stub completed tasks and 1 stub active tasks in the repository
    Task newTask = new Task(TASK_TITLE, "Some Task Description", true);
    mTasksRepository.saveTask(newTask);
    Task newTask2 = new Task(TASK_TITLE2, "Some Task Description");
    mTasksRepository.saveTask(newTask2);
    Task newTask3 = new Task(TASK_TITLE3, "Some Task Description", true);
    mTasksRepository.saveTask(newTask3);
    // When a completed tasks are cleared to the tasks repository
    mTasksRepository.clearCompletedTasks();
    // Then the service API and persistent repository are called and the cache is updated
    verify(mTasksRemoteDataSource).clearCompletedTasks();
    verify(mTasksLocalDataSource).clearCompletedTasks();
    assertThat(mTasksRepository.mCachedTasks.size(), is(1));
    assertTrue(mTasksRepository.mCachedTasks.get(newTask2.getId()).isActive());
    assertThat(mTasksRepository.mCachedTasks.get(newTask2.getId()).getTitle(), is(TASK_TITLE2));
}
Also used : Task(com.example.android.architecture.blueprints.todoapp.data.Task) Test(org.junit.Test)

Example 12 with Task

use of com.example.android.architecture.blueprints.todoapp.data.Task in project todo-mvp-rxjava by albertizzy.

the class TasksRepositoryTest method getTask_requestsSingleTaskFromLocalDataSource.

@Test
public void getTask_requestsSingleTaskFromLocalDataSource() {
    // Given a stub completed task with title and description in the local repository
    Task task = new Task(TASK_TITLE, "Some Task Description", true);
    Optional<Task> taskOptional = Optional.of(task);
    setTaskAvailable(mTasksLocalDataSource, taskOptional);
    // And the task not available in the remote repository
    setTaskNotAvailable(mTasksRemoteDataSource, taskOptional.get().getId());
    // When a task is requested from the tasks repository
    TestSubscriber<Optional<Task>> testSubscriber = new TestSubscriber<>();
    mTasksRepository.getTask(task.getId()).subscribe(testSubscriber);
    // Then the task is loaded from the database
    verify(mTasksLocalDataSource).getTask(eq(task.getId()));
    testSubscriber.assertValue(taskOptional);
}
Also used : Task(com.example.android.architecture.blueprints.todoapp.data.Task) Optional(com.google.common.base.Optional) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Test(org.junit.Test)

Example 13 with Task

use of com.example.android.architecture.blueprints.todoapp.data.Task in project todo-mvp-rxjava by albertizzy.

the class StatisticsPresenterTest method setupStatisticsPresenter.

@Before
public void setupStatisticsPresenter() {
    // Mockito has a very convenient way to inject mocks by using the @Mock annotation. To
    // inject the mocks in the test the initMocks method needs to be called.
    MockitoAnnotations.initMocks(this);
    // Make the sure that all schedulers are immediate.
    mSchedulerProvider = new ImmediateSchedulerProvider();
    // Get a reference to the class under test
    mStatisticsPresenter = new StatisticsPresenter(mTasksRepository, mStatisticsView, mSchedulerProvider);
    // The presenter won't update the view unless it's active.
    when(mStatisticsView.isActive()).thenReturn(true);
    // We subscribe the tasks to 3, with one active and two completed
    TASKS = Lists.newArrayList(new Task("Title1", "Description1"), new Task("Title2", "Description2", true), new Task("Title3", "Description3", true));
}
Also used : Task(com.example.android.architecture.blueprints.todoapp.data.Task) ImmediateSchedulerProvider(com.example.android.architecture.blueprints.todoapp.util.schedulers.ImmediateSchedulerProvider) Before(org.junit.Before)

Example 14 with Task

use of com.example.android.architecture.blueprints.todoapp.data.Task in project todo-mvp-rxjava by albertizzy.

the class AddEditTaskScreenTest method toolbarTitle_editTask_persistsRotation.

@Test
public void toolbarTitle_editTask_persistsRotation() {
    // Put a task in the repository and start the activity to edit it
    TasksRepository.destroyInstance();
    FakeTasksRemoteDataSource.getInstance().addTasks(new Task("Title1", "", TASK_ID, false));
    launchNewTaskActivity(TASK_ID);
    // Check that the toolbar shows the correct title
    onView(withId(toolbar)).check(matches(withToolbarTitle(R.string.edit_task)));
    // Rotate activity
    TestUtils.rotateOrientation(mActivityTestRule.getActivity());
    // check that the toolbar title is persisted
    onView(withId(toolbar)).check(matches(withToolbarTitle(R.string.edit_task)));
}
Also used : Task(com.example.android.architecture.blueprints.todoapp.data.Task) Test(org.junit.Test) LargeTest(android.support.test.filters.LargeTest)

Example 15 with Task

use of com.example.android.architecture.blueprints.todoapp.data.Task in project todo-mvp-rxjava by albertizzy.

the class StatisticsScreenTest method intentWithStubbedTaskId.

/**
 * Setup your test fixture with a fake task id. The {@link TaskDetailActivity} is started with
 * a particular task id, which is then loaded from the service API.
 *
 * <p>
 * Note that this test runs hermetically and is fully isolated using a fake implementation of
 * the service API. This is a great way to make your tests more reliable and faster at the same
 * time, since they are isolated from any outside dependencies.
 */
@Before
public void intentWithStubbedTaskId() {
    // Given some tasks
    TasksRepository.destroyInstance();
    TasksRepository repository = Injection.provideTasksRepository(InstrumentationRegistry.getContext());
    repository.saveTask(new Task("Title1", "", false));
    repository.saveTask(new Task("Title2", "", true));
    // Lazily start the Activity from the ActivityTestRule
    Intent startIntent = new Intent();
    mStatisticsActivityTestRule.launchActivity(startIntent);
}
Also used : TasksRepository(com.example.android.architecture.blueprints.todoapp.data.source.TasksRepository) Task(com.example.android.architecture.blueprints.todoapp.data.Task) Intent(android.content.Intent) Before(org.junit.Before)

Aggregations

Task (com.example.android.architecture.blueprints.todoapp.data.Task)35 Test (org.junit.Test)20 Optional (com.google.common.base.Optional)3 Before (org.junit.Before)3 TasksRepository (com.example.android.architecture.blueprints.todoapp.data.source.TasksRepository)2 ImmediateSchedulerProvider (com.example.android.architecture.blueprints.todoapp.util.schedulers.ImmediateSchedulerProvider)2 Flowable (io.reactivex.Flowable)2 CompositeDisposable (io.reactivex.disposables.CompositeDisposable)2 Disposable (io.reactivex.disposables.Disposable)2 TestSubscriber (io.reactivex.subscribers.TestSubscriber)2 Activity (android.app.Activity)1 Intent (android.content.Intent)1 NonNull (android.support.annotation.NonNull)1 LargeTest (android.support.test.filters.LargeTest)1 AddEditTaskActivity (com.example.android.architecture.blueprints.todoapp.addedittask.AddEditTaskActivity)1 TasksDataSource (com.example.android.architecture.blueprints.todoapp.data.source.TasksDataSource)1 EspressoIdlingResource (com.example.android.architecture.blueprints.todoapp.util.EspressoIdlingResource)1 BaseSchedulerProvider (com.example.android.architecture.blueprints.todoapp.util.schedulers.BaseSchedulerProvider)1 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 List (java.util.List)1