# TimerHandler.test --
#
#	Tests the implementation of the TimerHandler.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: TimerHandler.test,v 1.1 1998/11/05 19:48:44 hylands Exp $

# Note: This test file must be sourced from the parent directory
#       or else the defs file may not be properly loaded.

if {[string compare test [info procs test]] == 1} then {source defs}

setupJavaPackage

proc timer_cleanup {} {
    foreach id [after info] {
	after cancel $id
    }
}

test TimerHandler-1.1 {TimerHandler(), sorting} {
    timer_cleanup
    set x {}
    after 100 lappend x a
    after 200 lappend x b
    after 300 lappend x c
    after 400
    vwait x
    set x
} {a b c}

test TimerHandler-1.2 {TimerHandler(), sorting} {
    timer_cleanup
    set x {}
    after 200 lappend x b
    after 300 lappend x c
    after 100 lappend x a
    after 400
    vwait x
    set x
} {a b c}

test TimerHandler-1.3 {TimerHandler(), sorting} {
    timer_cleanup
    set x {}
    after 100 lappend x a
    after 300 lappend x c
    after 200 lappend x b
    after 400
    vwait x
    set x
} {a b c}

test TimerHandler-1.4 {cancel()} {
    timer_cleanup
    set x {}
    after 10 lappend x a
    after 30 lappend x c
    after 20 lappend x b
    after cancel lappend x c
    after 40
    vwait x
    set x
} {a b}

test TimerHandler-1.5 {cancel() and invoke()} {
    timer_cleanup

    set x not_called
    set notifier [java::call tcl.lang.Notifier getNotifierForThread \
	[java::call Thread currentThread]]

    set handle [after 10 set x called]
    set timerList [java::field $notifier timerList]

    set timer [$timerList elementAt 0]
    set timer [java::cast [[$timer getClass] getName] $timer]
    after cancel $handle
    set msg [$timer invoke]

    after 40
    update

    list $msg $x
} {0 not_called}

test TimerHandler-1.6 {cancel() and invoke()} {
    timer_cleanup

    set x not_called
    set notifier [java::call tcl.lang.Notifier getNotifierForThread \
	[java::call Thread currentThread]]

    set handle [after 10 set x called]
    set timerList [java::field $notifier timerList]

    set timer [$timerList elementAt 0]
    set timer [java::cast [[$timer getClass] getName] $timer]
    after cancel $handle
    $timer cancel
    $timer cancel
    $timer cancel
    set msg [$timer invoke]

    after 40
    update

    list $msg $x
} {0 not_called}

timer_cleanup

