diff options
author | Bart Visscher <bartv@thisnet.nl> | 2013-08-02 08:27:24 -0700 |
---|---|---|
committer | Bart Visscher <bartv@thisnet.nl> | 2013-08-02 08:27:24 -0700 |
commit | 43b31f2161775c7db62b46b8e4044f328f774aae (patch) | |
tree | ccbeeb34424a11b8d6d670ad8574fcfb872e982b | |
parent | 821a99b937cc1c12315d23e805a80496ed54f70d (diff) | |
parent | de0dc30d8925f43ddec0e8bb5d2986ab3609919a (diff) | |
download | nextcloud-server-43b31f2161775c7db62b46b8e4044f328f774aae.tar.gz nextcloud-server-43b31f2161775c7db62b46b8e4044f328f774aae.zip |
Merge pull request #4283 from owncloud/add_ocimage_unittest
Add ocimage unittest
-rw-r--r-- | tests/data/testimage.gif | bin | 0 -> 362 bytes | |||
-rw-r--r-- | tests/data/testimage.jpg | bin | 0 -> 25392 bytes | |||
-rw-r--r-- | tests/data/testimage.png | bin | 0 -> 3670 bytes | |||
-rw-r--r-- | tests/lib/image.php | 221 |
4 files changed, 221 insertions, 0 deletions
diff --git a/tests/data/testimage.gif b/tests/data/testimage.gif Binary files differnew file mode 100644 index 00000000000..3026395c5e6 --- /dev/null +++ b/tests/data/testimage.gif diff --git a/tests/data/testimage.jpg b/tests/data/testimage.jpg Binary files differnew file mode 100644 index 00000000000..2f51a2a6cdd --- /dev/null +++ b/tests/data/testimage.jpg diff --git a/tests/data/testimage.png b/tests/data/testimage.png Binary files differnew file mode 100644 index 00000000000..257598f04f5 --- /dev/null +++ b/tests/data/testimage.png diff --git a/tests/lib/image.php b/tests/lib/image.php new file mode 100644 index 00000000000..0583c300075 --- /dev/null +++ b/tests/lib/image.php @@ -0,0 +1,221 @@ +<?php +/** + * Copyright (c) 2013 Christopher Schäpers <christopher@schaepers.it> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +class Test_Image extends PHPUnit_Framework_TestCase { + + public function testGetMimeTypeForFile() { + $mimetype = \OC_Image::getMimeTypeForFile(OC::$SERVERROOT.'/tests/data/testimage.png'); + $this->assertEquals('image/png', $mimetype); + + $mimetype = \OC_Image::getMimeTypeForFile(OC::$SERVERROOT.'/tests/data/testimage.jpg'); + $this->assertEquals('image/jpeg', $mimetype); + + $mimetype = \OC_Image::getMimeTypeForFile(OC::$SERVERROOT.'/tests/data/testimage.gif'); + $this->assertEquals('image/gif', $mimetype); + + $mimetype = \OC_Image::getMimeTypeForFile(null); + $this->assertEquals('', $mimetype); + } + + public function testConstructDestruct() { + $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png'); + $this->assertInstanceOf('\OC_Image', $img); + unset($img); + + $imgcreate = imagecreatefromjpeg(OC::$SERVERROOT.'/tests/data/testimage.jpg'); + $img = new \OC_Image($imgcreate); + $this->assertInstanceOf('\OC_Image', $img); + unset($img); + + $base64 = base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')); + $img = new \OC_Image($base64); + $this->assertInstanceOf('\OC_Image', $img); + unset($img); + + $img = new \OC_Image(null); + $this->assertInstanceOf('\OC_Image', $img); + unset($img); + } + + public function testValid() { + $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png'); + $this->assertTrue($img->valid()); + + $text = base64_encode("Lorem ipsum dolor sir amet …"); + $img = new \OC_Image($text); + $this->assertFalse($img->valid()); + + $img = new \OC_Image(null); + $this->assertFalse($img->valid()); + } + + public function testMimeType() { + $this->markTestSkipped("When loading from data or base64, imagetype is always image/png, see #4258."); + $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png'); + $this->assertEquals('image/png', $img->mimeType()); + + $img = new \OC_Image(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg')); + $this->assertEquals('image/jpeg', $img->mimeType()); + + $img = new \OC_Image(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif'))); + $this->assertEquals('image/gif', $img->mimeType()); + + $img = new \OC_Image(null); + $this->assertEquals('', $img->mimeType()); + } + + public function testWidth() { + $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png'); + $this->assertEquals(128, $img->width()); + + $img = new \OC_Image(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg')); + $this->assertEquals(1680, $img->width()); + + $img = new \OC_Image(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif'))); + $this->assertEquals(64, $img->width()); + + $img = new \OC_Image(null); + $this->assertEquals(-1, $img->width()); + } + + public function testHeight() { + $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png'); + $this->assertEquals(128, $img->height()); + + $img = new \OC_Image(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg')); + $this->assertEquals(1050, $img->height()); + + $img = new \OC_Image(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif'))); + $this->assertEquals(64, $img->height()); + + $img = new \OC_Image(null); + $this->assertEquals(-1, $img->height()); + } + + public function testSave() { + $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png'); + $img->resize(16); + $img->save(OC::$SERVERROOT.'/tests/data/testimage2.png'); + $this->assertEquals(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage2.png'), $img->data()); + } + + public function testData() { + $this->markTestSkipped("\OC_Image->data() converts to png before outputting data, see #4258."); + $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png'); + $expected = file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.png'); + $this->assertEquals($expected, $img->data()); + + $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.jpg'); + $expected = file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'); + $this->assertEquals($expected, $img->data()); + + $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.gif'); + $expected = file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif'); + $this->assertEquals($expected, $img->data()); + } + + public function testToString() { + $this->markTestSkipped("\OC_Image->data() converts to png before outputting data, see #4258."); + $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png'); + $expected = base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.png')); + $this->assertEquals($expected, (string)$img); + + $img = new \OC_Image(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg')); + $expected = base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg')); + $this->assertEquals($expected, (string)$img); + + $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.gif'); + $expected = base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')); + $this->assertEquals($expected, (string)$img); + } + + public function testResize() { + $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png'); + $this->assertTrue($img->resize(32)); + $this->assertEquals(32, $img->width()); + $this->assertEquals(32, $img->height()); + + $img = new \OC_Image(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg')); + $this->assertTrue($img->resize(840)); + $this->assertEquals(840, $img->width()); + $this->assertEquals(525, $img->height()); + + $img = new \OC_Image(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif'))); + $this->assertTrue($img->resize(100)); + $this->assertEquals(100, $img->width()); + $this->assertEquals(100, $img->height()); + } + + public function testPreciseResize() { + $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png'); + $this->assertTrue($img->preciseResize(128, 512)); + $this->assertEquals(128, $img->width()); + $this->assertEquals(512, $img->height()); + + $img = new \OC_Image(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg')); + $this->assertTrue($img->preciseResize(64, 840)); + $this->assertEquals(64, $img->width()); + $this->assertEquals(840, $img->height()); + + $img = new \OC_Image(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif'))); + $this->assertTrue($img->preciseResize(1000, 1337)); + $this->assertEquals(1000, $img->width()); + $this->assertEquals(1337, $img->height()); + } + + public function testCenterCrop() { + $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png'); + $img->centerCrop(); + $this->assertEquals(128, $img->width()); + $this->assertEquals(128, $img->height()); + + $img = new \OC_Image(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg')); + $img->centerCrop(); + $this->assertEquals(1050, $img->width()); + $this->assertEquals(1050, $img->height()); + + $img = new \OC_Image(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif'))); + $img->centerCrop(512); + $this->assertEquals(512, $img->width()); + $this->assertEquals(512, $img->height()); + } + + public function testCrop() { + $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png'); + $this->assertTrue($img->crop(0, 0, 50, 20)); + $this->assertEquals(50, $img->width()); + $this->assertEquals(20, $img->height()); + + $img = new \OC_Image(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg')); + $this->assertTrue($img->crop(500, 700, 550, 300)); + $this->assertEquals(550, $img->width()); + $this->assertEquals(300, $img->height()); + + $img = new \OC_Image(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif'))); + $this->assertTrue($img->crop(10, 10, 15, 15)); + $this->assertEquals(15, $img->width()); + $this->assertEquals(15, $img->height()); + } + + public function testFitIn() { + $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png'); + $this->assertTrue($img->fitIn(200, 100)); + $this->assertEquals(100, $img->width()); + $this->assertEquals(100, $img->height()); + + $img = new \OC_Image(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg')); + $this->assertTrue($img->fitIn(840, 840)); + $this->assertEquals(840, $img->width()); + $this->assertEquals(525, $img->height()); + + $img = new \OC_Image(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif'))); + $this->assertTrue($img->fitIn(200, 250)); + $this->assertEquals(200, $img->width()); + $this->assertEquals(200, $img->height()); + } +} |