package nxm.sys.test.junit.libg;

import org.junit.Before;

import nxm.sys.lib.Midas;
import nxm.sys.lib.Shell;
import nxm.sys.libg.MWindow;
import nxm.sys.libg.MidasFrame;
import nxm.sys.test.junit.NMTestCase;

/**
 * Test class for GraphicsUtil.
 * @author Kevin E.
 * @since 3.9.2
 */
public class GraphicsUtilTest extends NMTestCase{

  public GraphicsUtilTest (String methodName) {
    this(methodName, null);
  }

  public GraphicsUtilTest (String methodName, Midas midas) {
    super(methodName, midas);
  }

  MWindow mw;
  nxm.sys.test.junit.libg.MJPanelTester mjpExtendsInc;
  nxm.sys.libg.MJPanel mjpExtendsLibg;
  boolean skippingBecauseHeadless = false;

  @Before
  public void setUp () {
    if (Shell.isHeadless()) {
      skippingBecauseHeadless = true;
      return;
    }
    mw             = new MWindow();
    mjpExtendsInc  = new nxm.sys.test.junit.libg.MJPanelTester();
    mjpExtendsLibg = new nxm.sys.libg.MJPanel();

    mjpExtendsInc.setMWindow(mw);
    mjpExtendsLibg.setMWindow(mw);
  }

  /**
   * Tests that regardless of MidasFrame package, it returns true if it is indeed a MidasFrame
   * (For JDK 11 modularity, there are now two versions of MidasFrame - libg.MidasFrame and inc.MidasFrame)
   * @since NeXtMidas 3.9.2
   */
  public void testIsMidasFrame() {
    if (skippingBecauseHeadless) {
      getMidas().info("Skipping testIsMidasFrame because HEADLESS");
      return;
    }
    assertTrue("This is an instance of inc.MidasFrame", MidasFrame.isMidasFrame(mjpExtendsInc));
    assertTrue("This is an instance of libg.MidasFrame", MidasFrame.isMidasFrame(mjpExtendsLibg));
  }

  /**
   * Gets the MWindow from the MidasFrame object regardless of package
   * This method is used for JDK 11 modularity so that the code can use inc.MidasFrame
   * and libg.MidasFrame interchangeably.
   * @since NeXtMidas 3.9.2
   */
  public void testGetMidasFrameMWindowViaReflection () {
    if (skippingBecauseHeadless) {
      getMidas().info("Skipping testGetMidasFrameMWindowViaReflection because HEADLESS");
      return;
    }
    assertEquals("Should get the correct MWindow from the MidasFrame", mw,
        MidasFrame.getMidasFrameMWindowViaReflection(mjpExtendsInc));
    assertEquals("Should get the correct MWindow from the MidasFrame", mw,
        MidasFrame.getMidasFrameMWindowViaReflection(mjpExtendsLibg));
  }
}
