canvText.test
上传用户:rrhhcc
上传日期:2015-12-11
资源大小:54129k
文件大小:18k
源码类别:

通讯编程

开发平台:

Visual C++

  1. # This file is a Tcl script to test out the procedures in tkCanvText.c,
  2. # which implement canvas "text" items.  It is organized in the standard
  3. # fashion for Tcl tests.
  4. #
  5. # Copyright (c) 1996-1997 Sun Microsystems, Inc.
  6. # Copyright (c) 1998-1999 by Scriptics Corporation.
  7. # All rights reserved.
  8. #
  9. # RCS: @(#) $Id: canvText.test,v 1.12.2.1 2004/08/09 23:48:11 mdejong Exp $
  10. package require tcltest 2.1
  11. namespace import -force tcltest::configure
  12. namespace import -force tcltest::testsDirectory
  13. configure -testdir [file join [pwd] [file dirname [info script]]]
  14. configure -loadfile [file join [testsDirectory] constraints.tcl]
  15. tcltest::loadTestedCommands
  16. canvas .c -width 400 -height 300 -bd 2 -relief sunken
  17. pack .c
  18. update
  19. set i 1
  20. .c create text 20 20 -tag test
  21. set font "-adobe-times-medium-r-normal--*-200-*-*-*-*-*-*"
  22. set ay [font metrics $font -linespace]
  23. set ax [font measure $font 0]
  24. foreach test {
  25.     {-anchor nw nw xyz {bad anchor position "xyz": must be n, ne, e, se, s, sw, w, nw, or center}}
  26.     {-fill #ff0000 #ff0000 xyz {unknown color name "xyz"}}
  27.     {-fill {} {} {} {}}
  28.     {-font {Times 40} {Times 40} {} {font "" doesn't exist}}
  29.     {-justify left left xyz {bad justification "xyz": must be left, right, or center}}
  30.     {-stipple gray50 gray50 xyz {bitmap "xyz" not defined}}
  31.     {-tags {test a b c} {test a b c} {} {}}
  32.     {-text xyz xyz {} {}}
  33.     {-width 6 6 xyz {bad screen distance "xyz"}}
  34. } {
  35.     set name [lindex $test 0]
  36.     test canvText-1.$i {configuration options} {
  37. .c itemconfigure test $name [lindex $test 1]
  38. list [lindex [.c itemconfigure test $name] 4] [.c itemcget test $name]
  39.     } [list [lindex $test 2] [lindex $test 2]]
  40.     incr i
  41.     if {[lindex $test 3] != ""} {
  42. test canvText-1.$i {configuration options} {
  43.     list [catch {.c itemconfigure test $name [lindex $test 3]} msg] $msg
  44. } [list 1 [lindex $test 4]]
  45.     }
  46.     incr i
  47. }
  48. test canvText-1.$i {configuration options} {
  49.     .c itemconfigure test -tags {test xyz}
  50.     .c itemcget xyz -tags
  51. } {test xyz}
  52. .c delete test
  53. .c create text 20 20 -tag test
  54. test canvText-2.1 {CreateText procedure: args} {
  55.     list [catch {.c create text} msg] $msg
  56. } {1 {wrong # args: should be ".c create text coords ?arg arg ...?"}}
  57. test canvText-2.2 {CreateText procedure: args} {
  58.     list [catch {.c create text xyz 0} msg] $msg
  59. } {1 {bad screen distance "xyz"}}
  60. test canvText-2.3 {CreateText procedure: args} {
  61.     list [catch {.c create text 0 xyz} msg] $msg
  62. } {1 {bad screen distance "xyz"}}
  63. test canvText-2.4 {CreateText procedure: args} {
  64.     list [catch {.c create text 0 0 -xyz xyz} msg] $msg
  65. } {1 {unknown option "-xyz"}}
  66. test canvText-2.5 {CreateText procedure} {
  67.     .c create text 0 0 -tags x
  68.     set x [.c coords x]
  69.     .c delete x
  70.     set x
  71. } {0.0 0.0}
  72. focus -force .c
  73. .c focus test
  74. .c coords test 0 0
  75. update
  76. test canvText-3.1 {TextCoords procedure} {
  77.     .c coords test
  78. } {0.0 0.0}
  79. test canvText-3.2 {TextCoords procedure} {
  80.     list [catch {.c coords test xyz 0} msg] $msg
  81. } {1 {bad screen distance "xyz"}}
  82. test canvText-3.3 {TextCoords procedure} {
  83.     list [catch {.c coords test 0 xyz} msg] $msg
  84. } {1 {bad screen distance "xyz"}}
  85. test canvText-3.4 {TextCoords procedure} {
  86.     .c coords test 10 10
  87.     set result {}
  88.     foreach element [.c coords test] {
  89. lappend result [format %.1f $element]
  90.     }
  91.     set result
  92. } {10.0 10.0}
  93. test canvText-3.5 {TextCoords procedure} {
  94.     list [catch {.c coords test 10} msg] $msg
  95. } {1 {wrong # coordinates: expected 2, got 1}}
  96. test canvText-3.6 {TextCoords procedure} {
  97.     list [catch {.c coords test 10 10 10} msg] $msg
  98. } {1 {wrong # coordinates: expected 0 or 2, got 3}}
  99. test canvText-4.1 {ConfigureText procedure} {
  100.     list [catch {.c itemconfig test -fill xyz} msg] $msg
  101. } {1 {unknown color name "xyz"}}
  102. test canvText-4.2 {ConfigureText procedure} {
  103.     .c itemconfig test -fill blue
  104.     .c itemcget test -fill
  105. } {blue}
  106. test canvText-4.3 {ConfigureText procedure: construct font gcs} {
  107.     .c itemconfig test -font "times 20" -fill black -stipple gray50
  108.     list [.c itemcget test -font] [.c itemcget test -fill] [.c itemcget test -stipple]
  109. } {{times 20} black gray50}
  110. test canvText-4.4 {ConfigureText procedure: construct cursor gc} {
  111.     .c itemconfig test -text "abcdefg"
  112.     .c select from test 2
  113.     .c select to test 4
  114.     .c icursor test 3
  115.     # Both black -> cursor becomes white.
  116.     .c config -insertbackground black
  117.     .c config -selectbackground black
  118.     .c itemconfig test -just left
  119.     update
  120.     # Both same color (and not black) -> cursor becomes black.
  121.     .c config -insertbackground red
  122.     .c config -selectbackground red
  123.     .c itemconfig test -just left
  124.     update
  125. } {}
  126. test canvText-4.5 {ConfigureText procedure: adjust selection} {
  127.     set x {}
  128.     .c itemconfig test -text "abcdefghi"
  129.     .c select from test 2
  130.     .c select to test 6
  131.     lappend x [selection get]
  132.     .c dchars test 1 end
  133.     lappend x [catch {selection get}]
  134.     .c insert test end "bcdefghi"
  135.     .c select from test 2
  136.     .c select to test 6
  137.     lappend x [selection get]
  138.     .c dchars test 4 end
  139.     lappend x [selection get]
  140.     .c insert test end "efghi"
  141.     .c select from test 6
  142.     .c select to test 2
  143.     lappend x [selection get]
  144.     .c dchars test 4 end
  145.     lappend x [selection get]
  146. } {cdefg 1 cdefg cd cdef cd}
  147. test canvText-4.6 {ConfigureText procedure: adjust cursor} {
  148.     .c itemconfig test -text "abcdefghi"
  149.     set x {}
  150.     .c icursor test 6
  151.     .c dchars test 4 end
  152.     .c index test insert
  153. } {4}
  154. test canvText-5.1 {ConfigureText procedure: adjust cursor} {
  155.     .c create text 10 10 -tag x -fill blue -font "times 40" -stipple gray50 -text "xyz"
  156.     .c delete x
  157. } {}
  158. test canvText-6.1 {ComputeTextBbox procedure} {fonts nonPortable} {
  159.     .c itemconfig test -font $font -text 0
  160.     .c coords test 0 0
  161.     set x {}
  162.     lappend x [.c itemconfig test -anchor n; .c bbox test]
  163.     lappend x [.c itemconfig test -anchor nw; .c bbox test]
  164.     lappend x [.c itemconfig test -anchor w; .c bbox test]
  165.     lappend x [.c itemconfig test -anchor sw; .c bbox test]
  166.     lappend x [.c itemconfig test -anchor s; .c bbox test]
  167.     lappend x [.c itemconfig test -anchor se; .c bbox test]
  168.     lappend x [.c itemconfig test -anchor e; .c bbox test]
  169.     lappend x [.c itemconfig test -anchor ne; .c bbox test]
  170.     lappend x [.c itemconfig test -anchor center; .c bbox test]
  171. } "{[expr -$ax/2-1] 0 [expr $ax/2+1] $ay}
  172. {-1 0 [expr $ax+1] $ay}
  173. {-1 [expr -$ay/2] [expr $ax+1] [expr $ay/2]}
  174. {-1 -$ay [expr $ax+1] 0}
  175. {[expr -$ax/2-1] -$ay [expr $ax/2+1] 0}
  176. {[expr -$ax-1] -$ay 1 0}
  177. {[expr -$ax-1] [expr -$ay/2] 1 [expr $ay/2]}
  178. {[expr -$ax-1] 0 1 $ay}
  179. {[expr -$ax/2-1] [expr -$ay/2] [expr $ax/2+1] [expr $ay/2]}"
  180. focus .c
  181. .c focus test
  182. .c itemconfig test -text "abcdnefghinjklmnopq"
  183. test canvText-7.0 {DisplayText procedure: stippling} {
  184.     .c itemconfig test -stipple gray50
  185.     update
  186.     .c itemconfig test -stipple {}
  187.     update
  188. } {}
  189. test canvText-7.2 {DisplayText procedure: draw selection} {
  190.     .c select from test 0
  191.     .c select to test end
  192.     update
  193.     selection get
  194. } "abcdnefghinjklmnopq"
  195. test canvText-7.3 {DisplayText procedure: selection} {
  196.     .c select from test 0
  197.     .c select to test end
  198.     update
  199.     selection get
  200. } "abcdnefghinjklmnopq"
  201. test canvText-7.4 {DisplayText procedure: one line selection} {
  202.     .c select from test 2
  203.     .c select to test 3
  204.     update
  205. } {}
  206. test canvText-7.5 {DisplayText procedure: multi-line selection} {
  207.     .c select from test 2
  208.     .c select to test 12
  209.     update
  210. } {}
  211. test canvText-7.6 {DisplayText procedure: draw cursor} {
  212.     .c icursor test 3
  213.     update
  214. } {}
  215. test canvText-7.7 {DisplayText procedure: selected text different color} {
  216.     .c config -selectforeground blue
  217.     .c itemconfig test -anchor n
  218.     update
  219. } {}
  220. test canvText-7.8 {DisplayText procedure: not selected} {
  221.     .c select clear
  222.     update
  223. } {}
  224. test canvText-7.9 {DisplayText procedure: select end} {
  225.     catch {destroy .t}
  226.     toplevel .t
  227.     wm geometry .t +0+0
  228.     canvas .t.c
  229.     pack .t.c
  230.     set id [.t.c create text 0 0 -text Dummy -anchor nw]
  231.     update
  232.     .t.c select from $id 0
  233.     .t.c select to $id end
  234.     update
  235.     #catch {destroy .t}
  236.     update
  237. } {}
  238. test canvText-8.1 {TextInsert procedure: 0 length insert} {
  239.     .c insert test end {}
  240. } {}
  241. test canvText-8.2 {TextInsert procedure: before beginning/after end} {
  242.     # Can't test this because GetTextIndex filters out those numbers.
  243. } {}
  244. test canvText-8.3 {TextInsert procedure: inserting in a selected item} {
  245.     .c itemconfig test -text "abcdefg"
  246.     .c select from test 2
  247.     .c select to test 4
  248.     .c insert test 1 "xyz"
  249.     .c itemcget test -text
  250. } {axyzbcdefg}
  251. test canvText-8.4 {TextInsert procedure: inserting before selection} {
  252.     .c itemconfig test -text "abcdefg"
  253.     .c select from test 2
  254.     .c select to test 4
  255.     .c insert test 1 "xyz"
  256.     list [.c index test sel.first] [.c index test sel.last]
  257. } {5 7}
  258. test canvText-8.5 {TextInsert procedure: inserting in selection} {
  259.     .c itemconfig test -text "abcdefg"
  260.     .c select from test 2
  261.     .c select to test 4
  262.     .c insert test 3 "xyz"
  263.     list [.c index test sel.first] [.c index test sel.last]
  264. } {2 7}
  265. test canvText-8.6 {TextInsert procedure: inserting after selection} {
  266.     .c itemconfig test -text "abcdefg"
  267.     .c select from test 2
  268.     .c select to test 4
  269.     .c insert test 5 "xyz"
  270.     list [.c index test sel.first] [.c index test sel.last]
  271. } {2 4}
  272. test canvText-8.7 {TextInsert procedure: inserting in unselected item} {
  273.     .c itemconfig test -text "abcdefg"
  274.     .c select clear
  275.     .c insert test 5 "xyz"
  276.     .c itemcget test -text
  277. } {abcdexyzfg}
  278. test canvText-8.8 {TextInsert procedure: inserting before cursor} {
  279.     .c itemconfig test -text "abcdefg"
  280.     .c icursor test 3
  281.     .c insert test 2 "xyz"
  282.     .c index test insert
  283. } {6}
  284. test canvText-8.9 {TextInsert procedure: inserting after cursor} {
  285.     .c itemconfig test -text "abcdefg"
  286.     .c icursor test 3
  287.     .c insert test 4 "xyz"
  288.     .c index test insert
  289. } {3}
  290. test canvText-9.1 {TextInsert procedure: before beginning/after end} {
  291.     # Can't test this because GetTextIndex filters out those numbers.
  292. } {}
  293. test canvText-9.2 {TextInsert procedure: start > end} {
  294.     .c itemconfig test -text "abcdefg"
  295.     .c dchars test 4 2
  296.     .c itemcget test -text
  297. } {abcdefg}
  298. test canvText-9.3 {TextInsert procedure: deleting from a selected item} {
  299.     .c itemconfig test -text "abcdefg"
  300.     .c select from test 2
  301.     .c select to test 4
  302.     .c dchars test 3 5
  303.     .c itemcget test -text
  304. } {abcg}
  305. test canvText-9.4 {TextInsert procedure: deleting before start} {
  306.     .c itemconfig test -text "abcdefghijk"
  307.     .c select from test 4
  308.     .c select to test 8
  309.     .c dchars test 1 1
  310.     list [.c index test sel.first] [.c index test sel.last]
  311. } {3 7}
  312. test canvText-9.5 {TextInsert procedure: keep start > first char deleted} {
  313.     .c itemconfig test -text "abcdefghijk"
  314.     .c select from test 4
  315.     .c select to test 8
  316.     .c dchars test 2 6
  317.     list [.c index test sel.first] [.c index test sel.last]
  318. } {2 3}
  319. test canvText-9.6 {TextInsert procedure: deleting inside selection} {
  320.     .c itemconfig test -text "abcdefghijk"
  321.     .c select from test 4
  322.     .c select to test 8
  323.     .c dchars test 6 6
  324.     list [.c index test sel.first] [.c index test sel.last]
  325. } {4 7}
  326. test canvText-9.7 {TextInsert procedure: keep end > first char deleted} {
  327.     .c itemconfig test -text "abcdefghijk"
  328.     .c select from test 4
  329.     .c select to test 8
  330.     .c dchars test 6 10
  331.     list [.c index test sel.first] [.c index test sel.last]
  332. } {4 5}
  333. test canvText-9.8 {TextInsert procedure: selectFirst > selectLast: deselect} {
  334.     .c itemconfig test -text "abcdefghijk"
  335.     .c select from test 4
  336.     .c select to test 8
  337.     .c dchars test 3 10
  338.     list [catch {.c index test sel.first} msg] $msg
  339. } {1 {selection isn't in item}}
  340. test canvText-9.9 {TextInsert procedure: selectFirst <= selectLast} {
  341.     .c itemconfig test -text "abcdefghijk"
  342.     .c select from test 4
  343.     .c select to test 8
  344.     .c dchars test 4 7
  345.     list [.c index test sel.first] [.c index test sel.last]
  346. } {4 4}
  347. test canvText-9.10 {TextInsert procedure: move anchor} {
  348.     .c itemconfig test -text "abcdefghijk"
  349.     .c select from test 6
  350.     .c select to test 8
  351.     .c dchars test 2 4
  352.     .c select to test 1
  353.     list [.c index test sel.first] [.c index test sel.last]
  354. } {1 2}
  355. test canvText-9.11 {TextInsert procedure: keep anchor >= first} {
  356.     .c itemconfig test -text "abcdefghijk"
  357.     .c select from test 6
  358.     .c select to test 8
  359.     .c dchars test 5 7
  360.     .c select to test 1
  361.     list [.c index test sel.first] [.c index test sel.last]
  362. } {1 4}
  363. test canvText-9.12 {TextInsert procedure: anchor doesn't move} {
  364.     .c itemconfig test -text "abcdefghijk"
  365.     .c select from test 2
  366.     .c select to test 5
  367.     .c dchars test 6 8
  368.     .c select to test 8
  369.     list [.c index test sel.first] [.c index test sel.last]
  370. } {2 8}
  371. test canvText-9.13 {TextInsert procedure: move cursor} {
  372.     .c itemconfig test -text "abcdefghijk"
  373.     .c icursor test 6
  374.     .c dchars test 2 4
  375.     .c index test insert
  376. } {3}
  377. test canvText-9.14 {TextInsert procedure: keep cursor >= first} {
  378.     .c itemconfig test -text "abcdefghijk"
  379.     .c icursor test 6
  380.     .c dchars test 2 10
  381.     .c index test insert
  382. } {2}
  383. test canvText-9.15 {TextInsert procedure: cursor doesn't move} {
  384.     .c itemconfig test -text "abcdefghijk"
  385.     .c icursor test 5
  386.     .c dchars test 7 9
  387.     .c index test insert
  388. } {5}
  389.     
  390. test canvText-10.1 {TextToPoint procedure} {
  391.     .c coords test 0 0
  392.     .c itemconfig test -text 0 -anchor center
  393.     .c index test @0,0
  394. } {0}
  395. test canvText-11.1 {TextToArea procedure} {
  396.     .c coords test 0 0
  397.     .c itemconfig test -text 0 -anchor center
  398.     .c find overlapping 0 0 1 1
  399. } [.c find withtag test]
  400. test canvText-11.2 {TextToArea procedure} {
  401.     .c coords test 0 0
  402.     .c itemconfig test -text 0 -anchor center
  403.     .c find overlapping 1000 1000 1001 1001
  404. } {}
  405. test canvText-12.1 {ScaleText procedure} {
  406.     .c coords test 100 100
  407.     .c scale all 50 50 2 2
  408.     .c coords test
  409. } {150.0 150.0}
  410. test canvText-13.1 {TranslateText procedure} {
  411.     .c coords test 100 100
  412.     .c move all 10 10
  413.     .c coords test
  414. } {110.0 110.0}
  415.     
  416. .c itemconfig test -text "abcdefghijklmno" -anchor nw
  417. .c select from test 5
  418. .c select to test 8
  419. .c icursor test 12
  420. .c coords test 0 0
  421. test canvText-14.1 {GetTextIndex procedure} {
  422.     list [.c index test end] [.c index test insert] 
  423. [.c index test sel.first] [.c index test sel.last] 
  424. [.c index test @0,0] 
  425. [.c index test -1] [.c index test 10] [.c index test 100]
  426. } {15 12 5 8 0 0 10 15}
  427. test canvText-14.2 {GetTextIndex procedure: select error} {
  428.     .c select clear
  429.     list [catch {.c index test sel.first} msg] $msg
  430. } {1 {selection isn't in item}}
  431. test canvText-14.3 {GetTextIndex procedure: select error} {
  432.     .c select clear
  433.     list [catch {.c index test sel.last} msg] $msg
  434. } {1 {selection isn't in item}}
  435. test canvText-14.4 {GetTextIndex procedure: select error} {
  436.     .c select clear
  437.     list [catch {.c index test sel.} msg] $msg
  438. } {1 {bad index "sel."}}
  439. test canvText-14.5 {GetTextIndex procedure: bad int or unknown index} {
  440.     list [catch {.c index test xyz} msg] $msg
  441. } {1 {bad index "xyz"}}
  442. test canvText-15.1 {SetTextCursor procedure} {
  443.     .c itemconfig -text "abcdefg"
  444.     .c icursor test 3
  445.     .c index test insert
  446. } {3}
  447. test canvText-16.1 {GetSelText procedure} {
  448.     .c itemconfig test -text "abcdefghijklmno" -anchor nw
  449.     .c select from test 5
  450.     .c select to test 8
  451.     selection get
  452. } {fghi}
  453. set font {Courier 12 italic}
  454. set ax [font measure $font 0]
  455. set ay [font metrics $font -linespace]
  456. test canvText-17.1 {TextToPostscript procedure} {
  457.     .c delete all
  458.     .c config -height 300 -highlightthickness 0 -bd 0
  459.     update
  460.     .c create text 100 100 -tags test 
  461.     .c itemconfig test -font $font -text "00000000" -width [expr 3*$ax]
  462.     .c itemconfig test -anchor n -fill black
  463.     set x [.c postscript]
  464.     set x [string range $x [string first "/Courier-Oblique" $x] end]
  465. } "/Courier-Oblique findfont [font actual $font -size] scalefont ISOEncode setfont
  466. 0.000 0.000 0.000 setrgbcolor AdjustColor
  467. 100 200 [
  468. [(000)]
  469. [(000)]
  470. [(00)]
  471. ] $ay -0.5 0 0 false DrawText
  472. grestore
  473. restore showpage
  474. %%Trailer
  475. end
  476. %%EOF
  477. "
  478. test canvText-18.1 {bug fix 2525, find enclosed on text with newlines} {
  479.     catch {destroy .c}
  480.     canvas .c
  481.     pack .c
  482.     .c delete all
  483.     .c create text 100 100 -text Hellon -anchor nw
  484.     set bbox [.c bbox 1]
  485.     set x2 [lindex $bbox 2]
  486.     set y2 [lindex $bbox 3]
  487.     incr y2
  488.     update
  489.     .c find enclosed 99 99 [expr $x2 + $i] [expr $y2 + 1]
  490. } 1
  491. test canvText-19.1 {patch 1006286, leading space caused wrap under Win32} {
  492.     catch {destroy .c}
  493.     set c [canvas .c -bg black -width 964]
  494.     pack $c
  495.     $c delete all
  496.     after 1000 "set done 1" ; vwait done
  497.     set f {Arial 28 bold}
  498.     set s1 {    Yeah-ah-ah-ah-oh-oh-oh-oh-oh-oh-oh-oh-oh-oh-oh-oh-oh-oh-oh-oh-Yow}
  499.     set s2 {    Yeah ah ah ah oh oh oh oh oh oh oh oh oh oh oh oh oh oh oh oh Yow}
  500.     $c create text 21 18 
  501.         -font $f 
  502.         -text $s1 
  503.         -fill white 
  504.         -width 922 
  505.         -anchor nw 
  506.         -tags tbox1
  507.     eval {$c create rect} [$c bbox tbox1] -outline red
  508.     $c create text 21 160 
  509.         -font $f 
  510.         -text $s2 
  511.         -fill white 
  512.         -width 922 
  513.         -anchor nw 
  514.         -tags tbox2
  515.     eval {$c create rect} [$c bbox tbox2] -outline red
  516.     after 1000 "set done 1" ; vwait done
  517.     set results [list]
  518.     $c select from tbox2 4
  519.     $c select to tbox2 8
  520.     lappend results [selection get]
  521.     $c select from tbox1 4
  522.     $c select to tbox1 8
  523.     lappend results [selection get]
  524.     array set metrics [font metrics $f]
  525.     set x [expr {21 + [font measure $f "    "] 
  526.         + ([font measure {Arial 28 bold} "Y"] / 2)}]
  527.     set y1 [expr {18 + ($metrics(-linespace) / 2)}]
  528.     set y2 [expr {160 + ($metrics(-linespace) / 2)}]
  529.     
  530.     lappend results [$c index tbox1 @$x,$y1]
  531.     lappend results [$c index tbox2 @$x,$y2]
  532.     set results
  533. } {{Yeah } Yeah- 4 4}
  534. # cleanup
  535. ::tcltest::cleanupTests
  536. return