!------------------------------------------------------------------------------!
! Tests for LIST command.
! NOTE:  this is an interactive test and requires operator verification
!        in order to determine test success.
!
! Use the /WAYNE switch to include the tests for Wayne-only features.
!
!   Author : J. Hallam
!   Version: $Id: test_list_select.mm,v 1.12 2010/08/04 15:55:13 ntn Exp $
!
!------------------------------------------------------------------------------!

startmacro

switch INTERACTIVE interAct GET DEF=TRUE
if interAct then

  ! INPUT files used for tests
  fname infile area=test name=input_test_list_01 ext=tmp opt=sys

  res testList "Row,MultiRow,Deselect,Popup"

  if /WAYNE then
    res testList "^{testList},DoubleClick"
    res count 1
  endif

  pipe init
    panel/msgid=MAIN/controls=gc/setup
    list/grid=on/msgid=MAIN/win=pane1/id=IDLIST infile

    set startStr "Press the 'Start' TEST button."
    gcontrol label  macro   "MACRO"
    gcontrol menu MAIN "Exit with Status = " "PASS,FAIL" /tleft
    set gc.MAIN.itemColors {PASS=GREEN,FAIL=RED}
    gcontrol text INSTRUCTIONS "INSTRUCTIONS" "^startStr" 24 5 /title
    gcontrol text SELECTIONTEXT "SELECTED ROWS" "NONE SELECTED" 24 10 /title
    gcontrol button TEST "TEST" "Start" 0
    gcontrol button SELECTTYPE "CURRENT TEST" "^testList" "NONE" /NC=2 /edit=f /msg=f

  pipe off

  ! Erase temporary files
  erase/quiet/warn=no temp_list_scrollend
  erase/quiet/warn=no temp_list_insert

endif

endmacro

!------------------------------------------------------------------------------!
procedure processMessage m:msg
!
! Procedure for handling all messages
!
  if msg.fid EQS "IDLIST" then
    if /VERBOSE then
      say "Got ^msg.name from ^msg.fid, info ^msg.info. &
         expected msg: ^expectedMessage, TYPE = ^gc.SELECTTYPE.value
      res/all msg.info
      res/all msg.data
      say "----------"
      res/all expectedSelMsg
    endif

    if msg.name EQS expectedMessage then
      assert/text="^reg.main.name: got expected message ^msg.name from ^msg.fid" TRUE
      set gc.SELECTIONTEXT.value msg.data
    else
      assert/text="^reg.main.name: unexpected message ^msg.name from ^msg.fid" FALSE
      set gc.SELECTIONTEXT.value reg.IDLIST.getSelRowsTable
    endif

    if     gc.SELECTTYPE.value EQS "NONE" then
      beep 1
      set gc.INSTRUCTIONS.value "Select a test to perform on the SELECTTYPE button."

    else
      call compareData sel_status
      assert/text="^reg.main.name: ^gc.SELECTTYPE.value. data in ^msg.name = expected msg " &
                           sel_status
      if sel_status isFalse then
        say "^reg.main.name: ---------- DUMP MSG.DATA and EXPECTED MESSAGE -------------------"
        res/all msg.info
        res/all msg.data
        say "----------"
        res/all expectedSelMsg
        say "^reg.main.name: -----------------------------------------------------------------"
      endif

      if gc.SELECTTYPE.value EQS "ROW" then
        ! Now do the MULTIROW selection
        set gc.SELECTTYPE.value "MULTIROW"
        call test_select_multirow
      elseif gc.SELECTTYPE.value EQS "MULTIROW" then
        set gc.SELECTTYPE.value "DESELECT"
        call test_deselect
      elseif gc.SELECTTYPE.value EQS "DESELECT"
        set gc.SELECTTYPE.value "POPUP"
        call test_popup
      elseif gc.SELECTTYPE.value EQS "POPUP"
        gcontrol/panel="IDLIST"/wait/temp=true prompt newval "Prompt:"
        set gc.SELECTTYPE.value "NONE"
        set reg.IDLIST.deselectAll()
        if /WAYNE then
          set gc.SELECTTYPE.value "DOUBLECLICK"
          call test_doubleclick 1
          set count 1
        else
          set gc.INSTRUCTIONS.value "^startStr"
          set gc.SELECTIONTEXT.value "NONE SELECTED"
        endif
      elseif gc.SELECTTYPE.value EQS "DOUBLECLICK"
        if count eq 1 then
          call test_doubleclick 2
          set count 2
        else
          set gc.INSTRUCTIONS.value "^startStr"
          set gc.SELECTIONTEXT.value "NONE SELECTED"
        endif
      endif
    endif

  elseif msg.name EQS "TEST" then
    if msg.data EQS "START" then
      set gc.SELECTTYPE.value "ROW"
      call test_select_row

    else
      ! NONE
      call test_reset
    endif

  elseif msg.name EQS "MAIN" then
    assert/text="^reg.main.name: Interactive tests" msg.data eqss "PASS"
    pipe stop

  elseif msg.name EQS "SHOWN" then
    ! Do nothing

  else
    say "Unhandled messagename: ^msg.name from fid: ^msg.fid data: ^msg.data"
