package kr.co.zumo.app.lifeplus.view;

import android.widget.Checkable;

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

import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.powermock.api.mockito.PowerMockito.mock;

/**
 * CheckBoxAllDriverTest
 * <pre>
 * </pre>
 *
 * @author 민효동
 * @version 1.0
 * @history 민효동   [2018. 9. 19.]   [최초 작성]
 * @since 2018. 9. 19.
 */
public class CheckBoxAllDriverTest {

  @InjectMocks
  CheckBoxAllDriver driver;

  @Mock
  Checkable allBox;

  @Before
  public void setup() throws Exception {
    MockitoAnnotations.initMocks(this);
  }

  @Test
  public void addChildBox() {
    TestCheckBox box = spy(TestCheckBox.class);
    driver.addChildBox(box);
    driver.check();

    verify(box, times(1)).isChecked();
  }

  @Test
  public void check() {
    TestCheckBox box = spy(TestCheckBox.class);
    driver.addChildBox(box);
    driver.addChildBox(box);
    driver.addChildBox(box);
    driver.addChildBox(box);
    driver.addChildBox(box);
    driver.check();

    verify(box, times(5)).isChecked();

    verify(allBox).setChecked(eq(false));

    box.setChecked(true);
    driver.check();

    verify(allBox).setChecked(eq(true));

  }

  @Test
  public void applyCheckAll() {

    Checkable box = mock(Checkable.class);
    driver.addChildBox(box);
    driver.addChildBox(box);
    driver.addChildBox(box);
    driver.addChildBox(box);
    driver.addChildBox(box);
    driver.applyCheckAll(true);

    verify(box, times(5)).setChecked(eq(true));
  }

  static class TestCheckBox implements Checkable {
    boolean isChecked = false;

    @Override
    public void setChecked(boolean checked) {
      isChecked = checked;
    }

    @Override
    public boolean isChecked() {
      return isChecked;
    }

    @Override
    public void toggle() {
      isChecked = !isChecked;
    }
  }
}