!******************************************************************************!
! Test that covers a lot of Jeremy's Gak option tree
! LayerDrawable has multiple LayerT5s and possibly other layers associated with it
! Features can be added
!
! @bug      NXM-3652 ZOOMKEEP
!
! @since    4.0.0
! @author:  Jeanette McCall
!******************************************************************************!
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)
  switch DEBUG       debug    GET DEF=FALSE SDEF=TRUE

  call setDatDir

  res layeradded false
  res unzoomed   false

  res shapefile "^{datDir}.world.shp" ! SET DEFAULT SHAPE FILE

  res limits    "{VIEWLIMITS={X2=D:200,X1=D:180.21,Y2=L:-1,Y1=D:-50.65,Z2=2},&
                  OUTERBOUNDS={X2=D:360,X1=D:160.7776,Y2=L:-1,Y1=L:-85,Z2=2}}"
  res xyzlimits "{VIEWLIMITS={X2=D:100,X1=D:0,Y2=D:100,Y1=D:0},&
                  OUTERBOUNDS={X2=D:180,X1=D:-180,Y2=D:100,Y1=D:-80}}"


  if interAct then
    set instructions "Test ZOOMKEEP switch on Map & non-Map Views\n\n&
                      1) Click 'Test' under Run Check Bounds Test\n&
                      2) Test ZoomKeep by clicking 'add layer ...'\n&
                      3) Again, Click 'Test' under Run Check Bounds Test\n&
                      4) Finally, Right Click in upper right plot to unzoom\n\n&
                      - All tests in terminal should pass\n&
                      - Lower 2 plots should have expanded their view range\n&
                      - Upper 2 plots (with ZOOMKEEP) should keep the same view range"


    invoke booleanfalse java.lang.Boolean.valueOf("false")
    invoke booleantrue java.lang.Boolean.valueOf("true")

    set this.timer(0) 0.2
    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 14 /title

      nxm.sys.test.plotellipsetester/BQ/DIRECTCOLOR=MAGENTA/NOFLAGS/RT/NOTEMODETYPE=VA/RATE=40 OUT=_t5pipe2{SIZE=10000} ELEM=Inf

      plot/id=keepzoom/msgid=main ^{datDir}.world.prm|{LT="Drawable",name=zoomdrawable,listlayers=false} view=latloncontinuous /ZOOMKEEP
      plot/id=resize/msgid=main ^{datDir}.world.prm|{LT="Drawable",name=resizedrawable,listlayers=true} view=latloncontinuous
      plot/id=znotmap/msgid=main ^{datDir}.apenny.prm|{LT="Drawable",name=notmapzdrawable,listlayers=true} /ZOOMKEEP
      plot/id=rnotmap/msgid=main ^{datDir}.apenny.prm|{LT="Drawable",name=notmaprdrawable,listlayers=true}

      gcontrol line spaceFile "SomeWorthlessText" "BLUE" 7 ,, "CENTER"    ! Blue spacer
      gcontrol label fileSection "File / Pipe Control Section"
      gcontrol button AddLAYER "Test ZoomKeep" "Add layer with values beyond current 'range'"
      gcontrol label fileSection "Check Bounds"
      gcontrol button checkBounds "Run Check Bounds Test" "Test"

      FEATURE label=pipebox table={NAME=PIPEBOX,TYPE=BOX,COLOR=AQUA,ENABLE=1} plot=timepipe

    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 "UNZOOM" then
    res unzoomed true
  elseif msg.name eqs "TIMER" then
    set this.timer(0) 0
    invoke ,, reg.keepzoom.MP.loadLimits(^limits)
    invoke ,, reg.resize.MP.loadLimits(^limits)
    invoke ,, reg.znotmap.MP.loadLimits(^xyzlimits)
    invoke ,, reg.rnotmap.MP.loadLimits(^xyzlimits)
  elseif msg.name EQS "ADDLAYER" then
    invoke ,, reg.keepzoom.openFile(_t5pipe2{Layer={Group=zoomdrawable,NAME=SHAPEZOOM}})
    invoke ,, reg.resize.openFile(_t5pipe2{Layer={Group=resizedrawable,NAME=SHAPEFULL}})
    invoke ,, reg.znotmap.openFile(^shapeFile{Layer={Group=notmapzdrawable,NAME=SHAPENOTMAPZ}})
    invoke ,, reg.rnotmap.openFile(^shapeFile{Layer={Group=notmaprdrawable,NAME=SHAPENOTMAPFULL}})
    res layeradded true
  elseif msg.name EQS "CHECKBOUNDS" then
    if DEBUG then
      res reg.keepzoom.outerbounds
      say "  keepzoom  x1,y1,z1 ^{reg.keepzoom.MP.x1},^{reg.keepzoom.MP.y1},^{reg.keepzoom.MP.z1}"
      say "  keepzoom  x2,y2,z2 ^{reg.keepzoom.MP.x2},^{reg.keepzoom.MP.y2},^{reg.keepzoom.MP.z2}"
      res reg.resize.outerbounds
      say "  resize    x1,y1,z1 ^{reg.resize.MP.x1},^{reg.resize.MP.y1},^{reg.resize.MP.z1}"
      say "  resize    x2,y2,z2 ^{reg.resize.MP.x2},^{reg.resize.MP.y2},^{reg.resize.MP.z2}"
      res reg.znotmap.outerbounds
      say "  znotmap   x1,y1,z1 ^{reg.znotmap.MP.x1},^{reg.znotmap.MP.y1},^{reg.znotmap.MP.z1}"
      say "  znotmap   x2,y2,z2 ^{reg.znotmap.MP.x2},^{reg.znotmap.MP.y2},^{reg.znotmap.MP.z2}"
      res reg.rnotmap.outerbounds
      say "  rnotmap   x1,y1,z1 ^{reg.rnotmap.MP.x1},^{reg.rnotmap.MP.y1},^{reg.rnotmap.MP.z1}"
      say "  rnotmap   x2,y2,z2 ^{reg.rnotmap.MP.x2},^{reg.rnotmap.MP.y2},^{reg.rnotmap.MP.z2}"
    endif
    res keepy2     reg.keepzoom.outerbounds.y2
    res resizey2   reg.resize.outerbounds.y2
    res keepnotmap reg.znotmap.outerbounds.y1
    res keepnotmapy1 reg.znotmap.MP.y1
    res resizenotmap reg.rnotmap.outerbounds.y1
    if LAYERADDED THEN
      info "Results AFTER layers are added"
      if UNZOOMED then
        warn "Instructions completed out of order - UNZOOMED layers may fail their test"
      endif
      assert/text="^reg.main.name: Map View /ZOOMKEEP OUTERBOUNDS did not change" keepy2 EQT -1
      assert/text="^reg.main.name: Map View no switch OUTERBOUNDs updated" resizey2 GT 83
      assert/text="^reg.main.name: Non-Map View /ZOOMKEEP OUTERBOUNDS updated" keepnotmap EQT -90
      assert/text="^reg.main.name: Non-Map View /ZOOMKEEP view area did not change" keepnotmapy1 EQT -1.0E-12
      assert/text="^reg.main.name: Non-Map View no switch OUTERBOUNDSs updated" resizenotmap EQT -90
    else
      info "Results BEFORE layers are added"
      assert/text="^reg.main.name: Map View /ZOOMKEEP OUTERBOUNDS before layer added" keepy2 EQT -1
      assert/text="^reg.main.name: Map View no switch OUTERBOUNDs before layer added" resizey2 EQT -1
      assert/text="^reg.main.name: Non-Map View /ZOOMKEEP OUTERBOUNDS before layer added" keepnotmap EQT -80
      assert/text="^reg.main.name: Non-Map View no switch OUTERBOUNDS before layer added" resizenotmap EQT -80
    endif
  else

  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

