!******************************************************************************!
! Tests for files with a TimeLine.
!
! Arguments: None
!
! Note: Even though auto-correction of wrapped timelines is the default (3.9.4)
! this test will continue to have a t=0 on the left plot, since there is no TIMECODE entry
!
! Issue:   NXM-907 TimeLine: STATUS should indicate if a TimeLine has overflowed
! Since:   NeXtMidas 2.5.0
! Author:  J. Czechowski IV
!******************************************************************************!
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 COREGRAPHICS coregraphics GET DEF=FALSE SDEF=TRUE  ! for a quick check of graphics - more extreme tests - since 3.8.1

  call setDatDir
  ! Only allow interAct if PANEL command Exists - may be running in LIBNXM mode
  invoke panelExists reg.shell.M.dictionary.isDictionaryEntry(PANEL)

  res choice1 "Status/t penny1 with TLL=<def>"
  res choice2 "Status/t penny2 with TLL=-1"

  call test_notimeline

  if interAct AND panelExists then
    set instr "Should see two sets of plots with three upside-down pennies each.\n&
               1) In the left plot the timecode (you mouse-over the top of the plot) &
               will say 't=0', and will be good (01 Jan 2007) on the bottom of &
               the plot.\n&
               2) The timecode in the right plot will be good throughout.\n&
               3) On the right plot, zoom in where the top and middle penny meet, the time code there &
               should be between 00:01:01 and 00:01:06.\n&
               4) Zoom out and make sure that same time range is valid when zoomed out.\n&
               5) In the shell on the far right you should see a warning about the TimeLine &
               being full (You may need to scroll up/down in the shells to see the messages.)\n&
               6) Scroll to the top of the shell and there should be a Start time status &
                entry of '00:00:00' with 'TimeLine' in parens, since it is the source of the start time\n&
               7) Change the CHOICE to '^{choice2}'\n&
               8) Now the shell on the far right should have no such warning.\n&
               9) Scroll to the top of the shell and there should be a Start time status\n&
                entry from 01 Jan 2007 with the 'TimeLine' in parens, since it is the source of the start time"

    nxm.sys.test.timelinetester/wrap=2 IN=^{datDir}.apenny.prm{YU=TIME,XU=FREQ} OUT=mypenny1{AUX=RAM,USETIMELINEFILE=true} &
          TSTART=2007:01:01::00:00:00 TDELTA=.25 JITTER=0.125

    ! Note: Using the switch /TLL is the more official way to control timeline length
    ! (see switch.exp).  Using the TLL as a qualifier works KeyObject finds the setter
    ! method DataFile.setTimeLineLength
    nxm.sys.test.timelinetester/wrap=2 IN=^{datDir}.apenny.prm{YU=TIME,XU=FREQ,YD=0.25} OUT=mypenny2{AUX=RAM,TLL=-1} &
          TSTART=2007:01:01::00:00:00 TDELTA=.50 JITTER=0.125 TFREQ=4

    pipe on
      panel/msgid=MAIN/id=panel/setup/controls=gc
      gcontrol label macro  "MACRO"
      gcontrol menu   MAIN   "Exit with Status = " "PASS,FAIL" /tleft
      gcontrol text   idtext "Instructions" "^instr" 30 35 /title
      gcontrol choice name=statchoice P1="^choice1,^choice2'" P2=^choice1 ! since 3.7.1 for NXM-2872
      set gc.MAIN.itemColors {PASS=GREEN,FAIL=RED}

      plot/id=plot1 mypenny1{AUX=RAM} READOUT=+OnAxis|+T|-X|-Y|-DX|-DY|-View /all
      plot/id=plot2 mypenny2{AUX=RAM,TIMELINEHANDLER={UseInterpolation=true}} READOUT=+OnAxis|+T|-X|-Y|-DX|-DY|-View /all

      shellgui/id=shell1/attach                 ! updated to use just 1 attached shellgui in 3.7.1 for NXM-2872
      say "********************************"
      say "status/t mypenny1 with TLL=<def>"
      say "********************************"
      status/t mypenny1{AUX=RAM}

    pipe off
  endif

  ! Additional tests, only run with /BASELINE
  if baseline then
    %test_timeline_as_is            ! automated test for NXM-3532

    invoke convertExists reg.shell.M.dictionary.isDictionaryEntry(CONVERT)
    if convertExists then
      %test_timeline_offset_not_first ! NXM-3087
      %test_timeline_propagation   ! series of automated tests NXM-3637
      %test_timeline_tc_timetag ! Tests reading and writing of high precision keywords AND TimeLine replacement
    endif

    if interAct then
      %test_timeline_bad_entries ! NXM-3040
      %test_timeline_on_existing_pipe
      %test_timeline_paging
      %test_timeline_pipe_init    ! needs reworking to just replace test_timeline_restart
      %test_timeline_pipe_init_tlentries
      %test_timeline_restart     ! for NXM-3073, NXM-3040, etc.
      %test_timeline_restart_keywords
    endif

  ! If COREGRAPHICS run a short list of extreme tests that tend to be the ones to show graphical issues
  elseif coregraphics then
    %test_timeline_restart/COREGRAPHICS ! for NXM-3073, NXM-3040, etc.
  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.name EQS "statchoice" then    ! added to accommodate only attaching 1 shellgui - since 3.7.1 for NXM-2872
    invoke ,, reg.shell1.clear()
    say ""
    say "*****************************"
    say "^{msg.data}"
    say "*****************************"
    if "penny2" SUBS msg.data 
      status/t mypenny2{AUX=RAM}
    else
      status/t mypenny1{AUX=RAM}
    endif
  else
    say "Unhandled messagename: ^msg.name from fid: ^msg.fid data: ^msg.data"
  endif
