package com.readytalk.swt.util;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.fail;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

public class DisplaySafeTest {

  @Before
  public void initializeEmptyShell() {
    // If we don't make a shell, Display will not initialize
    new Shell();
  }

  @Test
  public void getLatestDisplay_CurrentIsNull_DisplayUnchanged() {
    DisplaySafe safe = new DisplaySafe(null);
    DisplaySafe safeSpy = spy(safe);
    when(safeSpy.getCurrent()).thenReturn(null);
    boolean nullDisplayExceptionThrown = false;
    try {
      safeSpy.getLatestDisplay();
    } catch (DisplaySafe.NullDisplayException nde) {
      nullDisplayExceptionThrown = true;
    }

    assertTrue(nullDisplayExceptionThrown);
  }

  @Test
  public void getLatestDisplay_CurrentIsNull_DisplayChanged() {
    DisplaySafe safe = new DisplaySafe(null);
    Display display = null;

    try {
      display = safe.getLatestDisplay();
    } catch (DisplaySafe.NullDisplayException nde) {
      fail();
    }

    assertNotNull(display);
  }

  @Test
  public void getLatestDisplay_CurrentNotNull_DisplayUnchanged() {
    Display display = Display.getCurrent();
    DisplaySafe safe = new DisplaySafe(display);
    DisplaySafe safeSpy = spy(safe);
    when(safeSpy.getCurrent()).thenReturn(display);

    Display sameDisplay = null;

    try {
      sameDisplay = safeSpy.getLatestDisplay();
    } catch (DisplaySafe.NullDisplayException nde) {
      fail();
    }

    assertEquals(display, sameDisplay);
  }

  @Test
  public void getLatestDisplay_CurrentNotNull_DisplayNowNull() {
    DisplaySafe safe = new DisplaySafe(Display.getCurrent());
    DisplaySafe safeSpy = spy(safe);
    when(safeSpy.getCurrent()).thenReturn(null);

    Display display = null;

    try {
      display = safeSpy.getLatestDisplay();
    } catch (DisplaySafe.NullDisplayException nde) {
      fail();
    }

    assertNotNull(display);
  }
}

@@ -285,51 +285,8 @@
 ull;
-%0Aimport static org.junit.Assert.assertTrue;
 %0A%0Aim
@@ -526,32 +526,83 @@
 ();%0A  %7D%0A%0A  @Test
+(expected = DisplaySafe.NullDisplayException.class)
 %0A  public void g
@@ -641,32 +641,72 @@
 splayUnchanged()
+ throws DisplaySafe.NullDisplayException
  %7B%0A    DisplaySa
@@ -832,238 +832,33 @@
     
-boolean nullDisplayExceptionThrown = false;%0A    try %7B%0A      safeSpy.getLatestDisplay();%0A    %7D catch (DisplaySafe.NullDisplayException nde) %7B%0A      nullDisplayExceptionThrown = true;%0A    %7D%0A%0A    assertTrue(nullDisplayExceptionThrown
+safeSpy.getLatestDisplay(
 );%0A 

update test to use expected exception