1###############################################################################
2#
3# A test for Spreadsheet::WriteExcel::Chart.
4#
5# Tests for the set*area() Chart methods.
6#
7# reverse('�'), January 2010, John McNamara, jmcnamara@cpan.org
8#
9
10# prove -I../lib --nocolor -v 63_chart_area_formats.t
11
12use strict;
13
14use Spreadsheet::WriteExcel;
15
16use Test::More tests => 52;
17#use Test::More 'no_plan';
18
19
20###############################################################################
21#
22# Tests setup
23#
24my $test_file = 'temp_test_file.xls';
25my $workbook  = Spreadsheet::WriteExcel->new( $test_file );
26my $chart     = $workbook->add_chart( type => 'column' );
27$chart->{_using_tmpfile} = 0;
28
29my $got_line;
30my $got_area;
31my $expected_line;
32my $expected_area;
33my $caption1 = " \tChart: chartarea format - line";
34my $caption2 = " \tChart: chartarea format - area";
35
36
37###############################################################################
38#
39# 1. Test the chartarea format methods. See the set_*area() properties below.
40#
41reset_chart( $chart );
42
43$chart->set_chartarea(
44    color        => 'red',
45    line_color   => 'black',
46    line_pattern => 2,
47    line_weight  => 3,
48);
49
50$expected_line = join ' ', qw(
51  07 10 0C 00 00 00 00 00 01 00 01 00 00 00 08 00
52);
53
54$expected_area = join ' ', qw(
55  0A 10 10 00 FF 00 00 00 00 00 00 00 01 00 00 00
56  0A 00 08 00
57);
58
59( $got_line, $got_area ) = get_chartarea_formats( $chart );
60
61is( $got_line, $expected_line, $caption1 );
62is( $got_area, $expected_area, $caption2 );
63
64
65###############################################################################
66#
67# 3. Test the chartarea format methods. See the set_*area() properties below.
68#
69reset_chart( $chart );
70
71$chart->set_chartarea( color => 'red', );
72
73$expected_line = join ' ', qw(
74  07 10 0C 00 00 00 00 00 05 00 FF FF 08 00 4D 00
75);
76
77$expected_area = join ' ', qw(
78  0A 10 10 00 FF 00 00 00 00 00 00 00 01 00 00 00
79  0A 00 08 00
80);
81
82( $got_line, $got_area ) = get_chartarea_formats( $chart );
83
84is( $got_line, $expected_line, $caption1 );
85is( $got_area, $expected_area, $caption2 );
86
87
88###############################################################################
89#
90# 5. Test the chartarea format methods. See the set_*area() properties below.
91#
92reset_chart( $chart );
93
94$chart->set_chartarea( line_color => 'red', );
95
96$expected_line = join ' ', qw(
97  07 10 0C 00 FF 00 00 00 00 00 FF FF 00 00 0A 00
98);
99
100$expected_area = join ' ', qw(
101  0A 10 10 00 FF FF FF 00 00 00 00 00 00 00 00 00
102  4E 00 4D 00
103);
104
105( $got_line, $got_area ) = get_chartarea_formats( $chart );
106
107is( $got_line, $expected_line, $caption1 );
108is( $got_area, $expected_area, $caption2 );
109
110
111###############################################################################
112#
113# 7. Test the chartarea format methods. See the set_*area() properties below.
114#
115reset_chart( $chart );
116
117$chart->set_chartarea( line_pattern => 2, );
118
119$expected_line = join ' ', qw(
120  07 10 0C 00 00 00 00 00 01 00 FF FF 00 00 4F 00
121);
122
123$expected_area = join ' ', qw(
124  0A 10 10 00 FF FF FF 00 00 00 00 00 00 00 00 00
125  4E 00 4D 00
126);
127
128( $got_line, $got_area ) = get_chartarea_formats( $chart );
129
130is( $got_line, $expected_line, $caption1 );
131is( $got_area, $expected_area, $caption2 );
132
133
134###############################################################################
135#
136# 9. Test the chartarea format methods. See the set_*area() properties below.
137#
138reset_chart( $chart );
139
140$chart->set_chartarea( line_weight => 3, );
141
142$expected_line = join ' ', qw(
143  07 10 0C 00 00 00 00 00 00 00 01 00 00 00 4F 00
144);
145
146$expected_area = join ' ', qw(
147  0A 10 10 00 FF FF FF 00 00 00 00 00 00 00 00 00
148  4E 00 4D 00
149);
150
151( $got_line, $got_area ) = get_chartarea_formats( $chart );
152
153is( $got_line, $expected_line, $caption1 );
154is( $got_area, $expected_area, $caption2 );
155
156
157###############################################################################
158#
159# 11. Test the chartarea format methods. See the set_*area() properties below.
160#
161reset_chart( $chart );
162
163$chart->set_chartarea(
164    color      => 'red',
165    line_color => 'black',
166);
167
168$expected_line = join ' ', qw(
169  07 10 0C 00 00 00 00 00 00 00 FF FF 00 00 08 00
170);
171
172$expected_area = join ' ', qw(
173  0A 10 10 00 FF 00 00 00 00 00 00 00 01 00 00 00
174  0A 00 08 00
175);
176
177( $got_line, $got_area ) = get_chartarea_formats( $chart );
178
179is( $got_line, $expected_line, $caption1 );
180is( $got_area, $expected_area, $caption2 );
181
182
183###############################################################################
184#
185# 13. Test the chartarea format methods. See the set_*area() properties below.
186#
187reset_chart( $chart );
188
189$chart->set_chartarea(
190    color        => 'red',
191    line_pattern => 2,
192);
193
194$expected_line = join ' ', qw(
195  07 10 0C 00 00 00 00 00 01 00 FF FF 00 00 4F 00
196);
197
198$expected_area = join ' ', qw(
199  0A 10 10 00 FF 00 00 00 00 00 00 00 01 00 00 00
200  0A 00 08 00
201);
202
203( $got_line, $got_area ) = get_chartarea_formats( $chart );
204
205is( $got_line, $expected_line, $caption1 );
206is( $got_area, $expected_area, $caption2 );
207
208
209###############################################################################
210#
211# 15. Test the chartarea format methods. See the set_*area() properties below.
212#
213reset_chart( $chart );
214
215$chart->set_chartarea(
216    color       => 'red',
217    line_weight => 3,
218);
219
220$expected_line = join ' ', qw(
221  07 10 0C 00 00 00 00 00 00 00 01 00 00 00 4F 00
222);
223
224$expected_area = join ' ', qw(
225  0A 10 10 00 FF 00 00 00 00 00 00 00 01 00 00 00
226  0A 00 08 00
227);
228
229( $got_line, $got_area ) = get_chartarea_formats( $chart );
230
231is( $got_line, $expected_line, $caption1 );
232is( $got_area, $expected_area, $caption2 );
233
234
235###############################################################################
236#
237# Set some variables for embedded chart tests.
238#
239$caption1           = " \tChart: embedded chartarea format - line";
240$caption2           = " \tChart: embedded chartarea format - area";
241$chart->{_embedded} = 1;
242
243
244###############################################################################
245#
246# 17. Test the chartarea format methods. See the set_*area() properties below.
247#
248reset_chart( $chart, 1 );
249
250$chart->set_chartarea();
251
252$expected_line = join ' ', qw(
253  07 10 0C 00 00 00 00 00 00 00 00 00 09 00 4D 00
254);
255
256$expected_area = join ' ', qw(
257  0A 10 10 00 FF FF FF 00 00 00 00 00 01 00 01 00
258  4E 00 4D 00
259);
260
261( $got_line, $got_area ) = get_chartarea_formats( $chart );
262
263is( $got_line, $expected_line, $caption1 );
264is( $got_area, $expected_area, $caption2 );
265
266
267###############################################################################
268#
269# 19. Test the chartarea format methods. See the set_*area() properties below.
270#
271reset_chart( $chart, 1 );
272
273$chart->set_chartarea(
274    color        => 'red',
275    line_color   => 'black',
276    line_pattern => 2,
277    line_weight  => 3,
278);
279
280$expected_line = join ' ', qw(
281  07 10 0C 00 00 00 00 00 01 00 01 00 00 00 08 00
282);
283
284$expected_area = join ' ', qw(
285  0A 10 10 00 FF 00 00 00 00 00 00 00 01 00 00 00
286  0A 00 08 00
287);
288
289( $got_line, $got_area ) = get_chartarea_formats( $chart );
290
291is( $got_line, $expected_line, $caption1 );
292is( $got_area, $expected_area, $caption2 );
293
294
295###############################################################################
296#
297# 21. Test the chartarea format methods. See the set_*area() properties below.
298#
299reset_chart( $chart, 1 );
300
301$chart->set_chartarea( color => 'red', );
302
303$expected_line = join ' ', qw(
304  07 10 0C 00 00 00 00 00 00 00 FF FF 09 00 4D 00
305);
306
307$expected_area = join ' ', qw(
308  0A 10 10 00 FF 00 00 00 00 00 00 00 01 00 00 00
309  0A 00 08 00
310);
311
312( $got_line, $got_area ) = get_chartarea_formats( $chart );
313
314is( $got_line, $expected_line, $caption1 );
315is( $got_area, $expected_area, $caption2 );
316
317
318###############################################################################
319#
320# 23. Test the chartarea format methods. See the set_*area() properties below.
321#
322reset_chart( $chart, 1 );
323
324$chart->set_chartarea( line_color => 'red', );
325
326$expected_line = join ' ', qw(
327  07 10 0C 00 FF 00 00 00 00 00 FF FF 00 00 0A 00
328);
329
330$expected_area = join ' ', qw(
331  0A 10 10 00 FF FF FF 00 00 00 00 00 01 00 01 00
332  4E 00 4D 00
333);
334
335( $got_line, $got_area ) = get_chartarea_formats( $chart );
336
337is( $got_line, $expected_line, $caption1 );
338is( $got_area, $expected_area, $caption2 );
339
340
341###############################################################################
342#
343# 25. Test the chartarea format methods. See the set_*area() properties below.
344#
345reset_chart( $chart, 1 );
346
347$chart->set_chartarea( line_pattern => 2, );
348
349$expected_line = join ' ', qw(
350  07 10 0C 00 00 00 00 00 01 00 FF FF 00 00 4F 00
351);
352
353$expected_area = join ' ', qw(
354  0A 10 10 00 FF FF FF 00 00 00 00 00 01 00 01 00
355  4E 00 4D 00
356);
357
358( $got_line, $got_area ) = get_chartarea_formats( $chart );
359
360is( $got_line, $expected_line, $caption1 );
361is( $got_area, $expected_area, $caption2 );
362
363
364###############################################################################
365#
366# 27. Test the chartarea format methods. See the set_*area() properties below.
367#
368reset_chart( $chart, 1 );
369
370$chart->set_chartarea( line_weight => 3, );
371
372$expected_line = join ' ', qw(
373  07 10 0C 00 00 00 00 00 00 00 01 00 00 00 4F 00
374);
375
376$expected_area = join ' ', qw(
377  0A 10 10 00 FF FF FF 00 00 00 00 00 01 00 01 00
378  4E 00 4D 00
379);
380
381( $got_line, $got_area ) = get_chartarea_formats( $chart );
382
383is( $got_line, $expected_line, $caption1 );
384is( $got_area, $expected_area, $caption2 );
385
386
387###############################################################################
388#
389# 29. Test the chartarea format methods. See the set_*area() properties below.
390#
391reset_chart( $chart, 1 );
392
393$chart->set_chartarea(
394    color      => 'red',
395    line_color => 'black',
396);
397
398$expected_line = join ' ', qw(
399  07 10 0C 00 00 00 00 00 00 00 FF FF 00 00 08 00
400);
401
402$expected_area = join ' ', qw(
403  0A 10 10 00 FF 00 00 00 00 00 00 00 01 00 00 00
404  0A 00 08 00
405);
406
407( $got_line, $got_area ) = get_chartarea_formats( $chart );
408
409is( $got_line, $expected_line, $caption1 );
410is( $got_area, $expected_area, $caption2 );
411
412
413###############################################################################
414#
415# 31. Test the chartarea format methods. See the set_*area() properties below.
416#
417reset_chart( $chart, 1 );
418
419$chart->set_chartarea(
420    color        => 'red',
421    line_pattern => 2,
422);
423
424$expected_line = join ' ', qw(
425  07 10 0C 00 00 00 00 00 01 00 FF FF 00 00 4F 00
426);
427
428$expected_area = join ' ', qw(
429  0A 10 10 00 FF 00 00 00 00 00 00 00 01 00 00 00
430  0A 00 08 00
431);
432
433( $got_line, $got_area ) = get_chartarea_formats( $chart );
434
435is( $got_line, $expected_line, $caption1 );
436is( $got_area, $expected_area, $caption2 );
437
438
439###############################################################################
440#
441# 33. Test the chartarea format methods. See the set_*area() properties below.
442#
443reset_chart( $chart, 1 );
444
445$chart->set_chartarea(
446    color       => 'red',
447    line_weight => 3,
448);
449
450$expected_line = join ' ', qw(
451  07 10 0C 00 00 00 00 00 00 00 01 00 00 00 4F 00
452);
453
454$expected_area = join ' ', qw(
455  0A 10 10 00 FF 00 00 00 00 00 00 00 01 00 00 00
456  0A 00 08 00
457);
458
459( $got_line, $got_area ) = get_chartarea_formats( $chart );
460
461is( $got_line, $expected_line, $caption1 );
462is( $got_area, $expected_area, $caption2 );
463
464
465###############################################################################
466#
467# Set some variables for embedded chart tests.
468#
469$caption1           = " \tChart: plotarea format - line";
470$caption2           = " \tChart: plotarea format - area";
471$chart->{_embedded} = 0;
472
473
474###############################################################################
475#
476# 35. Test the plotarea format methods. See the set_*area() properties below.
477#
478reset_chart( $chart );
479
480$chart->set_plotarea();
481
482$expected_line = join ' ', qw(
483  07 10 0C 00 80 80 80 00 00 00 00 00 00 00 17 00
484);
485
486$expected_area = join ' ', qw(
487  0A 10 10 00 C0 C0 C0 00 00 00 00 00 01 00 00 00
488  16 00 4F 00
489);
490
491( $got_line, $got_area ) = get_plotarea_formats( $chart );
492
493is( $got_line, $expected_line, $caption1 );
494is( $got_area, $expected_area, $caption2 );
495
496
497###############################################################################
498#
499# 37. Test the plotarea format methods. See the set_*area() properties below.
500#
501reset_chart( $chart );
502
503$chart->set_plotarea(
504    color        => 'red',
505    line_color   => 'black',
506    line_pattern => 2,
507    line_weight  => 3,
508);
509
510$expected_line = join ' ', qw(
511  07 10 0C 00 00 00 00 00 01 00 01 00 00 00 08 00
512);
513
514$expected_area = join ' ', qw(
515  0A 10 10 00 FF 00 00 00 00 00 00 00 01 00 00 00
516  0A 00 08 00
517);
518
519( $got_line, $got_area ) = get_plotarea_formats( $chart );
520
521is( $got_line, $expected_line, $caption1 );
522is( $got_area, $expected_area, $caption2 );
523
524
525###############################################################################
526#
527# 39. Test the plotarea format methods. See the set_*area() properties below.
528#
529reset_chart( $chart );
530
531$chart->set_plotarea( color => 'red', );
532
533$expected_line = join ' ', qw(
534  07 10 0C 00 80 80 80 00 00 00 00 00 00 00 17 00
535);
536
537$expected_area = join ' ', qw(
538  0A 10 10 00 FF 00 00 00 00 00 00 00 01 00 00 00
539  0A 00 08 00
540);
541
542( $got_line, $got_area ) = get_plotarea_formats( $chart );
543
544is( $got_line, $expected_line, $caption1 );
545is( $got_area, $expected_area, $caption2 );
546
547
548###############################################################################
549#
550# 41. Test the plotarea format methods. See the set_*area() properties below.
551#
552reset_chart( $chart );
553
554$chart->set_plotarea( line_color => 'red', );
555
556$expected_line = join ' ', qw(
557  07 10 0C 00 FF 00 00 00 00 00 00 00 00 00 0A 00
558);
559
560$expected_area = join ' ', qw(
561  0A 10 10 00 C0 C0 C0 00 00 00 00 00 01 00 00 00
562  16 00 08 00
563);
564
565( $got_line, $got_area ) = get_plotarea_formats( $chart );
566
567is( $got_line, $expected_line, $caption1 );
568is( $got_area, $expected_area, $caption2 );
569
570
571###############################################################################
572#
573# 43. Test the plotarea format methods. See the set_*area() properties below.
574#
575reset_chart( $chart );
576
577$chart->set_plotarea( line_pattern => 2, );
578
579$expected_line = join ' ', qw(
580  07 10 0C 00 80 80 80 00 01 00 00 00 00 00 17 00
581);
582
583$expected_area = join ' ', qw(
584  0A 10 10 00 C0 C0 C0 00 00 00 00 00 01 00 00 00
585  16 00 08 00
586);
587
588( $got_line, $got_area ) = get_plotarea_formats( $chart );
589
590is( $got_line, $expected_line, $caption1 );
591is( $got_area, $expected_area, $caption2 );
592
593
594###############################################################################
595#
596# 45. Test the plotarea format methods. See the set_*area() properties below.
597#
598reset_chart( $chart );
599
600$chart->set_plotarea( line_weight => 3, );
601
602$expected_line = join ' ', qw(
603  07 10 0C 00 80 80 80 00 00 00 01 00 00 00 17 00
604);
605
606$expected_area = join ' ', qw(
607  0A 10 10 00 C0 C0 C0 00 00 00 00 00 01 00 00 00
608  16 00 08 00
609);
610
611( $got_line, $got_area ) = get_plotarea_formats( $chart );
612
613is( $got_line, $expected_line, $caption1 );
614is( $got_area, $expected_area, $caption2 );
615
616
617###############################################################################
618#
619# 47. Test the plotarea format methods. See the set_*area() properties below.
620#
621reset_chart( $chart );
622
623$chart->set_plotarea(
624    color      => 'red',
625    line_color => 'black',
626);
627
628$expected_line = join ' ', qw(
629  07 10 0C 00 00 00 00 00 00 00 00 00 00 00 08 00
630);
631
632$expected_area = join ' ', qw(
633  0A 10 10 00 FF 00 00 00 00 00 00 00 01 00 00 00
634  0A 00 08 00
635);
636
637( $got_line, $got_area ) = get_plotarea_formats( $chart );
638
639is( $got_line, $expected_line, $caption1 );
640is( $got_area, $expected_area, $caption2 );
641
642
643###############################################################################
644#
645# 49. Test the plotarea format methods. See the set_*area() properties below.
646#
647reset_chart( $chart );
648
649$chart->set_plotarea(
650    color        => 'red',
651    line_pattern => 2,
652);
653
654$expected_line = join ' ', qw(
655  07 10 0C 00 80 80 80 00 01 00 00 00 00 00 17 00
656);
657
658$expected_area = join ' ', qw(
659  0A 10 10 00 FF 00 00 00 00 00 00 00 01 00 00 00
660  0A 00 08 00
661);
662
663( $got_line, $got_area ) = get_plotarea_formats( $chart );
664
665is( $got_line, $expected_line, $caption1 );
666is( $got_area, $expected_area, $caption2 );
667
668
669###############################################################################
670#
671# 51. Test the plotarea format methods. See the set_*area() properties below.
672#
673reset_chart( $chart );
674
675$chart->set_plotarea(
676    color       => 'red',
677    line_weight => 3,
678);
679
680$expected_line = join ' ', qw(
681  07 10 0C 00 80 80 80 00 00 00 01 00 00 00 17 00
682);
683
684$expected_area = join ' ', qw(
685  0A 10 10 00 FF 00 00 00 00 00 00 00 01 00 00 00
686  0A 00 08 00
687);
688
689( $got_line, $got_area ) = get_plotarea_formats( $chart );
690
691is( $got_line, $expected_line, $caption1 );
692is( $got_area, $expected_area, $caption2 );
693
694
695###############################################################################
696#
697# Utility functions used by the test suite.
698#
699###############################################################################
700
701
702###############################################################################
703#
704# Reset the chart data for testing.
705#
706sub reset_chart {
707
708    my $chart    = shift;
709    my $embedded = shift;
710
711    # Reset the chart data.
712    $chart->{_data} = '';
713    $chart->_set_default_properties();
714
715    if ( $embedded ) {
716        $chart->_set_embedded_config_data();
717    }
718}
719
720
721###############################################################################
722#
723# Extract Line and Area format records from the Chartarea Frame stream.
724#
725sub get_chartarea_formats {
726
727    $chart = shift;
728
729    $chart->_store_chartarea_frame_stream();
730
731    my $line = unpack_record( substr $chart->{_data}, 12, 16 );
732    my $area = unpack_record( substr $chart->{_data}, 28, 20 );
733
734    return ( $line, $area );
735}
736
737
738###############################################################################
739#
740# Extract Line and Area format records from the Chartarea Frame stream.
741#
742sub get_plotarea_formats {
743
744    $chart = shift;
745
746    $chart->_store_plotarea_frame_stream();
747
748    my $line = unpack_record( substr $chart->{_data}, 12, 16 );
749    my $area = unpack_record( substr $chart->{_data}, 28, 20 );
750
751    return ( $line, $area );
752}
753
754
755###############################################################################
756#
757# Unpack the binary data into a format suitable for printing in tests.
758#
759sub unpack_record {
760    return join ' ', map { sprintf '%02X', $_ } unpack 'C*', shift;
761}
762
763
764###############################################################################
765#
766# Clean up.
767#
768$workbook->close();
769unlink $test_file;
770
771
772__END__
773