! Tests Metadata on LayerT5
!
! @bug      NXM-3955 Point3D/LayerT5 support for METADATA
! @since    4.1.2
! @author:  Zachary Matthews
!******************************************************************************!
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
    call setDatDir

    ! Table for the starter Type 5000 Blue file to be generated
    set t:t5000_metadata {}
    set t:t5000_metadata.0 {POS=(0,20,10),NAME="String",METADATA="Just a String"}
    set t:t5000_metadata.1 {POS=(0,20,20),NAME="Table",METADATA={key="This is a Table"}}
    set t:t5000_metadata.2 {POS=(0,20,30),NAME="TableNested",METADATA={outerkey={innerkey1="first",innerkey2="second"}}}
    set t:t5000_metadata.3 {POS=(4000000,20,40),NAME="TableMultiNested",METADATA={key1={key1="one",key2="two"},key2={key3="three",key4="four"}}}

    ! Generate the starter Type 5000 Blue file
    table t5000_metadata getsize tblSize
    header/create ram:t5_starter.tmp TYPE=5000 RF=ECR COMPONENTS=(POS/VD/GEODETIC/DISTANCE_M,NAME/2A/UNDEFINED/NONE,METADATA/9A/UNDEFINED/NONE)
    foreach keyIdx INTABLE t5000_metadata
      file open/d/a out ram:t5_starter.tmp
        invoke ,, out.setData(keyIdx,t5000_metadata.^{keyIdx})
      file close out
    endfor

    ! Set the layer
    set lay "LT=T5,NAME=LAYT5,LABEL=NAME,AutoselectEnable=CLICK,AutoSelectMode=PIXEL,SELECTCOLOR=BLUE"

    ! Set the instructions
    set instructions "This test case tests the METADATA option for LayerT5\n\n&
                      1. Ctrl-Click each point left to right and observe its Metadata:\n\n    &
                          a. The first point's metadata should just be a String\n    &
                          b. The second point's metadata should be a Table with one key\n    &
                          c. The third point's metadata should be a Table with an outer key with multiple inner keys\n    &
                          d. The fourth point's metadata should be a Table with multiple outer keys, each with multiple inner keys\n\n&
                      2. Click \"Run Auto Test\" to run the automated portion of this test.\n\n&
                      If all tests passed, please PASS this test."
    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 20 /title

      plot/id=layt5plot/msgid=main ^{datDir}.world.prm|t5_starter.tmp{Layer={^lay}} &
      x1=0 x2=50 y1=0 y2=50 readoutExt=+SHOW|-FEATURES|+POINTS readout=+OnAxis 

      gcontrol button autotest ""  "Run Auto Test"

      res layert5 reg.layt5plot.layers.layt5
    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"
    pipe stop
  elseif msg.name eqs "AUTOTEST" then
    call autoTestPoints
  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

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

!******************************************************************************!
! Run the automated portion of this test
!******************************************************************************!
procedure autoTestPoints
  invoke point layert5.getPoint(0)
  res ptMeta point.metadata
  assert/text="Point 1 has maintained the right value in its metadata" "^{ptMeta}" eqs "Just a String"

  invoke point layert5.getPoint(1)
  res ptMeta point.metadata
  res val ptMeta.key
  assert/text="Point 2 has maintained the right value in its metadata" "^{val}" eqs "This is a Table"
  
  invoke point layert5.getPoint(2)
  res ptMeta point.metadata
  res val ptMeta.outerkey.innerkey1
  assert/text="Point 3 has maintained the right inner value in its metadata" "^{val}" eqs "first"

  invoke point layert5.getPoint(3)
  res ptMeta point.metadata
  res val ptMeta.key2.key3
  assert/text="Point 4 has maintained the right inner value in its metadata" "^{val}" eqs "three"
return
