!******************************************************************************!
! Test support of using OPENFILE message with plot prescaler
! Note: This uses the helper class nxm.sys.test.testprescalerhelper
!       to provide a timer that does not stop the plot
!
! @bug      NXM-3078 PLOT: Does not display zero-padded Pre-D data
! @since    3.9.2
! @author:  Kevin Esterheld
!******************************************************************************!
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 DEBUG       debug    GET DEF=FALSE SDEF=TRUE

  switch RANDOM      randomPercent GET DEF=100   SDEF=10    ! get percentage of random frames/blocks to use

  if debug then
    res debugswitch "/DEBUG=PRESCALE"   ! todo use this in all test cases
  else
    res debugswitch ""
  endif

  res scaletbl_outside {} ! outside pipe
  res scaletbl_pipe     {} ! inside pipe

  if randomPercent lt 21 then
    res possibleWarn "WARN: View Y Min and Y Max both set to ..."
    info "When doing only a random sampling for scaling, it is 'okay' to see '^{possibleWarn}' as it does not always result in good scale values"
  endif

  res zero_padded_data ram:data
  res psd_file_name nxm.sys.test.test2000_psd.prm

  ! create the 1000 data
  call generate1000data
  ! create copies so that there are no race conditions for each test
  file copy/f zero_padded_data zero_padded_data2
  file copy/f zero_padded_data zero_padded_data3

  if interAct then
    set instructions "The plots populate after a 3 second delay.\n\n&
                      This tests that the openfile message uses the &
                      qualifiers correctly.\n\n&
                      They should all render correctly and there &
                      should be no flickering"
   res testingOutsidePipe true
   if testingOutsidePipe then

   plot/id=OUTSIDEPIPE1/all/bg
   prescaler/random=^randomPercent IN=zero_padded_data SCALETABLE=scaletbl_outside
   prescaler/random=^randomPercent IN=zero_padded_data /SCALEKW
   ! the /ALL is included in the OPENFILE message qualifiers
   plot/id=OUTSIDEPIPE2^debugswitch/bg
   plot/id=OUTSIDEPIPE3^debugswitch/bg
   plot/id=OUTSIDEPIPE4^debugswitch/bg
   pause 0.1
   assert/text="^reg.main.name: plot internal prescaler makes a copy with correct header" zero_padded_data FEQ/H plot_makes_copy.tmp
   assert/text="^reg.main.name: plot internal prescaler makes a copy with correct data" zero_padded_data FEQ/D plot_makes_copy.tmp

   endif
   ! **********  1000 FILE TESTING - INSIDE A PIPE************************
   set this.timer(0) 1
   invoke midasContext reg.main.getMidas()
   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 20 /title
     noop zero_padded_data3 _zero_padded_in
     prescaler/random=^randomPercent IN=_zero_padded_in OUT=ram:prescale_out SCALETABLE=scaletbl_pipe

     ! the /ALL is included in the OPENFILE message qualifiers
     plot/id=INSIDEPIPE1^debugswitch
     plot/id=INSIDEPIPE2^debugswitch
     plot/id=INSIDEPIPE3^debugswitch

     invoke nxm.sys.test.testprescalerhelper.sendCommands(reg)

   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"
    message FUNC=send ID=OUTSIDEPIPE1 NAME=EXIT
    message FUNC=send ID=OUTSIDEPIPE2 NAME=EXIT
    message FUNC=send ID=OUTSIDEPIPE3 NAME=EXIT
    message FUNC=send ID=OUTSIDEPIPE4 NAME=EXIT
    pause 0.1
    pipe stop
  elseif msg.name eqs "TIMER" then
    set this.timer(0) 0
    !say "message name: ^msg.name from fid: ^msg.fid data: ^msg.data"
    res pane reg.win.outsidepipe1
    invoke ,, pane.attach(reg.OUTSIDEPIPE1,"OUTSIDEPIPE1")
    res pane reg.win.outsidepipe2
    invoke ,, pane.attach(reg.OUTSIDEPIPE2,"OUTSIDEPIPE2")
    res pane reg.win.outsidepipe3
    invoke ,, pane.attach(reg.OUTSIDEPIPE3,"OUTSIDEPIPE3")
    res pane reg.win.outsidepipe4
    invoke ,, pane.attach(reg.OUTSIDEPIPE4,"OUTSIDEPIPE4")
  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

!******************************************************************************!
! If an unexpected exception is caught, mark test as failed, unless it is expected
!******************************************************************************!
procedure generate1000data
    ! Generate 0-padded type 1000 data
    waveform ram:f1 FORM=CI ELEM=67500  SHAPE=CONST AMP=0  !DR value 67500
    waveform ram:f2 FORM=CI ELEM=30500  SHAPE=CONST AMP=0  !DR value 30500
    waveform ram:f3 FORM=CI ELEM=672000 SHAPE=CONST AMP=1  !DR value 672000
    waveform ram:f4 FORM=CI ELEM=30000  SHAPE=CONST AMP=0  !DR value 30000
    waveform ram:f5 FORM=CI ELEM=7000   SHAPE=CONST AMP=1  !DR value 7000
    
    mergefile ram:f1 zero_padded_data
    mergefile ram:f2 zero_padded_data
    mergefile ram:f3 zero_padded_data
    mergefile ram:f4 zero_padded_data
    mergefile ram:f5 zero_padded_data

return