return

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

!******************************************************************************!
! tests for a file with no timeline
!******************************************************************************!
procedure test_notimeline
  set  fname1 test_notimeline1{AUX=RAM}
  set  fname2 test_notimeline2{AUX=RAM}
  set  fname3 test_notimeline3{AUX=RAM}

  set t 2007:07:17::12:00:04
  set o 4

  headermod/create fname1 delta=1 xunits="TIME_S" timecode=2007:07:17::12:00:00 start=0
  headermod/create fname2 delta=1 xunits="TIME_S" timecode=0                    start=2007:07:17::12:00:00
  headermod/create fname3 delta=1 xunits="TIME_S" timecode=2007:07:17::00:00:00 start=12:00:00

  set t1 FILE(fname1).getTimeAt(^o)
  set t2 FILE(fname2).getTimeAt(^o)
  set t3 FILE(fname3).getTimeAt(^o)
  assert/text="^reg.main.name: timecode=2007:07:17::12:00:00 start=0                    -> getTimeAt(^o) = ^t1 " t1 eq t
  assert/text="^reg.main.name: timecode=0                    start=2007:07:17::12:00:00 -> getTimeAt(^o) = ^t2 " t2 eq t
  assert/text="^reg.main.name: timecode=2007:07:17::00:00:00 start=12:00:00             -> getTimeAt(^o) = ^t3 " t3 eq t

  set o1 FILE(fname1).getIndexAt(^t)
  set o2 FILE(fname2).getIndexAt(^t)
  set o3 FILE(fname3).getIndexAt(^t)
  assert/text="^reg.main.name: timecode=2007:07:17::12:00:00 start=0                    -> getIndexAt(^t) = ^o1 " o1 eq o
  assert/text="^reg.main.name: timecode=0                    start=2007:07:17::12:00:00 -> getIndexAt(^t) = ^o2 " o2 eq o
  assert/text="^reg.main.name: timecode=2007:07:17::00:00:00 start=12:00:00             -> getIndexAt(^t) = ^o3 " o3 eq o

return
