!******************************************************************************!
! Tests related to sharing pipes with child macros using /SHAREMASK=pipes switch
!
! Author:  Jay Hallam / Neon Ngo
!******************************************************************************!
startmacro

switch INTERACTIVE interAct GET DEF=FALSE

pipe on
  waveform _parentpipe elem=inf
  call test_share_pipe_subroutine
  call test_share_pipe_procedure

  %test_pipe_share_child/id=child/bg/sharemask="PIPES"/interactive=^interact true
  assert/text="Test PIPES sharing: _childpipe exists in PARENT." _childpipe FEXISTS

  %test_pipe_share_child/id=child_noshare/bg/interactive=^interact/wpos=(25,480,600,300) false
  assert/text="Test PIPES sharing: _childpipe_not_shared NOT exists in PARENT." _childpipe_not_shared NFEXISTS

  if interact then
    panel/controls=gc/setup
    gcontrol menu done "Exit with Status = " "PASS,FAIL" /tleft
    set gc.done.itemColors {PASS=GREEN,FAIL=RED}
    gcontrol text instructions "Instructions" "Should see two real-time WAVE plots" ,,3
    plot _parentpipe
    plot/id=plotchild _childpipe
    set this.timer(0) 1
  else
    message FUNC=send ID="MAIN" NAME="DONE"
  endif

pipe off

endmacro

!******************************************************************************!
procedure processMessage
  if msg.name EQS "TIMER" then
    if reg.child NREXISTS then
      message FUNC=send ID="MAIN" NAME="DONE"
    endif
  elseif msg.name EQS "DONE" then
    call cleanUp
  else
    info "Got unprocessed msg = ^msg"
  endif
return

!******************************************************************************!
procedure cleanUp
  message send "CHILD_NOSHARE" NAME="EXIT"
!  assert/text="Test PIPES sharing: _childpipe exists in PARENT." _childpipe FEXISTS
  res L:count 0
  while reg.child REXISTS and count < 10 then
    info "Sending EXIT message to CHILD"
    message send "CHILD" NAME="EXIT"
    pause 0.2
    calc L:count count+1
    if count > 10 then
      warning "^reg.main.name: I give up! I can not close the CHILD"
    endif
  endwhile

  ! test case for BUG 2586
  assert/text="Test PIPES exist after child closes: _parentpipe exists." _parentpipe FEXISTS
  pipe stop
return

!******************************************************************************!
subroutine test_share_pipe_subroutine
!******************************************************************************!
  assert/text="SUBROUTINE can see calling macro's pipes" _parentpipe FEXISTS
return

!******************************************************************************!
procedure test_share_pipe_procedure
  assert/text="PROCEDURE can see calling macro's pipes" _parentpipe FEXISTS
return
