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

  if interAct OR coreGraphics then
    ! INPUT files used for tests
    fname infile1 area=test name=input_test_list_01 ext=tmp opt=sys
    fname infile2 area=test name=input_test_list_02 ext=tmp opt=sys

    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" "Initializing..." 24 13 /title
      gcontrol/nc=2 button test "ListTest" &
              "NoTemplate,XmTemplates,NmTemplates,FormatsNum,FormatsStr,&
               AngleFormat,DateFormat,Ordering,AutoHide,Hide" none
      gcontrol/nc=2 button update "Ordering:" "UpdateTemplate" /tleft
      set defItemColors {UpdateTemplate=GRAY}
      set gc.update.itemColors defItemColors

    pipe off

  endif
endmacro

!------------------------------------------------------------------------------!
procedure processMessage m:msg
!
! Procedure for handling all messages
!
  set gc.update.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 "UPDATE" then
    if msg.data EQS "UPDATETEMPLATE" then
      table {ORD2={FGCOLOR="gray"}} save test_upd_templ.tbl
      invoke ,, reg.idlist.updateTemplate(test_upd_templ.tbl)   ! test for Bug 2602
      !invoke ,, reg.idlist.updateTemplate({ORD2={FGCOLOR="gray"}})
      set reg.panel.controls.idtext.value &
               "Check the order of the columns. The order should be: ABSC (red), &
                ORD3 (yellow), ORD2 (gray), ORD (blue). The only change should be &
                that the ORD2 column is now gray rather than green."
      erase test_upd_templ.tbl
    endif
  elseif msg.name EQS "TEST" then
    if msg.data EQS "NOTEMPLATE" then
      call test_notemplate
    elseif msg.data EQS "XMTEMPLATES" then
      call test_xmtemplates
    elseif msg.data EQS "NMTEMPLATES" then
      call test_nmtemplates
    elseif msg.data EQS "ANGLEFORMAT" then
      call test_angle
    elseif msg.data EQS "DATEFORMAT" then
      call test_date
    elseif msg.data EQS "FORMATSNUM" then
      call test_formats "NUM"
    elseif msg.data EQS "FORMATSSTR" then
      call test_formats "STR"
    elseif msg.data EQS "ORDERING" then
      call test_ordering
    elseif msg.data EQS "AUTOHIDE" then
      call test_autohide
    elseif msg.data EQS "HIDE" then
      call test_hide
    else
      call test_none
    endif
    
  else
    say "Unhandled messagename: ^msg.name from fid: ^msg.fid data: ^msg.data"
  endif


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_none
  set reg.idlist.openFile ""
  set reg.panel.controls.idtext.value "List should be empty"
return


!-----------------------------------------------------------------
! Test AUTOHIDE=TRUE (default)
procedure test_autohide
  set reg.idlist.openFile ^{datDir}.testxy3000.prm
  set reg.idlist.setTemplate {ABSC={FGCOLOR=RED,FORMAT="#"}}
  set reg.panel.controls.idtext.value &
               "Should see only one column, ABSC. The ABSC column should be RED."
return

!-----------------------------------------------------------------
! Test AUTOHIDE=FALSE but hide one of the columns
procedure test_hide
  set reg.idlist.openFile ^{datDir}.testxy3000.prm
  set reg.idlist.setTemplate {_GLOBAL_={AUTOHIDE="FALSE"},&
                              ABSC={FGCOLOR=RED,FORMAT="#",HIDE="TRUE"}}
  set reg.panel.controls.idtext.value &
               "Should see only three columns ORD, ORD2, ORD3."
return


!-----------------------------------------------------------------
!   Test list template processing, allowing color by enum or field
procedure test_ordering
  set gc.update.itemColors ^{defItemColors}{UpdateTemplate=GREEN}
  set reg.idlist.openFile ^{datDir}.testxy3000.prm
  set reg.idlist.setTemplate {ABSC={FGCOLOR=RED},ORD3={FGCOLOR=YELLOW},&
                              ORD2={FGCOLOR=GREEN},ORD={FGCOLOR=BLUE}}
  set reg.panel.controls.idtext.value &
               "Check the order of the columns. The order should be: ABSC (red), &
                ORD3 (yellow), ORD2 (green), ORD (blue). Now click the 'UpdateTemplate' &
                button to change the ORD2 column to gray."
return

!-----------------------------------------------------------------
!   Test list template processing, allowing color by enum or field
procedure test_xmtemplates
  convert ^infile2 TEMPLATE2TBL mytemplate
  
  set reg.idlist.openFile ^infile1
  set reg.idlist.setTemplate mytemplate
  set reg.panel.controls.idtext.value &
               "Column labels should be centered. They should be colored as follows: &
                SR01 - Same as SR02, SR02 - Each 'One' should be in red, 'Two' in &
                yellow, Three' in green, SR04 - Red, SR03 - Green."

