Merge "Blacklist Google Glass web browser from JS"
[mediawiki.git] / tests / phpunit / includes / media / PNGMetadataExtractorTest.php
blob84deb1bfd5b3f777548db036098d7c18eaf0425a
1 <?php
3 /**
4 * @covers PNGMetadataExtractor
5 */
6 class PNGMetadataExtractorTest extends MediaWikiTestCase {
8 protected function setUp() {
9 parent::setUp();
10 $this->filePath = __DIR__ . '/../../data/media/';
13 /**
14 * Tests zTXt tag (compressed textual metadata)
16 public function testPngNativetZtxt() {
17 $this->checkPHPExtension( 'zlib' );
19 $meta = PNGMetadataExtractor::getMetadata( $this->filePath .
20 'Png-native-test.png' );
21 $expected = "foo bar baz foo foo foo foof foo foo foo foo";
22 $this->assertArrayHasKey( 'text', $meta );
23 $meta = $meta['text'];
24 $this->assertArrayHasKey( 'Make', $meta );
25 $this->assertArrayHasKey( 'x-default', $meta['Make'] );
27 $this->assertEquals( $expected, $meta['Make']['x-default'] );
30 /**
31 * Test tEXt tag (Uncompressed textual metadata)
33 public function testPngNativeText() {
34 $meta = PNGMetadataExtractor::getMetadata( $this->filePath .
35 'Png-native-test.png' );
36 $expected = "Some long image desc";
37 $this->assertArrayHasKey( 'text', $meta );
38 $meta = $meta['text'];
39 $this->assertArrayHasKey( 'ImageDescription', $meta );
40 $this->assertArrayHasKey( 'x-default', $meta['ImageDescription'] );
41 $this->assertArrayHasKey( '_type', $meta['ImageDescription'] );
43 $this->assertEquals( $expected, $meta['ImageDescription']['x-default'] );
46 /**
47 * tEXt tags must be encoded iso-8859-1 (vs iTXt which are utf-8)
48 * Make sure non-ascii characters get converted properly
50 public function testPngNativeTextNonAscii() {
51 $meta = PNGMetadataExtractor::getMetadata( $this->filePath .
52 'Png-native-test.png' );
54 // Note the Copyright symbol here is a utf-8 one
55 // (aka \xC2\xA9) where in the file its iso-8859-1
56 // encoded as just \xA9.
57 $expected = "© 2010 Bawolff";
59 $this->assertArrayHasKey( 'text', $meta );
60 $meta = $meta['text'];
61 $this->assertArrayHasKey( 'Copyright', $meta );
62 $this->assertArrayHasKey( 'x-default', $meta['Copyright'] );
64 $this->assertEquals( $expected, $meta['Copyright']['x-default'] );
67 /**
68 * Test extraction of pHYs tags, which can tell what the
69 * actual resolution of the image is (aka in dots per meter).
72 public function testPngPhysTag() {
73 $meta = PNGMetadataExtractor::getMetadata( $this->filePath .
74 'Png-native-test.png' );
76 $this->assertArrayHasKey( 'text', $meta );
77 $meta = $meta['text'];
79 $this->assertEquals( '2835/100', $meta['XResolution'] );
80 $this->assertEquals( '2835/100', $meta['YResolution'] );
81 $this->assertEquals( 3, $meta['ResolutionUnit'] ); // 3 = cm
85 /**
86 * Given a normal static PNG, check the animation metadata returned.
88 public function testStaticPngAnimationMetadata() {
89 $meta = PNGMetadataExtractor::getMetadata( $this->filePath .
90 'Png-native-test.png' );
92 $this->assertEquals( 0, $meta['frameCount'] );
93 $this->assertEquals( 1, $meta['loopCount'] );
94 $this->assertEquals( 0, $meta['duration'] );
97 /**
98 * Given an animated APNG image file
99 * check it gets animated metadata right.
101 public function testApngAnimationMetadata() {
102 $meta = PNGMetadataExtractor::getMetadata( $this->filePath .
103 'Animated_PNG_example_bouncing_beach_ball.png' );
105 $this->assertEquals( 20, $meta['frameCount'] );
106 // Note loop count of 0 = infinity
107 $this->assertEquals( 0, $meta['loopCount'] );
108 $this->assertEquals( 1.5, $meta['duration'], '', 0.00001 );
111 public function testPngBitDepth8() {
112 $meta = PNGMetadataExtractor::getMetadata( $this->filePath .
113 'Png-native-test.png' );
115 $this->assertEquals( 8, $meta['bitDepth'] );
118 public function testPngBitDepth1() {
119 $meta = PNGMetadataExtractor::getMetadata( $this->filePath .
120 '1bit-png.png' );
121 $this->assertEquals( 1, $meta['bitDepth'] );
124 public function testPngIndexColour() {
125 $meta = PNGMetadataExtractor::getMetadata( $this->filePath .
126 'Png-native-test.png' );
128 $this->assertEquals( 'index-coloured', $meta['colorType'] );
131 public function testPngRgbColour() {
132 $meta = PNGMetadataExtractor::getMetadata( $this->filePath .
133 'rgb-png.png' );
134 $this->assertEquals( 'truecolour-alpha', $meta['colorType'] );
137 public function testPngRgbNoAlphaColour() {
138 $meta = PNGMetadataExtractor::getMetadata( $this->filePath .
139 'rgb-na-png.png' );
140 $this->assertEquals( 'truecolour', $meta['colorType'] );
143 public function testPngGreyscaleColour() {
144 $meta = PNGMetadataExtractor::getMetadata( $this->filePath .
145 'greyscale-png.png' );
146 $this->assertEquals( 'greyscale-alpha', $meta['colorType'] );
149 public function testPngGreyscaleNoAlphaColour() {
150 $meta = PNGMetadataExtractor::getMetadata( $this->filePath .
151 'greyscale-na-png.png' );
152 $this->assertEquals( 'greyscale', $meta['colorType'] );