1use Test::More;
2BEGIN { use_ok( 'UI::Dialog::Backend::CDialog' ); }
3require_ok( 'UI::Dialog::Backend::CDialog' );
4
5#########################
6
7#
8#: First, need to make sure /usr/bin/dialog exists and if the backend
9#: module is even usable.
10#
11
12eval { new UI::Dialog::Backend::CDialog(test_mode=>1); };
13if ( $@ ) {
14  if ($@ =~ m!binary could not be found!) {
15    diag("Tests skipped, backend binary not found.");
16  }
17  else {
18    diag("An unknown error occurred while trying to use backend: ".$@);
19  }
20  done_testing();
21}
22else {
23
24  #: Setup obj in test_mode
25  my $obj = new UI::Dialog::Backend::CDialog( test_mode => 1 );
26  isa_ok( $obj, 'UI::Dialog::Backend::CDialog' );
27
28  #: Check for all the standard UI::Dialog backend methods
29  my @methods = qw( new state ra rs rv nautilus xosd beep clear
30                    yesno msgbox inputbox password textbox menu
31                    checklist radiolist fselect dselect );
32  can_ok( 'UI::Dialog::Backend::CDialog', @methods );
33
34  # Track $bin is needed to validate the command line constructs
35  my $bin = $obj->get_bin();
36
37  #
38  #: Test the standard dialog widgets
39  #
40
41  #: Test the yes/no prompt
42  $obj->yesno( title=>"TITLE", backtitle => "BACKTITLE", text => "TEXT",
43               width => 64, height => 16 );
44  is( $obj->get_unit_test_result(),
45      $bin.' --title TITLE --backtitle BACKTITLE --colors --cr-wrap --yesno TEXT 16 64'
46    );
47
48  #: Test the message box
49  $obj->msgbox( title=>"TITLE", backtitle => "BACKTITLE", text => "TEXT",
50                width => 64, height => 16 );
51  is( $obj->get_unit_test_result(),
52      $bin.' --title TITLE --backtitle BACKTITLE --colors --cr-wrap --msgbox TEXT 16 64'
53    );
54
55  #: Test the info box
56  $obj->infobox( title=>"TITLE", backtitle => "BACKTITLE", text => "TEXT",
57                 width => 64, height => 16 );
58  is( $obj->get_unit_test_result(),
59      $bin.' --title TITLE --backtitle BACKTITLE --colors --cr-wrap --infobox TEXT 16 64'
60    );
61
62  #: Test the input box
63  $obj->inputbox( title=>"TITLE", backtitle => "BACKTITLE", text => "TEXT",
64                  width => 64, height => 16, entry => ENTRY );
65  is( $obj->get_unit_test_result(),
66      $bin.' --title TITLE --backtitle BACKTITLE --colors --cr-wrap --inputbox TEXT 16 64 ENTRY'
67    );
68
69  #: Test the password box
70  $obj->password( title=>"TITLE", backtitle => "BACKTITLE", text => "TEXT",
71                  width => 64, height => 16, entry => "ENTRY" );
72  is( $obj->get_unit_test_result(),
73      $bin.' --title TITLE --backtitle BACKTITLE --colors --cr-wrap --insecure --passwordbox TEXT 16 64 ENTRY'
74    );
75
76  #: Test the password box
77  $obj->password( title=>"TITLE", backtitle => "BACKTITLE", text => "TEXT",
78                  width => 64, height => 16 );
79  is( $obj->get_unit_test_result(),
80      $bin.' --title TITLE --backtitle BACKTITLE --colors --cr-wrap --passwordbox TEXT 16 64 \'\''
81    );
82
83  #: Test the text file box
84  $obj->textbox( title=>"TITLE", backtitle => "BACKTITLE", path => "$0",
85                 width => 64, height => 16 );
86  is( $obj->get_unit_test_result(),
87      $bin.' --title TITLE --backtitle BACKTITLE --colors --cr-wrap --scrolltext --textbox t/UI-Dialog-Backend-CDialog.t 16 64'
88    );
89
90  #: Test the menu prompt
91  $obj->menu( title=>"TITLE", backtitle => "BACKTITLE", text => "TEXT",
92              width => 64, height => 16,
93              list => [ "tag0", "item0", "tag1", "item1" ] );
94  is( $obj->get_unit_test_result(),
95      $bin.' --title TITLE --backtitle BACKTITLE --colors --cr-wrap --menu TEXT 16 64 5  tag0 item0 tag1 item1'
96    );
97
98  #: Test the checklist
99  $obj->checklist( title=>"TITLE", backtitle => "BACKTITLE", text => "TEXT",
100                   width => 64, height => 16,
101                   list => [ "tag0", [ "item0", 0 ], "tag1", [ "item1", 1 ] ] );
102  is( $obj->get_unit_test_result(),
103      $bin.' --title TITLE --backtitle BACKTITLE --colors --cr-wrap --separate-output --checklist TEXT 16 64 5  tag0 item0 off tag1 item1 on'
104    );
105
106  #: Test the radiolist
107  $obj->radiolist( title=>"TITLE", backtitle => "BACKTITLE", text => "TEXT",
108                   width => 64, height => 16,
109                   list => [ "tag0", [ "item0", 0 ], "tag1", [ "item1", 1 ] ] );
110  is( $obj->get_unit_test_result(),
111      $bin.' --title TITLE --backtitle BACKTITLE --colors --cr-wrap --radiolist TEXT 16 64 5  tag0 item0 off tag1 item1 on'
112    );
113
114
115  #
116  #: Test the non-standard but supported CDialog widgets
117  #
118
119  #: Test the file selector
120  $obj->fselect( title=>"TITLE", backtitle => "BACKTITLE", path => "$0",
121                 width => 64, height => 16 );
122  is( $obj->get_unit_test_result(),
123      $bin.' --title TITLE --backtitle BACKTITLE --colors --cr-wrap --fselect t/UI-Dialog-Backend-CDialog.t 16 64'
124    );
125
126  #: Test the calendar
127  my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
128  $mon += 1;
129  $year += 1900;
130  $obj->calendar( title=>"TITLE", backtitle => "BACKTITLE", text => "TEXT",
131                  width => 64, height => 4,
132                  day => $mday, month => $mon, year => $year
133                );
134  is( $obj->get_unit_test_result(),
135      $bin.' --title TITLE --backtitle BACKTITLE --colors --cr-wrap --calendar TEXT 5 64 '.$mday.' '.$mon.' '.$year
136    );
137
138  #: Test the time box
139  $obj->timebox( title=>"TITLE", backtitle => "BACKTITLE", text => "TEXT",
140                 width => 64, height => 4,
141                 hour => 16, minute => 20, second => 42
142               );
143  is( $obj->get_unit_test_result(),
144      $bin.' --title TITLE --backtitle BACKTITLE --colors --cr-wrap --timebox TEXT 4 64 16 20 42'
145    );
146
147  #: Test the tail box
148  $obj->tailbox( title=>"TITLE", backtitle => "BACKTITLE", text => "TEXT",
149                 width => 64, height => 16, path => $0
150               );
151  is( $obj->get_unit_test_result(),
152      $bin.' --title TITLE --backtitle BACKTITLE --colors --cr-wrap --tailbox '.$0.' 16 64'
153    );
154
155  #: Test the tail box bg
156  $obj->tailboxbg( title=>"TITLE", backtitle => "BACKTITLE", text => "TEXT",
157                   width => 64, height => 16, path => $0
158                 );
159  is( $obj->get_unit_test_result(),
160      $bin.' --title TITLE --backtitle BACKTITLE --colors --cr-wrap --tailboxbg '.$0.' 16 64'
161    );
162
163  #: Test the form
164  $obj->form
165    ( title=>"TITLE", backtitle => "BACKTITLE", text => "TEXT",
166      width => 64, height => 16,
167      list =>
168      [
169       [ "label0", 1, 1], ["item0", 1, 10, 10, 10 ],
170       [ "label1", 2, 1], ["item1", 2, 10, 10, 10 ],
171      ]
172    );
173  is( $obj->get_unit_test_result(),
174      $bin.' --title TITLE --backtitle BACKTITLE --colors --cr-wrap --form TEXT 16 64 5  "label0" "1" "1" "item0" "1" "10" "10" "10" "label1" "2" "1" "item1" "2" "10" "10" "10"'
175    );
176
177  #: Note that the guage/progress widget is not unit-testable at this time.
178  #: This is due to the fact that the gauge_start() method opens a background
179  #: command pipe so that IPC can happen to update the text and progress of
180  #: the widget.
181
182  #
183  #: Now test the trust-input feature for the CDialog backend.
184  #
185
186  $obj->msgbox( title=>'TITLE: `head -1 '.$0.'`',
187                backtitle => 'BACKTITLE: `head -1 '.$0.'`',
188                text => 'TEXT: $(head -1 '.$0.')',
189                width => 64, height => 16 );
190  is( $obj->get_unit_test_result(),
191      $bin.q| --title 'TITLE: `head -1 t/UI-Dialog-Backend-CDialog.t`' --backtitle 'BACKTITLE: `head -1 t/UI-Dialog-Backend-CDialog.t`' --colors --cr-wrap --msgbox 'TEXT: $(head -1 t/UI-Dialog-Backend-CDialog.t)' 16 64|
192    );
193
194  $obj->msgbox( title=>'TITLE: `head -1 '.$0.'`',
195                backtitle => 'BACKTITLE: `head -1 '.$0.'`',
196                text => 'TEXT: $(head -1 '.$0.')',
197                'trust-input' => 1,
198                width => 64, height => 16 );
199  is( $obj->get_unit_test_result(),
200      $bin.q| --title "TITLE: `head -1 t/UI-Dialog-Backend-CDialog.t`" --backtitle "BACKTITLE: `head -1 t/UI-Dialog-Backend-CDialog.t`" --colors --cr-wrap --msgbox "TEXT: $(head -1 t/UI-Dialog-Backend-CDialog.t)" "16" "64"|
201    );
202
203  #
204  #: Testing completed
205  #
206
207  done_testing();
208}
209