res/all msg.data
  endif

return

!------------------------------------------------------------------------------!
procedure test_reset
  invoke multi reg.IDLIST.gs.multiselmsgmode
  if  multi NEQS "REG" then
    set reg.IDLIST.gs.multiselmsgmode "OFF"
  endif
  mess send IDLIST,,"OPENFILE" ,,""
  set gc.INSTRUCTIONS.value "List should be empty."

return
!------------------------------------------------------------------------------!
procedure test_doubleclick l:count
!
!   Test select a double click
!
  set gc.INSTRUCTIONS.value "Double click on row 1."
  set row 1
  set tabValue reg.IDLIST.getRow(^row)
  set expectedMessage "SELECT"

  if count eq 1 then
    set expectedSelMsg {INFO=0,DATA={TYPE="ROW",ROW=^row,COLUMN=-1,NAME="",VALUE=^tabValue,CURRSEL={},PREVSEL={},SELECTONE=1,SELECTRANGE=0}}
  elseif count eq 2 then
    set expectedSelMsg {INFO=0,DATA={TYPE="ROW",ROW=^row,COLUMN=-1,NAME="",VALUE=^tabValue,CURRSEL={},PREVSEL={1=^tabValue},SELECTONE=1,SELECTRANGE=0,DOUBLECLICK="TRUE"}}
  endif
return

!------------------------------------------------------------------------------!
procedure test_select_row
!
!   Test select a single row
!
  set gc.INSTRUCTIONS.value "Select row 3 with a LEFT MOUSE button click on the row button."
  set row 3
  set tabValue reg.IDLIST.getRow(^row)
  set expectedMessage  "SELECT"
  invoke newVer nxm.sys.lib.StringUtil.compareVersions("^env.nmversion","2.0.0")
  if newVer then
    set expectedSelMsg {INFO=0,DATA={TYPE="ROW",ROW=^row,COLUMN=-1,VALUE=^tabValue,NAME="",SELECTONE=1,SELECTRANGE=0}}
  else
    set expectedSelMsg {INFO=0,DATA={TYPE="ROW",ROW=^row,COLUMN=-1,VALUE=^tabValue,NAME=""}}
  endif
return

!------------------------------------------------------------------------------!
procedure test_popup
  set gc.INSTRUCTIONS.value "To see a popup, select row 17 with a LEFT MOUSE button click on the row button. &
                             Click popup with RMB to make it go away."
    set row 17
  set tabValue reg.IDLIST.getRow(^row)
    set expectedMessage  "SELECT"
  invoke newVer nxm.sys.lib.StringUtil.compareVersions("^env.nmversion","2.0.0")
  if newVer then
    set expectedSelMsg {INFO=0,DATA={TYPE="ROW",ROW=^row,COLUMN=-1,VALUE=^tabValue,NAME="",CURRSEL={},PREVSEL={},SELECTONE=1,SELECTRANGE=0}}
  else
    set expectedSelMsg {INFO=0,DATA={TYPE="ROW",ROW=^row,COLUMN=-1,VALUE=^tabValue,NAME="",CURRSEL={},PREVSEL={}}}
  endif

return

!------------------------------------------------------------------------------!
!   Test select multiple rows
!
procedure test_select_multirow
  invoke multi reg.IDLIST.gs.multiselmsgmode
  if  multi NEQS "REG" then
    set reg.IDLIST.gs.multiselmsgmode "FULL"
  else
    warning "This version of NeXtMidas ^env.version does not support multi selection."
    return
  endif
  set gc.INSTRUCTIONS.value &
       "MULTI SELECT: &
       1) With row 3 highlighted, select row 5 with SHIFT-LMB."
  set expectedMessage  "SELECT"
  set row 5
  set tabValue reg.IDLIST.getRow(^row)
  invoke newVer nxm.sys.lib.StringUtil.compareVersions("^env.nmversion","2.0.0")
  if newVer then
    set expectedSelMsg {INFO=0,&
        DATA={TYPE="ROW",ROW=^row,COLUMN=-1,VALUE=^tabValue,NAME="",CURRSEL={},PREVSEL={},SELECTONE=0,SELECTRANGE=1}}
  else
    set expectedSelMsg {INFO=0,&
        DATA={TYPE="ROW",ROW=^row,COLUMN=-1,VALUE=^tabValue,NAME="",CURRSEL={},PREVSEL={}}}

  endif
  set expectedSelMsg.DATA.prevsel.3 reg.IDLIST.getRow(3)
  do row 3 5
    set tab reg.IDLIST.getRow(^row)
    set expectedSelMsg.DATA.currsel.^{row} ^tab
  enddo

