! Tests sending/receiving "translated" NeXtMidas messages from X-Midas.
! NOTE: This test requires a graphics display to run.
!
!  @author  J. Czechowski IV / Neon Ngo
!  @version $Id: test_xmmessage_translated.txt,v 1.5 2011/01/06 23:00:38 ntn Exp $
!  @since   NeXtMidas 2.1.1
!
startmacro/nmq=8k u:resTests["numTested"] u:resFails["numFailed"]
  local test_count, fail_count
  local dataPassFail,len,open,close,data
  local foundLON,foundLAT,foundALT,N
  local /M/
  local instructions
  local xmversion,supportsXMValue
  local item
  local mytimer
  local baseln       ! Baseline testing?
  local interAct     ! Interactive? testing

  switch BASELINE    baseln GET               ! Baseline testing?
  switch INTERACTIVE interAct GET             ! Interactive testing?
  if baseln   eq -1 then res baseln    1      ! switch not specified, default to enable Baseline testing
  if interAct eq -1 then res interAct ^baseln ! switch not specified, default to ^baseline

  res test_count 0   ! number of test cases executed
  res fail_count 0   ! number of test cases that failed
  res mytimer    1.0 ! pause before running non-interactive portion of testing

if interAct le 0 then
  info "Run %test_xmmessage_translated/baseline or with /interactive to enable additional tests that requires X11 display"
else

  ! Get the current X-Midas version
  res xmversion       0          ! 0 for unknown X-Midas version
  res supportsXMValue "FALSE"    ! assume no XMValue support initially
  trap error skip_vers
  import XM_VERSION xmversion
  label skip_vers
  trap error off
  if xmversion ge 450 then
    res supportsXMValue "TRUE"
  endif

  ! Start the tests
  message down   ! make sure current messages queue is down before calling message up
  message up
  res instructions "Wait for the automatic zoom and unzoom tests to complete (5 secs)."

  nm/on/bg/id=999
  xpipe/setup/controls on
    !xmessage/xs=3/full
    nm/xs=2 plot/id=2 nxm.sys.dat.world.prm /wpos=(275,25,600,400) /msgid=xm.1
  xpipe off

  nm/off
  message down

endif  ! interAct

  res ^resTests test_count
  res ^resFails fail_count

  ! cleanup results
  remove/quiet x1, x2, y1, y2
endmacro

