!******************************************************************************!
! Test the ablity to set state to Finish and Stop
!
! @bug      NXM-3006 Test the ability to set state to Finish/Stop
! @since    NeXtMidas 3.9.3
! @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)

  if interAct then
    set instructions "This tests the setting of state. Start the test by clicking &
                      'Test Finish'.\n\n&
                      Now click on the same button that is renamed to 'Test Stop.\n\n&
                      If there are any failures, click 'FAIL' otherwise, 'PASS'"
    set runTestsFinish true
    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 14 /title
      gcontrol/nc=1 button stateTest "" "Test Finish"
      call startPlot

    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.data eqs "TEST FINISH" then
    call runFinishTest
  elseif msg.data eqs "TEST STOP" then
    call runStopTest
  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

procedure runFinishTest 
  call setState "FINISH"
  pause .25
  assert/text="^reg.main.name: reg.idwave has been finihsed"  reg.idwave NREXISTS
  assert/text="^reg.main.name: reg.idfft has been finihsed" reg.idfft NREXISTS
  assert/text="^reg.main.name: reg.idplot has been finihsed" reg.idplot NREXISTS
  invoke gc.stateTest.setItem(1,"Test Stop")
  call startPlot
return

procedure runStopTest
  call setState "STOP"
  assert/text="^reg.main.name: reg.idwave has been stopped"  reg.idwave REXISTS
  assert/text="^reg.main.name: reg.idfft has been stopped" reg.idfft REXISTS
  assert/text="^reg.main.name: reg.idplot has been stopped" reg.idplot REXISTS
return

procedure setState s:state
   invoke reg.idwave.setState(^state)
   invoke reg.idfft.setState(^state)
   invoke reg.idplot.setState(^state)
return

procedure startPlot
  WAVEFORM/BG/ID=idwave _wave_set_state FORM=CF ELEM=inf SHAPE=SINE RF=300.1e6 VRF=300e6 BW=1e6 /RT
  FFT/BG/ID=idfft _wave_set_state _fft_set_state NFFT=16K OVER=0.0 /PSD=1
  PLOT/BG/ID=idplot _fft_set_state /THEME=WoB /WTITLE="Plot FFT for setState test" /READOUT=OnAxis
return