summaryrefslogtreecommitdiffstats
path: root/tests/select.test
blob: 657786b7976e54de395ff3b728debba5ab5dd7e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
# This file is a Tcl script to test out Tk's selection management code,
# especially the "selection" command.  It is organized in the standard
# fashion for Tcl tests.
#
# Copyright (c) 1994 Sun Microsystems, Inc.
# Copyright (c) 1998-1999 by Scriptics Corporation.
# All rights reserved.
#
# RCS: @(#) $Id: select.test,v 1.8 2002/07/13 00:30:25 dgp Exp $

#
# Note: Multiple display selection handling will only be tested if the
# environment variable TK_ALT_DISPLAY is set to an alternate display.
#

package require tcltest 2.1
namespace import -force tcltest::test
namespace import -force tcltest::testsDirectory
namespace import -force tcltest::configure
namespace import -force tcltest::interpreter
configure -testdir [file join [pwd] [file dirname [info script]]]
configure -loadfile [file join [testsDirectory] constraints.tcl]
tcltest::loadTestedCommands
eval configure $argv

eval destroy [winfo child .]

global longValue selValue selInfo

set selValue {}
set selInfo {}

proc handler {type offset count} {
    global selValue selInfo
    lappend selInfo $type $offset $count
    set numBytes [expr {[string length $selValue] - $offset}]
    if {$numBytes <= 0} {
	return ""
    }
    string range $selValue $offset [expr $numBytes+$offset]
}

proc errIncrHandler {type offset count} {
    global selValue selInfo pass
    if {$offset == 4000} {
	if {$pass == 0} {
	    # Just sizing the selection;  don't do anything here.
	    set pass 1
	} else {
	    # Fetching the selection;  wait long enough to cause a timeout.
	    after 6000
	}
    }
    lappend selInfo $type $offset $count
    set numBytes [expr {[string length $selValue] - $offset}]
    if {$numBytes <= 0} {
	return ""
    }
    string range $selValue $offset [expr $numBytes+$offset]
}

proc errHandler args {
    error "selection handler aborted"
}

proc badHandler {path type offset count} {
    global selValue selInfo
    selection handle -type $type $path {}
    lappend selInfo $path $type $offset $count
    set numBytes [expr {[string length $selValue] - $offset}]
    if {$numBytes <= 0} {
	return ""
    }
    string range $selValue $offset [expr $numBytes+$offset]
}
proc reallyBadHandler {path type offset count} {
    global selValue selInfo pass
    if {$offset == 4000} {
	if {$pass == 0} {
	    set pass 1
	} else {
	    selection handle -type $type $path {}
	}
    }
    lappend selInfo $path $type $offset $count
    set numBytes [expr {[string length $selValue] - $offset}]
    if {$numBytes <= 0} {
	return ""
    }
    string range $selValue $offset [expr $numBytes+$offset]
}

# Eliminate any existing selection on the screen.  This is needed in case
# there is a selection in some other application, in order to prevent races
# from causing false errors in the tests below.

selection clear .
after 1500

# common setup code
proc setup {{path .f1} {display {}}} {
    catch {destroy $path}
    if {$display == {}} {
	frame $path
    } else {
	toplevel $path -screen $display
	wm geom $path +0+0
    }
    selection own $path
}

# set up a very large buffer to test INCR retrievals
set longValue ""
foreach i {a b c d e f g j h i j k l m o p q r s t u v w x y z} {
    set j $i.1$i.2$i.3$i.4$i.5$i.6$i.7$i.8$i.9$i.10$i.11$i.12$i.13$i.14
    append longValue A$j B$j C$j D$j E$j F$j G$j H$j I$j K$j L$j M$j N$j
}

# Now we start the main body of the test code

