!******************************************************************************!
! Test manually cutting files with msg's
!
! @bug      2640  SINKFILE: Add /NFN Option
! @author:  J Bauer
! @version: $Id: test_sinkfile_msg.mm,v 1.2 2012/12/04 13:16:00 jwb Exp $
!******************************************************************************!
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 MANUAL      manual   GET DEF=FALSE SDEF=TRUE ! used to run the test manually (see the GUI)

  res l:counter 0
  res l:success 0

  if interAct then
    if manual then
      set instructions "Click the button to spawn a new file a few time. \n\nYou should see an &
                      info message stating the original file (test_outsinkfile) is closing &
                      and test_spawnedfile_00x is opening. \n\nThe subsequent info &
                      messages should show the spawned file sequence number incrementing." 
                      
    else
      set instructions "This test should execute and close automatically.\n\n&
                        To run by hand, use the /manual switch."
    endif

    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 18 /title

      shellgui/id=shell1/attach
      
      waveform _waveb form=sf shape=sin elem=inf 
      sinkfile/id=sink1 _waveb out=test_outsinkfile start=0 duration=-1
      
      gcontrol line
      gcontrol button myButt "" "CUT FILE" 
      
      if manual isFalse then
        set l:iteration 1
        set this.timer(0) .5
      endif
      
    pipe off
    
    files/f=files1/nameonly test_spawnedfile_* TMP
    
    erase test_outsinkfile.tmp
    FORALL files1 erase #
    erase files1
    
  endif
endmacro

!******************************************************************************!
! Main process message for this macro. Should be right after the ENDMACRO.
!******************************************************************************!
procedure processMessage m:msg
  if msg.name eqs "MAIN" then
    invoke ,, reg.shell1.close()   ! this should be done with all tests with shellgui's to see the result of the tests below
    assert/text="^reg.main.name: Got INFO msg's" counter eq success
    assert/text="^reg.main.name: User indicated tests" msg.data eqss "PASS"
    pipe stop
    
  elseif msg.name EQS "MYBUTT" then
    message FUNC=send ID=SINK1 NAME=CUT INFO=-1 DATA="test_spawnedfile_"
    message send main name="DOASSERT"
    
  elseif msg.name eqs "DOASSERT" then
    pause .1
    
  elseif msg.name eqs "TIMER" then
    if iteration gt 5 then
      message send main name="MAIN" data="PASS"
    else 
      message send main name="MYBUTT"
      calc newTime iteration 3 / 
      set this.timer(0) newTime
      calc iteration iteration 1 +
    endif
    
  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

!******************************************************************************!
! Check for the spawned file, and do an assert if it does not exist
!******************************************************************************!
procedure doAssert 
  !info "GOT THE ASSERT MSG.....counter=^counter"
  if counter lt 10 then 
    set fname "test_spawnedfile_000^{counter}"
  else 
    set fname "test_spawnedfile_00^{counter}"
  endif
      
  if "^fname" fexists then
    calc l:success success 1 +
  else 
    assert/text="^reg.main.name: Spawned file exists." "^{fname}" fexists
  endif
  
  calc l:counter counter 1 +

return
