Add libwx-perl
[pkg-perl] / deb-src / libwx-perl / libwx-perl-0.96 / t / 04_userdata.t
1 #!/usr/bin/perl -w
2
3 # test for client data handling in various controls
4
5 use strict;
6 use Wx;
7 use lib './t';
8 use Tests_Helper qw(in_frame);
9 use Wx::Event qw(EVT_BUTTON);
10
11 package MyClass;
12
13 sub new {
14   my $class = shift;
15   my $code = shift;
16   die "want a CODE reference" unless ref $code eq 'CODE';
17
18   return bless [ $code ], $class;
19 }
20
21 sub DESTROY { &{$_[0][0]} }
22
23 package main;
24
25 use Test::More 'tests' => 53;
26
27 use strict;
28 #use base 'Wx::Frame';
29 use vars '$TODO';
30
31 sub tdata($) { Wx::TreeItemData->new( MyClass->new( $_[0] ) ) }
32 sub cdata($) { MyClass->new( $_[0] ) }
33
34 sub tests {
35   my $this = shift;
36
37   ############################################################################
38   # wxTreeCtrl
39   ############################################################################
40
41   my $tree = Wx::TreeCtrl->new( $this, -1 );
42   my $root = $tree->AddRoot( 'Root', -1, -1,
43                              Wx::TreeItemData->new( 'Frobnicate' ) );
44
45   my $trdata = $tree->GetItemData( $root );
46   my $data = $trdata->GetData();
47   is( $data, 'Frobnicate', "Wx::TreeItemData::GetData" );
48   $data = $trdata->GetData();
49
50   is( $data, 'Frobnicate', "Wx::TreeItemData::GetData (again)" );
51   $data = $tree->GetPlData( $root );
52   is( $data, 'Frobnicate', "Wx::TreeCtrl::GetPlData" );
53
54   $trdata = $tree->GetItemData( $root );
55   $trdata->SetData( 'Baz' );
56   $trdata = $tree->GetItemData( $root );
57   $data = $trdata->GetData();
58   is( $data, 'Baz', "Wx::TreeItemData::SetData" );
59   $tree->SetItemData( $root, Wx::TreeItemData->new( 'Boo' ) );
60   $data = $tree->GetPlData( $root );
61   is( $data, 'Boo', "Wx::TreeCtrl::SetItemData" );
62   $tree->SetPlData( $root, 'XyZ' );
63   $data = $tree->GetPlData( $root );
64   is( $data, 'XyZ', "Wx::TreeCtrl::SetPlData" );
65
66   # test deleting and setting again
67   my( $deleting, $setting, $ctrldelete ) = ( 0, 0, 0 );
68
69   my $item1 = $tree->AppendItem( $root, 'An item', -1, -1,
70                                  tdata sub { $deleting = 1 } );
71   my $item2 = $tree->AppendItem( $root, 'An item', -1, -1,
72                                  tdata sub { $setting = 1 } );
73   my $item3 = $tree->AppendItem( $root, 'An item', -1, -1,
74                                  tdata sub { $ctrldelete = 1 } );
75
76   $tree->Delete( $item1 );
77   ok( $deleting, 'WxTreeCtrl: deleting an item deletes the data' );
78   $tree->SetItemData( $item2, Wx::TreeItemData->new( 'foo' ) );
79   ok( $setting, 'Wx::TreeCtrl: setting again item data deletes old data' );
80   # and hope the tree is deleted NOW
81   $tree->Destroy;
82   ok( $ctrldelete, 'Wx::TreeCtrl: deleting the tree deletes the data' );
83
84   ############################################################################
85   # wxListBox & co.
86   ############################################################################
87
88   my $list = Wx::ListBox->new( $this, -1 );
89   my $combo = Wx::ComboBox->new( $this, -1, 'foo' );
90   my $choice = Wx::Choice->new( $this, -1 );
91   my $checklist = Wx::CheckListBox->new( $this, -1, [-1, -1], [-1, -1], [1] );
92   my $odncombo = undef;
93
94   if( defined &Wx::PlOwnerDrawnComboBox::new ) {
95       $odncombo = Wx::PlOwnerDrawnComboBox->new( $this, -1, 'foo', [-1, -1],
96                                                  [-1, -1], [] );
97   }
98
99   # test deleting and setting again
100   for my $x ( [ $list, 'Wx::ListBox' ],
101               [ $choice, 'Wx::Choice' ],
102               [ $combo, 'Wx::ComboBox' ],
103               [ $checklist, 'Wx::CheckListBox' ],
104               [ $odncombo, 'Wx::OwnerDrawnComboBox' ],
105               ) {
106   SKIP: {
107       my( $list, $name ) = @$x;
108       ( $deleting, $setting, $ctrldelete ) = ( 0, 0, 0 );
109
110       skip( $x->[1] . ": not available", 8 )
111         if !defined $x->[0];
112       skip( "wxMSW wxCheckListBox can't store client data yet", 8 )
113         if Wx::wxMSW && $name eq 'Wx::CheckListBox';
114
115       $list->Clear;
116
117       # diag "starting tests for $name";
118       my $data = 'Foo';
119
120       $list->Append( 'An item', $data );
121       $list->SetClientData( 0, $data ); # workaround bug in HEAD
122       $list->Append( 'An item' );
123
124       $data = 'Frobnication';
125
126       is( $list->GetClientData( 0 ), 'Foo', "$name: some client data" );
127       is( $list->GetClientData( 1 ), undef, "$name: no client data" );
128       $list->SetClientData( 0, 'Bar' );
129       $list->SetClientData( 1, 'Baz' );
130       is( $list->GetClientData( 0 ), 'Bar', "$name: setting client data" );
131       is( $list->GetClientData( 1 ), 'Baz',
132           "$name: setting client data (again)" );
133
134       my $x = 1;
135       $list->SetClientData( 0, \$x );
136       $x = 2;
137       is( ${$list->GetClientData( 0 )}, 2,
138           "$name: client data is a reference" );
139
140       $list->Append( 'An item', cdata sub { $setting = 1 } );
141       $list->Append( 'An item', cdata sub { $ctrldelete = 1 } );
142       $list->Append( 'An item', cdata sub { $deleting = 1 } );
143
144       SKIP: {
145         skip "delayed on Mac", 1 if Wx::wxMAC && $list->isa( 'Wx::ListBox' );
146         $list->Delete( 4 );
147         ok( $deleting, "$name: deleting an item deletes the data" );
148       }
149       $list->SetClientData( 2, 'foo' );
150       ok( $setting, "$name: setting again item data deletes old data" );
151       SKIP: {
152         # and hope the control is deleted NOW
153         $list->Destroy;
154         skip "delayed on Mac/MSW", 1 if ( Wx::wxMAC || Wx::wxMSW ) && $list->isa( 'Wx::ListBox' );
155         ok( $ctrldelete, "$name: deleting the control deletes the data" );
156     }
157     }
158   }
159
160   ############################################################################
161   # wxListCtrl
162   ############################################################################
163
164   my $listctrl = Wx::ListCtrl->new( $this, -1, [-1, -1], [-1, -1],
165                                     Wx::wxLC_REPORT() );
166   $listctrl->InsertColumn( 1, "Type" );
167
168   $listctrl->InsertStringItem( 0, 'text0' );
169   $listctrl->InsertStringItem( 1, 'text1' );
170   $listctrl->InsertStringItem( 2, 'text2' );
171
172   $listctrl->SetItemData( 0, 123 );
173   $listctrl->SetItemData( 1, 456 );
174   $listctrl->SetItemData( 2, 789 );
175
176   is( $listctrl->GetItemData( 0 ), 123, "Wx::ListCtrl first item data" );
177   is( $listctrl->GetItemData( 1 ), 456, "Wx::ListCtrl second item data" );
178   is( $listctrl->GetItemData( 2 ), 789, "Wx::ListCtrl third item data" );
179
180   $listctrl->SetItemData( 1, 135 );
181
182   is( $listctrl->GetItemData( 1 ), 135, "Wx::ListCtrl, changing item data" );
183 }
184
185 in_frame( \&tests );
186
187 # local variables:
188 # mode: cperl
189 # end:
190