#
# item.test --
#
#     This file contains some automated tests to exercise code in
#     c3ditem.c. This includes the [$win itemconfigure] and [$win itemcget]
#     commands.
#

package require tcltest 2.2
namespace import -force ::tcltest::*

package require Tk
package require Canvas3d

set ::COORDS {1.0 1.0 1.0}
set ::PCOORDS {0.0 0.0 0.0 1.0 1.0 1.0 2.0 2.0 2.0}

canvas3d .win

set ::light1 [.win create light $::COORDS]
set ::light2 [.win create light $::COORDS]
set ::polygon1 [.win create polygon $::PCOORDS]

#---------------------------------------------------------------------------
# These tests test the simplest form of [$win itemconfigure] where a single
# option is being set, and that [$win itemcget] can return both default and
# configured values.
#
test canvas3d-item.1.1 {} -body {
    .win itemcget $::light1 -ambient
} -result {#000000}
test canvas3d-item.1.2 {} -body {
    .win itemconfigure $::light1 -ambient blue
} -result {}
test canvas3d-item.1.3 {} -body {
    .win itemcget $::light1 -ambient
} -result {blue}
test canvas3d-item.1.4 {} -body {
    .win itemcget $::light2 -ambient
} -result {#000000}
test canvas3d-item.1.5 {} -body {
    .win itemconfigure $::polygon1 -style outline
} -result {}
test canvas3d-item.1.6 {} -body {
    .win itemcget $::polygon1 -style
} -result {outline}

#---------------------------------------------------------------------------
# These tests test other forms of [$win itemconfigure]
#
test canvas3d-item.2.1 {} -body {
    .win itemconfigure $::light1 -diffuse red -specular green
} -result {}
test canvas3d-item.2.2 {} -body {
    list [.win itemcget $::light1 -diffuse] [.win itemcget $::light1 -specular]
} -result {red green}
test canvas3d-item.2.3 {} -body {
    .win itemconfigure $::light1 -ambient
} -result [list -ambient {} {} #000000 blue]
test canvas3d-item.2.4 {} -body {
    .win itemconfigure $::light1
} -result [list \
  [list -tags {} {} {} {}] \
  [list -hidden {} {} false 0] \
  [list -horizon {} {} false 0] \
  [list -ambient {} {} #000000 blue] \
  [list -diffuse {} {} #FFFFFF red] \
  [list -specular {} {} #FFFFFF green] \
  [list -constantattenuation {} {} 1.0 1.0] \
  [list -linearattenuation {} {} 0.0 0.0] \
  [list -quadraticattenuation {} {} 0.0 0.0] \
  [list -spotexponent {} {} 0.0 0.0] \
  [list -spotcutoff {} {} 180.0 180.0]
]

#---------------------------------------------------------------------------
# Test cases item.3.* test that the "-tags" option interface works.
#
test canvas-item.3.1 {} -body {
  .win delete all
  set ::item1 [.win create polygon $::PCOORDS]
  set ::item2 [.win create polygon $::PCOORDS]
  .win gettags all
} -result {}
test canvas-item.3.2 {} -body {
  .win itemcget $::item1 -tags
} -result {}
test canvas-item.3.3 {} -body {
  .win itemconfigure $::item1 -tags {tag1 tag2 tag3}
} -result {}
test canvas-item.3.4 {} -body {
  lsort [.win itemcget $::item1 -tags]
} -result {tag1 tag2 tag3}
test canvas-item.3.5 {} -body {
  .win find {tag1 && tag2 && tag3}
} -result $::item1

#---------------------------------------------------------------------------
# Test rollback of options with itemconfigure.
#
test canvas-item.4.1 {} -body {
  .win delete all
  set ::item1 [.win create polygon $::PCOORDS]
  set ::item2 [.win create polygon $::PCOORDS]
  set ::item3 [.win create light $::COORDS]
  .win gettags all
} -result {}
test canvas-item.4.2 {} -body {
  .win itemcget $::item1 -color
} -result {white}
test canvas-item.4.3 {} -body {
  set rc [catch {
    .win itemconfigure $::item1 -color red -style hello
  } msg]
  list $rc $msg
} -result {1 {bad style "hello": must be solid or outline}}
test canvas-item.4.4 {} -body {
  .win itemcget $::item1 -color
} -result {white}
test canvas-item.4.5 {} -body {
  .win itemcget $::item1 -style
} -result {solid}
test canvas-item.4.6 {} -body {
  set rc [catch {
    .win itemconfigure "$::item1 || $::item3" -style outline
  } msg]
  list $rc $msg
} -result {1 {unknown option "-style"}}
test canvas-item.4.7 {} -body {
  .win itemcget $::item1 -style
} -result {solid}
test canvas-item.4.8 {} -body {
  .win itemcget $::item3 -horizon
} -result {0}
test canvas-item.4.9 {} -body {
  set rc [catch {
    .win itemconfigure "$::item1 || $::item3" -horizon true
  } msg]
  list $rc $msg
} -result {1 {unknown option "-horizon"}}
test canvas-item.4.10 {} -body {
  .win itemcget $::item3 -horizon
} -result {0}

#---------------------------------------------------------------------------
# Test rollback of -tags option using itemconfigure.
#
test canvas-item.5.1 {} -body {
  .win delete all
  set ::item1 [.win create polygon $::PCOORDS]
  set ::item2 [.win create polygon $::PCOORDS]
  set ::item3 [.win create light $::COORDS]
  .win gettags all
} -result {}
test canvas-item.5.2 {} -body {
  .win itemconfigure "$::item1 || $::item3" -tags {tag1 tag2}
  set rc [catch {
    .win itemconfigure "$::item1 || $::item3" -tags tag3 -style outline
  } msg]
  list $rc $msg
} -result {1 {unknown option "-style"}}
test canvas-item.5.3 {} -body {
  lsort [.win gettags $::item1]
} -result {tag1 tag2}
test canvas-item.5.4 {} -body {
  lsort [.win gettags $::item3]
} -result {tag1 tag2}

#---------------------------------------------------------------------------
# The following test cases exercise the [.win coords] command, as used to
# read and set coordinates of items.
#
set ::C1 [list 0.0 0.0 0.0 1.0 1.0 1.0 2.0 2.0 2.0 3.0 3.0 3.0]
set ::C2 [list 4.0 4.0 4.0 5.0 5.0 5.0 6.0 6.0 6.0 7.0 7.0 7.0]

foreach type [list polygon line 2dline] {
  test canvas-item.5.$type.1 {} -body {
    .win delete all
    set ::item1 [.win create $type $::C1]
    set ::item2 [.win create $type $::C2]
    .win gettags all
  } -result {}

  .win itemconfigure $::item1 -color red -tags {a b c} -hidden 1
  .win itemconfigure $::item2 -color green -tags {d e f}

  set item1config [.win itemconfigure $::item1]
  set item2config [.win itemconfigure $::item2]

  test canvas-item.5.$type.2 {} -body {
    .win coords $::item1
  } -result [list $::C1]
  test canvas-item.5.$type.3 {} -body {
    .win coords $::item1 $::C2
  } -result [list $::C2]
  test canvas-item.5.$type.4 {} -body {
    .win coords $::item1
  } -result [list $::C2]
  test canvas-item.5.$type.5 {} -body {
    .win coords $::item2
  } -result [list $::C2]
  test canvas-item.5.$type.6 {} -body {
    .win coords $::item2 $::C1 $::C2
  } -result [list $::C1 $::C2]
  test canvas-item.5.$type.7 {} -body {
    .win coords $::item2
  } -result [list $::C1 $::C2]

  test canvas-item.5.$type.8 {} -body {
    .win itemconfigure $::item1
  } -result $item1config
  test canvas-item.5.$type.9 {} -body {
    .win itemconfigure $::item2
  } -result $item2config
}

destroy .win

# cleanup
cleanupTests
return
