package nxm.sys.test.junit.lib;

import javax.swing.LookAndFeel;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;

import nxm.sys.lib.Midas;
import nxm.sys.lib.Shell;
import nxm.sys.lib.Theme;
import nxm.sys.test.junit.NMTestCase;

/** Test class for lib/Theme
 *
 *  @author Zachary Matthews
 *  @since  NeXtMidas 4.1.1
 */
public class ThemeTest extends NMTestCase {

  boolean isWindows = Shell.isWindows();

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

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

  public void testGetThemeFor() {
    LookAndFeel origLaf = UIManager.getLookAndFeel();

    // Setting a common LookAndFeel that is supported NXM-3830
    LookAndFeelInfo motifLafInfo = new LookAndFeelInfo("CDE/Motif","com.sun.java.swing.plaf.motif.MotifLookAndFeel");
    try {
      Theme motifTheme = Theme.getThemeFor(motifLafInfo);
      assertEquals("getThemeFor handles common LookAndFeel","CDE/Motif",motifTheme.getName());
    } catch (Exception e1) {
      fail("getThemeFor handles common LookAndFeel");
    }

    // Initializing GTK+ with no Midas context should return null due to BUG 2749 NXM-3830
    LookAndFeelInfo gtkLafInfo = new LookAndFeelInfo("GTK+","com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
    try {
      Theme gtkTheme = Theme.getThemeFor(gtkLafInfo);
      assertNull("Initializing GTK+ LAF with no Midas context should return null", gtkTheme);
    } catch (Exception e) {
      fail("Initializing GTK+ LAF with no Midas context should return null");
    }

    // Defaulting to Nimbus LookAndFeel when the specified LookAndFeel is not supported NXM-3830
    if (!isWindows && !Shell.isHeadless()) {
      LookAndFeelInfo winLafInfo = new LookAndFeelInfo("Windows","com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
      try {
        Theme winTheme = Theme.getThemeFor(winLafInfo);
        assertEquals("getThemeFor handles unsupported LookAndFeel without throwing Exception","Nimbus",winTheme.getName());
      } catch (Exception e) {
        fail("getThemeFor handles unsupported LookAndFeel without throwing Exception");
      }
    }

    // Restore original LookAndFeel
    try {
      UIManager.setLookAndFeel(origLaf);
    } catch (Exception e) {
      // Assuming original LookAndFeel is valid
    }
  }
}