test select-1.1 {Tk_CreateSelHandler procedure} {
    setup
    lsort [selection get TARGETS]
} {MULTIPLE TARGETS TIMESTAMP TK_APPLICATION TK_WINDOW}
test select-1.2 {Tk_CreateSelHandler procedure} {
    setup
    selection handle .f1 {handler TEST} TEST
    lsort [selection get TARGETS]
} {MULTIPLE TARGETS TEST TIMESTAMP TK_APPLICATION TK_WINDOW}
test select-1.3 {Tk_CreateSelHandler procedure} {
    global selValue selInfo
    setup
    selection handle .f1 {handler TEST} TEST
    set selValue "Test value"
    set selInfo ""
    list [selection get TEST] $selInfo
} {{Test value} {TEST 0 4000}}
test select-1.4.1 {Tk_CreateSelHandler procedure} {unixOnly} {
    setup
    selection handle .f1 {handler TEST} TEST
    selection handle .f1 {handler STRING}
    lsort [selection get TARGETS]
} {MULTIPLE STRING TARGETS TEST TIMESTAMP TK_APPLICATION TK_WINDOW UTF8_STRING}
test select-1.4.2 {Tk_CreateSelHandler procedure} {macOrPc} {
    setup
    selection handle .f1 {handler TEST} TEST
    selection handle .f1 {handler STRING}
    lsort [selection get TARGETS]
} {MULTIPLE STRING TARGETS TEST TIMESTAMP TK_APPLICATION TK_WINDOW}
test select-1.5 {Tk_CreateSelHandler procedure} {
    global selValue selInfo
    setup
    selection handle .f1 {handler TEST} TEST
    selection handle .f1 {handler STRING}
    set selValue ""
    set selInfo ""
    list [selection get] $selInfo
} {{} {STRING 0 4000}}
test select-1.6.1 {Tk_CreateSelHandler procedure} {unixOnly} {
    global selValue selInfo
    setup
    selection handle .f1 {handler TEST} TEST
    selection handle .f1 {handler STRING}
    set selValue ""
    set selInfo ""
    selection get
    selection get -type TEST
    selection handle .f1 {handler TEST2} TEST
    selection get -type TEST
    list [set selInfo] [lsort [selection get TARGETS]]
} {{STRING 0 4000 TEST 0 4000 TEST2 0 4000} {MULTIPLE STRING TARGETS TEST TIMESTAMP TK_APPLICATION TK_WINDOW UTF8_STRING}}
test select-1.6.2 {Tk_CreateSelHandler procedure} {macOrPc} {
    global selValue selInfo
    setup
    selection handle .f1 {handler TEST} TEST
    selection handle .f1 {handler STRING}
    set selValue ""
    set selInfo ""
    selection get
    selection get -type TEST
    selection handle .f1 {handler TEST2} TEST
    selection get -type TEST
    list [set selInfo] [lsort [selection get TARGETS]]
} {{STRING 0 4000 TEST 0 4000 TEST2 0 4000} {MULTIPLE STRING TARGETS TEST TIMESTAMP TK_APPLICATION TK_WINDOW}}
test select-1.7.1 {Tk_CreateSelHandler procedure} {unixOnly} {
    setup
    selection own -selection CLIPBOARD .f1
    selection handle -selection CLIPBOARD .f1 {handler TEST} TEST
    selection handle -selection PRIMARY .f1 {handler TEST2} STRING
    list [lsort [selection get -selection PRIMARY TARGETS]] \
	[lsort [selection get -selection CLIPBOARD TARGETS]] 
} {{MULTIPLE STRING TARGETS TIMESTAMP TK_APPLICATION TK_WINDOW UTF8_STRING} {MULTIPLE TARGETS TEST TIMESTAMP TK_APPLICATION TK_WINDOW}}
test select-1.7.2 {Tk_CreateSelHandler procedure} {macOrPc} {
    setup
    selection own -selection CLIPBOARD .f1
    selection handle -selection CLIPBOARD .f1 {handler TEST} TEST
    selection handle -selection PRIMARY .f1 {handler TEST2} STRING
    list [lsort [selection get -selection PRIMARY TARGETS]] \
	[lsort [selection get -selection CLIPBOARD TARGETS]] 
} {{MULTIPLE STRING TARGETS TIMESTAMP TK_APPLICATION TK_WINDOW} {MULTIPLE TARGETS TEST TIMESTAMP TK_APPLICATION TK_WINDOW}}
test select-1.8 {Tk_CreateSelHandler procedure} {
    setup
    selection handle -format INTEGER -type TEST .f1 {handler TEST}
    lsort [selection get TARGETS]
} {MULTIPLE TARGETS TEST TIMESTAMP TK_APPLICATION TK_WINDOW}

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

test select-2.1 {Tk_DeleteSelHandler procedure} {unixOnly} {
    setup
    selection handle .f1 {handler STRING} 
    selection handle -type TEST .f1 {handler TEST} 
    selection handle -type USER .f1 {handler USER} 
    set result [list [lsort [selection get TARGETS]]]
    selection handle -type TEST .f1 {}
    lappend result [lsort [selection get TARGETS]]
} {{MULTIPLE STRING TARGETS TEST TIMESTAMP TK_APPLICATION TK_WINDOW USER UTF8_STRING} {MULTIPLE STRING TARGETS TIMESTAMP TK_APPLICATION TK_WINDOW USER UTF8_STRING}}
test select-2.2 {Tk_DeleteSelHandler procedure} {unixOnly} {
    setup
    selection handle .f1 {handler STRING} 
    selection handle -type TEST .f1 {handler TEST} 
    selection handle -type USER .f1 {handler USER} 
    set result [list [lsort [selection get TARGETS]]]
    selection handle -type USER .f1 {}
    lappend result [lsort [selection get TARGETS]]
} {{MULTIPLE STRING TARGETS TEST TIMESTAMP TK_APPLICATION TK_WINDOW USER UTF8_STRING} {MULTIPLE STRING TARGETS TEST TIMESTAMP TK_APPLICATION TK_WINDOW UTF8_STRING}}
test select-2.3 {Tk_DeleteSelHandler procedure} {unixOnly} {
    setup
    selection own -selection CLIPBOARD .f1
    selection handle -selection PRIMARY .f1 {handler STRING} 
    selection handle -selection CLIPBOARD .f1 {handler STRING} 
    selection handle -selection CLIPBOARD .f1 {}
    list [lsort [selection get TARGETS]] \
	[lsort [selection get -selection CLIPBOARD TARGETS]]
} {{MULTIPLE STRING TARGETS TIMESTAMP TK_APPLICATION TK_WINDOW UTF8_STRING} {MULTIPLE TARGETS TIMESTAMP TK_APPLICATION TK_WINDOW}}
test select-2.4 {Tk_DeleteSelHandler procedure} {macOrPc} {
    setup
    selection handle .f1 {handler STRING} 
    selection handle -type TEST .f1 {handler TEST} 
    selection handle -type USER .f1 {handler USER} 
    set result [list [lsort [selection get TARGETS]]]
    selection handle -type TEST .f1 {}
    lappend result [lsort [selection get TARGETS]]
} {{MULTIPLE STRING TARGETS TEST TIMESTAMP TK_APPLICATION TK_WINDOW USER} {MULTIPLE STRING TARGETS TIMESTAMP TK_APPLICATION TK_WINDOW USER}}
test select-2.5 {Tk_DeleteSelHandler procedure} {macOrPc} {
    setup
    selection handle .f1 {handler STRING} 
    selection handle -type TEST .f1 {handler TEST} 
    selection handle -type USER .f1 {handler USER} 
    set result [list [lsort [selection get TARGETS]]]
    selection handle -type USER .f1 {}
    lappend result [lsort [selection get TARGETS]]
} {{MULTIPLE STRING TARGETS TEST TIMESTAMP TK_APPLICATION TK_WINDOW USER} {MULTIPLE STRING TARGETS TEST TIMESTAMP TK_APPLICATION TK_WINDOW}}
test select-2.6 {Tk_DeleteSelHandler procedure} {macOrPc} {
    setup
    selection own -selection CLIPBOARD .f1
    selection handle -selection PRIMARY .f1 {handler STRING} 
    selection handle -selection CLIPBOARD .f1 {handler STRING} 
    selection handle -selection CLIPBOARD .f1 {}
    list [lsort [selection get TARGETS]] \
	[lsort [selection get -selection CLIPBOARD TARGETS]]
} {{MULTIPLE STRING TARGETS TIMESTAMP TK_APPLICATION TK_WINDOW} {MULTIPLE TARGETS TIMESTAMP TK_APPLICATION TK_WINDOW}}
test select-2.7 {Tk_DeleteSelHandler procedure} {
    setup
    selection handle .f1 {handler STRING}
    list [selection handle .f1 {}] [selection handle .f1 {}]
} {{} {}}   

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

