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