!******************************************************************************!
! Tests interrupt a command (BUG 2150)
!
! Author:  JC4, J. Bauer
!******************************************************************************!
startmacro
  switch INTERACTIVE interAct GET DEF=TRUE  SDEF=TRUE  ! for normal interactive test(s)

  if interAct then
    ! NXM-3845 since NeXtMidas 4.1.1
    if ^{env.NMROOT} EQS "classpath:" then
      info "SKIPPING ^reg.main.name because ctrl-c behaves differently when running from a jar file"
    else
      ! test case #1 interrupting primitive command that takes a long time during it's open method
      pipe on
        nxm.sys.test.test_interrupt
      pipe off

      if env.osname neqs "LINUX" then
        ! NTN: There is a known issue under non-Linux based OS (e.g. Windows and Solaris) where
        !      the default interrupt signal handler gets restored after the initial ctrl+c
        !      which causes subsequent ctrl+c to kill the JVM. A potential workaround to
        !      that would be to update NTerminal.c to restore our signal handler after
        !      we process an interrupt.
        ! TODO: until the above issue is addressed, we will be skipping the rest of the tests
        warn "^reg.main.name: Skipping the rest of the interrupt tests for non-Linux platforms..."
      else
        call setDatDir

        ! test case #2 interrupting a primitive command outside of pipe section
        info "Directions: Use CTRL+C to exit the plot."
        plot ^{datDir}.world.prm
        assert/text="^reg.main.name: Ctrl+C (interrupt) command run outside the pipe section." TRUE

        ! test case #3 interrupting a child macro (sub-macro) that is piped
        info "Directions: Use CTRL+C to exit the DEMO macro."
        DEMO;SYS
        assert/text="^reg.main.name: Ctrl+C (interrupt) piped sub-macro." TRUE

        ! test case #4 interrupting long running series of piped sub-macros (test scenario for ICE)
        !info "Directions: Press CTRL+C very quickly twice to interrupt the loop that calls a long running piped macro"
        !do ii 1 20
          ! todo check
        !enddo
        !assert/text="^reg.main.name: Ctrl+C (interrupt) piped sub-macros in loop." TRUE

        ! test case #5 unable to break out of a tight loop in a macro
        if FALSE isTrue ! This test currently fails
          %test_interrupt_tight_loop
        else
          warn "^reg.main.name: Skipping Test Case #5 (known issue)..."
        endif

        ! test case #6 background plot gets closed, not foreground (this doesn't happen all
        ! the time, so we run it four times since it seems to fail ~75% of the time).
        if FALSE isTrue ! This test currently fails
          do i 1 4
            info "Directions: Use CTRL+C to exit the foreground PLOT (^{i} of 4)."
            plot/id=PLOT_WORLD_^{i} ^{datDir}.world.prm    /BG
            plot/id=PLOT_PENNY_^{i} ^{datDir}.apenny.prm   /BG
            plot/id=PLOT_PSD_^{i}   ^{datDir}.fftdemo.prm  ! not BG
            assert/text="^reg.main.name: Ctrl+C (interrupt) infinite loop in macro." REG.PLOT_WORLD_^{i}  REXISTS
            assert/text="^reg.main.name: Ctrl+C (interrupt) infinite loop in macro." REG.PLOT_PENNY_^{i}  REXISTS
            assert/text="^reg.main.name: Ctrl+C (interrupt) infinite loop in macro." REG.PLOT_PSD_^{i}   NREXISTS

            if (REG.PLOT_WORLD_^{i} REXISTS) then message send PLOT_WORLD_^{i} NAME=EXIT
            if (REG.PLOT_PENNY_^{i} REXISTS) then message send PLOT_PENNY_^{i} NAME=EXIT
            if (REG.PLOT_PSD_^{i}   REXISTS) then message send PLOT_PSD_^{i}   NAME=EXIT
          enddo
        else
          warn "^reg.main.name: Skipping Test Case #6 (known issue)..."
        endif
      endif ! LINUX
    endif ! classpath
  endif ! interAct
endmacro

!******************************************************************************!
! 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
