!******************************************************************************!
! Tests panel/controls=gc in headless mode
!
! Note: It should be run with and without the /HEADLESSMODE switch
!
! @issue:   NXM-4064 GCONTROL/panel isHeadless() logic causing gc.main table to
!           not to get created.
! @author:  Jeanette McCall / Fix and failure scenario contributed by Jeff Schoen
! @since:   NeXtMidas 4.1.4
!******************************************************************************!
startmacro
  switch INTERACTIVE  interAct     GET DEF=TRUE  SDEF=TRUE  ! for normal interactive test(s)
  switch HEADLESSMODE headlessMode GET DEF=FALSE SDEF=TRUE  ! run tests with panel headless

  if ENV.OSNAME EQSS "Linux" then
    invoke shellHeadless nxm.sys.lib.Shell.isHeadless()
    if shellHeadless then
      res exceptionBeforeFix "nxm.sys.lib.MidasException: Key XFREQ does not exist"

      set this.timer(0) 0.25
      pipe on
        if headlessMode then
          panel/headless/msgid=MAIN/controls=gc
        else
          panel/msgid=MAIN/controls=gc
        endif

        gcontrol label  MACRO "MACRO"
        gcontrol lval "XFREQ" "Freq" 5000 0 17500 5 /fmt="#0" /units="MHz"
        ! Note: assert/text="^reg.main.name:..." was not used, because not populated in error case
        assert/stat=xfreqExists/text="%TEST_GCONTROL_PANEL_HEADLESS: GC.XFREQ Table created in headless" gc.xfreq REXISTS
        if xfreqExists then
          assert/text="^reg.main.name: GC.XFREQ.VALUE correct" gc.xfreq.value EQS 5000
        endif
      pipe off
    elseif interact then
      res instructions "Run the following in a separate OS terminal:\n\n&
                        1. ssh -x localhost\n&
                        2. cd ^{env.nmroot}\n&
                        3. Start a new NeXtMidas session\n&
                        4. nm\n&
                        5. nM> home test\n&
                        6. nM> %test_gcontrol_panel_headless/headlessmode\n    &
                           => Tests should pass\n&
                        7. nM> %test_gcontrol_panel_headless\n    &
                           => Tests should pass"
      pipe on
        panel/msgid=MAIN/controls=gc/setup
        gcontrol label  MACRO "MACRO"
        gcontrol menu main "Exit with Status = " "PASS,FAIL" /tleft
        set gc.main.itemcolors {PASS=GREEN,FAIL=RED}
        gcontrol text idtext "Instructions" "^{instructions}" 24 16 /title
      pipe off
    else
      info "^reg.main.name: Skipping since not in headless environment nor in interactive mode."
    endif
  else
    info "^reg.main.name: Skipping since not in Linux environment."
  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
  elseif msg.name eqs "TIMER"
    info "stopping with timer"
    set this.timer(0) 0
    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
  invoke cause emsg.data.getCause()
  ! Interested in root cause of Exception (may be wrapped)
  if cause ISNULL then
    res causeStr emsg.data.toString()
  else
    res causeStr cause.toString()
  endif
  ! Check if Exception is expected at this point in the test
  if causeStr EQSS exceptionBeforeFix then
  say " caught"
    assert/text="^reg.main.name: Gcontrol.open regression - widget should be added to WidgetPanel even in headless" &
                true ISFALSE
  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
