Per CR on r86169, start adding unit tests for metadata extraction.
[lhc/web/wiklou.git] / tests / phpunit / includes / media / PNGMetadataExtractorTest.php
1 <?php
2 class PNGMetadataExtractorTest extends MediaWikiTestCase {
3 /**
4 * Tests zTXt tag (compressed textual metadata)
5 */
6 function testPNGNativetzTXt() {
7 $meta = PNGMetadataExtractor::getMetadata( dirname( __FILE__ ) .
8 '/Png-native-test.png' );
9 $expected = "foo bar baz foo foo foo foof foo foo foo foo";
10 $this->assertArrayHasKey( 'text', $meta );
11 $meta = $meta['text'];
12 $this->assertArrayHasKey( 'Make', $meta );
13 $this->assertArrayHasKey( 'x-default', $meta['Make'] );
14
15 $this->assertEquals( $expected, $meta['Make']['x-default'] );
16 }
17
18 /**
19 * Test tEXt tag (Uncompressed textual metadata)
20 */
21 function testPNGNativetEXt() {
22 $meta = PNGMetadataExtractor::getMetadata( dirname( __FILE__ ) .
23 '/Png-native-test.png' );
24 $expected = "Some long image desc";
25 $this->assertArrayHasKey( 'text', $meta );
26 $meta = $meta['text'];
27 $this->assertArrayHasKey( 'ImageDescription', $meta );
28 $this->assertArrayHasKey( 'x-default', $meta['ImageDescription'] );
29 $this->assertArrayHasKey( '_type', $meta['ImageDescription'] );
30
31 $this->assertEquals( $expected, $meta['ImageDescription']['x-default'] );
32 }
33
34 /**
35 * tEXt tags must be encoded iso-8859-1 (vs iTXt which are utf-8)
36 * Make sure non-ascii characters get converted properly
37 */
38 function testPNGNativettEXtNonASCII() {
39 $meta = PNGMetadataExtractor::getMetadata( dirname( __FILE__ ) .
40 '/Png-native-test.png' );
41
42 // Note the Copyright symbol here is a utf-8 one
43 // (aka \xC2\xA9) where in the file its iso-8859-1
44 // encoded as just \xA9.
45 $expected = "© 2010 Bawolff";
46
47
48 $this->assertArrayHasKey( 'text', $meta );
49 $meta = $meta['text'];
50 $this->assertArrayHasKey( 'Copyright', $meta );
51 $this->assertArrayHasKey( 'x-default', $meta['Copyright'] );
52
53 $this->assertEquals( $expected, $meta['Copyright']['x-default'] );
54 }
55
56 /**
57 * Test extraction of pHYs tags, which can tell what the
58 * actual resolution of the image is (aka in dots per meter).
59 function testPNGpHYsTag () {
60 $meta = PNGMetadataExtractor::getMetadata( dirname( __FILE__ ) .
61 '/Png-native-test.png' );
62
63 $this->assertArrayHasKey( 'text', $meta );
64 $meta = $meta['text'];
65
66 $this->assertEquals( '2835/100', $meta['XResolution'] );
67 $this->assertEquals( '2835/100', $meta['YResolution'] );
68 $this->assertEquals( 3, $meta['ResolutionUnit'] ); // 3 = cm
69 }
70
71 /**
72 * Given a normal static PNG, check the animation metadata returned.
73 */
74 function testStaticPNGAnimationMetadata() {
75 $meta = PNGMetadataExtractor::getMetadata( dirname( __FILE__ ) .
76 '/Png-native-test.png' );
77
78 $this->assertEquals( 0, $meta['frameCount'] );
79 $this->assertEquals( 1, $meta['loopCount'] );
80 $this->assertEquals( 0, $meta['duration'] );
81 }
82
83 /**
84 * Given an animated APNG image file
85 * check it gets animated metadata right.
86 */
87 function testAPNGAnimationMetadata() {
88 $meta = PNGMetadataExtractor::getMetadata( dirname( __FILE__ ) .
89 '/Animated_PNG_example_bouncing_beach_ball.png' );
90
91 $this->assertEquals( 20, $meta['frameCount'] );
92 // Note loop count of 0 = infinity
93 $this->assertEquals( 0, $meta['loopCount'] );
94 $this->assertEquals( 1.5, $meta['duration'], '', 0.00001 );
95 }
96
97 function testPNGBitDepth8() {
98 $meta = PNGMetadataExtractor::getMetadata( dirname( __FILE__ ) .
99 '/Png-native-test.png' );
100
101 $this->assertEquals( 8, $meta['bitDepth'] );
102 }
103 function testPNGBitDepth1() {
104 $meta = PNGMetadataExtractor::getMetadata( dirname( __FILE__ ) .
105 '/1bit-png.png' );
106 $this->assertEquals( 1, $meta['bitDepth'] );
107 }
108
109
110 function testPNGindexColour() {
111 $meta = PNGMetadataExtractor::getMetadata( dirname( __FILE__ ) .
112 '/Png-native-test.png' );
113
114 $this->assertEquals( 'index-coloured', $meta['colorType'] );
115 }
116 function testPNGrgbColour() {
117 $meta = PNGMetadataExtractor::getMetadata( dirname( __FILE__ ) .
118 '/rgb-png.png' );
119 $this->assertEquals( 'truecolour-alpha', $meta['colorType'] );
120 }
121 function testPNGrgbNoAlphaColour() {
122 $meta = PNGMetadataExtractor::getMetadata( dirname( __FILE__ ) .
123 '/rgb-na-png.png' );
124 $this->assertEquals( 'truecolour', $meta['colorType'] );
125 }
126 function testPNGgreyscaleColour() {
127 $meta = PNGMetadataExtractor::getMetadata( dirname( __FILE__ ) .
128 '/greyscale-png.png' );
129 $this->assertEquals( 'greyscale-alpha', $meta['colorType'] );
130 }
131 function testPNGgreyscaleNoAlphaColour() {
132 $meta = PNGMetadataExtractor::getMetadata( dirname( __FILE__ ) .
133 '/greyscale-na-png.png' );
134 $this->assertEquals( 'greyscale', $meta['colorType'] );
135 }
136
137
138 }