test select-3.1 {Tk_OwnSelection procedure} {
    setup
    selection own
} {.f1}
test select-3.2 {Tk_OwnSelection procedure} {
    setup .f1
    set result [selection own]
    setup .f2
    lappend result [selection own]
} {.f1 .f2}
test select-3.3 {Tk_OwnSelection procedure} {
    setup .f1
    setup .f2
    selection own -selection CLIPBOARD .f1
    list [selection own] [selection own -selection CLIPBOARD]
} {.f2 .f1}
test select-3.4 {Tk_OwnSelection procedure} {
    global lostSel
    setup
    set lostSel {owned}
    selection own -command { set lostSel {lost} } .f1
    selection clear .f1
    set lostSel
} {lost}
test select-3.5 {Tk_OwnSelection procedure} {
    global lostSel
    setup .f1
    setup .f2
    set lostSel {owned}
    selection own -command { set lostSel {lost1} } .f1
    selection own -command { set lostSel {lost2} } .f2
    list $lostSel [selection own]
} {lost1 .f2}
test select-3.6 {Tk_OwnSelection procedure} {
    global lostSel
    setup
    set lostSel {owned}
    selection own -command { set lostSel {lost1} } .f1
    selection own -command { set lostSel {lost2} } .f1
    set result $lostSel
    selection clear .f1
    lappend result $lostSel
} {owned lost2}
test select-3.7 {Tk_OwnSelection procedure} {unixOnly} {
    global lostSel
    setup
    setupbg
    set lostSel {owned}
    selection own -command { set lostSel {lost1} } .f1
    update
    set result {}
    lappend result [dobg { selection own . }]
    lappend result [dobg {selection own}]
    update
    cleanupbg
    lappend result $lostSel
} {{} . lost1}
# check reentrancy on selection replacement
test select-3.8 {Tk_OwnSelection procedure} {
    setup
    selection own -selection CLIPBOARD -command { destroy .f1 } .f1
    selection own -selection CLIPBOARD .
} {}
test select-3.9 {Tk_OwnSelection procedure} {
    setup .f2
    setup .f1
    selection own -selection CLIPBOARD -command { destroy .f2 } .f1
    selection own -selection CLIPBOARD .f2
} {}

# multiple display tests
test select-3.10 {Tk_OwnSelection procedure} {altDisplay} {
    setup .f1
    setup .f2 $env(TK_ALT_DISPLAY)
    list [selection own -displayof .f1] [selection own -displayof .f2]
} {.f1 .f2}
test select-3.11 {Tk_OwnSelection procedure} {altDisplay} {
    setup .f1
    setup .f2 $env(TK_ALT_DISPLAY)
    setupbg
    update
    set result ""
    lappend result [dobg "toplevel .t -screen $env(TK_ALT_DISPLAY); wm geom .t +0+0; selection own .t; update"]
    lappend result [selection own -displayof .f1] \
	    [selection own -displayof .f2]
    cleanupbg
    set result
} {{} .f1 {}}

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

test select-4.1 {Tk_ClearSelection procedure} {
    setup
    set result [selection own]
    selection clear .f1
    lappend result [selection own]
} {.f1 {}}
test select-4.2 {Tk_ClearSelection procedure} {
    setup
    selection own -selection CLIPBOARD .f1
    selection clear .f1
    selection own -selection CLIPBOARD
} {.f1}
test select-4.3 {Tk_ClearSelection procedure} {
    setup
    list [selection clear .f1] [selection clear .f1]
} {{} {}}
test select-4.4 {Tk_ClearSelection procedure} {unixOnly} {
    global lostSel
    setup
    setupbg
    set lostSel {owned}
    selection own -command { set lostSel {lost1} } .f1
    update
    set result {}
    lappend result [dobg {selection clear; update}]
    update
    cleanupbg
    lappend result [selection own]
} {{} {}}

