! Allows WMS configuration before calling tests
!
! @issue   NXM-3684 LayerWMS issues
! @author  Jeanette McCall
! @since   NeXtMidas 4.0.0
!          (layerwms_config.tbl loading logic moved to include file in 4.0.1)
!
!******************************************************************************!
startmacro
  switch INTERACTIVE  interAct     GET DEF=TRUE  SDEF=TRUE
  switch BASELINE     baseline     GET DEF=TRUE  SDEF=TRUE
  switch COREGRAPHICS coreGraphics GET DEF=FALSE SDEF=TRUE ! for a quick check of graphics - more extreme tests - since 3.8.1
  switch EVALONLY     eval         GET DEF=FALSE SDEF=TRUE
  switch NOCONFIGMENU noconfigMenu GET DEF=FALSE SDEF=TRUE
  switch FULLCONFIG   fullconfig   GET DEF=FALSE SDEF=TRUE

  call setDatDir

  ! See if custom URLs configuration table exists
  ! If not, use default one: nxm.sys.test.layerwms_config_default.tbl
  if layerwms_config.tbl fexists then
    say "Using layerwms_config.tbl for configuration"
    table layerwms_options CREATE layerwms_config.tbl
  else
    say "layerwms_config.tbl does not exist, using nxm.sys.test.layerwms_config_default.tbl"
    table layerwms_options CREATE nxm.sys.test.layerwms_config_default.tbl
  endif

  ! Initial setup from layerwms_config.tbl or URLs, CRSs, layers, and WMS Version(s)
  call loadTableSetDefaults

  set this.timer(0) 0.5
  if interAct or coreGraphics then

    pipe on
      panel/msgid=MAIN/setup/controls=gc

      gcontrol label macro "MACRO"
      gcontrol menu    main "Exit with Status = " "PASS,FAIL" /tleft
      set gc.main.itemColors {PASS=GREEN,FAIL=RED}

      set instructions "1) Pick URLs and layers that are supported\n&
                        2) Select the WMS Version and Coordinate Reference System to be tested\n   &
                           blank defaults:\n   & 
                           CRS    : CRS:84\n   &
                           VERSION: 1.3.0\n&
                        3) Select the NeXtMidas test to run\n\n&
                        Note: this defaults to standard nextmidas.com test config"

      gcontrol text   idtext "Instructions" "^instructions" 24 8 /title
      gcontrol label testselection "URL, CRS, and WMS VERSION SELECTION - CRS & WMS UNCLASSIFIED - URL as marked"
      gcontrol choice urlchoice    "URL    " "^{urlopts}" "URL1: ^defaultURL_class ^defaultURL"
      gcontrol choice crschoice    "CRS    " "^{crsValues}" "^crsDefault" /EDIT=fullConfig
      gcontrol choice version      "VERSION" "^{requestedVersion}" "^versionDefault" /EDIT=fullConfig
      gcontrol label layerClass "^layersLabelRootTitle ^layerClassification"
      gcontrol button layers       "LAYERS " "^layerOptions" "" /nc=4  /flags=chkbox|toggle
      gcontrol label ordering "Layer Ordering"
      gcontrol button name=orderlayers title="" "ORDER LAYERS"

      gcontrol text layerOrder      "Layer order" ""
      gcontrol label testselection "TEST CASE SELECTION - (UNCLASSIFIED)"
      gcontrol choice test  "Select test: test_plot_" &
         "layerwms,layerwms_defaultsize,layerwms_pan,layerwms_range,layerwms_toggle,layer_autoscale,layerkml_kmz,&
         layerwms_multi (layer-transitions),layerwms_multi (wms-version-111),layerwms_multi (bad-background-only-tiles),&
         layerwms_multi (map-shift-elongation-download-threads),layerwms_multi (brightness-panning),layerwms_gwc"

    pipe off

  endif  ! end if interAct

endmacro

