Sw-spec/easy-card-ver1

จาก Theory Wiki
รุ่นแก้ไขเมื่อ 06:50, 16 มีนาคม 2560 โดย Jittat (คุย | มีส่วนร่วม) (หน้าที่ถูกสร้างด้วย '== CardView == <syntaxhighlight lang="java"> package com.example.jittat.easycard.card; →‎* * Created by jittat on 16/3/2560.: pub...')
(ต่าง) ←รุ่นแก้ไขก่อนหน้า | รุ่นแก้ไขล่าสุด (ต่าง) | รุ่นแก้ไขถัดไป→ (ต่าง)
ไปยังการนำทาง ไปยังการค้นหา

CardView

package com.example.jittat.easycard.card;

/**
 * Created by jittat on 16/3/2560.
 */

public interface CardView {
    void setCardLocation(int number, int numberOfCards);

    void setCardTitle(String title);

    void setCardDescription(String description);

    void hideCardDescription();
    void showCardDescription();
}

CardPresenter

package com.example.jittat.easycard.card;

import com.example.jittat.easycard.data.Card;
import com.example.jittat.easycard.data.CardRepository;

/**
 * Created by jittat on 16/3/2560.
 */
public class CardPresenter {
    private int currentCardIndex;
    private CardRepository repository;
    private CardView view;
    private boolean isDescriptionShown;

    public CardPresenter(CardRepository repository, CardView view) {
        this.repository = repository;
        this.view = view;

        currentCardIndex = 0;
    }

    public void start() {
        currentCardIndex = 0;
        Card currentCard = repository.getCardAt(currentCardIndex);

        isDescriptionShown = false;
        updateCardIndex();
        updateCardTitleAndDescription(currentCard);
    }

    private void updateCardTitleAndDescription(Card currentCard) {
        view.setCardTitle(currentCard.getTitle());
        view.setCardDescription(currentCard.getDescription());
        view.hideCardDescription();
    }

    private void updateCardIndex() {
        int numberOfCards = repository.getCardCount();
        view.setCardLocation(currentCardIndex + 1,
                numberOfCards);
    }

    public void onCardClick() {
        if(isDescriptionShown) {
            view.hideCardDescription();
        } else {
            view.showCardDescription();
        }
        isDescriptionShown = !isDescriptionShown;
    }

    public void nextCard() {
    }

    public void previousCard() {
    }
}

CardActivity

package com.example.jittat.easycard.card;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.example.jittat.easycard.R;
import com.example.jittat.easycard.data.CardRepository;

public class CardActivity extends AppCompatActivity implements CardView {
    private CardPresenter presenter;
    private CardRepository cardRepository;

    private TextView locationTextView;
    private TextView titleTextView;
    private TextView descriptionTextView;

    private String currentDescription;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_card);

        cardRepository = CardRepository.getInstance();
        presenter = new CardPresenter(cardRepository, this);

        initViewHolders();

        presenter.start();
    }

    private void initViewHolders() {
        locationTextView = (TextView) findViewById(R.id.textview_card_index);
        titleTextView = (TextView) findViewById(R.id.textview_card_title);
        descriptionTextView = (TextView) findViewById(R.id.textview_card_description);
    }

    @Override
    public void setCardLocation(int number, int numberOfCards) {
        locationTextView.setText("" + number + "/" + numberOfCards);
    }

    @Override
    public void setCardTitle(String title) {
        titleTextView.setText(title);
    }

    @Override
    public void setCardDescription(String description) {
        currentDescription = description;
        descriptionTextView.setText(currentDescription);
    }

    @Override
    public void hideCardDescription() {
        descriptionTextView.setText("");
    }

    @Override
    public void showCardDescription() {
        descriptionTextView.setText(currentDescription);
    }

    public void onCardClick(View view) {
        presenter.onCardClick();
    }
}

CardPresenterTest

package com.example.jittat.easycard.card;

import com.example.jittat.easycard.data.Card;
import com.example.jittat.easycard.data.CardRepository;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
 * Created by jittat on 16/3/2560.
 */

public class CardPresenterTest {
    @Mock
    CardRepository cardRepository;

    @Mock
    CardView cardView;

    private CardPresenter presenter;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        presenter = new CardPresenter(cardRepository, cardView);

        when(cardRepository.getCardCount())
                .thenReturn(5);
        when(cardRepository.getCardAt(0))
                .thenReturn(new Card("DOG","little animal"));
    }

    @Test
    public void testStart() {
        presenter.start();

        verify(cardRepository).getCardCount();
        verify(cardView).setCardLocation(1,5);
        verify(cardView).setCardTitle("DOG");
    }

    @Test
    public void testShowAndHideDescription() {
        presenter.start();
        verify(cardView).hideCardDescription();

        presenter.onCardClick();
        verify(cardView).showCardDescription();

        presenter.onCardClick();
        verify(cardView, times(1)).showCardDescription();
        verify(cardView, times(2)).hideCardDescription();
    }
}