# multiple display tests
test select-4.5 {Tk_ClearSelection procedure} {altDisplay} {
    global lostSel lostSel2
    setup .f1
    setup .f2 $env(TK_ALT_DISPLAY)
    set lostSel {owned}
    set lostSel2 {owned2}
    selection own -command { set lostSel {lost1} } .f1
    selection own -command { set lostSel2 {lost2} } .f2
    update
    selection clear -displayof .f2
    update
    list $lostSel $lostSel2
} {owned lost2}
test select-4.6 {Tk_ClearSelection procedure} {unixOnly altDisplay} {
    setup .f1
    setup .f2 $env(TK_ALT_DISPLAY)
    setupbg
    set lostSel {owned}
    set lostSel2 {owned2}
    selection own -command { set lostSel {lost1} } .f1
    selection own -command { set lostSel2 {lost2} } .f2
    update
    set result ""
    lappend result [dobg "toplevel .t -screen $env(TK_ALT_DISPLAY); wm geom .t +0+0; selection own .t; update"]
    lappend result [selection own -displayof .f1] \
	    [selection own -displayof .f2] $lostSel $lostSel2
    cleanupbg
    set result
} {{} .f1 {} owned lost2}

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

test select-5.1 {Tk_GetSelection procedure} {
    setup
    list [catch {selection get TEST} msg] $msg
} {1 {PRIMARY selection doesn't exist or form "TEST" not defined}}
test select-5.2 {Tk_GetSelection procedure} {
    setup
    selection get TK_WINDOW
} {.f1}
test select-5.3 {Tk_GetSelection procedure} {
    setup
    selection handle -selection PRIMARY .f1 {handler TEST} TEST
    set selValue "Test value"
    set selInfo ""
    list [selection get TEST] $selInfo
} {{Test value} {TEST 0 4000}}
test select-5.4 {Tk_GetSelection procedure} {
    setup
    selection handle .f1 ERROR errHandler
    list [catch {selection get ERROR} msg] $msg
} {1 {PRIMARY selection doesn't exist or form "ERROR" not defined}}
test select-5.5 {Tk_GetSelection procedure} {
    setup
    set selValue $longValue
    set selInfo ""
    selection handle .f1 {handler STRING}
    list [selection get] $selInfo
} "$longValue {STRING 0 4000 STRING 4000 4000 STRING 8000 4000 STRING 12000 4000 STRING 16000 4000}"
test select-5.6 {Tk_GetSelection procedure} {
    proc weirdHandler {type offset count} {
	selection handle .f1 {}
	handler $type $offset $count
    }
    setup
    set selValue $longValue
    set selInfo ""
    selection handle .f1 {weirdHandler STRING}
    list [catch {selection get} msg] $msg
} {1 {PRIMARY selection doesn't exist or form "STRING" not defined}}
test select-5.7 {Tk_GetSelection procedure} {
    proc weirdHandler {type offset count} {
	destroy .f1
	handler $type $offset $count
    }
    setup
    set selValue "Test Value"
    set selInfo ""
    selection handle .f1 {weirdHandler STRING}
    list [catch {selection get} msg] $msg
} {1 {PRIMARY selection doesn't exist or form "STRING" not defined}}
test select-5.8 {Tk_GetSelection procedure} {
    proc weirdHandler {type offset count} {
	selection clear
	handler $type $offset $count
    }
    setup
    set selValue $longValue
    set selInfo ""
    selection handle .f1 {weirdHandler STRING}
    list [selection get] $selInfo [catch {selection get} msg] $msg
} "$longValue {STRING 0 4000 STRING 4000 4000 STRING 8000 4000 STRING 12000 4000 STRING 16000 4000} 1 {PRIMARY selection doesn't exist or form \"STRING\" not defined}"
test select-5.9 {Tk_GetSelection procedure} {unixOnly} {
    setup
    setupbg
    selection handle -selection PRIMARY .f1 {handler TEST} TEST
    update
    set selValue "Test value"
    set selInfo ""
    set result ""
    lappend result [dobg {selection get TEST}]
    cleanupbg
    lappend result $selInfo
} {{Test value} {TEST 0 4000}}
test select-5.10 {Tk_GetSelection procedure} {unixOnly} {
    setup
    setupbg
    selection handle -selection PRIMARY .f1 {handler TEST} TEST
    update
    set selValue "Test value"
    set selInfo ""
    selection own .f1
    set result ""
    lappend result [dobg {selection get TEST} 1]
    cleanupbg
    lappend result $selInfo
} {{selection owner didn't respond} {}}

# multiple display tests

test select-5.11 {Tk_GetSelection procedure} {altDisplay} {
    setup .f1
    setup .f2 $env(TK_ALT_DISPLAY)
    selection handle -selection PRIMARY .f1 {handler TEST} TEST
    selection handle -selection PRIMARY .f2 {handler TEST2} TEST
    set selValue "Test value"
    set selInfo ""
    set result [list [selection get TEST] $selInfo]
    set selValue "Test value2"
    set selInfo ""
    lappend result [selection get -displayof .f2 TEST] $selInfo
} {{Test value} {TEST 0 4000} {Test value2} {TEST2 0 4000}}
test select-5.12 {Tk_GetSelection procedure} {altDisplay} {
    global lostSel lostSel2
    setup .f1
    setup .f2 $env(TK_ALT_DISPLAY)
    selection handle -selection PRIMARY .f1 {handler TEST} TEST
    selection handle -selection PRIMARY .f2 {} TEST
    set selValue "Test value"
    set selInfo ""
    set result [list [catch {selection get TEST} msg] $msg $selInfo]
    set selValue "Test value2"
    set selInfo ""
    lappend result [catch {selection get -displayof .f2 TEST} msg] $msg \
	    $selInfo
} {0 {Test value} {TEST 0 4000} 1 {PRIMARY selection doesn't exist or form "TEST" not defined} {}}
test select-5.13 {Tk_GetSelection procedure} {unixOnly altDisplay} {
    setup .f1
    setup .f2 $env(TK_ALT_DISPLAY)
    setupbg
    selection handle -selection PRIMARY .f1 {handler TEST} TEST
    selection own .f1
    selection handle -selection PRIMARY .f2 {handler TEST2} TEST
    selection own .f2
    set selValue "Test value"
    set selInfo ""
    update
    set result ""
    lappend result [dobg "toplevel .t -screen $env(TK_ALT_DISPLAY); wm geom .t +0+0; selection get -displayof .t TEST"]
    set selValue "Test value2"
    lappend result [dobg "selection get TEST"]
    cleanupbg
    lappend result $selInfo
} {{Test value} {Test value2} {TEST2 0 4000 TEST 0 4000}}
test select-5.14 {Tk_GetSelection procedure} {unixOnly altDisplay} {
    setup .f1
    setup .f2 $env(TK_ALT_DISPLAY)
    setupbg
    selection handle -selection PRIMARY .f1 {handler TEST} TEST
    selection own .f1
    selection handle -selection PRIMARY .f2 {} TEST
    selection own .f2
    set selValue "Test value"
    set selInfo ""
    update
    set result ""
    lappend result [dobg "toplevel .t -screen $env(TK_ALT_DISPLAY); wm geom .t +0+0; selection get -displayof .t TEST"]
    set selValue "Test value2"
    lappend result [dobg "selection get TEST"]
    cleanupbg
    lappend result $selInfo
} {{PRIMARY selection doesn't exist or form "TEST" not defined} {Test value2} {TEST 0 4000}}

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

test select-6.1 {Tk_SelectionCmd procedure} {
    list [catch {selection} cmd] $cmd
} {1 {wrong # args: should be "selection option ?arg arg ...?"}}

# selection clear
test select-6.2 {Tk_SelectionCmd procedure} {
    list [catch {selection clear -selection} cmd] $cmd
} {1 {value for "-selection" missing}}
test select-6.3 {Tk_SelectionCmd procedure} {
    setup
    selection own .
    set result [selection own]
    selection clear -displayof .f1
    lappend result [selection own]
} {. {}}
test select-6.4 {Tk_SelectionCmd procedure} {
    setup
    selection own -selection CLIPBOARD .f1
    set result [list [selection own] [selection own -selection CLIPBOARD]]
    selection clear -selection CLIPBOARD .f1
    lappend result [selection own] [selection own -selection CLIPBOARD]
} {.f1 .f1 .f1 {}}
test select-6.5 {Tk_SelectionCmd procedure} {
    setup
    selection own -selection CLIPBOARD .
    set result [list [selection own] [selection own -selection CLIPBOARD]]
    selection clear -selection CLIPBOARD -displayof .f1
    lappend result [selection own] [selection own -selection CLIPBOARD]
} {.f1 . .f1 {}}
test select-6.6 {Tk_SelectionCmd procedure} {
    list [catch {selection clear -badopt foo} cmd] $cmd
} {1 {bad option "-badopt": must be -displayof or -selection}}
test select-6.7 {Tk_SelectionCmd procedure} {
    list [catch {selection clear -selectionfoo foo} cmd] $cmd
} {1 {bad option "-selectionfoo": must be -displayof or -selection}}
test select-6.8 {Tk_SelectionCmd procedure} {
    catch {destroy .f2}
    list [catch {selection clear -displayof .f2} cmd] $cmd
} {1 {bad window path name ".f2"}}
test select-6.9 {Tk_SelectionCmd procedure} {
    catch {destroy .f2}
    list [catch {selection clear .f2} cmd] $cmd
} {1 {bad window path name ".f2"}}
test select-6.10 {Tk_SelectionCmd procedure} {
    setup
    set result [selection own -selection PRIMARY]
    selection clear
    lappend result [selection own -selection PRIMARY]
} {.f1 {}}
test select-6.11 {Tk_SelectionCmd procedure} {
    setup
    selection own -selection CLIPBOARD .f1
    set result [selection own -selection CLIPBOARD]
    selection clear -selection CLIPBOARD
    lappend result [selection own -selection CLIPBOARD]
} {.f1 {}}
test select-6.12 {Tk_SelectionCmd procedure} {
    list [catch {selection clear foo bar} cmd] $cmd
} {1 {wrong # args: should be "selection clear ?options?"}}

# selection get
test select-6.13 {Tk_SelectionCmd procedure} {
    list [catch {selection get -selection} cmd] $cmd
} {1 {value for "-selection" missing}}
test select-6.14 {Tk_SelectionCmd procedure} {
    global selValue selInfo
    setup
    selection handle .f1 {handler TEST}
    set selValue "Test value"
    set selInfo ""
    list [selection get -displayof .f1] $selInfo
} {{Test value} {TEST 0 4000}}
test select-6.15 {Tk_SelectionCmd procedure} {
    global selValue selInfo
    setup
    selection handle .f1 {handler STRING}
    selection handle -selection CLIPBOARD .f1 {handler TEST}
    selection own -selection CLIPBOARD .f1
    set selValue "Test value"
    set selInfo ""
    list [selection get -selection CLIPBOARD] $selInfo
} {{Test value} {TEST 0 4000}}
test select-6.16 {Tk_SelectionCmd procedure} {
    global selValue selInfo
    setup
    selection handle -type TEST .f1 {handler TEST}
    selection handle -type STRING .f1 {handler STRING}
    set selValue "Test value"
    set selInfo ""
    list [selection get -type TEST] $selInfo
} {{Test value} {TEST 0 4000}}
test select-6.17 {Tk_SelectionCmd procedure} {
    list [catch {selection get -badopt foo} cmd] $cmd
} {1 {bad option "-badopt": must be -displayof, -selection, or -type}}
test select-6.18 {Tk_SelectionCmd procedure} {
    list [catch {selection get -selectionfoo foo} cmd] $cmd
} {1 {bad option "-selectionfoo": must be -displayof, -selection, or -type}}
test select-6.19 {Tk_SelectionCmd procedure} {
    catch { destroy .f2 }
    list [catch {selection get -displayof .f2} cmd] $cmd
} {1 {bad window path name ".f2"}}
test select-6.20 {Tk_SelectionCmd procedure} {
    list [catch {selection get foo bar} cmd] $cmd
} {1 {wrong # args: should be "selection get ?options?"}}
test select-6.21 {Tk_SelectionCmd procedure} {
    global selValue selInfo
    setup
    selection handle -type TEST .f1 {handler TEST}
    selection handle -type STRING .f1 {handler STRING}
    set selValue "Test value"
    set selInfo ""
    list [selection get TEST] $selInfo
} {{Test value} {TEST 0 4000}}

# selection handle
# most of the handle section has been covered earlier
test select-6.22 {Tk_SelectionCmd procedure} {
    list [catch {selection handle -selection} cmd] $cmd
} {1 {value for "-selection" missing}}
test select-6.23 {Tk_SelectionCmd procedure} {
    global selValue selInfo
    setup
    set selValue "Test value"
    set selInfo ""
    list [selection handle -format INTEGER .f1 {handler TEST}] [selection get -displayof .f1] $selInfo
} {{} {Test value} {TEST 0 4000}}
test select-6.24 {Tk_SelectionCmd procedure} {
    list [catch {selection handle -badopt foo} cmd] $cmd
} {1 {bad option "-badopt": must be -format, -selection, or -type}}
test select-6.25 {Tk_SelectionCmd procedure} {
    list [catch {selection handle -selectionfoo foo} cmd] $cmd
} {1 {bad option "-selectionfoo": must be -format, -selection, or -type}}
test select-6.26 {Tk_SelectionCmd procedure} {
    list [catch {selection handle} cmd] $cmd
} {1 {wrong # args: should be "selection handle ?options? window command"}}
test select-6.27 {Tk_SelectionCmd procedure} {
    list [catch {selection handle .} cmd] $cmd
} {1 {wrong # args: should be "selection handle ?options? window command"}}
test select-6.28 {Tk_SelectionCmd procedure} {
    list [catch {selection handle . foo bar baz blat} cmd] $cmd
} {1 {wrong # args: should be "selection handle ?options? window command"}}
test select-6.29 {Tk_SelectionCmd procedure} {
    catch { destroy .f2 }
    list [catch {selection handle .f2 dummy} cmd] $cmd
} {1 {bad window path name ".f2"}}

# selection own
test select-6.30 {Tk_SelectionCmd procedure} {
    list [catch {selection own -selection} cmd] $cmd
} {1 {value for "-selection" missing}}
test select-6.31 {Tk_SelectionCmd procedure} {
    setup
    selection own .
    selection own -displayof .f1
} {.}
test select-6.32 {Tk_SelectionCmd procedure} {
    setup
    selection own .
    selection own -selection CLIPBOARD .f1
    list [selection own] [selection own -selection CLIPBOARD]
} {. .f1}
test select-6.33 {Tk_SelectionCmd procedure} {
    global lostSel
    setup
    set lostSel owned
    selection own -command { set lostSel lost } .
    selection own -selection CLIPBOARD .f1
    set result $lostSel
    selection own .f1
    lappend result $lostSel
} {owned lost}
test select-6.34 {Tk_SelectionCmd procedure} {
    list [catch {selection own -badopt foo} cmd] $cmd
} {1 {bad option "-badopt": must be -command, -displayof, or -selection}}
test select-6.35 {Tk_SelectionCmd procedure} {
    list [catch {selection own -selectionfoo foo} cmd] $cmd
} {1 {bad option "-selectionfoo": must be -command, -displayof, or -selection}}
test select-6.36 {Tk_SelectionCmd procedure} {
    catch {destroy .f2}
    list [catch {selection own -displayof .f2} cmd] $cmd
} {1 {bad window path name ".f2"}}
test select-6.37 {Tk_SelectionCmd procedure} {
    catch {destroy .f2}
    list [catch {selection own .f2} cmd] $cmd
} {1 {bad window path name ".f2"}}
test select-6.38 {Tk_SelectionCmd procedure} {
    list [catch {selection own foo bar baz} cmd] $cmd
} {1 {wrong # args: should be "selection own ?options? ?window?"}}

test select-6.39 {Tk_SelectionCmd procedure} {
    list [catch {selection foo} cmd] $cmd
} {1 {bad option "foo": must be clear, get, handle, or own}}

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

    # This test is non-portable because some old X11/News servers ignore
    # a selection request when the window doesn't exist, which causes a
    # different error message.

    test select-7.1 {TkSelDeadWindow procedure} {nonPortable} {
	setup
	selection handle .f1 { handler TEST }
	set result [selection own]
	destroy .f1
	lappend result [selection own] [catch { selection get } msg] $msg
    } {.f1 {} 1 {PRIMARY selection doesn't exist or form "STRING" not defined}}

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

# Check reentrancy on losing selection

test select-8.1 {TkSelEventProc procedure} {unixOnly} {
    setup
    setupbg
    selection own -selection CLIPBOARD -command { destroy .f1 } .f1
    update
    set result [dobg {selection own -selection CLIPBOARD .}]
    cleanupbg
    set result
} {}

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

test select-9.1 {SelCvtToX and SelCvtFromX procedures} {unixOnly} {
    global selValue selInfo
    setup
    setupbg
    set selValue "1024"
    set selInfo ""
    selection handle -selection PRIMARY -format INTEGER -type TEST \
	.f1 {handler TEST}
    update
    set result ""
    lappend result [dobg {selection get TEST}]
    cleanupbg
    lappend result $selInfo
} {0x400 {TEST 0 4000}}
test select-9.2 {SelCvtToX and SelCvtFromX procedures} {unixOnly} {
    global selValue selInfo
    setup
    setupbg
    set selValue "1024 0xffff  2048 -2  "
    set selInfo ""
    selection handle -selection PRIMARY -format INTEGER -type TEST \
	.f1 {handler TEST}
    set result ""
    lappend result [dobg {selection get TEST}]
    cleanupbg
    lappend result $selInfo
} {{0x400 0xffff 0x800 0xfffffffe} {TEST 0 4000}}
test select-9.3 {SelCvtToX and SelCvtFromX procedures} {unixOnly} {
    global selValue selInfo
    setup
    setupbg
    set selValue "   "
    set selInfo ""
    selection handle -selection PRIMARY -format INTEGER -type TEST \
	.f1 {handler TEST}
    set result ""
    lappend result [dobg {selection get TEST}]
    cleanupbg
    lappend result $selInfo
} {{} {TEST 0 4000}}
test select-9.4 {SelCvtToX and SelCvtFromX procedures} {unixOnly} {
    global selValue selInfo
    setup
    setupbg
    set selValue "16 foobar 32"
    set selInfo ""
    selection handle -selection PRIMARY -format INTEGER -type TEST \
	.f1 {handler TEST}
    set result ""
    lappend result [dobg {selection get TEST}]
    cleanupbg
    lappend result $selInfo
} {{0x10 0x0 0x20} {TEST 0 4000}}

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

# note, we are not testing MULTIPLE style selections

# most control paths have been exercised above
test select-10.1 {ConvertSelection procedure, race with selection clear} {unixOnly} {
    setup
    proc Ready {fd} {
	variable x
	lappend x [gets $fd]
    }
    set fd [open "|[list [interpreter] -geometry +0+0 -name tktest]" r+]
    puts $fd "puts foo; flush stdout"
    flush $fd
    gets $fd
    fileevent $fd readable [list Ready $fd]
    set selValue "Just a simple test"
    set selInfo ""
    selection handle .f1 {handler STRING}
    update
    puts $fd {puts "[catch {selection get} msg] $msg"; puts **DONE**; flush stdout}
    flush $fd
    after 200
    selection own .
    set x {}
    vwait [namespace which -variable x]
    puts $fd {exit}
    flush $fd
    close $fd
    lappend x $selInfo
} {{1 PRIMARY selection doesn't exist or form "STRING" not defined} {}}
test select-10.2 {ConvertSelection procedure} {unixOnly} {
    setup
    setupbg
    set selValue [string range $longValue 0 3999]
    set selInfo ""
    selection handle .f1 {handler STRING}
    set result ""
    lappend result [dobg {selection get}]
    cleanupbg
    lappend result $selInfo
} [list [string range $longValue 0 3999] {STRING 0 4000 STRING 4000 4000 STRING 0 4000 STRING 4000 4000}]
test select-10.3 {ConvertSelection procedure} {unixOnly} {
    setup
    setupbg
    selection handle .f1 ERROR errHandler
    set result ""
    lappend result [dobg {selection get ERROR}]
    cleanupbg
    set result
} {{PRIMARY selection doesn't exist or form "ERROR" not defined}}
# testing timers
# This one hangs in Exceed
test select-10.4 {ConvertSelection procedure} {unixOnly noExceed} {
    setup
    setupbg
    set selValue $longValue
    set selInfo ""
    selection handle .f1 {errIncrHandler STRING}
    set result ""
    set pass 0
    lappend result [dobg {selection get}]
    cleanupbg
    lappend result $selInfo
} {{selection owner didn't respond} {STRING 0 4000 STRING 4000 4000 STRING 8000 4000 STRING 12000 4000 STRING 16000 4000 STRING 0 4000 STRING 4000 4000}}
test select-10.5 {ConvertSelection procedure, reentrancy issues} {unixOnly} {
    setup
    setupbg
    set selValue "Test value"
    set selInfo ""
    selection handle -type TEST .f1 { handler TEST }
    selection handle -type STRING .f1 { badHandler .f1 STRING }
    set result ""
    lappend result [dobg {selection get}]
    cleanupbg
    lappend result $selInfo
} {{PRIMARY selection doesn't exist or form "STRING" not defined} {.f1 STRING 0 4000}}
test select-10.6 {ConvertSelection procedure, reentrancy issues} {unixOnly} {
    proc weirdHandler {type offset count} {
	destroy .f1
	handler $type $offset $count
    }
    setup
    setupbg
    set selValue $longValue
    set selInfo ""
    selection handle .f1 {weirdHandler STRING}
    set result ""
    lappend result [dobg {selection get}]
    cleanupbg
    lappend result $selInfo
} {{PRIMARY selection doesn't exist or form "STRING" not defined} {STRING 0 4000}}

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

# testing reentrancy
test select-11.1 {TkSelPropProc procedure} {unixOnly} {
    setup
    setupbg
    set selValue $longValue
    set selInfo ""
    selection handle -type TEST .f1 { handler TEST }
    selection handle -type STRING .f1 { reallyBadHandler .f1 STRING }
    set result ""
    set pass 0
    lappend result [dobg {selection get}]
    cleanupbg
    lappend result $selInfo
} {{selection owner didn't respond} {.f1 STRING 0 4000 .f1 STRING 4000 4000 .f1 STRING 8000 4000 .f1 STRING 12000 4000 .f1 STRING 16000 4000 .f1 STRING 0 4000 .f1 STRING 4000 4000}}

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

# Note, this assumes we are using CurrentTtime
test select-12.1 {DefaultSelection procedure} {unixOnly} {
    setup
    set result [selection get -type TIMESTAMP]
    setupbg
    lappend result [dobg {selection get -type TIMESTAMP}]
    cleanupbg
    set result
} {0x0 0x0}
test select-12.2 {DefaultSelection procedure} {unixOnly} {
    setup
    set result [lsort [list [selection get -type TARGETS]]]
    setupbg
    lappend result [dobg {lsort [selection get -type TARGETS]}]
    cleanupbg
    set result
} {{MULTIPLE TARGETS TIMESTAMP TK_APPLICATION TK_WINDOW} {MULTIPLE TARGETS TIMESTAMP TK_APPLICATION TK_WINDOW}}
test select-12.3 {DefaultSelection procedure} {unixOnly} {
    setup
    selection handle .f1 {handler TEST} TEST
    set result [list [lsort [selection get -type TARGETS]]]
    setupbg
    lappend result [dobg {lsort [selection get -type TARGETS]}]
    cleanupbg
    set result
} {{MULTIPLE TARGETS TEST TIMESTAMP TK_APPLICATION TK_WINDOW} {MULTIPLE TARGETS TEST TIMESTAMP TK_APPLICATION TK_WINDOW}}
test select-12.4 {DefaultSelection procedure} {unixOnly} {
    setup
    set result ""
    lappend result [selection get -type TK_APPLICATION]
    setupbg
    lappend result [dobg {selection get -type TK_APPLICATION}]
    cleanupbg
    set result
} [list [winfo name .] [winfo name .]]
test select-12.5 {DefaultSelection procedure} {unixOnly} {
    setup
    set result [selection get -type TK_WINDOW]
    setupbg
    lappend result [dobg {selection get -type TK_WINDOW}]
    cleanupbg
    set result
} {.f1 .f1}
test select-12.6 {DefaultSelection procedure} {
    global selValue selInfo
    setup
    selection handle .f1 {handler TARGETS.f1} TARGETS
    set selValue "Targets value"
    set selInfo ""
    set result [list [selection get TARGETS] $selInfo]
    selection handle .f1 {} TARGETS
    lappend result [selection get TARGETS]
} {{Targets value} {TARGETS.f1 0 4000} {MULTIPLE TARGETS TIMESTAMP TK_APPLICATION TK_WINDOW}}

test select-13.1 {SelectionSize procedure, handler deleted} {unixOnly} {
    proc badHandler {path type offset count} {
	global selValue selInfo abortCount
	incr abortCount -1
	if {$abortCount == 0} {
	    selection handle -type $type $path {}
	}
	lappend selInfo $path $type $offset $count
	set numBytes [expr {[string length $selValue] - $offset}]
	if {$numBytes <= 0} {
	    return ""
	}
	string range $selValue $offset [expr $numBytes+$offset]
    }
    setup
    setupbg
    set selValue $longValue
    set selInfo ""
    selection handle .f1 {badHandler .f1 STRING}
    set result ""
    set abortCount 2
    lappend result [dobg {selection get}]
    cleanupbg
    lappend result $selInfo
} {{PRIMARY selection doesn't exist or form "STRING" not defined} {.f1 STRING 0 4000 .f1 STRING 4000 4000}}

catch {rename weirdHandler {}}

# cleanup
::tcltest::cleanupTests
return