return

!-----------------------------------------------------------------
!   Test list template processing, allowing color by enum or field
procedure test_nmtemplates
  res t:mytemplate {_GLOBAL_={GRID="ON",FGCOLOR="WHITE",LABELS="BOTH",SORT="+SR02"},&
                    SR01={LABEL="SR01",FGCOLOR="BLUE",WIDTH="15"},&
                    SR02={LABEL="SR02",WIDTH="15",TYPE="ENUM",ALIGN="CENTER",&
                          ENUM={E1={LABEL="One",COLOR="Red",VALUE="1"},&
                                E2={LABEL="Two",COLOR="Yellow",VALUE="2"},&
                                E3={LABEL="Three",COLOR="Green",VALUE="3"}}},&
                    SR04={LABEL="SR04",FGCOLOR="RED",WIDTH="15",TYPE="NUMBER",FORMAT="Std"},&
                    SR03={LABEL="SR03",FGCOLOR="GREEN",WIDTH="15"}}

  set reg.idlist.openFile ^infile1
  set reg.idlist.setTemplate mytemplate
  set reg.panel.controls.idtext.value &
               "Column labels should be centered. They should be colored as follows: &
                SR01 - Blue, SR02 - [DEF=WHITE] Each 'One' should be in red, 'Two' in &
                yellow, Three' in green, SR04 - Red, SR03 - Green."
return

!-----------------------------------------------------------------
!   Test list template processing, allowing color by enum or field
procedure test_notemplate
  set t:testTemplate {_GLOBAL_={},&
                      ORD2={FGCOLOR=GREEN},ORD3={FGCOLOR=YELLOW},&
                      ABSC={FGCOLOR=RED},ORD={FGCOLOR=BLUE}}

  set reg.idlist.openFile ^{datDir}.testxy3000.prm

  set reg.idlist.setTemplate null
  res templ reg.idlist.template
  assert/text="Got default template from LIST2"    templ rexists
  assert/text="Default template has 5 fields"      templ.getsize eq 5
  assert/text="Default template contains _GLOBAL_" templ._GLOBAL_ rexists
  assert/text="Default template contains ABSC"     templ.ABSC     rexists
  assert/text="Default template contains ORD"      templ.ORD      rexists
  assert/text="Default template contains ORD2"     templ.ORD2     rexists
  assert/text="Default template contains ORD3"     templ.ORD3     rexists
  assert/text="Default template contains ORD.TYPE" templ.ORD.TYPE rexists
  assert/text="Default template ORD.TYPE = NUMBER" templ.ORD.TYPE eqs "NUMBER"
  
  set reg.idlist.setTemplate testTemplate
  res templ reg.idlist.template
  assert/text="Got template from LIST2"      templ rexists
  assert/text="Template has 5 fields"        templ.getsize eq 5
  assert/text="Template contains _GLOBAL_"   templ._GLOBAL_ rexists
  assert/text="Template contains ABSC"       templ.ABSC     rexists
  assert/text="Template contains ORD"        templ.ORD      rexists
  assert/text="Template contains ORD2"       templ.ORD2     rexists
  assert/text="Template contains ORD3"       templ.ORD3     rexists
  assert/text="Template ABSC.FGCOLOR is set" templ.ABSC.FGCOLOR eqs testTemplate.ABSC.FGCOLOR
  assert/text="Template ORD.FGCOLOR is set"  templ.ORD.FGCOLOR  eqs testTemplate.ORD.FGCOLOR
  assert/text="Template ORD2.FGCOLOR is set" templ.ORD2.FGCOLOR eqs testTemplate.ORD2.FGCOLOR
  assert/text="Template ORD3.FGCOLOR is set" templ.ORD3.FGCOLOR eqs testTemplate.ORD3.FGCOLOR

  set reg.idlist.setTemplate null
  res templ reg.idlist.template
  assert/text="Got default template from LIST2"    templ rexists
  assert/text="Default template has 5 fields"      templ.getsize eq 5
  assert/text="Default template contains _GLOBAL_" templ._GLOBAL_ rexists
  assert/text="Default template contains ABSC"     templ.ABSC     rexists
  assert/text="Default template contains ORD"      templ.ORD      rexists
  assert/text="Default template contains ORD2"     templ.ORD2     rexists
  assert/text="Default template contains ORD3"     templ.ORD3     rexists
  assert/text="Default template contains ORD.TYPE" templ.ORD.TYPE rexists
  assert/text="Default template ORD.TYPE = NUMBER" templ.ORD.TYPE eqs "NUMBER"
  ! The next tests are prone to change in the furure if the defaults change...
  ! they are here just to show that the template was cleared out and a new
  ! default one is being used.
  assert/text="Default template no ABSC.FGCOLOR"    templ.ABSC.FGCOLOR nrexists
  assert/text="Default template no ORD.FGCOLOR"     templ.ORD.FGCOLOR  nrexists
  assert/text="Default template no ORD2.FGCOLOR"    templ.ORD2.FGCOLOR nrexists
  assert/text="Default template no ORD3.FGCOLOR"    templ.ORD3.FGCOLOR nrexists
  
  set reg.panel.controls.idtext.value &
               "Done Running Automated Tests. Should now see four columns, in &
                order they are ABSC, ORD, ORD2, ORD3. All columns should be &
                colored the same and should be using the same numeric format."
