!******************************************************************************!
! Test case for the specified PLOT bug.
!
! @bug      2457 Plot: invoking openFile(..) causes frequent NullPointerExceptions
! @see      related test cases: test_plot_framesize_updates and test_plot_pipe_restarts
! @author:  Neon Ngo
!******************************************************************************!
startmacro
  switch INTERACTIVE interAct GET DEF=TRUE  SDEF=TRUE  ! for normal interactive test(s)
  switch INTERVAL    interval GET DEF=0.1              ! time in between invoking openFile(..)
  switch DURATION    duration GET DEF=300              ! how long to run test case for (seconds)
  switch TYPE        ftype    GET DEF="PIPES"          ! file type to swap (PIPES or STATIC FILES)

  call setDatDir

  set count 0
  call setSwapFileType ftype

  if interAct then
    set instructions "This runs automatically for /duration=^duration seconds (flagging any exception as FAILURE). &
                      Take an eye break while this test runs.\nNext time you run this, change swap file type."

    pipe on
      panel/msgid=MAIN/setup/controls=gc
      gcontrol label macro "Test MACRO"
      gcontrol menu  main "Exit with Status =" "PASS,FAIL" TABLE={ItemColors={PASS="GREEN",FAIL="RED"}} /tleft

      gcontrol text   idtext    "Instructions"   instructions 24 12 /title
      gcontrol dval   dInterval "Swap interval"  interval 0 10 0.1 /FMT="#0.###"
      gcontrol button fileType  "Swap File Type" "Static Files,Pipes" ftype /FLAGS=-OneRow|+ChkBox|

      waveform/id=1whi _wave1 "SF" inf White
      waveform/id=2lrs _wave2 "SF" inf LRS
      plot/id=idplot

      gc label  debug "Debug"
      gc button pipeFunc "Pipe" "Run,Pause" 1 /tleft
      gcontrol/id=pmon1 pipemon pmon1 _WAVE1
      gcontrol/id=pmon2 pipemon pmon2 _WAVE2

      ! two timers, second one to stop test case
      set this.timers 2

      ! set timer to change plot
      set this.timer(0) interval

      ! set timer to exit test case after 5 minutes
      set this.timer(1) duration
    pipe off

  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: User indicated tests" msg.data eqss "PASS"
    pipe stop

  elseif msg.name eqs "PIPEFUNC" then
    pipe ^msg.data

  elseif msg.name eqs "TIMER" then
    if msg.info eq 1 then
      ! second timer is to stop test case
      pipe stop
    endif

    calc count count 1 + 2 mod
    if count > 0 then
      set file testfile1
    else
      set file testfile2
    endif
    invoke ,, reg.idplot.closeFile("*")
    invoke ,, reg.idplot.openFile(file)

  elseif msg.name eqs "DINTERVAL" then
    set this.timer(0) msg.data

  elseif msg.name eqs "FILETYPE" then
    call setSwapFileType msg.data

  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
  if 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

!******************************************************************************!
procedure setSwapFileType S:ftype["STATIC"]
  if ftype eqss "PIPES" then
    set testfile1 _wave1{fs=8k}
    set testfile2 _wave2{fs=8k}
  else
    ! Default to swapping static files
    ! Swapping between Layer1D and Layer2D, since they don't set BStore (like LayerPoly: otherwise
    ! when swapping with pipes, there will be a Backing Store not recommended for Realtime plots
    ! due to Graphics performance warning msg.
    set testfile1 "^{datDir}.sinewave.prm"
    set testfile2 "^{datDir}.apenny.prm"
  endif
return