!******************************************************************************!
! Start Controls Section
!******************************************************************************!
startcontrols
  label initialentry
    xcontrol/noedit aprompt instructions instructions ,, 120
    xcontrol/lab=domenu menu item ,, "MACRO;Pass,Fail,ZoomAfrica,UnZoom"
    xcontrol/lab=getmsg msmonitor 1 10
    xcontrol/lab=timermsg/countdown tmonitor mytimer ^mytimer
  return

  label domenu
    if item eq 1 then
      call assert_str "Interactive tests" test_count fail_count "PASS" "PASS"
      stop
    elseif item eq 2 then
      call assert_str "Interactive tests" test_count fail_count "PASS" "FAIL"
      stop
    elseif item eq 3 then
      message send /NM/ 2 ,, "ZOOM" 0 {XMIN=-20,XMAX=55,YMIN=-40,YMAX=40}
      res instructions "The plot should zoom in on Africa. Press UnZoom."
    elseif item eq 4 then
      message send /NM/ 2 ,, "UNZOOM"
      res instructions "The plot should zoom out. Click on Africa in the plot (do not zoom in/out)."
    else
      warn "Unknown option ^item"
    endif
  return

  label getmsg
    message get /M/
    if /debug gt 0 then say "DEBUG: Got a ^M.name message from ID=^M.id INFO=^M.info"
    if M.name eqs "NM" then
      if /debug gt 0 then
        say "Got NeXtMidas Message:"
        say "  NAME : ^{M.nm_msg_name}"
        say "  INFO : ^{M.nm_msg_info}"
        say "  DATA : ^{M.nm_msg_data}"
        say "  FID  : ^{M.nm_msg_fid}"
        say "  TID  : ^{M.nm_msg_tid}"
      endif

      ! Make sure the data looks like a table
      res dataPassFail "FAIL"
      sedit M.nm_msg_data len LENGTH
      if len gt 2 then
        sedit M.nm_msg_data open  RANGE  1   1
        sedit M.nm_msg_data close RANGE len len

        if open eqs "{" and close eqs "}" then
          res dataPassFail "PASS"
        endif
      endif

      call assert_str "Got NeXtMidas message NAME=MARK"      test_count fail_count "MARK" M.nm_msg_name
      call assert_num "Got NeXtMidas message INFO=1 (Click)" test_count fail_count 1      M.nm_msg_info
      call assert_str "Got NeXtMidas message DATA={...}"     test_count fail_count "PASS" dataPassFail
      call assert_str "Got NeXtMidas message FID=2"          test_count fail_count "2"    M.nm_msg_fid
      call assert_str "Got NeXtMidas message TID=XM.1"       test_count fail_count "XM.1" M.nm_msg_tid

      ! Check X-Midas 4.6 syntax with XMValue
      if supportsXMValue eqs "TRUE" then
        res v:data ^{M.nm_msg_data}
        if /debug gt 0 then res data
        call assert_rexists "Checking DATA, has LON (XMValue)" test_count fail_count data.lon
        call assert_rexists "Checking DATA, has LAT (XMValue)" test_count fail_count data.lat
        call assert_rexists "Checking DATA, has ALT (XMValue)" test_count fail_count data.alt
      endif

      ! Check X-Midas 4.4 syntax with RMSG (should also work in X-Midas 4.6)
      res foundLON "FALSE"
      res foundLAT "FALSE"
      res foundALT "FALSE"
      rmsg T2R ,,,, M.nm_msg_data "TEST_XMMESSAGE_TRANSLATED_"
      if /debug gt 0 then res TEST_XMMESSAGE_TRANSLATED_*
      loop TEST_XMMESSAGE_TRANSLATED_NK N
        if TEST_XMMESSAGE_TRANSLATED_K^N eqs "LON" then res foundLON "TRUE"
        if TEST_XMMESSAGE_TRANSLATED_K^N eqs "LAT" then res foundLAT "TRUE"
        if TEST_XMMESSAGE_TRANSLATED_K^N eqs "ALT" then res foundALT "TRUE"
      endloop
      call assert_str "Checking DATA, has LON (RMSG)" test_count fail_count foundLON "TRUE"
      call assert_str "Checking DATA, has LAT (RMSG)" test_count fail_count foundLAT "TRUE"
      call assert_str "Checking DATA, has ALT (RMSG)" test_count fail_count foundALT "TRUE"

      rem/quiet TEST_XMMESSAGE_TRANSLATED_*

      res instructions "Done with interactive tests. Press Pass or Fail as appropriate."
    endif
  return

  label timermsg
    ! info "timer msg mytimer = ^mytimer"
    ! for non interactive part of testing sending messages to NeXtMidas command (plot)
    message send /NM/ 2 ,, "ZOOM" 0 {XMIN=-45,XMAX=80,YMIN=-40,YMAX=40}
    pause 1.0  ! give time for message to be process by PLOT
    jump VerifyPlotBounds "after sent zoom message" -45 80 -40 40

    message send /NM/ 2 ,, "UNZOOM"
    pause 1.0  ! give time for message to be process by PLOT
    jump VerifyPlotBounds "after sent unzoom message" -179 179 -113 111

    if interAct le 0 then stop ! end test case if no doing interactive testing

    res instructions "Done with automatic tests. Click in the middle of the plot (DO NOT zoom in/out)."
  return
endcontrols

!******************************************************************************!
! Verify bounds of NeXtMidas PLOT (id=2)
! @since NeXtMidas 2.7.1
procedure VerifyPlotBounds a:msg L:exp_x1 L:exp_x2 L:exp_y1 L:exp_y2
  local   tol
  results tol 5  ! absolute tolerance (This used to be 1 but with the graphical resizing to fit the screen, there needs to be a bigger tolerance)
  nm results L:xm.x1 reg.2.x1
  nm results L:xm.x2 reg.2.x2
  nm results L:xm.y1 reg.2.y1
  nm results L:xm.y2 reg.2.y2

  call assert_num "Checking PLOT x1 - ^msg" test_count fail_count exp_x1 x1 tol
  call assert_num "Checking PLOT x2 - ^msg" test_count fail_count exp_x2 x2 tol
  call assert_num "Checking PLOT y1 - ^msg" test_count fail_count exp_y1 y1 tol
  call assert_num "Checking PLOT y2 - ^msg" test_count fail_count exp_y2 y2 tol
return

include "%xm_assert" ! Includes the assertion subroutines
