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

  call setDatDir

  set newInstr "After each test, the RAM aux is checked to ensure that &
                temp files are deleted. Results are displayed to the &
                command prompt."

  if interAct OR coreGraphics then
    pipe init
      panel/msgid=MAIN/id=panel/controls=gc
      list2/grid=on/msgid=MAIN/id=idlist

      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" ^newInstr 24 10 /title
      gcontrol/nc=2 button test "ListTest" &
              "Type2000,ComplexData,Type5000,Type6000,TextFile,CsvFile,CsvFile2,&
               DbfFile,MissingFile" none
      gcontrol/nc=2 button task "Task" "ChangeTitle,CreateFile" none
      set defItemColors {ChangeTitle=GRAY,CreateFile=GRAY}
      set gc.task.itemColors defItemColors

    pipe off

  endif
endmacro

!------------------------------------------------------------------------------!
! Procedure for handling all messages
procedure processMessage m:msg
  set gc.task.itemColors defItemColors
  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
    !say "Got ^msg.name from ^msg.fid"

  elseif msg.name EQS "TEST" then
    if msg.data EQS "TYPE2000" then
      call test_type2000
    elseif msg.data EQS "COMPLEXDATA" then
      call test_complex_data
    elseif msg.data EQS "TYPE5000" then
      call test_type5000
    elseif msg.data EQS "TYPE6000" then
      call test_type6000
    elseif msg.data EQS "TEXTFILE" then
      call test_textfile
    elseif msg.data EQS "CSVFILE" then
      call test_csvfile
    elseif msg.data EQS "CSVFILE2" then
      call test_csvfile2
    elseif msg.data EQS "DBFFILE" then
      call test_dbffile
    elseif msg.data EQS "MISSINGFILE" then
      call test_missingfile
    else
      call test_none
    endif
  elseif msg.name EQS "TASK" then
    if msg.data eqs "CREATEFILE" then
      ! Tests issues related to Bug 810
      noop ^{datDir}.testxy3000.prm ram:test_missing_file
    elseif msg.data eqs "CHANGETITLE"
      ! Tests issues related to Bug 381 and Bug 418
      set reg.idlist.updateTemplate {F1={LABEL="DATA"}}
    endif
  else
    say "Unhandled messagename: ^msg.name from fid: ^msg.fid data: ^msg.data"
  endif

  call check_ram
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

!------------------------------------------------------------------------------!
procedure test_textfile
  set reg.idlist.openFile "^{datDir}.cities.txt"
  set reg.idlist.setTemplate "{_GLOBAL_={AUTOHIDE=FALSE}}"
  set reg.panel.controls.idtext.value "Should see information about major cities."
return

!------------------------------------------------------------------------------!
procedure test_csvfile
  set reg.idlist.openFile "^{datDir}.airports.csv{HEADERROWS=1,&
                        COLUMNNAMES=AIRPORT_NAME|CODE|SF:LATITUDE|SF:LONGITUDE}"
  set reg.idlist.setTemplate "{_GLOBAL_={AUTOHIDE=FALSE},CODE={},LATITUDE={FORMAT=LAT},&
                                                         LONGITUDE={FORMAT=LON}}"
  set reg.panel.controls.idtext.value "Should see information about major airports."
return

!------------------------------------------------------------------------------!
procedure test_csvfile2
  invoke PI nxm.sys.inc.Constants.PI

  res s:COLS  "SD:DOUBLE,SF:FLOAT,SX:XLONG,SL:LONG,SI:INT,SB:BYTE,SZ:BOOLEAN"
  res s:FNAME ram:csvfile2{AUX=RAM,FG=CSV,HEADERROWS=1,COLUMNNAMES="^COLS"}

  res t:ROWS  {0={DOUBLE=0,FLOAT=0,XLONG=0,LONG=0,INT=0,BYTE=0,BOOLEAN=0},&
               1={DOUBLE=1,FLOAT=1,XLONG=1,LONG=1,INT=1,BYTE=1,BOOLEAN=1},&
               2={DOUBLE=-1,FLOAT=-1,XLONG=-1,LONG=-1,INT=-1,BYTE=-1,BOOLEAN=FALSE},&
               3={DOUBLE=^PI,FLOAT=^PI,XLONG=^PI,LONG=^PI,INT=^PI,BYTE=^PI,BOOLEAN=TRUE},&
               4={DOUBLE=-1,FLOAT=-1,XLONG=-1,LONG=-1,INT=-1,BYTE=-1,BOOLEAN=NO},&
               5={DOUBLE=^PI,FLOAT=^PI,XLONG=^PI,LONG=^PI,INT=^PI,BYTE=^PI,BOOLEAN=YES}}

  file open/n csvfile2 FNAME
    foreach key inTable rows
      invoke ,, csvfile2.setData(^key,rows.^{key})
    endfor
  file close csvfile2

  set reg.idlist.openFile FNAME
  set reg.idlist.setTemplate ""
  set reg.panel.controls.idtext.value "Should see a list with different types of data."
