<commit_before><commit_msg>#85 Add unit tests for ItemListPresenter<commit_after>package com.github.ayltai.newspaper.app.view;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import android.support.annotation.NonNull;

import org.junit.Test;
import org.mockito.Mockito;
import org.robolectric.Robolectric;

import com.github.ayltai.newspaper.app.MainActivity;
import com.github.ayltai.newspaper.app.data.model.Item;
import com.github.ayltai.newspaper.app.data.model.Video;
import com.github.ayltai.newspaper.util.Irrelevant;
import com.github.ayltai.newspaper.view.PresenterTest;

import io.reactivex.processors.FlowableProcessor;
import io.reactivex.processors.PublishProcessor;
import io.realm.RealmList;

public final class ItemListPresenterTest extends PresenterTest<MainActivity, ItemListPresenter, ItemListPresenter.View> {
    private final FlowableProcessor<List<Item>> load                           = PublishProcessor.create();
    private final FlowableProcessor<Irrelevant> clears                         = PublishProcessor.create();
    private final FlowableProcessor<Irrelevant> pullToRefreshes                = PublishProcessor.create();
    private final FlowableProcessor<Irrelevant> bestVisibleItemPositionChanges = PublishProcessor.create();

    @NonNull
    @Override
    protected ItemListPresenter createPresenter() {
        final ItemListPresenter presenter = Mockito.spy(new ItemListPresenter(Arrays.asList("港聞", "國際")));

        Mockito.doReturn(this.load).when(presenter).load();

        return presenter;
    }

    @NonNull
    @Override
    protected ItemListPresenter.View createView() {
        final ItemListPresenter.View view = Mockito.mock(ItemListPresenter.View.class);

        Mockito.doReturn(this.clears).when(view).clears();
        Mockito.doReturn(this.pullToRefreshes).when(view).pullToRefreshes();
        Mockito.doReturn(this.bestVisibleItemPositionChanges).when(view).bestVisibleItemPositionChanges();

        return view;
    }

    @NonNull
    @Override
    protected MainActivity createActivity() {
        return Robolectric.buildActivity(MainActivity.class).get();
    }

    @Test
    public void Given_model_When_onViewAttached_Then_modelIsBound() {
        final List<Item> models = this.getModels();

        this.getPresenter().bindModel(models);

        this.attachments.onNext(true);

        Mockito.verify(this.getView(), Mockito.times(1)).showLoadingView();

        this.load.onNext(models);

        Mockito.verify(this.getView(), Mockito.times(1)).clear();
        Mockito.verify(this.getView(), Mockito.times(1)).bind(models);
    }

    @NonNull
    private List<Item> getModels() {
        return Arrays.asList(this.getModel(), this.getModel());
    }

    @NonNull
    private Item getModel() {
        final Item model = Mockito.mock(Item.class);

        Mockito.doReturn("蘋果日報").when(model).getSource();
        Mockito.doReturn(new Date()).when(model).getPublishDate();
        Mockito.doReturn("").when(model).getTitle();
        Mockito.doReturn("").when(model).getDescription();
        Mockito.doReturn("").when(model).getLink();
        Mockito.doReturn(new RealmList<>()).when(model).getImages();
        Mockito.doReturn(new Video()).when(model).getVideo();

        return model;
    }
}

{'fraction_non_alphanumeric': 0.12110202845897669, 'fraction_numerical': 0.0015137753557372085, 'mean_word_length': 3.7471264367816093, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5835528', 'n_tokens_mistral': 1001, 'n_tokens_neox': 986, 'n_words': 144}