!******************************************************************************!
procedure processMessage m:msg

  if msg.name eqs "MAIN" then
    assert/text="^reg.main.name: Interactive tests" msg.data eqss "PASS"
    pipe stop

  elseif msg.name eqs "TIMER" then
    set this.timer(0) 0
    res gc.layers.value "^LayerDefault"

  elseif msg.name EQS "URLCHOICE" then
    res wholeSelection msg.data 
    invoke urlAttr wholeSelection.split(" ")
    res urlKey urlAttr.0
    invoke length urlKey.length
    calc end length 1 -
    res urlKey urlKey.substring(0,^end)
    table urlsTable GETKEY ^urlKey urlEntry
    res actualURL urlEntry.URL
    invoke actualURL actualURL.trim()
    res actualURL
    invoke numstr urlKey.substring(3)
    ! get the next url in the table, if there is one
    ! for use in tests that require a second url (for now)
    res l:num numStr
    calc l:plus1 num 1 +
    res plus1
    res nexturlkey "URL^{plus1}"
    res nexturlnum "URL^{plus1}"
    res nexturlkey
    table urlsTable GETKEY "^nexturlkey" nexturlentry
    res nexturlentry
    IF nexturlentry ISNULL THEN
      res nexturl ""
    ELSE
      res nexturl nexturlentry.url
    ENDIF
    call createLayerDefault urlKey
    res actualLayers "^urlsLayers"
    res gc.layers.items "^layerOptions"
    res gc.layers.value "^LayerDefault"
    res gc.layerClass.title "^layersLabelRootTitle ^layerClassification"
    if crsstable NEQS "" THEN
      call setUrlCrsUsingCRSS CRS_^{urlKey}
      res gc.crschoice.value "^crsValues"
    endif
    if versionstbl NEQS "" THEN
      call setUrlVersionUsingVERSIONS VER_^{urlKey}
      if actualVersion EQS "" then
       set gc.version.value "<Negotiated with WMS 1.3.0 given preference>"
      else
        set gc.version.value "^actualVersion"
      endif
    endif
    INFO "URL selected: ^actualURL*"
    invoke myLayers gc.layers.getValue()
    res gc.layerOrder.value "^myLayers"

  elseif msg.name eqs "CRSCHOICE" then
    res upperCaseCRS "^{msg.data}"
    call getMixedCaseCRS
    INFO "Coordinate Reference System Table: ^actualCRS"

  elseif msg.name eqs "LAYERS" then
    ! save off layer set and show order
    invoke myLayers gc.layers.getValue()
    res gc.layerOrder.value "^myLayers"

  elseif msg.name eqs "ORDERLAYERS" then
    call orderLayers

  elseif msg.name eqs "VERSION" then
    res actualVersion "^{msg.data}"
    INFO "Version selected: ^actualVersion"

  elseif msg.name eqs "TEST" then
    res testSuffix "^{msg.data}"
    call runTest

  else
    say "Unhandled message name: ^msg.name from fid: ^msg.fid dats: ^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

