!******************************************************************************!
! Test case for BUG 2817
!
! Arguments: None
!
! Note1: In the 3.4 branch, panel does not throw any exception when it is called
!        in a headless environment. Instead, this situation is caught via the
!        exceptions thrown by other graphical primities such as plot and list2.
!
! Note2: The handling of this situation is done in the processException
!        procedure 
!
! @bug      2817 Plot/Panel behavior in absence of display server
! @author:  Adam Weisz
!******************************************************************************!
startmacro
  switch INTERACTIVE interAct  GET DEF=TRUE  SDEF=TRUE  ! for normal interactive test(s)
  switch BASELINE    baseline  GET DEF=FALSE SDEF=TRUE  ! for defects or more obscure test case(s)
  
  switch SELFTEST    selftest  GET DEF=FALSE SDEF=TRUE
  
  switch ONLY        only      GET DEF="ALL" SDEF="ALL"

  set gotHeadlessError false
  call setDatDir

  if interAct then
    set background "This tests the fix for BUG 2817 in which panel and other &
                    graphical primitives would appear to hang on a headless &
                    system. To prevent this from happening, the user should &
                    process the error messages sent by plot and call 'pipe stop'.&
                    \n\n&
                    Note: Panel will now also send a warning to the console. It &
                    did not previously send a message.\n\n&
                    See test_panel_no_display_server.mm for example implementation."

    set instructions "1. You should NOT see any warnings in the console\n\n&
                      2. You should see a plot of world.prm, a list of capital_cities.prm, &
                         and a list2 of cities.prm.\n\n&
                      3. There should be a green circle over the Yucatan Peninsula\n\n\n&
                      *** Steps 4-6 are for Linux only ***\n&
                      4. Open a new console and set the DISPLAY variable to an invalid value:&
                      \n   $> setenv DISPLAY :4\n\n&
                      5. Start NeXtMidas and run:&
                      \n   nM> %test_panel_no_display_server/selftest\n\n&
                      6. You should see a WARN followed by several INFO's\n\n&
                      7. If the assert indicates PASS, Click PASS"

    pipe on
      if only eqs "ALL" or only eqs "PANEL" then
        panel/msgid=MAIN/controls=gc/setup
      endif
      if only eqs "ALL" or only eqs "GCONTROL" then
        gcontrol/id=gc_label label macro "Test MACRO"
        gcontrol/id=gc_menu menu  main "Exit with Status =" "PASS,FAIL" TABLE={ItemColors={PASS="GREEN",FAIL="RED"}} /tleft
        gcontrol/id=gc_instructions text idtext "Instructions" instructions 50 25 /title
        gcontrol/id=gc_background text idtext "Background" background 50 15 /title
      endif
      if only eqs "ALL" or only eqs "PLOT" then
        plot/id=plot ^{datDir}.world.prm
      endif
      if only eqs "ALL" or only eqs "LIST" then
        list/id=list ^{datDir}.capital_cities.prm
      endif
      if only eqs "ALL" or only eqs "LIST2" then
        list2/id=list2 ^{datDir}.cities.prm
      endif
      if only eqs "ALL" or only eqs "FEATURE" then
        feature/id=feature label=fcirc plot=plot &
                table={NAME="CIRCLE",TYPE=CIRC,X=-89,Y=22,DX=3,COLOR=GREEN}
      endif
      
    pipe off

  endif

  invoke mqa=this.MQ.avail()
  while mqa ge 1
    message get
    invoke mqa=this.MQ.avail()
  endwhile

  if selftest istrue then
    assert/text="^reg.main.name: Got Expected Headless Exception" gotHeadlessError istrue
  endif
endmacro

!******************************************************************************!
! Main process message for this macro. Should be right after the ENDMACRO.
!******************************************************************************!
procedure processMessage m:msg
  if msg.name eqs "MAIN" then
    assert/text="^reg.main.name:" msg.data eqss "PASS"
    pipe stop
  else
    say "Unhandled message name: ^msg.name from fid: ^msg.fid data: ^msg.data"
  endif
return

!******************************************************************************!
! If an unexpected exception is caught, mark test as failed, unless it is expected
!******************************************************************************!
procedure processException m:emsg
! The following block checks for headless exceptions and stops the pipe if one exists
! (Note that there were are specifically checking for a headless exception from PANEL)
  if emsg.data instanceof java.awt.HeadlessException
    info "GOT HEADLESS EXCEPTION FROM ^emsg.fid..."
    if emsg.fid eqs "PANEL"
      set gotHeadlessError true
      pipe stop
    endif
  elseif expectedException REXISTS then
    assert/text="^reg.main.name: Expected ERROR starts with: ^{expectedException}" expectedException EQSS emsg.data.message
    remove expectedException
  else
    assert/text="^reg.main.name: Caught unexpected exception ^{emsg.data}" "FALSE" isTrue
    if "Trace" SUBS ENV.DEBUG then invoke ,, emsg.data.printStackTrace()
  endif
return

!******************************************************************************!
! Set the dat directory
!******************************************************************************!
procedure setDatDir
  invoke nxmMajor env.nmversion.subString(0,1)
  invoke major java.lang.Integer.valueOf(nxmMajor)
  if major GE 4 then
    invoke s:datDir           nxm.sys.test.DatLocator.getDatDirLocation()
    invoke s:datDirSlash      nxm.sys.test.DatLocator.getDatDirSlashLocation()
    invoke z:sysDatFull       nxm.sys.test.DatLocator.sysDatFull()
    invoke z:sysTestDatExists nxm.sys.test.DatLocator.sysTestDatExists()
  else
    set s:datDir           "nxm.sys.dat"
    set s:datDirSlash      "nxm/sys/dat"
    set z:sysDatFull       true
    set z:sysTestDatExists false
  endif
return
