package de.lessvoid.nifty.controls.checkbox;

import static org.easymock.EasyMock.capture;
import static org.easymock.classextension.EasyMock.createMock;
import static org.easymock.classextension.EasyMock.replay;
import static org.easymock.classextension.EasyMock.verify;
import static org.junit.Assert.*;

import org.easymock.Capture;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import de.lessvoid.nifty.controls.CheckBoxStateChangedEvent;

public class CheckBoxImplTest {
  private CheckBoxImpl checkBox = new CheckBoxImpl(null);
  private CheckBoxView view;
  private Capture<CheckBoxStateChangedEvent> capturedEvent = new Capture<CheckBoxStateChangedEvent>();

  @Before
  public void before() {
    view = createMock(CheckBoxView.class);
    checkBox.bindToView(view);
  }

  @After
  public void after() {
    verify(view);
  }

  @Test
  public void testDefault() {
    replay(view);
    assertFalse(checkBox.isChecked());
  }

  @Test
  public void testCheck() {
    expectViewUpdate(true);
    checkBox.check();
    assertCheckBoxState(true);
  }

  @Test
  public void testUncheck() {
    expectViewUpdate(false);
    checkBox.uncheck();
    assertCheckBoxState(false);
  }

  @Test
  public void testSetToChecked() {
    expectViewUpdate(true);
    checkBox.setChecked(true);
    assertCheckBoxState(true);
  }

  @Test
  public void testSetToUnchecked() {
    replay(view);
    checkBox.setChecked(false);
    assertEquals(false, checkBox.isChecked());
  }

  @Test
  public void testToggle() {
    expectViewUpdate(true);
    checkBox.toggle();
    assertCheckBoxState(true);
  }

  @Test
  public void testToggleToOff() {
    view.update(false);
    view.publish(capture(capturedEvent));
    view.update(true);
    view.publish(capture(capturedEvent));
    replay(view);

    checkBox.uncheck();
    assertEquals(false, capturedEvent.getValue().isChecked());

    checkBox.toggle();
    assertEquals(true, capturedEvent.getValue().isChecked());

    assertEquals(true, checkBox.isChecked());
  }

  private void expectViewUpdate(final boolean expectedCheck) {
    view.update(expectedCheck);
    view.publish(capture(capturedEvent));
    replay(view);
  }

  private void assertCheckBoxState(final boolean expectedState) {
    assertEquals(expectedState, checkBox.isChecked());
    assertEquals(expectedState, capturedEvent.getValue().isChecked());
  }
}

@@ -1125,30 +1125,19 @@
     
-expectViewUpdate(false
+replay(view
 );%0A 
@@ -1173,27 +1173,42 @@
 sert
-CheckBoxState(false
+Equals(false, checkBox.isChecked()
 );%0A 
@@ -1666,74 +1666,8 @@
 ) %7B%0A
-    view.update(false);%0A    view.publish(capture(capturedEvent));%0A
     
@@ -1774,72 +1774,8 @@
 ();%0A
-    assertEquals(false, capturedEvent.getValue().isChecked());%0A%0A
     
@@ -1855,17 +1855,16 @@
 ked());%0A
-%0A
     asse

fix failing tests