!------------------------------------------------------------------------------!
! Tests for LIST2 command.
!
! The following switches useful for debugging the scroll bar:
!   /SHOWNEW=NO     - Do not show top list with new settings
!   /SHOWLEGACY=NO  - Do not show btm list with old settings
!
!   Author : J. Czechowski IV / Neon Ngo
!------------------------------------------------------------------------------!
startmacro/tsetup=nxm.sys.test.test_list2_setup_split.tbl/shownew/showlegacy
  switch INTERACTIVE  interAct     GET DEF=TRUE
  switch standalone   standalone   GET DEF=TRUE
  switch COREGRAPHICS coregraphics GET DEF=FALSE SDEF=TRUE  ! for a quick check of graphics - more extreme tests - since 3.8.1

  res instructions "Initially the file should have 3 rows with values 100, 200 and &
                    300. Each time you click [WriteRow] a row will be written to the &
                    file with value N (N=OneUpCounter). The first three clicks will &
                    overwrite existing rows, subsequent ones will add new rows. When &
                    number of rows fills the display a scroll bar should appear."

  set fname "ram:temp_list_poll2"
  set pollTime 0.5
  set colorList "Red,Yellow,Green,Blue"
  sedit "^{colorList}" colorCount "NELEM"

  set t:template {}
  set template._GLOBAL_ {FGCOLOR=FIELD_CLR}
  set template.NUM {FORMAT="0",ALIGN="CENTER",WIDTH=8}
  set template.CLR {ALIGN="CENTER",WIDTH=8}

  call resetFile
  calc l:count 0

  ! NTN: Test data for Bug 1973
  !      row1.CLR should be a java.lang.String
  !      row2.CLR should be a Data (1A) - simulate
  set tab {}
  set tab.row1 {ROW=1,NUM=10,CLR="Green"}
  set tab.row2 {ROW=2,NUM=20}
  new nxm.sys.lib.Data(1A,1) tab.row2.CLR
  invoke ,, tab.row2.CLR.packS(0,4,"Blue")

  if interAct OR coreGraphics then
    pipe init
      panel/msgid=MAIN/id=panel/controls=gc
      if /shownew then
        list2/poll=^{pollTime}/msgid=MAIN/id=idlist ^{fname} template
      endif
      if /showlegacy then
        list2/poll=^{pollTime}/msgid=MAIN/id=idlegacy/legacy ^{fname} template
      endif
      if standalone isTrue then
        gcontrol label  macro   "MACRO"
        gcontrol menu MAIN "Exit with Status = " "PASS,FAIL" /tleft
        set gc.MAIN.itemColors {PASS=GREEN,FAIL=RED}
      endif
      gcontrol text idtext "Instructions" "^instructions" 24 12 /title
      gcontrol/nc=2 button write "Write" "WriteRow,Write5Rows,AutoTest,Bug_1973" none
      gcontrol/nc=2 button file  "File"  "ResetFile,EraseFile"          none
      gcontrol DVAL   polltime "Poll Time" pollTime 0.5 10 ^pollTime 0.5  /fmt="0.0"
      gcontrol button pollnow  "" "PollImmediately"

      set reg.panel.timer(0) 1
    pipe off
  endif
  erase/quiet/warn=no ram:temp_list_poll
endmacro

!------------------------------------------------------------------------------!
! Procedure for handling all messages
procedure processMessage m:msg
  if msg.name EQS "MAIN" then
    if msg.data EQS "EXIT" then
      file close fileTag
      rem fileTag
      pipe stop
    else
      assert/text="LIST2 tests" "PASS" SUBS msg.data
      file close fileTag
      pipe stop
    endif
  elseif msg.name EQS "WRITE" then
    if msg.data eqs "WRITE5ROWS" then
      call writeRows 5
      set gc.idtext.value "Should have written 5 rows."
    elseif msg.data eqs "WRITEROW" then
      call writeRows 1
      set gc.idtext.value "Should have written 1 row."
    elseif msg.data eqs "AUTOTEST" then
      ! This test is related to Bug 848
      set gc.idtext.value "Should flash as many rows are written, cleared, and &
                           polled in rapid succession. When done five rows should &
                           be visible. No 'Invalid row number' errors should be &
                           printed to the console."
      do i 1 20 1
        call writeRows 50
        invoke ,, reg.idlist.pollImmediately()
        invoke ,, reg.idlegacy.pollImmediately()
        call resetFile
        invoke ,, reg.idlist.pollImmediately()
        invoke ,, reg.idlegacy.pollImmediately()
        call writeRows 5
      enddo
      invoke ,, reg.idlist.pollImmediately()
      invoke ,, reg.idlegacy.pollImmediately()

    elseif msg.data eqs "BUG_1973" then
      ! NTN: The following sequence should was carefully crafted to cause the
      !      failure scenario that somewhat matches the what the user was doing
      !      which resulted in ClassCastException (String vs Data) when sorting.
      set gc.idtext.value "First three rows should be updated and LIST2 should be &
          sorted by CLR column. There should NOT be any Exceptions/Error in console."

      invoke ,, reg.idlist.updateRow(tab.row1)
      invoke ,, reg.idlist.updateRow(tab.row2)
      set reg.idlist.setCell(3,"CLR","ACTIVE")
      invoke ,, reg.idlist.setSort("CLR")
      invoke ,, reg.idlist.updateRow(tab.row1)
      invoke ,, reg.idlist.updateRow(tab.row2)
    endif


  elseif msg.name EQS "FILE" then
    if msg.data eqs "ERASEFILE" then
      info "*****************************************************************"
      info "* File being listed has been erased, this will cause errors to  *"
      info "* be print until the user pressed 'ResetFile'. These errors are *"
      info "* normal and indicate correct functioning of LIST2.             *"
      info "*****************************************************************"
      set msg "The file being listed has been deleted, this will cause error &
               messages in the console until 'ResetFile' is pressed."
      erase "^fname"
      set gc.idtext.value "^msg"
    elseif msg.data eqs "RESETFILE" then
      call resetFile
      set gc.idtext.value "^{instructions}"
    endif
  elseif msg.name EQS "POLLNOW" then
    invoke ,, reg.idlist.pollImmediately()
    invoke ,, reg.idlegacy.pollImmediately()

  elseif msg.name EQS "POLLTIME" then
    set reg.idlist.pollinterval   gc.POLLTIME.value
    set reg.idlegacy.pollinterval gc.POLLTIME.value
  endif
return


procedure resetFile
  if fileTag rexists then
    file close fileTag
    rem fileTag
  endif
  header/create ^{fname} SR=(NUM/SI,CLR/SA)
  file open/d/w fileTag ^{fname}{FLAGS=FLUSH}
  file write fileTag {NUM=100,CLR=GREY}
  file write fileTag {NUM=200,CLR=GREY}
  file write fileTag {NUM=300,CLR=GREY}
  file close fileTag
  file open/d/w fileTag ^{fname}{FLAGS=FLUSH}
return

procedure writeRows L:num
  do i 1 num 1
    calc l:cnum  count colorCount mod 1 +
    calc l:count count 1 +
    sedit "^{colorList}" color "ELEM" cnum ","
    file write filetag {NUM=^{count},CLR=^{color}}
  enddo
return
