!******************************************************************************!
! Tests the ability to display Midas BLUE files with corrupted
! keyword blocks in the extended header (within first 1024 offset and
! beyond 1024 offset).
!
! Author:  Neon Ngo
! BUG   :  2141 Corrupt keywords should not prevent reading header
! BUG   :  2222 SHELLGUI: add ability to CLEAR text in terminal (all tests)
! BUG   :  2548 Keyword / Status: Hangs with error in extended header (tests 3-6)
!******************************************************************************!
startmacro
  switch INTERACTIVE interAct GET DEF=TRUE  SDEF=TRUE
  switch BASELINE    baseline GET DEF=FALSE SDEF=TRUE

  set testfile1 "nxm.sys.test.bad_keywords.tmp"   ! invalid keyword block beyond 1024 offset of extended header
  set testfile2 "nxm.sys.test.bad_keywords2.tmp"  ! invalid keyword block within first 1024 offset of extended header
  set testfile3 "nxm.sys.test.bad_keywords3.tmp"  ! invalid keyword block (zero len) beyond 1024 offset of extended header
  set shortfn1  "bad_keywords.tmp"
  set shortfn2  "bad_keywords2.tmp"
  set shortfn3  "bad_keywords3.tmp"

  ! automatic test to status BLUE file with corrupted keyword blocks
  set tbl {}
  info "A warning for 'unable to read keywords ...' should be printed on the next line."
  status testfile1 headrep=tbl.headRep datarep=tbl.dataRep &
                   size=tbl.size type=tbl.ftype form=tbl.format &
                   xs=tbl.xstart xd=tbl.xdelta xu=tbl.xunits
  assert/text="^reg.main.name: STATUS BLUE file with corrupted keyword blocks in extended header beyond 1024 offset" tbl.getsize ge 8

  invoke GcontrolExists reg.shell.M.dictionary.isDictionaryEntry(GCONTROL)
  if interAct AND GcontrolExists then
    set instruct "Click on each test case below for more detailed instructions."

    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}
      gcontrol text idtext "INSTRUCTIONS" instruct 24 12 /title

      gcontrol menu testcases "test file with corrupted keywords" &
          "0.Initial Instructions,&
           1.STATUS     ^{shortfn1},&
           2.STATUS/KEY ^{shortfn1},&
           3.KEYWORD    ^{shortfn1},&
           4.STATUS/DATA ^{shortfn2},&
           5.KEYWORD    ^{shortfn2},&
           6.STATUS/K/D ^{shortfn3},&
           7.KEYWORD    ^{shortfn3},&
          " "0.Initial Instructions"
      shellgui/id=idshellgui/attach

    pipe off

  endif  ! end if interAct
endmacro

!******************************************************************************!
procedure processMessage m:msg
  if msg.name eqs "MAIN" then
    assert/text="^reg.main.name: Interactive tests for STATUS" msg.data eqss "PASS"
    pipe stop

  elseif msg.name eqs "TESTCASES" then
    if "STATUS" SUBS msg.data then
      ! clear text in terminal using "CLEAR" message via SENDX function (for BUG 2222 part 1)
      message func=sendx id=idshellgui name="CLEAR"
    else
      ! clear text in terminal using invoke clear() method (for BUG 2222 part 2)
      invoke ,, reg.idshellgui.clear()
    endif

    if msg.data eqss "0.Initial Instructions" then
      set gc.idtext.value instruct

    elseif msg.data eqss "1.STATUS" then
      set gc.idtext.value "The old contents should be cleared in the SHELL before this test (via CLEAR message). \n&
                           Should see header of BLUE file with corrupted keyword blocks. \n&
                           *One* warning should be printed."
      say "nM> STATUS ^testfile1"
      status testfile1

    elseif msg.data eqss "2.STATUS/KEY" then
      set gc.idtext.value "The old contents should be cleared in the SHELL before this test (via CLEAR message). \n&
                           Should see header of BLUE file with corrupted keyword blocks. \n&
                           *One* warning should be printed and *one* ERROR for invalid keyword block."
      say "nM> STATUS/KEY ^testfile1"
      status/key testfile1

    elseif msg.data eqss "3.KEYWORD" then
      set gc.idtext.value "The old contents should be cleared in the SHELL before this test (via clear() method). \n&
                           Should see several keywords from BLUE file with corrupted keyword blocks. \n&
                           *Several* warnings should be printed for invalid keyword block."
      say "nM> KEYWORD ^testfile1 func=LIST"
      keyword testfile1 func=LIST

    elseif msg.data eqss "4.STATUS" then
      set gc.idtext.value "The old contents should be cleared in the SHELL before this test (via CLEAR message). \n&
                           Should see header of BLUE file with corrupted keyword blocks within 1024 offset. \n&
                           *Two* warnings should be printed."
      say "nM> STATUS/DATA ^testfile2"
      status/data testfile2

    elseif msg.data eqss "5.KEYWORD" then
      set gc.idtext.value "The old contents should be cleared in the SHELL before this test (via clear() method). \n&
                           *Two* warnings should be printed and no keywords are listed from BLUE file &
                           with corrupted keyword blocks within 1024 offset."
      say "nM> KEYWORD ^testfile2 func=LIST"
      keyword testfile2 func=LIST

    elseif msg.data eqss "6.STATUS" then
      set gc.idtext.value "The old contents should be cleared in the SHELL before this test (via CLEAR message). \n&
                           Should see header of BLUE file with corrupted keyword blocks beyond 1024 offset. \n&
                           *One* warning should be printed and *one* ERROR for invalid keyword block."
      say "nM> STATUS/K/D ^testfile3 func=LIST"
      status/K/D testfile3

    elseif msg.data eqss "7.KEYWORD" then
      set gc.idtext.value "The old contents should be cleared in the SHELL before this test (via clear() method). \n&
                           Should see several keywords from BLUE file with corrupted keyword blocks beyond 1024 offset. \n&
                           *Two* warnings should be printed for invalid keyword block."
      say "nM> KEYWORD ^testfile3 func=LIST"
      keyword testfile3 func=LIST

    else
      say "^reg.main.name: unknown test case: ^msg.data"
    endif

  else
    say "^reg.main.name: Unhandled message name: ^msg.name from fid: ^msg.fid data: ^msg.data"
  endif
return
