/*
 * Copyright (C) 2013-2015 Craig Thomas
 * This project uses an MIT style license - see LICENSE for details.
 */
package com.chip8java.emulator.listeners;

import com.chip8java.emulator.CentralProcessingUnit;
import com.chip8java.emulator.Keyboard;
import com.chip8java.emulator.Memory;
import com.chip8java.emulator.Screen;
import com.chip8java.emulator.listeners.PauseMenuItemListener;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

import javax.swing.*;
import java.awt.event.ItemEvent;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;

/**
 * Tests for the PauseMenuItemListener.
 */
public class PauseMenuItemListenerTest {

    private Memory mMemoryMock;
    private Keyboard mKeyboardMock;
    private Screen mScreenMock;
    private CentralProcessingUnit mCPU;
    private PauseMenuItemListener mPauseMenuItemListener;
    private ItemEvent mMockItemEvent;

    @Before
    public void setUp() {
        mMemoryMock = mock(Memory.class);
        mKeyboardMock = mock(Keyboard.class);
        mScreenMock = mock(Screen.class);
        mCPU = new CentralProcessingUnit(mMemoryMock, mKeyboardMock);
        mCPU.setScreen(mScreenMock);
        mPauseMenuItemListener = new PauseMenuItemListener(mCPU);
    }

    @Test
    public void testCPUPausedWhenItemListenerTriggered() {
        ButtonModel buttonModel = mock(ButtonModel.class);
        Mockito.when(buttonModel.isSelected()).thenReturn(true);
        AbstractButton button = mock(AbstractButton.class);
        Mockito.when(button.getModel()).thenReturn(buttonModel);
        mMockItemEvent = mock(ItemEvent.class);
        Mockito.when(mMockItemEvent.getSource()).thenReturn(button);
        mPauseMenuItemListener.itemStateChanged(mMockItemEvent);
        assertTrue(mCPU.getPaused());
    }

    @Test
    public void testCPUNotPausedWhenItemListenerTriggeredTwice() {
        ButtonModel buttonModel = mock(ButtonModel.class);
        Mockito.when(buttonModel.isSelected()).thenReturn(true).thenReturn(false);
        AbstractButton button = mock(AbstractButton.class);
        Mockito.when(button.getModel()).thenReturn(buttonModel);
        mMockItemEvent = mock(ItemEvent.class);
        Mockito.when(mMockItemEvent.getSource()).thenReturn(button);
        mPauseMenuItemListener.itemStateChanged(mMockItemEvent);
        mPauseMenuItemListener.itemStateChanged(mMockItemEvent);
        assertFalse(mCPU.getPaused());
    }
}

@@ -1317,84 +1317,8 @@
 U);%0A
-    %7D%0A%0A    @Test%0A    public void testCPUPausedWhenItemListenerTriggered() %7B%0A
     
@@ -1435,16 +1435,34 @@
 rn(true)
+.thenReturn(false)
 ;%0A      
@@ -1689,32 +1689,108 @@
 Return(button);%0A
+    %7D%0A%0A    @Test%0A    public void testCPUPausedWhenItemListenerTriggered() %7B%0A
         mPauseMe
@@ -1885,32 +1885,32 @@
    %7D%0A%0A    @Test%0A
+
     public void 
@@ -1964,392 +1964,8 @@
 ) %7B%0A
-        ButtonModel buttonModel = mock(ButtonModel.class);%0A        Mockito.when(buttonModel.isSelected()).thenReturn(true).thenReturn(false);%0A        AbstractButton button = mock(AbstractButton.class);%0A        Mockito.when(button.getModel()).thenReturn(buttonModel);%0A        mMockItemEvent = mock(ItemEvent.class);%0A        Mockito.when(mMockItemEvent.getSource()).thenReturn(button);%0A
     

Move mock code into setUp method.