return

!------------------------------------------------------------------------------!
!@since NeXtMidas 3.8+                                                         !
!------------------------------------------------------------------------------!
procedure test_dbffile
  set reg.idlist.openFile "^{datDir}.world.dbf"
  set reg.idlist.setTemplate "{_GLOBAL_={AUTOHIDE=FALSE},POPULATION={FORMAT=0}}"
  set reg.panel.controls.idtext.value &
      "Should see information about a number of countries. Columns include: &
       POPULATION, FIPS (FIPS Country Code), CODE (Country Code), &
       Code_3 (3 letter country code), &
       NAME (Country Name), &
       Longitude, Lattitude.

return

!------------------------------------------------------------------------------!
procedure test_type2000
  ! First copy the file to RAM - this helps test bug 433.
  noop ^{datDir}.apenny.prm rampenny{aux=ram}
  set reg.idlist.openFile "ram:rampenny"
  set reg.idlist.setTemplate "{_GLOBAL_={AUTOHIDE=FALSE}}"
  set reg.panel.controls.idtext.value "Should see the listing of 'apenny'."
return

!-----------------------------------------------------------------
!   Test list display of large window with evenly spaced columns"
procedure test_complex_data
  set gc.task.itemColors ^{defItemColors}{ChangeTitle=GREEN}
  set reg.idlist.openFile "^{datDir}.fftdemo.prm"
  set reg.idlist.setTemplate    {F1={FGCOLOR="GREEN",TYPE="NUMBER",FORMAT="###&SCI",LABEL="JUNK"}}

  set reg.panel.controls.idtext.value &
      "You should see a single column with complex numbers in green. &
       The real part should be displayed as an integer and the complex &
       should be in SCI notation. The title at the top of the column &
       will initially be 'JUNK', click the 'ChangeTitle' button to change &
       the title to 'DATA'."
return

!-----------------------------------------------------------------
!   Test list display of large window with evenly spaced columns"
procedure test_type5000
  set reg.idlist.openFile "^{datDir}.coneflags5000.prm"
  set reg.idlist.setTemplate {POS={FGCOLOR="GREEN",FORMAT="GEN&LAT&LON"},&
                              POSP={FGCOLOR="GREEN",FORMAT="0.0&0.0&0.0"},&
                              CONE={FGCOLOR="RED",FORMAT="0&0&0"},&
                              NAME={FGCOLOR="GREEN"},&
                              FLAG={FGCOLOR="RED"}}
  set reg.panel.controls.idtext.value &
      "POS VD with (ALT,LAT,LON). POSP should be a VD with 1 decimal place. CONE should be a VD &
       with no decimal places. FLAG should be a 8B."
return

!-----------------------------------------------------------------
!
procedure test_type6000
  set reg.idlist.openFile "^{datDir}.test6000.prm"
  set reg.idlist.setTemplate {_GLOBAL_={AUTOHIDE=FALSE}}
  set reg.panel.controls.idtext.value "Should see columns F1,R1,CC,FF and GG &
                                       with 4 rows."
return

!-----------------------------------------------------------------
!   Test list display of large window with evenly spaced columns"
procedure test_missingfile
  set gc.task.itemColors ^{defItemColors}{CreateFile=GREEN}
  erase/warn=N ram:test_missing_file

  info "The next line should say that test_missing_file{AUX=RAM} was not found"
  set reg.idlist.openFile "test_missing_file{AUX=RAM}"
  set reg.idlist.setTemplate {_GLOBAL_={AUTOHIDE=FALSE,FGCOLOR="BLUE"},ABSC={FGCOLOR="RED"}}
  set reg.panel.controls.idtext.value &
      "List should come up displaying nothing until 'CreateFile' is pressed. &
       Once 'CreateFile' is pressed is should show a file with four columns, the &
       ABSC column should have the text colored red while all other columns are &
       blue."
return

!-----------------------------------------------------------------
!   Ensure that the temp files in the RAM aux are getting cleaned out
!
!   The ASSERT below is based on the temp file naming convention found in LIST2.
!   Any changes to that convention should be reflected here.
procedure check_ram
  set S:delFileName list2_temp_001   ! the filename that shoud not ever be created

  assert/text="Checking that RAM temp files cleaned out" ^delFileName nfexists
return