!*****************************************************************************
! create blue file for this test
!*****************************************************************************
procedure createBlue

  %CONVERT_CSV_TO_T5000 ^csvFile ^grpTable ^test_blue_file rf=ecr

return

!*****************************************************************************
! add Feature
!*****************************************************************************
procedure addFeature s:tagtype
res tagtype
  res addon ""
  if tagtype NEQS "" then
    res addon "|^tagtype"
  endif
  res addon
  invoke xvalue nxm.sys.test.plotellipsetester.randomLon()
  invoke yvalue nxm.sys.test.plotellipsetester.randomLat()
  res name "^gc.featuretext.value"
  feature label=icon       table={NAME="^name",TYPE=SYMBOL|TEXT^{addOn},X=^xvalue,Y=^yvalue,&
                                  LINE={SYMBOL="^{datDir}.Arrow.png"}} &
                                  plot=timepipe
return

!*****************************************************************************
procedure addCustomFeature

  invoke xvalue nxm.sys.test.plotellipsetester.randomLon()
  invoke yvalue nxm.sys.test.plotellipsetester.randomLat()
  feature label=icon       table={NAME="^name",TYPE=SYMBOL|TEXT|CUSTOMTAG,X=^xvalue,Y=^yvalue,&
                                  CUSTOMTAGXPIX=^tagx,CUSTOMTAGYPIX=^tagy,CUSTOMTAGREF=^tagref,LINE={SYMBOL="^{datDir}.Arrow.png"}} &
                                  plot=timepipe
return
