###############################################################################
#
# host.eagle --
#
# Extensible Adaptable Generalized Logic Engine (Eagle)
#
# Copyright (c) 2007-2012 by Joe Mistachkin.  All rights reserved.
#
# See the file "license.terms" for information on usage and redistribution of
# this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# RCS: @(#) $Id: $
#
###############################################################################

source [file join [file normalize [file dirname [info script]]] prologue.eagle]

###############################################################################
# MONO: Make this test work on Mono.  Currently, [host close] relies on
#       framework internals that may not be present in Mono.  Also, various
#       custom hosts may not support the Open and Close methods; therefore,
#       only perform this test if the interpreter host is the default console
#       host.
###############################################################################

runTest {test host-1.1 {close and re-open console host} -setup {
  proc hostReset { x } {
    #
    # HACK: For the .NET Framework 4.5.x on Windows 8.x (only?), we need to
    #       reset the host interface and the standard channels in order to
    #       restore normal functioning for the default console host.  This
    #       only seems to be strictly required when running in the debugger;
    #       however, it should be harmless in any case.
    #
    if {([haveConstraint osVersion.WindowsNT_6.2] || \
         [haveConstraint osVersion.WindowsNT_6.3]) && \
        [haveConstraint dotNet45OrHigher]} then {
      if {[catch {host reset -interface -channels} error]} then {
        tlog [appendArgs \
            "---- could not reset host interface and channels (" $x \
            "): " $error \n]
      }
    }
  }
} -body {
  set i1 [host isopen]
  host close
  set i2 [host isopen]

  after 1000 {
    #
    # NOTE: Attempt to reset the host interface and standard channels now.
    #       This is necessary on some operating system versions in order
    #       for us to be able to fully succeed at re-opening the console
    #       window; specifically, the _Hosts.Console.Open() method cannot
    #       fully succeed unless at least the standard input channel for
    #       the System.Console class is open and valid, due to the check
    #       in the _Hosts.Console.SetupTitle() method.
    #
    hostReset before

    if {[catch {host open} error]} then {
      tlog [appendArgs \
          "---- could not re-open host: " $error \n]
    }

    set i3 [host isopen]

    #
    # NOTE: Attempt to reset the host interface and standard channels now.
    #       This is necessary on some operating system versions in order
    #       to allow us to read/write from/to the standard channels using
    #       the System.Console class after re-opening the console window.
    #
    hostReset after
  }
  after 2000 [list set forever true]

  list [catch {vwait forever} msg] $msg $i1 $i2 $i3
} -cleanup {
  rename hostReset ""

  unset -nocomplain error i1 i2 i3 msg forever
} -constraints {eagle shell native windows dotNet interactive hostIsOpen\
hostType.Eagle__Hosts_Console compile.NATIVE compile.WINDOWS} -result {0 {}\
True False True}}

###############################################################################

#
# HACK: This test is disabled on non-Windows platforms because we have no idea
#       if the Console.Title property will actually work there (e.g. Mono has
#       several console "drivers" on Unix that may or may not support the Title
#       property).  Also, this test relies on the interpreter host being the
#       standard console host; therefore, enforce this assumption in the test
#       constraints.
#
runTest {test host-1.2 {get/set host title} -body {
  set savedTitle [host title]
  host title "Tu ne cede malis sed contra audentior ito"; \
      after 2000; list [object invoke -type \
      Eagle._Interfaces.Public.IInteractiveHost \
      Interpreter.GetActive.Host Title] \
      [object invoke Console Title]
} -cleanup {
  catch {host title $savedTitle}
  unset -nocomplain savedTitle
} -constraints {eagle shell windows interactive hostIsOpen\
hostType.Eagle__Hosts_Console compile.CONSOLE} -match regexp -result {^\{Tu ne\
cede malis sed contra audentior ito\} \{(?:Administrator: )?Eagle v1\.0\
(?:alpha|beta|rc|final) \d{4}\.\d{2}\.\d{2} \((?:Debug|Release)\) Tu ne cede\
malis sed contra audentior ito(?: - Mistachkin Systems)?\}$}}

###############################################################################

source [file join [file normalize [file dirname [info script]]] epilogue.eagle]