return

!------------------------------------------------------------------------------!
procedure test_deselect
  set gc.INSTRUCTIONS.value "Rows 3-5 should be highlighted. &
                                   Deselect with the RMB."
  set expectedMessage  "DESELECT"
  set row 5
  set tabValue reg.IDLIST.getRow(^row)
  set tempTable ^expectedSelMsg
  set expectedSelMsg {INFO=0,&
                      DATA={TYPE="ROW",ROW=^row,COLUMN=-1,&
                            VALUE=^tempTable.DATA.value,&
                            PREVSEL=^tempTable.DATA.currsel}}

return

!------------------------------------------------------------------------------!
procedure compareData U:cd_status
    set ok TRUE
    if ^msg.info NEQ ^expectedSelMsg.info set OK FALSE
    set L:msgDataSize      ^msg.data.getSize
    set L:expectedDataSize ^expectedSelMsg.data.getSize
    if msgDataSize NEQ expectedDataSize then
      if /VERBOSE then WARNING "data sizes differ! ^msgDataSize , ^expectedDataSize"
      set OK FALSE
    else
      ! Makes sense to do DATA comparisons

      foreach dataKey INTABLE expectedSelMsg.data
        if datakey EQS "VALUE" then
          ! Check the msg.data.value
          set valSize         ^msg.data.value.getSize
          set expectedValSize ^expectedSelMsg.data.value.getSize
          if valSize EQ expectedValSize then
            foreach key INTABLE expectedSelMsg.data.value
              set val          msg.data.value.^{key}
              set expectedVal expectedSelMsg.data.value.^{key}
              if "^val" NEQS "^expectedVal" then
                set ok "FALSE"
                break
              endif
            endfor
          else
            set OK FALSE
          endif

        elseif datakey EQS "CURRSEL" then
          ! Check the msg.data.value
          set valSize         ^msg.data.currsel.getSize
          set expectedValSize ^expectedSelMsg.data.currsel.getSize
          if valSize EQ expectedValSize then
            foreach key INTABLE expectedSelMsg.data.currsel
              set val          msg.data.currsel.^{key}
              set expectedVal expectedSelMsg.data.currsel.^{key}
              if "^val" NEQS "^expectedVal" then
                set ok "FALSE"
                break
              endif
            endfor
          else
            set OK FALSE
          endif

        elseif datakey EQS "PREVSEL" then
          ! Check the msg.data.value
          set valSize         ^msg.data.prevsel.getSize
          set expectedValSize ^expectedSelMsg.data.prevsel.getSize
          if valSize EQ expectedValSize then
            foreach key INTABLE expectedSelMsg.data.prevsel
              set val          msg.data.prevsel.^{key}
              set expectedVal expectedSelMsg.data.prevsel.^{key}
              if "^val" NEQS "^expectedVal" then
                set ok "FALSE"
                break
              endif
            endfor
          else
            set OK FALSE
          endif

        else
          if  "^msg.data.^{dataKey}" NEQS "^expectedSelMsg.data.^{dataKey}" then
            set OK FALSE
          endif
        endif
        if /VERBOSE then info "^reg.main.name: ^dataKey OK = ^OK"
      endfor
    endif
    set ^cd_status ^OK

return

!------------------------------------------------------------------------------!
procedure compareDeselectData U:cd_status
    set ok TRUE
    if ^msg.info NEQ ^expectedDeselMsg.info set OK FALSE
    set L:msgDataSize      ^msg.data.getSize
    set L:expectedDataSize ^expectedDeselMsg.data.getSize
    if msgDataSize NEQ expectedDataSize then
      set OK FALSE
    else
      ! Makes sense to do DATA comparisons
      foreach key INTABLE expectedDeselMsg.data
        if key EQS "PREVSEL" then
          foreach pslKey INTABLE pslKey
            set val         msg.data.prevsel.^{pslKey}
            set expectedVal expectedDeselMsg.data.prevsel.^{pslKey}
            if "^val" NEQS "^expectedVal" then
              set ok "FALSE"
              break
            endif
          endfor
        else
          if ^msg.data.^{key} NEQ ^expectedDeselMsg.data.^{key} set OK FALSE
        endif
      endfor
    endif
    set ^cd_status ^OK

return