return

!------------------------------------------------------------------------------!
! Tests angle formats (Bug 1020)
procedure test_angle
  set s:fname     test_file{AUX=RAM,FG=CSV,HEADERROWS=1}
  set t:testTempl {F1={TYPE=ANGLE,FORMAT="dddomm''ss.sssE"},&
                   F2={TYPE=ANGLE,FORMAT="+ddd'd' mm'm' ss.sss's'"},&
                   F3={TYPE=ANGLE,FORMAT="+ddd''mm''ss"}}

  table testTempl getkeys/s colNames
  set d:angleVal  12.3456789
  
  file open/n testFile ^{fname}{COLUMNNAMES="^{colNames}"}
    set t:row {}
    
    foreach key inList colNames
      set row.^{key} angleVal
    endfor
    set testFile.insertData(-1) row
  file close testFile

  set reg.idlist.openFile    ^{fname}
  set reg.idlist.setTemplate testTempl
  set reg.panel.controls.idtext.value &
               "Should see one row with the same date printed three different ways. &
                F1='012'20'44.444E' (should have little deg sign between 012 and 20), &
                F2='+012d 20m 44.444s', F3='+012'20'44'."

return

!------------------------------------------------------------------------------!
! Tests time formats (Bug 994)
procedure test_date
  set s:fname     test_file{AUX=RAM,FG=CSV,HEADERROWS=1}
  set t:testTempl {F1={TYPE=DATE,FORMAT="yyyy.MM.dd 'at' HH:mm:ss z"},&
                   F2={TYPE=DATE,FORMAT="yyyy.MM.dd 'at' HH:mm:ss z",TIMEZONE="GMT-5"},&
                   F3={TYPE=DATE,FORMAT="yyyy-MM-dd'T'HH:mm:ss.SSSZ"}}

  table testTempl getkeys/s colNames
  set s:timeStr   "2007:05:31::16:09:27.595"
  set d:timeVal   timeStr
  
  file open/n testFile ^{fname}{COLUMNNAMES="^{colNames}"}
    set t:row {}
    
    foreach key inList colNames
      set row.^{key} timeVal
    endfor
    set testFile.insertData(-1) row
  file close testFile

  set reg.idlist.openFile    ^{fname}
  set reg.idlist.setTemplate testTempl
  set reg.panel.controls.idtext.value &
               "Should see one row with the same date printed three different ways. &
                F1='2007.05.31 at 16:09:27 UTC', F2='2007.05.31 at 11:09:27 GMT-5:00', &
                F3='2007-05-31T16:09:27.595+0000'."

return