!******************************************************************************!
!* Run the test matching testSuffix
!******************************************************************************!
procedure runTest
  res testSuffix
  if myLayers NEQS "|" then
    call calcURLsLayers myLayers
    res actualLayers "^urlsLayers"
  else
    res actualLayers ""
  endif
  if testSuffix EQS "LAYERWMS" then
    if eval THEN
      res numFeatures 0
    ELSE 
      res numFeatures 60
    ENDIF
    if actualLayers NEQS "" then
      res actualLayers
      %test_plot_layerwms url="^actualUrl" layers="^actualLayers" crsOverride="^actualCRS" requestedVersion="^actualVersion" numFeatures=^numFeatures /IGNOREHREF /NOCONFIGMENU=^noconfigMenu
    else
      %test_plot_layerwms url="^actualUrl" crsOverride="^actualCRS" requestedVersion="^actualVersion" numFeatures=^numFeatures /IGNOREHREF /NOCONFIGMENU=^noconfigMenu
    endif
  elseif testSuffix EQS "LAYERWMS_DEFAULTSIZE" then
    if actualLayers NEQS "" then
      %test_plot_layerwms_defaultsize url="^actualUrl" layers="^actualLayers" crsOverride="^actualCRS" requestedVersion="^actualVersion" /IGNOREHREF
    else
      %test_plot_layerwms_defaultsize url="^actualUrl" crsOverride="^actualCRS" requestedVersion="^actualVersion" /IGNOREHREF
    endif
  elseif testSuffix EQS "LAYERWMS_LOOSE_SPEC" then
    say "^testSuffix is disabled - no longer applicable"
    !%test_plot_layerwms_loose_spec url="^actualUrl" /baseline=^baseline
  elseif testSuffix EQS "LAYERWMS_PAN" then
    if actualLayers NEQS "" then
      %test_plot_layerwms_pan url="^actualUrl" layers="^actualLayers" crsOverride="^actualCRS" requestedVersion="^actualVersion" /IGNOREHREF
    else
      %test_plot_layerwms_pan url="^actualUrl" crsOverride="^actualCRS" requestedVersion="^actualVersion" /IGNOREHREF
    endif
  elseif testSuffix EQS "LAYERWMS_RANGE" then
    if actualLayers NEQS "" then
      call formAllButLayers
      res allButLayers
      %test_plot_layerwms_range url="^actualUrl" layers="^allButLayers" crsOverride="^actualCRS" requestedVersion="^actualVersion" /IGNOREHREF
    else
      %test_plot_layerwms_range url="^actualUrl" crsOverride="^actualCRS" requestedVersion="^actualVersion" /IGNOREHREF
    endif
  elseif testSuffix EQS "LAYERWMS_TOGGLE" then
    if actualLayers NEQS "" then
      %test_plot_layerwms_toggle url="^actualUrl" layers="^actualLayers" crsOverride="^actualCRS" requestedVersion="^actualVersion" /IGNOREHREF
    else
      %test_plot_layerwms_toggle url="^actualUrl" crsOverride="^actualCRS" requestedVersion="^actualVersion" /IGNOREHREF
    endif
  elseif testSuffix EQS "LAYER_AUTOSCALE" then
    if actualLayers NEQS "" then
      call formAllButLayers
      res allButLayers
      %test_plot_layer_autoscale url="^actualUrl" layers="^allButLayers" crsOverride="^actualCRS" requestedVersion="^actualVersion" /ALTTESTENV /ALTWMS /IGNOREHREF
    else
      %test_plot_layer_autoscale url="^actualUrl" crsOverride="^actualCRS" requestedVersion="^actualVersion" /IGNOREHREF
    endif
  elseif testSuffix EQS "LAYERKML_KMZ" then
    res url2 ""
    if nexturl NISNULL then
      res nexturl
      invoke url2 nexturl.trim()
      call createURLsLayers nexturlnum
      res nextLayers "^urlsLayers"
    endif
    if actualLayers NEQS "" then
      %test_plot_layerkml_kmz url1="^actualUrl" url2="^url2" layers1="^actualLayers" layers2="^nextLayers" crsOverride="^actualCRS" requestedVersion="^actualVersion" /IGNOREHREF
    else
      %test_plot_layerkml_kmz url1="^actualUrl" url2="" crsOverride="^actualCRS" requestedVersion="^actualVersion" /IGNOREHREF
    endif
  elseif testSuffix EQS "LAYERWMS_GWC" then
    invoke layerList actualLayers.split(",")
    invoke numLayers layerList.size()
    if numLayers eq 4 then
      %test_plot_layerwms_gwc url="^actualUrl" layers="^actualLayers" transitions="500,1.5e3,1e8"
    elseif numLayers eq 3 then
      %test_plot_layerwms_gwc url="^actualUrl" layers="^actualLayers" transitions="500,1.5e3"
    elseif numLayers eq 2 then
      %test_plot_layerwms_gwc url="^actualUrl" layers="^actualLayers" transitions="L10"
    else
      popup func=ok message="To run the test LAYERWMS_GWC, between 2 and 4 layers must be selected (i.e. supports up to 3 transitions)." type=err
    endif
  elseif testSuffix ENDS "(LAYER-TRANSITIONS)" then
    set testCaseInstructions "This test case verifies the use of layer transitions when zooming in and out:\n\n  &
                              1. Click the FIRST layer button\n    &
                                a. This layer should be rendered in the\n       map\n    &
                                b. LayerWMS Config Menu -> Maps -> Click\n       on the name of the layer that's\n       checked\n    &
                                c. LayerWMS Config Menu -> Maps -> Name\n       -> This layer name should match the\n       name on the layer button clicked\n    &
                                d. LayerWMS Map Panel -> Bottom right ->\n       WMS_level -> should be less than 500\n  &
                              2. Repeat above for the other three layer\n     buttons, with some exceptions:\n    &
                                a. Second layer: 500 <= expected\n       WMS_level < 1500\n    &
                                b. Third layer: 1500 <= expected\n       WMS_level < 1e8\n    &
                                c. Fourth layer: expected WMS_level > 1e8\n  &
                              3. If all steps pass, then pass this test"
    invoke layerList actualLayers.split(",")
    invoke numLayers layerList.size()
    if numLayers ge 4 then
      %test_plot_layerwms_multi url="^actualUrl" layers="^actualLayers" crsOverride="^actualCRS" requestedVersion="^actualVersion" testCaseInstructions="^testCaseInstructions" transitions="500,1.5e3,1e8" /IGNOREHREF
    else
      popup func=ok message="To run the test LAYER-TRANSITIONS, at least 4 layers must be selected since this test runs 3 layer transitions." type=err
    endif
  elseif testSuffix ENDS "(WMS-VERSION-111)" then
    set testCaseInstructions "This test case verifies the functionality of WMS version 1.1.1:\n\n  &
                              1. LayerWMS Config Menu -> 2nd row from top\n     -> verify version mentioned here is 1.1.1\n  &
                              2. If the WMS layer renders successfully,\n     then pass this test\n\n&
                              NOTE: This test will not work if the selected url is not compatible with WMS version 1.1.1"
    %test_plot_layerwms_multi url="^actualUrl" layers="^actualLayers" crsOverride="^actualCRS" requestedVersion="1.1.1" testCaseInstructions="^testCaseInstructions" /IGNOREHREF
  elseif testSuffix ENDS "(BAD-BACKGROUND-ONLY-TILES)" then
    set testCaseInstructions "This test case verifies that background-only tiles are shown only where they need to be:\n\n&
                              1. Click on 'Zoom to Good Map Tiles'.\n&
                              => After 4 seconds, the number of retries should be 1.\n&
                              2. Click on 'Switch to Layer for Bad Background Color Test'.\n&
                              3. (Optional) Modify the BG-only coordinates if need be.\n&
                              4. Click on 'Zoom To Solid (Bad) Tiles'.\n&
                              => After 10 seconds, the number of retries should be 4.\n\n&
                              If all tests passed, please PASS this test."
    set L:numRetries 3
    %test_plot_layerwms_multi url="^actualUrl" layers="^actualLayers" badbackgroundLay="^badBackgroundLay" crsOverride="^actualCRS" requestedVersion="^actualVersion" testCaseInstructions="^testCaseInstructions" noCache=true numRetriesIfBGColor=^numRetries /IGNOREHREF /NOCONFIGMENU=^noconfigMenu
  elseif testSuffix ENDS "(MAP-SHIFT-ELONGATION-DOWNLOAD-THREADS)" then
    set testCaseInstructions "This test case verifies elongation and download threads:\n  &
                              1. The plot should show no elongation\n  &
                              2. Config Menu -> Download threads: -> ~32\n  &
                              3. Config Menu -> Configuration:\n     -> \"DOWNLOADTHREADS\" -> ~32\n  &
                              4. If all steps pass, then pass this test\n\n&
                              WARNING: The elongation test case is only valid with specific map proportions. Please do not modify the current map layout.\n\n&
                              NOTE: Depending on the server, the map may or may not be elongated or shifted down by 90 degrees."
    %test_plot_layerwms_multi url="^actualUrl" layers="^actualLayers" crsOverride="^actualCRS" requestedVersion="^actualVersion" testCaseInstructions="^testCaseInstructions" resizeWindow=true /IGNOREHREF
  elseif testSuffix ENDS "(BRIGHTNESS-PANNING)" then
    set testCaseInstructions "This test case verifies panning with image adjusting enabled:\n  &
                              1. LayerWMS Config Menu -> Brightness ->\n     should be set to 30\n  &
                              2. LayerWMS Config Menu -> Contrast -> should\n     be set to 50\n  &
                              3. Click on the zoom button below\n  &
                              4. Pan left and right with MMB -> Panning may\n     be slow, but should show minimal lag"
    %test_plot_layerwms_multi url="^actualUrl" layers="^actualLayers" crsOverride="^actualCRS" requestedVersion="^actualVersion" testCaseInstructions="^testCaseInstructions" adjustImageOnPanning=true /IGNOREHREF
  endif

return

**************************************************************************************
*  allow layers to be ordered for transitions
**************************************************************************************
procedure orderLayers
  invoke myLayers gc.layers.getValue()
  if mylayers NEQS "|" then
    invoke myChoices nxm.sys.test.TestUtil.getPermutations(myLayers)
    invoke myChoices myChoices.replaceAll(", ",",")
    popup/blocking="Mouse|Macro" answer choice "order preference?" Quest "^myChoices"
  else
    popup/blocking="Mouse|Macro" func=ok message="At least one layer must be selected" type=err
  endif

  if answer NEQS "CANCEL" then
    res pipeChar "|"
    invoke myLayers pipeChar.concat(answer)
    invoke myLayers myLayers.replaceAll("::","|")
    invoke myLayers myLayers.concat(pipeChar)
    res gc.layerOrder.value "^myLayers"
  endif

return

**************************************************************************************
*  include layerwms_config.tbl processing methods
*  < these set actual, default, and values type results >
**************************************************************************************
include test_plot_layerwms_configure.inc