1#!/usr/bin/perl -w
2use strict;
3
4$ENV{'LANG'} = 'C';
5
6my $gphoto2 = "gphoto2";
7
8my $debug = 0;
9
10##################################################
11
12# auto configured
13my $imageformat;
14my $havecapture = 0;
15my $havetriggercapture = 0;
16my $havepreview = 0;
17my $havecapturetarget = 0;
18my %formats = ();
19
20# internal
21
22my $debugcnt = 0;
23
24my @lastresult = ();
25# returns TRUE on success, FALSE on fail
26sub run_gphoto2(@) {
27	my @cmdline = @_;
28
29	if ($debug) {
30		push @cmdline,"--debug","--debug-logfile=logfile.debug.$debugcnt";
31		$debugcnt++;
32	}
33
34	@lastresult = ();
35	print STDERR "running: " . join(" ",$gphoto2,@ARGV,@cmdline) . "\n";
36
37	print LOGFILE "running: " . join(" ",$gphoto2,@ARGV,@cmdline) . "\n";
38	open(GPHOTO,join(" ",$gphoto2,@ARGV,@cmdline)." 2>&1|")||die "open $gphoto2";
39	while (<GPHOTO>) {
40		print;
41		print LOGFILE;
42		chomp;
43		push @lastresult, $_;
44	}
45	my $rc = close(GPHOTO);
46	print LOGFILE "returned: $rc\n";
47	die "camera crashed?" if (grep /No camera found/, @lastresult);
48	die "camera crashed?" if (grep /No such device/, @lastresult);
49	return $rc;
50}
51
52sub ok($$) {
53	my ($testres,$text) = @_;
54
55	if ($testres) {
56		print "$text: SUCCESS\n";
57	} else {
58		print "$text: FAIL\n";
59	}
60}
61
62sub remove_all_files {
63	my @files = <*>;
64
65	foreach my $file (@files) {
66		next if ($file =~ /logfile/);
67		print STDERR "deleting $file\n";
68		print LOGFILE "deleting $file\n";
69		unlink $file;
70	}
71}
72
73sub run_gphoto2_capture_target($$@) {
74	my ($nrimages,$text,@cmd) = @_;
75
76	ok(&run_gphoto2(@cmd),"$text: " . join(" ",@cmd));
77
78	my @files = <*>;
79	if ($nrimages+1 != @files) {
80		my $nrfiles = @files;
81		print STDERR "*** expected $nrimages files, got " . ($nrfiles-1) . "\n";
82		print LOGFILE "*** expected $nrimages files, got " . ($nrfiles-1) . "\n";
83	}
84	&remove_all_files();
85}
86
87sub run_gphoto2_capture($$@) {
88	my ($nrimages,$text,@cmd) = @_;
89	my @newcmd = @cmd;
90
91	if ($havecapturetarget) {
92		my @newcmd = @cmd;
93		unshift @newcmd,"--set-config-index","capturetarget=0";
94		&run_gphoto2_capture_target($nrimages,$text,@newcmd);
95
96		@newcmd = @cmd;
97		unshift @newcmd,"--set-config-index","capturetarget=1";
98		&run_gphoto2_capture_target($nrimages,$text,@newcmd);
99	} else {
100		&run_gphoto2_capture_target($nrimages,$text,@newcmd);
101	}
102}
103
104sub run_gphoto2_capture_formats($$@) {
105	my ($nrimages,$text,@cmd) = @_;
106
107	if (%formats) {
108		my @newcmd = @cmd;
109
110		foreach my $format (sort keys %formats) {
111			print "testing image $format\n";
112			@newcmd = @cmd;
113			unshift @newcmd,"--set-config-index",$formats{$format};
114
115			if ($format eq "jpg") {
116				&run_gphoto2_capture($nrimages,$text,@newcmd);
117			} elsif ($format eq "both") {
118				&run_gphoto2_capture($nrimages*2,$text,@newcmd);
119				unshift @newcmd,"--keep-raw";
120				&run_gphoto2_capture($nrimages,$text,@newcmd);
121			} elsif ($format eq "raw") {
122				&run_gphoto2_capture($nrimages,$text,@newcmd);
123				unshift @newcmd,"--keep-raw";
124				&run_gphoto2_capture(0,$text,@newcmd);
125			}
126		}
127		&run_gphoto2("--set-config-index",$formats{'jpg'});
128	} else {
129		&run_gphoto2_capture($nrimages,$text,@cmd);
130	}
131}
132
133# START
134
135my $workdir = `mktemp -d /tmp/testcamera.XXXXXX`;
136chomp $workdir;
137die "no workdir created" unless -d $workdir;
138chdir ($workdir);
139
140print "Using temporary directory: $workdir\n";
141
142open(LOGFILE,">logfile.testcase")||die;
143
144if (!run_gphoto2("-L")) {
145	print LOGFILE "No camera attached?\n";
146	close(LOGFILE);
147	die "-L does not work, reason see above\n";
148}
149
150# Basics
151ok(run_gphoto2("--auto-detect"),"testing --auto-detect");
152ok(run_gphoto2("-L"),"testing -L");
153ok(run_gphoto2("-l"),"testing -l");
154
155# Autodetect capabilities: capture, preview or trigger capture
156ok(run_gphoto2("-a"),"testing -a");
157my @abilities = @lastresult;
158
159$havecapture = 1        if (grep (/Capture/,@abilities));
160$havetriggercapture = 1 if (grep (/Trigger Capture/,@abilities));
161$havepreview = 1        if (grep (/Preview/,@abilities));
162
163ok(run_gphoto2("--summary"),"testing --summary");
164ok(run_gphoto2("--list-config"),"testing --list-config");
165
166# Autodetect if some config variables are present
167
168ok(run_gphoto2("--list-all-config"),"testing --list-all-config");
169my @allconfig = @lastresult;
170
171$havecapturetarget = 1 if (grep(/capturetarget/,@allconfig));
172
173if (grep(/datetime/,@allconfig)) {
174	ok(run_gphoto2("--get-config","datetime"),"testing --get-config datetime before setting");
175	ok(run_gphoto2("--set-config","datetime=now"),"testing --set-config datetime=now");
176	ok(run_gphoto2("--get-config","datetime"),"testing --get-config datetime after setting");
177}
178if (grep(/artist/,@allconfig)) {
179	ok(run_gphoto2("--get-config","artist"),"testing --get-config artist before setting");
180	my $artist;
181	foreach (@lastresult) {
182		$artist = $1 if (/Current: (.*)/);
183	}
184	$artist = 0 if ($artist eq "(null)");
185	ok(run_gphoto2("--set-config","artist=GPHOTO"),"testing --set-config artist=GPHOTO");
186	ok(run_gphoto2("--get-config","artist"),"testing --get-config artist after setting");
187	# restore artist
188	if ($artist) {
189		ok(run_gphoto2("--set-config","artist=$artist"),"testing --set-config artist=$artist");
190	}
191}
192
193my $inimageformat = 0;
194my $jpgformat;
195my $rawformat;
196my $bothformat;
197foreach (@allconfig) {
198	if (/^Label:/) {
199		#last if ($imageformat);
200		$inimageformat = 0;
201	}
202	if (/^Label: Image Quality 2/) { # Nikon 1
203		$imageformat = "imagequality2";
204		undef $jpgformat;
205		undef $rawformat;
206		undef $bothformat;
207		$inimageformat = 1;
208		next;
209	}
210	if (/^Label: Image Quality/) {	# Nikon
211		last if ($imageformat eq "imagequality2");
212		$imageformat = "imagequality";
213		$inimageformat = 1;
214		next;
215	}
216	if (/^Label: Image Format/) {	# Canon
217		$imageformat = "imageformat";
218		$inimageformat = 1;
219		next;
220	}
221	next unless ($inimageformat);
222
223	# save only 1 RAW + JPEG format, it always has a "+" inside
224	if (/^Choice: (\d*) .*\+.*/) {
225		$bothformat = $1 if (!defined($bothformat));
226		next;
227	}
228	# Take first jpeg format as default
229	if (/^Choice: (\d*) .*JPEG/) {
230		$jpgformat = $1 if (!defined($jpgformat));
231		next;
232	}
233	if (/^Choice: (\d*) .*Fine/) {
234		$jpgformat = $1 if (!defined($jpgformat));
235		next;
236	}
237	if (/^Choice: (\d*) .*(RAW|NEF)/) {
238		$rawformat = $1 if (!defined($rawformat));
239		next;
240	}
241}
242
243if ($imageformat)  {
244	die "no jpgformat found in $imageformat" unless (defined($jpgformat));
245	$formats{'jpg'} = "$imageformat=$jpgformat";
246	print "jpg is $formats{'jpg'}\n";
247	print LOGFILE "jpg is $formats{'jpg'}\n";
248
249	if (defined($bothformat)) {
250		$formats{'both'} = "$imageformat=$bothformat";
251		print LOGFILE "both is $formats{'both'}\n";
252		print "both is $formats{'both'}\n";
253	} else {
254		warn "no bothformat found in $imageformat";
255	}
256
257	if (defined($rawformat)) {
258		$formats{'raw'} = "$imageformat=$rawformat";
259		print LOGFILE "raw is $formats{'raw'}\n";
260		print "raw is $formats{'raw'}\n";
261	} else {
262		warn "no rawformat found in $imageformat"
263	}
264	if (!defined($rawformat) && !defined($bothformat)) {
265		undef %formats;
266	}
267
268} else {
269	print "NO imageformat ... just jpeg?\n";
270}
271
272# Capture stuff lots of it
273
274if ($havecapture) {
275	run_gphoto2_capture_formats(1,"simple capture and download","--capture-image-and-download");
276	run_gphoto2_capture_formats(3,"multiframe capture and download","--capture-image-and-download","-F 3","-I 3");
277	run_gphoto2_capture_formats(1,"waitevent","--wait-event-and-download=10s");
278}
279if ($havetriggercapture) {
280	run_gphoto2_capture_formats(1,"trigger capture","--trigger-capture","--wait-event-and-download=5s");
281}
282if (grep (/eosremoterelease/,@allconfig)) {
283	run_gphoto2_capture(1,"eos remote release","--set-config","eosremoterelease=Immediate","--wait-event=2s","--set-config","eosremoterelease='Release Full'","--wait-event-and-download=5s");
284}
285
286if (grep (/eosremoterelease/,@allconfig)) {
287	run_gphoto2_capture(1,"eos remote release","--set-config","eosremoterelease=Immediate","--wait-event-and-download=2s","--set-config","eosremoterelease='Release Full'","--wait-event-and-download=5s");
288}
289
290if ($havepreview) {
291	ok(run_gphoto2("--capture-preview"),"testing --capture-preview");
292	remove_all_files();
293	ok(run_gphoto2("--capture-preview"),"testing --capture-preview 2nd time");
294	remove_all_files();
295	ok(run_gphoto2("--capture-movie=5s"),"testing --capture-movie=5s");
296	remove_all_files();
297}
298