!******************************************************************************!
! Test LIST2 polling and editing of files on disk via mouse clicks.
!
! @bug      2701 LIST2 Polling Issues
! @author:  J Bauer
!******************************************************************************!
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)

  if interAct then
    set instructions "You should see a LIST2 of a file created on disk. \n\n&
                      1. Attempt to edit a cell via mouse double-click. You should not be able to. \n\n&
                      2. Click the 'Add Rows' button to add two rows to the listed file. You &
                         should not see any change to the list. You should see the INFO &
                         statement in the SHELLGUI giving the new size.\n\n&
                      3. Click the 'Poll' button to poll the file and update the list. &
                         The list should display the correct amount of rows.\n\n&
                      4. Repeat steps 2 and 3 multiple times. The list should update correctly." 

    pipe on
      panel/msgid=MAIN/setup/controls=gc
      gcontrol label macro "Test MACRO"
      gcontrol menu  main "Exit with Status =" "PASS,FAIL" TABLE={ItemColors={PASS="GREEN",FAIL="RED"}} /tleft

      gcontrol text  idtext "Instructions" instructions 24 24 /title

      call loadData
      
      gcontrol line
      gcontrol button addRows "" "Add Rows" ""
      
      gcontrol line
      gcontrol button pollnow  "" "Poll"
      
      ! Note that the LIST2 command does not have the /POLLDATA switch
      list2/id=idlist/editable=f/useramfile=f myfile   ! /editable=f prevents editing via mouse
      
      shellgui/id=idshellgui/attach
      
    pipe off

  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"
    call eraseData
    pipe stop
    
  elseif msg.name EQS "POLLNOW" then
    call doPoll
    
  elseif msg.name eqs "ADDROWS" then
    call addRows  
    
  elseif msg.name eqs "GETSIZE" then
    call getSize  
    
  else
    say "Unhandled message name: ^msg.name from fid: ^msg.fid data: ^msg.data"
  endif
return

!******************************************************************************!
! If an unexpected exception is caught, mark test as failed, unless it is expected
!******************************************************************************!
procedure processException m:emsg
  if expectedException REXISTS then
    assert/text="^reg.main.name: Expected ERROR starts with: ^{expectedException}" expectedException EQSS emsg.data.message
    remove expectedException
  else
    assert/text="^reg.main.name: Caught unexpected exception ^{emsg.data}" "FALSE" isTrue
    if "Trace" SUBS ENV.DEBUG then invoke ,, emsg.data.printStackTrace()
  endif
return

!******************************************************************************!
! Create the file with data to be listed
!******************************************************************************!
procedure loadData
  ! create the file
  HEADERMOD/CREATE myfile TYPE=3000 FORM=NH NSR=3 RL=32 SR(1)=(LABL,2A,0) SR(2)=(ABSC,SD,16) SR(3)=(ORD,SD,24)
  
  ! load in the data
  res l:count 1

  file open/d/a tag myfile
  
  file write tag  {LABL="Row_^count",ABSC=1.2345,ORD=6.7891}
  calc l:count count 1 +
  
  file write tag  {LABL="Row_^count",ABSC=2.3456,ORD=7.8901}
  calc l:count count 1 +
  
  invoke ,, tag.flush()
  
  res curSize1 tag.size
  !say "Size before close = ^curSize1"
  file close tag
  

return

!******************************************************************************!
! Add two rows of data on disk
!******************************************************************************!
procedure addRows
  if myfile fexists then
    
    file open/d/a tag2 myfile
    
    file write tag2  {LABL="Row_^count",ABSC=1.2345,ORD=6.7891}
    calc l:count count 1 +
  
    file write tag2  {LABL="Row_^count",ABSC=2.3456,ORD=7.8901}
    calc l:count count 1 +
    
    invoke ,, tag2.flush()   ! this does nothing, but maybe it should so I left it here
    
    ! the close and reopen is needed because flush() does not reset internals
    file close tag2          
    file open/d/a tag2 myfile
    res l:rowSize tag2.numberOfRows
    
    
    INFO "^reg.main.name: ADDED ROWS. new number of rows in FILE: ^rowSize"
    
    file close tag2 
    
  endif
return

procedure doPoll
  invoke ,, reg.idlist.pollImmediately()
  call getSize
return

!******************************************************************************!
! Print out the size of the file being listed
!******************************************************************************!
procedure getSize
  if myfile fexists then
    file open/d tag3 myfile
    res l:curSize tag3.size
    res l:curRows tag3.numberOfRows
    res l:listRows reg.idlist.list.numberOfRows
    INFO "^reg.main.name: Current rows of file=^{curRows}, rows in LIST=^listRows"
    file close tag3
  endif
return

!******************************************************************************!
! Cleanup the file
!******************************************************************************!
procedure eraseData
  erase/q myfile
return
