!------------------------------------------------------------------------------!
! Tests for LIST2 command.
!
!   Author : J. Czechowski IV
!------------------------------------------------------------------------------!
startmacro/tsetup=nxm.sys.test.test_list2_setup.tbl
  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

  if interAct OR coreGraphics then
    set infile1 temp{AUX=RAM,FG=CSV,HEADERROWS=1}
    set L:numRows 30
    set t:allRows {}
    do L:rowNum 1 numRows
      set t:allRows.^{rowNum} {A="^{rowNum}A",B="^{rowNum}B",C="^{rowNum}C",D="^{rowNum}D"}
    enddo

    file open/n tag infile1{COLUMNNAMES="A|B|C|D"}
      do L:rowNum 1 numRows
        calc index rowNum 1 - ! Note change btw one-based and zero-based
        invoke ,, tag.setData(index,allRows.^{rowNum})
      enddo
    file close tag
    
    ! The template below is intentionally ordered D,C,A,B in order to force Bug 958 to show up
    set t:template {D={ALIGN=CENTER},&
                    C={ALIGN=CENTER},&
                    A={ALIGN=CENTER},&
                    B={ALIGN=CENTER}}
    
    set t:rows3to5   {}
    set t:rows3to5.3 allRows.3
    set t:rows3to5.4 allRows.4
    set t:rows3to5.5 allRows.5
    
    set z:allOk true
    set s:currentTest "Start"
    
    set t:tests {}
    set t:tests.Start                 {NEXTTEST="Row"}

    set t:tests.Row                   {NEXTTEST="MultiRow"}
    set s:tests.Row.MSG_NAME          "SELECT"
    set l:tests.Row.MSG_INFO           0
    set t:tests.Row.MSG_DATA          {TYPE="ROW",ROW=3,COLUMN=-1,NAME="",VALUE=^{allRows.3}}
    set s:tests.Row.INSTRUCTIONS      "Select row 3 with a LEFT MOUSE button click on the row button."

    set t:tests.MultiRow              {NEXTTEST="Deselect"}
    set s:tests.MultiRow.MSG_NAME     "SELECT"
    set l:tests.MultiRow.MSG_INFO      0
    set t:tests.MultiRow.MSG_DATA     {TYPE="ROW",ROW=5,COLUMN=-1,NAME="",VALUE=^{allRows.5},CURRSEL=^{rows3to5},PREVSEL={3=^{allRows.3}}}
    set s:tests.MultiRow.INSTRUCTIONS "MULTI SELECT: With row 3 highlighted, select row 5 with SHIFT-LMB."

    set t:tests.Deselect              {NEXTTEST="Cell"}
    set s:tests.Deselect.MSG_NAME     "DESELECT"
    set l:tests.Deselect.MSG_INFO      0
    set t:tests.Deselect.MSG_DATA     {TYPE="ROW",ROW=5,COLUMN=-1,VALUE=^{allRows.5},PREVSEL=^{rows3to5}}
    set s:tests.Deselect.INSTRUCTIONS "Rows 3-5 should be highlighted. Deselect with the &
                                       RMB on the highlighted area."

    set t:tests.Cell                  {NEXTTEST="Done"}
    set s:tests.Cell.MSG_NAME         "SELECT"
    set l:tests.Cell.MSG_INFO          0
    set t:tests.Cell.MSG_DATA         {TYPE="CELL",ROW=5,COLUMN=3,NAME="C",VALUE=^{allRows.5.C}}
    set s:tests.Cell.INSTRUCTIONS     "No rows should be highlighted. Select cell 5C with a LEFT MOUSE &
                                       button click."

    set t:tests.Done                  {NEXTTEST="Done",INSTRUCTIONS="<tbd>",MSG_NAME="",MSG_INFO=0,MSG_DATA={}}


    pipe init
      panel/msgid=MAIN/id=panel/controls=gc
      list2/prevandcurr=false/grid=on/msgid=MAIN/id=idlist infile1 template
      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   INSTRUCTIONS  "INSTRUCTIONS"  "<loading>"     24 5 /title
      gcontrol text   SELECTIONTEXT "SELECTED ROWS" "NONE SELECTED." 24 8 /title

      call next_test ! Begins the first test
    pipe off

  endif
endmacro

!------------------------------------------------------------------------------!
! Procedure for handling all messages
procedure processMessage m:msg
  !say "Got msg.name=^msg.name msg.info=^msg.info msg.data=^msg.data"

  if msg.name EQS "MAIN" then
    if msg.data EQS "EXIT" then
      pipe stop
    else
      assert/text="LIST2 tests" "PASS" SUBS msg.data
      pipe stop
    endif
    
  elseif msg.fid EQS "IDLIST" then
    assert/text="^reg.main.name: got expected message ^msg.name from ^msg.fid" msg.name EQS tests.^{currentTest}.MSG_NAME
    set gc.SELECTIONTEXT.value msg.data
    
    if /DEBUG then
      say "Expected Name"
      res/all tests.^{currentTest}.MSG_NAME
      say "Actual Name"
      res/all msg.name
      say "Expected Info"
      res/all tests.^{currentTest}.MSG_INFO
      say "Actual Info"
      res/all msg.info
      say "Expected Data"
      res/all tests.^{currentTest}.MSG_DATA
      say "Actual Data"
      res/all msg.data
    endif

    set okName TEST(tests.^{currentTest}.MSG_NAME,EQS,msg.name)
    set okInfo TEST(tests.^{currentTest}.MSG_INFO,EQ,msg.info)
    set okData false
    
    if msg.data INSTANCEOF nxm.sys.lib.Table then
      TABLE tests.^{currentTest}.MSG_DATA EQUALS msg.data okData
    endif
    
    set err   TEST(okName,isFalse,OR,okInfo,isFalse,OR,okData,isFalse)
    set allOk TEST(allOk,isTrue,AND,err,isFalse)

    assert/text="^reg.main.name: check message name" okName
    assert/text="^reg.main.name: check message info" okInfo
    assert/text="^reg.main.name: check message data" okData

    if err or /DEBUG then
      set allOk false
      say ">>>>>>>>>>>>>> EXPECTED: NAME=^{tests.^{currentTest}.MSG_NAME}"
      say ">>>>>>>>>>>>>> GOT:      NAME=^{msg.name}"
      say ">>>>>>>>>>>>>> EXPECTED: INFO=^{tests.^{currentTest}.MSG_INFO}"
      say ">>>>>>>>>>>>>> GOT:      INFO=^{msg.info}"
      say ">>>>>>>>>>>>>> EXPECTED: DATA=^{tests.^{currentTest}.MSG_DATA}"
      say ">>>>>>>>>>>>>> GOT:      DATA=^{msg.data}"
    endif
    call next_test
    
  else
    !say "Unhandled messagename: ^msg.name from fid: ^msg.fid data: ^msg.data"
  endif
return

!------------------------------------------------------------------------------!
procedure next_test
  set currentTest tests.^{currentTest}.NEXTTEST
  
  if currentTest eqs "Done" then
    if allOk isTrue then
      set gc.INSTRUCTIONS.value "All tests passed."
    else
      set gc.INSTRUCTIONS.value "Some tests failed!"
    endif
  else
    set gc.INSTRUCTIONS.value tests.^{currentTest}.INSTRUCTIONS
  endif
return
