###############################################################################
#
# winforms8.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: On Mono 3.0.2 beta, these tests do not function properly due to
#       a total failure to load the System.Windows.Forms assembly, resulting
#       from incorrect DLL mappings in the "etc/mono/config" configuration
#       file copied by the installer.  Attempts to load and/or use Windows
#       Forms functionality will result in a very complex looking exception,
#       with the innermost exception being very similar to the following:
#
#       System.DllNotFoundException: /tmp/install/lib/libgdiplus.so
#
#       The workaround to "fix" this issue on Windows is to simply remove
#       the both "gdiplus" related lines from the configuration file.
#
#       See: https://bugzilla.xamarin.com/show_bug.cgi?id=8309
###############################################################################
# MONO: Due to a Mono bug, some tests are disabled on Mono.
#       See: https://bugzilla.novell.com/show_bug.cgi?id=471257
###############################################################################
# MONO: *BUG* As of Mono 2.8, the "winForms-8.1" test fails on Mono due to an
#       apparent lack of support for resolving assemblies based on a partial
#       name.  This test was passing as recently as Mono 2.6.7.
#       See: https://bugzilla.novell.com/show_bug.cgi?id=645189
#       See: https://bugzilla.novell.com/show_bug.cgi?id=665981
###############################################################################

runTest {test winForms-8.1 {form with blocking event} -setup {
  proc handleClickEvent { sender e } {
    #
    # NOTE: Make sure the console user knows that we are waiting for input.
    #
    tputs $::test_channel [appendArgs \
        "please find the window marked \"" [info engine] \
        "\" and click \"OK\" to continue.\n"]

    #
    # NOTE: Prompt for some text and add it to the tracking
    #       variable.
    #
    lappend ::x [object invoke Interaction InputBox \
        "Please enter some text:" [info engine]]

    #
    # NOTE: Terminate our wait.
    #
    set ::forever 1
  }

  proc centerButton {} {
    $::button AutoSize true
    $::button Left [expr {([$::form ClientSize.Width] - [$::button Width]) / 2}]
    $::button Top [expr {([$::form ClientSize.Height] - [$::button Height]) / 2}]
  }

  set savedSleeptime [interp sleeptime {}]; # save
} -body {
  set assembly1 [object load -import System.Windows.Forms]
  set assembly2 [object load -import Microsoft.VisualBasic]
  set form [object create -alias Form]

  $form Text "[info engine] Test Form Title"
  $form TopMost true
  $form Show

  set button [object create -alias System.Windows.Forms.Button]

  $button Text "Click Here"
  $button add_Click handleClickEvent

  object invoke $form.Controls Add $button

  interp sleeptime {} 200
  after 100 [list centerButton]

  set x [list]
  set mouseEventArgs [object create -alias MouseEventArgs Left 1 12 34 0]
  after 1000 [list $button -flags +NonPublic OnClick $mouseEventArgs]

  vwait forever

  $button remove_Click handleClickEvent

  after 1000 [list $button -flags +NonPublic OnClick $mouseEventArgs]
  after 2000 [list set forever 1]

  vwait forever

  llength $x
} -cleanup {
  cleanupVisualBasic
  cleanupWinForms

  interp sleeptime {} $savedSleeptime; # restore

  unset -nocomplain x forever mouseEventArgs button form assembly2 \
      assembly1 savedSleeptime

  catch {object removecallback handleClickEvent}

  rename centerButton ""
  rename handleClickEvent ""
} -constraints {eagle compile.WINFORMS winForms interactive userInteraction\
monoBug28 monoCrash210} -result 1}

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

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