!******************************************************************************!
! Tests the MESSAGE command
!
! Author : Jay Hallam / Neon Ngo
!******************************************************************************!
startmacro

  invoke skipBecauseHeadless nxm.sys.lib.Shell.isHeadless()

  if skipBecauseHeadless then
    info "^reg.main.name: skipping tests because HEADLESS"
  else 
    pipe on
      ! This part of the pipe section is required as part of %test_message_child_macro,
      ! as is the "MSG_TEST" block in processMessage note that the asserts might not
      ! print out in the expected order (due to threading and the use of the message
      ! queues), however any that say "child macro" are from %test_message_child_macro.
      nxm.sys.test.message_echo/ID=ECHO COUNT=1 INFO=3 WAIT=5.0
      %test_message_child_macro/BG/ID=CHILD/MSGID=MAIN
  
      ! All other messages to send
      mess send MAIN ,, TEST   ,,{B=1,I=2,L=3,D=4.9,S="Old string"}
      mess send MAIN ,, TESTR1 ,,{ONE=1,TWO=2}
      mess send MAIN ,, TESTR2 ,,{ONE=1,TWO=2,TEN=10}
    pipe off
  
    call test_send_to_non_command
  endif
endmacro

!******************************************************************************!
procedure processMessage m:msg
  !res msg /all

  if msg.name EQS "MSG_TEST_1" then
    ! This is required by %test_message_child_macro
    message send "CHILD" NAME=msg.name INFO=1 DATA=msg.data

  elseif msg.name EQS "MSG_TEST_5" then
    ! This is required by %test_message_child_macro
    message send "CHILD" NAME=msg.name INFO=5 DATA=msg.data

  elseif msg.name EQS "TEST" then
    mess send MAIN ,, TEST2, ,{S="New string"}
    pause 1
    assert/text="Got sent ^{msg.name} msg, message timing test" msg.data.s EQS "Old string"

  elseif msg.name EQS "TEST2" then
    assert/text="Got sent ^{msg.name} msg, checking data is valid" msg.data.s EQS "New string"

    call test_send_replace

  elseif msg.name EQS "TESTR1" then
    assert/text="Got sent ^{msg.name} msg, checking nrexists in msg.data" msg.data.ten nrexists

  elseif msg.name EQS "TESTR2" then
    assert/text="Got sent ^{msg.name} msg, checking rexists in msg.data" msg.data.ten rexists

  elseif msg.name EQS "TESTREPLACEMSG" then
    assert/text="Got replace ^{msg.name} msg, checking data is valid" msg.data eqss "(3) This should replace"

  elseif msg.name EQS "TESTSENDREPLACEMSG" then
    assert/text="Got replace ^{msg.name} msg, checking data is valid" msg.data eqss "(5) This should replace"

    call test_replace

  else
    warning "Unrecognized message from ^msg.fid - ^msg"
  endif

return

!******************************************************************************!
! since NeXtMidas 3.3.0 for BUG 98
procedure test_replace
  message REPLACE MAIN replacedMsg TESTREPLACEMSG ,, "(0) TestReplaceNonMatchingMsg"
  assert/text="REPLACE non-matching message returns NULL" replacedMsg isNULL
  remove replacedMsg ! remove for next test case

  message SEND MAIN ,, TESTREPLACEMSG ,, "(1) TestReplaceNonMatchingMsg With /SEND modifier"
  message REPLACE MAIN replacedMsg TESTREPLACEMSG ,, "(2) This should replace message (1)"
  assert/text="REPLACE matching message returns replaced message" replacedMsg.data eqss "(1) TestReplace"
  remove replacedMsg ! remove for next test case

  message REPLACE MAIN ,, TESTREPLACEMSG ,, "(3) This should replace message (2)"
return

!******************************************************************************!
! since NeXtMidas 3.3.0 for BUG 98
procedure test_send_replace
  message SENDR MAIN replacedMsg TESTSENDREPLACEMSG ,, "(4) Test Send/Replace NonMatchingMsg"
  assert/text="SENDR (replace) non-matching message does not set REPLACEDMSG results" replacedMsg nrexists
  remove replacedMsg ! remove for next test case

  message SENDR MAIN replacedMsg TESTSENDREPLACEMSG ,, "(5) This should replace message (4)"
  assert/text="SENDR (replace) matching message returns replaced message" replacedMsg.data eqss "(4) Test Send/Replace"
  remove replacedMsg ! remove for next test case
return

!******************************************************************************!
! test case for BUG 2179
procedure test_send_to_non_command
  new nxm.sys.lib.MessageQueue(this.M,this) myMQ
  set REG.MYMQ myMQ
  info "!!!!! It is ok to see a warning 'Forcing use of MESSAGE SEND instead of SENDW ...' !!!!!"
  message SENDW MYMQ ,, "TEST_BUG_2179" 0 "This should not cause a ClassCastException"
return
