# TclEvent.test --
#
#	Tests the implementation of the TclEvent.java file.
#
# Copyright (c) 1997 by Sun Microsystems, Inc.
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# RCS: @(#) $Id: TclEvent.test,v 1.3 2000/12/22 09:47:15 mdejong Exp $

if {[lsearch [namespace children] ::tcltest] == -1} {
    package require tcltest
    namespace import -force ::tcltest::*
}

setupJavaPackage

set notifier [java::call tcl.lang.Notifier getNotifierForThread \
	[java::call Thread currentThread]]
set TAIL [java::field tcl.lang.TCL QUEUE_TAIL]

test TclEvent-1.1 {sync()} {
    set evt [java::new tcl.lang.TestEvent [java::getinterp] {
	set testEvtReply 1
    }]
    list [catch {$evt sync} msg] $msg
} {1 {tcl.lang.TclRuntimeError: TclEvent is not queued when sync() is called}}

test TclEvent-1.2 {sync(), in primary thread} {
    set evt [java::new tcl.lang.TestEvent [java::getinterp] {
	set testEvtReply 1
    }]
    $notifier queueEvent $evt $TAIL
    set testEvtReply ""
    list [catch {$evt sync} msg] $msg $testEvtReply
} {0 {} 1}

test TclEvent-1.3 {sync(), in non-primary thread} {
    set testEvtReply 0
    set thread [java::new tcl.lang.TestEventThread1 [java::getinterp] {
	set testEvtReply 1
    }]
    update
    $thread start
    vwait testEvtReply
    set testEvtReply
} 1

test TclEvent-1.4 { enqueue of a vwait event should still allow
        other events to be enqueued, was a bug in Tcl Blend Notifier } {
    set AQT [java::new tests.AppendEventQueueThread [java::getinterp]]
    $AQT start

    java::call tests.AppendEventQueueThread queueVwaitEvent [java::getinterp]
    update

    set numQueued [java::field $AQT numQueued]
    set expectedNumQueued [java::field $AQT expectedNumQueued]
    set numProcessed [java::field $AQT numProcessed]

    list $numQueued $expectedNumQueued $numProcessed
} {5 5 5}

# cleanup
::tcltest::cleanupTests
return