!------------------------------------------------------------------------------!
! Tests formatting. (Bug 409 and others)
! ffmt - Format of the data file.    ("NUM", "STR")
!
procedure test_formats s:ffmt
  ! Create test file for templates.
  if ffmt eqs "NUM" then
    res ffmtStr "SD"
    headermod/create template_test_file &
                   SR=(GEN/SD,VIS/SD,SCI/SD,ENG/SD,MAN/SD,&
                       DMS/SD,LAT/SD,LON/SD,&
                       STD/SD,ACQ/SD,EPOC/SD,NORA/SD,TCR/SD,VAX/SD,HMS/SD,&
                       NET/SD)
  elseif ffmt eqs "STR" then
    res ffmtStr "2A"
    headermod/create template_test_file &
                   SR=(GEN/2A,VIS/2A,SCI/2A,ENG/2A,MAN/2A,&
                       DMS/2A,LAT/2A,LON/2A,&
                       STD/2A,ACQ/2A,EPOC/2A,NORA/2A,TCR/2A,VAX/2A,HMS/2A,&
                       NET/2A)
  endif

  set templateTestTempl {GEN={ALIGN="RIGHT",FGCOLOR="WHITE",FORMAT="GEN",LABEL="GEN (No exp btw 1e-3,1e15)"},&
                         VIS={ALIGN="RIGHT",FGCOLOR="WHITE",FORMAT="VIS",LABEL="VIS (No exp btw 1e-3,1e7)"},&
                         SCI={ALIGN="RIGHT",FGCOLOR="WHITE",FORMAT="SCI",LABEL="SCI (Scientific)"},&
                         ENG={ALIGN="RIGHT",FGCOLOR="WHITE",FORMAT="ENG",LABEL="ENG (Engineering)"},&
                         MAN={ALIGN="RIGHT",FGCOLOR="WHITE",FORMAT="MAN",LABEL="MAN (No exponents)"},&
                         DMS={ALIGN="RIGHT",FGCOLOR="RED",FORMAT="DMS",LABEL="DMS (uses +/-)"},&
                         LAT={ALIGN="RIGHT",FGCOLOR="RED",FORMAT="LAT",LABEL="LAT (uses N/S)",ALIGN=CENTER},&
                         LON={ALIGN="RIGHT",FGCOLOR="RED",FORMAT="LON",LABEL="LON (uses E/W)",ALIGN=CENTER},&
                         STD={ALIGN="RIGHT",FGCOLOR="GREEN",FORMAT="STD",LABEL="STD (yyyy:mm:dd::hh:mm:ss)"},&
                         ACQ={ALIGN="RIGHT",FGCOLOR="GREEN",FORMAT="ACQ",LABEL="ACQ (yy.ddd:hh:mm:ss)"},&
                         EPOC={ALIGN="RIGHT",FGCOLOR="GREEN",FORMAT="EPOCH",LABEL="EPOCH (yyyy:sec_in_year)"},&
                         NORA={ALIGN="RIGHT",FGCOLOR="GREEN",FORMAT="NORAD",LABEL="NORAD (yyddd.frac_of_day)"},&
                         TCR={ALIGN="RIGHT",FGCOLOR="GREEN",FORMAT="TCR",LABEL="TCR (ddd:hh:mm:ss)"},&
                         VAX={ALIGN="RIGHT",FGCOLOR="GREEN",FORMAT="VAX",LABEL="VAX (dd-MMM-yyyy:hh:mm:ss)"},&
                         HMS={ALIGN="RIGHT",FGCOLOR="GREEN",FORMAT="HMS",LABEL="HMS (hh:mm:ss)"},&
                         NET={ALIGN="RIGHT",FGCOLOR="YELLOW",FORMAT="NET",LABEL="NET (Network Address)"}}

  timex now S:timestr mjs=time
  res s:inet "0x7f000001" ! 0x7f000001=127.0.0.1

  call addNumRow template_test_file 42     42        inet time
  call addStrRow template_test_file 42     42        inet time
  call addNumRow template_test_file -42.42 -42'42'42 inet time
  call addStrRow template_test_file -42.42 -42'42'42 inet time
  call addNumRow template_test_file -42e10 0         inet time
  call addStrRow template_test_file -42e10 0         inet time
  call addNumRow template_test_file -42e20 -42'42'42 inet time
  call addStrRow template_test_file -42e20 -42'42'42 inet time

  set reg.idlist.openFile    template_test_file
  set reg.idlist.setTemplate templateTestTempl
  set reg.panel.controls.idtext.value &
               "[File format: ^{ffmt}] The various columns display the special numeric &
                formats available. Note that the columns should be at least as wide &
                as the column label. File type is ^{ffmtStr}, data inserted as numbers &
                (odd numbered rows) and strings (even numbered rows). Note that the &
                FormatsNum and FormatsStr should look the same."
return

! Adds a row with numeric values
procedure addNumRow u:file d:anr_val d:anr_ang l:anr_net d:anr_time
  set t:row {GEN=^{anr_val},VIS=^{anr_val},SCI=^{anr_val},ENG=^{anr_val},MAN=^{anr_val},&
             DMS=^{anr_ang},LAT=^{anr_ang},LON=^{anr_ang},&
             STD=^{anr_time},ACQ=^{anr_time},EPOC=^{anr_time},NORA=^{anr_time},TCR=^{anr_time},&
             VAX=^{anr_time},HMS=^{anr_time},NET=^{anr_net}}
  set FILE(file).insertData(-1,row)
return

! Adds a row with numeric values
procedure addStrRow u:file s:asr_val s:asr_ang s:asr_net s:asr_time
  set t:row {GEN=S:^asr_val,VIS=S:^{asr_val},SCI=S:^{asr_val},ENG=S:^{asr_val},MAN=S:^{asr_val},&
             DMS=S:^{asr_ang},LAT=S:^{asr_ang},LON=S:^{asr_ang},&
             STD=S:^{asr_time},ACQ=S:^{asr_time},EPOC=S:^{asr_time},NORA=S:^{asr_time},TCR=S:^{asr_time},&
             VAX=S:^{asr_time},HMS=S:^{asr_time},NET=S:^{asr_net}}
  set FILE(file).insertData(-1,row)
return
