summaryrefslogtreecommitdiffstats
path: root/tests/lib
diff options
context:
space:
mode:
Diffstat (limited to 'tests/lib')
-rw-r--r--tests/lib/image.php86
-rw-r--r--tests/lib/preview.php886
-rw-r--r--tests/lib/preview/bitmap.php36
-rw-r--r--tests/lib/preview/image.php36
-rw-r--r--tests/lib/preview/movie.php46
-rw-r--r--tests/lib/preview/mp3.php36
-rw-r--r--tests/lib/preview/office.php43
-rw-r--r--tests/lib/preview/provider.php187
-rw-r--r--tests/lib/preview/svg.php41
-rw-r--r--tests/lib/preview/txt.php37
10 files changed, 1274 insertions, 160 deletions
diff --git a/tests/lib/image.php b/tests/lib/image.php
index a22e210947b..e74c75b48cf 100644
--- a/tests/lib/image.php
+++ b/tests/lib/image.php
@@ -238,21 +238,81 @@ class Test_Image extends \Test\TestCase {
$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());
+ public static function sampleProvider() {
+ return [
+ ['testimage.png', [200, 100], [100, 100]],
+ ['testimage.jpg', [840, 840], [840, 525]],
+ ['testimage.gif', [200, 250], [200, 200]]
+ ];
+ }
- $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());
+ /**
+ * @dataProvider sampleProvider
+ *
+ * @param string $filename
+ * @param int[] $asked
+ * @param int[] $expected
+ */
+ public function testFitIn($filename, $asked, $expected) {
+ $img = new \OC_Image(OC::$SERVERROOT . '/tests/data/' . $filename);
+ $this->assertTrue($img->fitIn($asked[0], $asked[1]));
+ $this->assertEquals($expected[0], $img->width());
+ $this->assertEquals($expected[1], $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());
+ public static function sampleFilenamesProvider() {
+ return [
+ ['testimage.png'],
+ ['testimage.jpg'],
+ ['testimage.gif']
+ ];
+ }
+
+ /**
+ * Image should not be resized if it's already smaller than what is required
+ *
+ * @dataProvider sampleFilenamesProvider
+ *
+ * @param string $filename
+ */
+ public function testScaleDownToFitWhenSmallerAlready($filename) {
+ $img = new \OC_Image(OC::$SERVERROOT . '/tests/data/' . $filename);
+ $currentWidth = $img->width();
+ $currentHeight = $img->height();
+ // We pick something larger than the image we want to scale down
+ $this->assertFalse($img->scaleDownToFit(4000, 4000));
+ // The dimensions of the image should not have changed since it's smaller already
+ $resizedWidth = $img->width();
+ $resizedHeight = $img->height();
+ $this->assertEquals(
+ $currentWidth, $img->width(), "currentWidth $currentWidth resizedWidth $resizedWidth \n"
+ );
+ $this->assertEquals(
+ $currentHeight, $img->height(),
+ "currentHeight $currentHeight resizedHeight $resizedHeight \n"
+ );
+ }
+
+ public static function largeSampleProvider() {
+ return [
+ ['testimage.png', [200, 100], [100, 100]],
+ ['testimage.jpg', [840, 840], [840, 525]],
+ ];
+ }
+
+ /**
+ * @dataProvider largeSampleProvider
+ *
+ * @param string $filename
+ * @param int[] $asked
+ * @param int[] $expected
+ */
+ public function testScaleDownWhenBigger($filename, $asked, $expected) {
+ $img = new \OC_Image(OC::$SERVERROOT . '/tests/data/' . $filename);
+ //$this->assertTrue($img->scaleDownToFit($asked[0], $asked[1]));
+ $img->scaleDownToFit($asked[0], $asked[1]);
+ $this->assertEquals($expected[0], $img->width());
+ $this->assertEquals($expected[1], $img->height());
}
function convertDataProvider() {
diff --git a/tests/lib/preview.php b/tests/lib/preview.php
index 20e4209dedf..27410187f43 100644
--- a/tests/lib/preview.php
+++ b/tests/lib/preview.php
@@ -1,9 +1,23 @@
<?php
/**
- * Copyright (c) 2013 Georg Ehrke <georg@ownCloud.com>
- * This file is licensed under the Affero General Public License version 3 or
- * later.
- * See the COPYING-README file.
+ * @author Georg Ehrke <georg@owncloud.com>
+ * @author Olivier Paroz <owncloud@interfasys.ch>
+ *
+ * @copyright Copyright (c) 2015, ownCloud, Inc.
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License, version 3,
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
*/
namespace Test;
@@ -12,121 +26,99 @@ class Preview extends TestCase {
const TEST_PREVIEW_USER1 = "test-preview-user1";
- /**
- * @var \OC\Files\View
- */
+ /** @var \OC\Files\View */
private $rootView;
+ /**
+ * Note that using 756 with an image with a ratio of 1.6 brings interesting rounding issues
+ *
+ * @var int maximum width allowed for a preview
+ * */
+ private $configMaxWidth = 756;
+ /** @var int maximum height allowed for a preview */
+ private $configMaxHeight = 756;
+ private $keepAspect;
+ private $scalingUp;
+
+ private $samples = [];
+ private $sampleFileId;
+ private $sampleFilename;
+ private $sampleWidth;
+ private $sampleHeight;
+ private $maxScaleFactor;
+ /** @var int width of the max preview */
+ private $maxPreviewWidth;
+ /** @var int height of the max preview */
+ private $maxPreviewHeight;
+ /** @var int height of the max preview, which is the same as the one of the original image */
+ private $maxPreviewRatio;
+ private $cachedBigger = [];
- /** @var \OC\Files\Storage\Storage */
- private $originalStorage;
-
+ /**
+ * Make sure your configuration file doesn't contain any additional providers
+ */
protected function setUp() {
parent::setUp();
- // FIXME: use proper tearDown with $this->loginAsUser() and $this->logout()
- // (would currently break the tests for some reason)
- $this->originalStorage = \OC\Files\Filesystem::getStorage('/');
-
- // create a new user with his own filesystem view
- // this gets called by each test in this test class
+ $userManager = \OC::$server->getUserManager();
+ $userManager->clearBackends();
$backend = new \OC_User_Dummy();
- \OC_User::useBackend($backend);
+ $userManager->registerBackend($backend);
$backend->createUser(self::TEST_PREVIEW_USER1, self::TEST_PREVIEW_USER1);
- $user = \OC::$server->getUserManager()->get(self::TEST_PREVIEW_USER1);
- \OC::$server->getUserSession()->setUser($user);
- \OC\Files\Filesystem::init(self::TEST_PREVIEW_USER1, '/' . self::TEST_PREVIEW_USER1 . '/files');
+ $this->loginAsUser(self::TEST_PREVIEW_USER1);
- \OC\Files\Filesystem::mount('OC\Files\Storage\Temporary', array(), '/');
+ $storage = new \OC\Files\Storage\Temporary([]);
+ \OC\Files\Filesystem::mount($storage, [], '/' . self::TEST_PREVIEW_USER1 . '/');
$this->rootView = new \OC\Files\View('');
- $this->rootView->mkdir('/'.self::TEST_PREVIEW_USER1);
- $this->rootView->mkdir('/'.self::TEST_PREVIEW_USER1.'/files');
+ $this->rootView->mkdir('/' . self::TEST_PREVIEW_USER1);
+ $this->rootView->mkdir('/' . self::TEST_PREVIEW_USER1 . '/files');
+
+ // We simulate the max dimension set in the config
+ \OC::$server->getConfig()
+ ->setSystemValue('preview_max_x', $this->configMaxWidth);
+ \OC::$server->getConfig()
+ ->setSystemValue('preview_max_y', $this->configMaxHeight);
+ // Used to test upscaling
+ $this->maxScaleFactor = 2;
+ \OC::$server->getConfig()
+ ->setSystemValue('preview_max_scale_factor', $this->maxScaleFactor);
+
+ // We need to enable the providers we're going to use in the tests
+ $providers = [
+ 'OC\\Preview\\JPEG',
+ 'OC\\Preview\\PNG',
+ 'OC\\Preview\\GIF',
+ 'OC\\Preview\\TXT',
+ 'OC\\Preview\\Postscript'
+ ];
+ \OC::$server->getConfig()
+ ->setSystemValue('enabledPreviewProviders', $providers);
+
+ // Sample is 1680x1050 JPEG
+ $this->prepareSample('testimage.jpg', 1680, 1050);
+ // Sample is 2400x1707 EPS
+ $this->prepareSample('testimage.eps', 2400, 1707);
+ // Sample is 1200x450 PNG
+ $this->prepareSample('testimage-wide.png', 1200, 450);
+ // Sample is 64x64 GIF
+ $this->prepareSample('testimage.gif', 64, 64);
}
protected function tearDown() {
- \OC\Files\Filesystem::clearMounts();
- \OC\Files\Filesystem::mount($this->originalStorage, array(), '/');
+ $this->logout();
parent::tearDown();
}
- public function testIsMaxSizeWorking() {
- // Max size from config
- $maxX = 1024;
- $maxY = 1024;
-
- \OC::$server->getConfig()->setSystemValue('preview_max_x', $maxX);
- \OC::$server->getConfig()->setSystemValue('preview_max_y', $maxY);
-
- // Sample is 1680x1050 JPEG
- $sampleFile = '/' . self::TEST_PREVIEW_USER1 . '/files/testimage.jpg';
- $this->rootView->file_put_contents($sampleFile, file_get_contents(\OC::$SERVERROOT.'/tests/data/testimage.jpg'));
- $fileInfo = $this->rootView->getFileInfo($sampleFile);
- $fileId = $fileInfo['fileid'];
-
- $largeX = 1920;
- $largeY = 1080;
- $preview = new \OC\Preview(self::TEST_PREVIEW_USER1, 'files/', 'testimage.jpg', $largeX, $largeY);
-
- $this->assertEquals($preview->isFileValid(), true);
-
- // There should be no cached copy
- $isCached = $preview->isCached($fileId);
-
- $this->assertNotEquals(\OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/' . $maxX . '-' . $maxY . '-max.png', $isCached);
- $this->assertNotEquals(\OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/' . $maxX . '-' . $maxY . '.png', $isCached);
- $this->assertNotEquals(\OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/' . $largeX . '-' . $largeY . '.png', $isCached);
-
- // The returned preview should be of max size
- $image = $preview->getPreview();
-
- $this->assertEquals($image->width(), $maxX);
- $this->assertEquals($image->height(), $maxY);
-
- // The max thumbnail should be created
- $maxThumbCacheFile = '/' . self::TEST_PREVIEW_USER1 . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/' . $maxX . '-' . $maxY . '-max.png';
-
- $this->assertEquals($this->rootView->file_exists($maxThumbCacheFile), true);
-
- // A preview of the asked size should not have been created
- $thumbCacheFile = \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/' . $largeX . '-' . $largeY . '.png';
-
- $this->assertEquals($this->rootView->file_exists($thumbCacheFile), false);
-
- // 2nd request should indicate that we have a cached copy of max dimension
- $isCached = $preview->isCached($fileId);
- $this->assertEquals(\OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/' . $maxX . '-' . $maxY . '.png', $isCached);
-
- // Smaller previews should be based on the cached max preview
- $smallX = 50;
- $smallY = 50;
- $preview = new \OC\Preview(self::TEST_PREVIEW_USER1, 'files/', 'testimage.jpg', $smallX, $smallY);
- $isCached = $preview->isCached($fileId);
-
- $this->assertEquals(\OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/' . $maxX . '-' . $maxY . '.png', $isCached);
-
- // A small preview should be created
- $image = $preview->getPreview();
- $this->assertEquals($image->width(), $smallX);
- $this->assertEquals($image->height(), $smallY);
-
- // The cache should contain the small preview
- $thumbCacheFile = '/' . self::TEST_PREVIEW_USER1 . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/' . $smallX . '-' . $smallY . '.png';
-
- $this->assertEquals($this->rootView->file_exists($thumbCacheFile), true);
-
- // 2nd request should indicate that we have a cached copy of the exact dimension
- $isCached = $preview->isCached($fileId);
-
- $this->assertEquals(\OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/' . $smallX . '-' . $smallY . '.png', $isCached);
- }
-
+ /**
+ * Tests if a preview can be deleted
+ */
public function testIsPreviewDeleted() {
- $sampleFile = '/'.self::TEST_PREVIEW_USER1.'/files/test.txt';
+ $sampleFile = '/' . self::TEST_PREVIEW_USER1 . '/files/test.txt';
$this->rootView->file_put_contents($sampleFile, 'dummy file data');
-
+
$x = 50;
$y = 50;
@@ -134,23 +126,30 @@ class Preview extends TestCase {
$preview->getPreview();
$fileInfo = $this->rootView->getFileInfo($sampleFile);
+ /** @var int $fileId */
$fileId = $fileInfo['fileid'];
+ $thumbCacheFile = $this->buildCachePath($fileId, $x, $y, true);
- $thumbCacheFile = '/' . self::TEST_PREVIEW_USER1 . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/' . $x . '-' . $y . '.png';
-
- $this->assertEquals($this->rootView->file_exists($thumbCacheFile), true);
+ $this->assertSame(
+ true, $this->rootView->file_exists($thumbCacheFile), "$thumbCacheFile \n"
+ );
$preview->deletePreview();
- $this->assertEquals($this->rootView->file_exists($thumbCacheFile), false);
+ $this->assertSame(false, $this->rootView->file_exists($thumbCacheFile));
}
+ /**
+ * Tests if all previews can be deleted
+ *
+ * We test this first to make sure we'll be able to cleanup after each preview generating test
+ */
public function testAreAllPreviewsDeleted() {
- $sampleFile = '/'.self::TEST_PREVIEW_USER1.'/files/test.txt';
+ $sampleFile = '/' . self::TEST_PREVIEW_USER1 . '/files/test.txt';
$this->rootView->file_put_contents($sampleFile, 'dummy file data');
-
+
$x = 50;
$y = 50;
@@ -158,104 +157,697 @@ class Preview extends TestCase {
$preview->getPreview();
$fileInfo = $this->rootView->getFileInfo($sampleFile);
+ /** @var int $fileId */
$fileId = $fileInfo['fileid'];
-
- $thumbCacheFolder = '/' . self::TEST_PREVIEW_USER1 . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/';
-
- $this->assertEquals($this->rootView->is_dir($thumbCacheFolder), true);
+
+ $thumbCacheFolder = '/' . self::TEST_PREVIEW_USER1 . '/' . \OC\Preview::THUMBNAILS_FOLDER .
+ '/' . $fileId . '/';
+
+ $this->assertSame(true, $this->rootView->is_dir($thumbCacheFolder), "$thumbCacheFolder \n");
$preview->deleteAllPreviews();
- $this->assertEquals($this->rootView->is_dir($thumbCacheFolder), false);
+ $this->assertSame(false, $this->rootView->is_dir($thumbCacheFolder));
}
public function txtBlacklist() {
$txt = 'random text file';
- return array(
- array('txt', $txt, false),
- );
+ return [
+ ['txt', $txt, false],
+ ];
}
/**
* @dataProvider txtBlacklist
+ *
+ * @param $extension
+ * @param $data
+ * @param $expectedResult
*/
public function testIsTransparent($extension, $data, $expectedResult) {
$x = 32;
$y = 32;
- $sample = '/'.self::TEST_PREVIEW_USER1.'/files/test.'.$extension;
+ $sample = '/' . self::TEST_PREVIEW_USER1 . '/files/test.' . $extension;
$this->rootView->file_put_contents($sample, $data);
- $preview = new \OC\Preview(self::TEST_PREVIEW_USER1, 'files/', 'test.'.$extension, $x, $y);
+ $preview = new \OC\Preview(
+ self::TEST_PREVIEW_USER1, 'files/', 'test.' . $extension, $x,
+ $y
+ );
$image = $preview->getPreview();
$resource = $image->resource();
//http://stackoverflow.com/questions/5702953/imagecolorat-and-transparency
$colorIndex = imagecolorat($resource, 1, 1);
$colorInfo = imagecolorsforindex($resource, $colorIndex);
- $this->assertEquals(
+ $this->assertSame(
$expectedResult,
$colorInfo['alpha'] === 127,
'Failed asserting that only previews for text files are transparent.'
);
}
- public function testCreationFromCached() {
+ /**
+ * We generate the data to use as it makes it easier to adjust in case we need to test
+ * something different
+ *
+ * @return array
+ */
+ public static function dimensionsDataProvider() {
+ $data = [];
+ $samples = [
+ [200, 800],
+ [200, 800],
+ [50, 400],
+ [4, 60],
+ ];
+ $keepAspect = false;
+ $scalingUp = false;
+
+ for ($a = 0; $a < sizeof($samples); $a++) {
+ for ($b = 0; $b < 2; $b++) {
+ for ($c = 0; $c < 2; $c++) {
+ for ($d = 0; $d < 4; $d++) {
+ $coordinates = [
+ [
+ -rand($samples[$a][0], $samples[$a][1]),
+ -rand($samples[$a][0], $samples[$a][1])
+ ],
+ [
+ rand($samples[$a][0], $samples[$a][1]),
+ rand($samples[$a][0], $samples[$a][1])
+ ],
+ [
+ -rand($samples[$a][0], $samples[$a][1]),
+ rand($samples[$a][0], $samples[$a][1])
+ ],
+ [
+ rand($samples[$a][0], $samples[$a][1]),
+ -rand($samples[$a][0], $samples[$a][1])
+ ]
+ ];
+ $row = [$a];
+ $row[] = $coordinates[$d][0];
+ $row[] = $coordinates[$d][1];
+ $row[] = $keepAspect;
+ $row[] = $scalingUp;
+ $data[] = $row;
+ }
+ $scalingUp = !$scalingUp;
+ }
+ $keepAspect = !$keepAspect;
+ }
+ }
+
+ return $data;
+ }
- $sampleFile = '/'.self::TEST_PREVIEW_USER1.'/files/test.txt';
+ /**
+ * Tests if a preview of max dimensions gets created
+ *
+ * @dataProvider dimensionsDataProvider
+ *
+ * @param int $sampleId
+ * @param int $widthAdjustment
+ * @param int $heightAdjustment
+ * @param bool $keepAspect
+ * @param bool $scalingUp
+ */
+ public function testCreateMaxAndNormalPreviewsAtFirstRequest(
+ $sampleId, $widthAdjustment, $heightAdjustment, $keepAspect = false, $scalingUp = false
+ ) {
+ //$this->markTestSkipped('Not testing this at this time');
+
+ // Get the right sample for the experiment
+ $this->getSample($sampleId);
+ $sampleWidth = $this->sampleWidth;
+ $sampleHeight = $this->sampleHeight;
+ $sampleFileId = $this->sampleFileId;
+
+ // Adjust the requested size so that we trigger various test cases
+ $previewWidth = $sampleWidth + $widthAdjustment;
+ $previewHeight = $sampleHeight + $heightAdjustment;
+ $this->keepAspect = $keepAspect;
+ $this->scalingUp = $scalingUp;
+
+ // Generates the max preview
+ $preview = $this->createPreview($previewWidth, $previewHeight);
+
+ // There should be no cached thumbnails
+ $thumbnailFolder = '/' . self::TEST_PREVIEW_USER1 . '/' . \OC\Preview::THUMBNAILS_FOLDER .
+ '/' . $sampleFileId;
+ $this->assertSame(false, $this->rootView->is_dir($thumbnailFolder));
- $this->rootView->file_put_contents($sampleFile, 'dummy file data');
+ $image = $preview->getPreview();
+ $this->assertNotSame(false, $image);
- // create base preview
- $x = 150;
- $y = 150;
+ $maxThumbCacheFile = $this->buildCachePath(
+ $sampleFileId, $this->maxPreviewWidth, $this->maxPreviewHeight, true, '-max'
+ );
- $preview = new \OC\Preview(self::TEST_PREVIEW_USER1, 'files/', 'test.txt', $x, $y);
- $preview->getPreview();
+ $this->assertSame(
+ true, $this->rootView->file_exists($maxThumbCacheFile), "$maxThumbCacheFile \n"
+ );
- $fileInfo = $this->rootView->getFileInfo($sampleFile);
- $fileId = $fileInfo['fileid'];
+ // We check the dimensions of the file we've just stored
+ $maxPreview = imagecreatefromstring($this->rootView->file_get_contents($maxThumbCacheFile));
- $thumbCacheFile = '/' . self::TEST_PREVIEW_USER1 . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/' . $x . '-' . $y . '.png';
+ $this->assertEquals($this->maxPreviewWidth, imagesx($maxPreview));
+ $this->assertEquals($this->maxPreviewHeight, imagesy($maxPreview));
- $this->assertEquals($this->rootView->file_exists($thumbCacheFile), true);
+ // A thumbnail of the asked dimensions should also have been created (within the constraints of the max preview)
+ list($limitedPreviewWidth, $limitedPreviewHeight) =
+ $this->simulatePreviewDimensions($previewWidth, $previewHeight);
+ $actualWidth = $image->width();
+ $actualHeight = $image->height();
- // create smaller previews
- $preview = new \OC\Preview(self::TEST_PREVIEW_USER1, 'files/', 'test.txt', 50, 50);
- $isCached = $preview->isCached($fileId);
+ $this->assertEquals(
+ (int)$limitedPreviewWidth, $image->width(), "$actualWidth x $actualHeight \n"
+ );
+ $this->assertEquals((int)$limitedPreviewHeight, $image->height());
+
+ // And it should be cached
+ $this->checkCache($sampleFileId, $limitedPreviewWidth, $limitedPreviewHeight);
- $this->assertEquals(self::TEST_PREVIEW_USER1 . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/150-150.png', $isCached);
+ $preview->deleteAllPreviews();
+ }
+
+ /**
+ * Tests if the second preview will be based off the cached max preview
+ *
+ * @dataProvider dimensionsDataProvider
+ *
+ * @param int $sampleId
+ * @param int $widthAdjustment
+ * @param int $heightAdjustment
+ * @param bool $keepAspect
+ * @param bool $scalingUp
+ */
+ public function testSecondPreviewsGetCachedMax(
+ $sampleId, $widthAdjustment, $heightAdjustment, $keepAspect = false, $scalingUp = false
+ ) {
+ //$this->markTestSkipped('Not testing this at this time');
+
+ $this->getSample($sampleId);
+ $sampleWidth = $this->sampleWidth;
+ $sampleHeight = $this->sampleHeight;
+ $sampleFileId = $this->sampleFileId;
+
+ //Creates the Max preview which will be used in the rest of the test
+ $this->createMaxPreview();
+
+ // Adjust the requested size so that we trigger various test cases
+ $previewWidth = $sampleWidth + $widthAdjustment;
+ $previewHeight = $sampleHeight + $heightAdjustment;
+ $this->keepAspect = $keepAspect;
+ $this->scalingUp = $scalingUp;
+
+ $preview = $this->createPreview($previewWidth, $previewHeight);
+
+ // A cache query should return the thumbnail of max dimension
+ $isCached = $preview->isCached($sampleFileId);
+ $cachedMaxPreview = $this->buildCachePath(
+ $sampleFileId, $this->maxPreviewWidth, $this->maxPreviewHeight, false, '-max'
+ );
+ $this->assertSame($cachedMaxPreview, $isCached);
}
- /*
- public function testScalingUp() {
+ /**
+ * Make sure that the max preview can never be deleted
+ *
+ * For this test to work, the preview we generate first has to be the size of max preview
+ */
+ public function testMaxPreviewCannotBeDeleted() {
+ //$this->markTestSkipped('Not testing this at this time');
- $sampleFile = '/'.$this->user.'/files/test.txt';
+ $this->keepAspect = true;
+ $this->getSample(0);
+ $fileId = $this->sampleFileId;
- $this->rootView->file_put_contents($sampleFile, 'dummy file data');
+ //Creates the Max preview which we will try to delete
+ $preview = $this->createMaxPreview();
- // create base preview
- $x = 150;
- $y = 150;
+ // We try to deleted the preview
+ $preview->deletePreview();
+ $this->assertNotSame(false, $preview->isCached($fileId));
- $preview = new \OC\Preview($this->user, 'files/', 'test.txt', $x, $y);
+ $preview->deleteAllPreviews();
+ }
+
+ public static function aspectDataProvider() {
+ $data = [];
+ $samples = 4;
+ $keepAspect = false;
+ $scalingUp = false;
+ for ($a = 0; $a < $samples; $a++) {
+ for ($b = 0; $b < 2; $b++) {
+ for ($c = 0; $c < 2; $c++) {
+ $row = [$a];
+ $row[] = $keepAspect;
+ $row[] = $scalingUp;
+ $data[] = $row;
+ $scalingUp = !$scalingUp;
+ }
+ $keepAspect = !$keepAspect;
+ }
+ }
+
+ return $data;
+ }
+
+ /**
+ * We ask for a preview larger than what is set in the configuration,
+ * so we should be getting either the max preview or a preview the size
+ * of the dimensions set in the config
+ *
+ * @dataProvider aspectDataProvider
+ *
+ * @param int $sampleId
+ * @param bool $keepAspect
+ * @param bool $scalingUp
+ */
+ public function testDoNotCreatePreviewsLargerThanConfigMax(
+ $sampleId, $keepAspect = false, $scalingUp = false
+ ) {
+ //$this->markTestSkipped('Not testing this at this time');
+
+ $this->getSample($sampleId);
+
+ //Creates the Max preview which will be used in the rest of the test
+ $this->createMaxPreview();
+
+ // Now we will create the real preview
+ $previewWidth = 4000;
+ $previewHeight = 4000;
+ $this->keepAspect = $keepAspect;
+ $this->scalingUp = $scalingUp;
+
+ // Tries to create the very large preview
+ $preview = $this->createPreview($previewWidth, $previewHeight);
+
+ $image = $preview->getPreview();
+ $this->assertNotSame(false, $image);
+
+ list($expectedWidth, $expectedHeight) =
+ $this->simulatePreviewDimensions($previewWidth, $previewHeight);
+ $this->assertEquals($expectedWidth, $image->width());
+ $this->assertEquals($expectedHeight, $image->height());
+
+ // A preview of the asked size should not have been created since it's larger that our max dimensions
+ $postfix = $this->getThumbnailPostfix($previewWidth, $previewHeight);
+ $thumbCacheFile = $this->buildCachePath(
+ $this->sampleFileId, $previewWidth, $previewHeight, false, $postfix
+ );
+ $this->assertSame(
+ false, $this->rootView->file_exists($thumbCacheFile), "$thumbCacheFile \n"
+ );
+
+ $preview->deleteAllPreviews();
+ }
+
+ /**
+ * Makes sure we're getting the proper cached thumbnail
+ *
+ * When we start by generating a preview which keeps the aspect ratio
+ * 200-125-with-aspect
+ * 300-300 ✓
+ *
+ * When we start by generating a preview of exact dimensions
+ * 200-200 ✓
+ * 300-188-with-aspect
+ *
+ * @dataProvider aspectDataProvider
+ *
+ * @param int $sampleId
+ * @param bool $keepAspect
+ * @param bool $scalingUp
+ */
+ public function testIsBiggerWithAspectRatioCached(
+ $sampleId, $keepAspect = false, $scalingUp = false
+ ) {
+ //$this->markTestSkipped('Not testing this at this time');
+
+ $previewWidth = 400;
+ $previewHeight = 400;
+ $this->getSample($sampleId);
+ $fileId = $this->sampleFileId;
+ $this->keepAspect = $keepAspect;
+ $this->scalingUp = $scalingUp;
+
+ // Caching the max preview in our preview array for the test
+ $this->cachedBigger[] = $this->buildCachePath(
+ $fileId, $this->maxPreviewWidth, $this->maxPreviewHeight, false, '-max'
+ );
+
+ $this->getSmallerThanMaxPreview($fileId, $previewWidth, $previewHeight);
+ // We switch the aspect ratio, to generate a thumbnail we should not be picked up
+ $this->keepAspect = !$keepAspect;
+ $this->getSmallerThanMaxPreview($fileId, $previewWidth + 100, $previewHeight + 100);
+
+ // Small thumbnails are always cropped
+ $this->keepAspect = false;
+ // Smaller previews should be based on the previous, larger preview, with the correct aspect ratio
+ $this->createThumbnailFromBiggerCachedPreview($fileId, 36, 36);
+
+ // 2nd cache query should indicate that we have a cached copy of the exact dimension
+ $this->getCachedSmallThumbnail($fileId, 36, 36);
+
+ // We create a preview in order to be able to delete the cache
+ $preview = $this->createPreview(rand(), rand());
+ $preview->deleteAllPreviews();
+ $this->cachedBigger = [];
+ }
+
+ /**
+ * Initialises the preview
+ *
+ * @param int $width
+ * @param int $height
+ *
+ * @return \OC\Preview
+ */
+ private function createPreview($width, $height) {
+ $preview = new \OC\Preview(
+ self::TEST_PREVIEW_USER1, 'files/', $this->sampleFilename, $width,
+ $height
+ );
+
+ $this->assertSame(true, $preview->isFileValid());
+
+ $preview->setKeepAspect($this->keepAspect);
+ $preview->setScalingup($this->scalingUp);
+
+ return $preview;
+ }
+
+ /**
+ * Creates the Max preview which will be used in the rest of the test
+ *
+ * @return \OC\Preview
+ */
+ private function createMaxPreview() {
+ $this->keepAspect = true;
+ $preview = $this->createPreview($this->maxPreviewWidth, $this->maxPreviewHeight);
$preview->getPreview();
- $fileInfo = $this->rootView->getFileInfo($sampleFile);
- $fileId = $fileInfo['fileid'];
+ return $preview;
+ }
+
+ /**
+ * Makes sure the preview which was just created has been saved to disk
+ *
+ * @param int $fileId
+ * @param int $previewWidth
+ * @param int $previewHeight
+ */
+ private function checkCache($fileId, $previewWidth, $previewHeight) {
+ $postfix = $this->getThumbnailPostfix($previewWidth, $previewHeight);
+
+ $thumbCacheFile = $this->buildCachePath(
+ $fileId, $previewWidth, $previewHeight, true, $postfix
+ );
+
+ $this->assertSame(
+ true, $this->rootView->file_exists($thumbCacheFile), "$thumbCacheFile \n"
+ );
+ }
+
+ /**
+ * Computes special filename postfixes
+ *
+ * @param int $width
+ * @param int $height
+ *
+ * @return string
+ */
+ private function getThumbnailPostfix($width, $height) {
+ // Need to take care of special postfix added to the dimensions
+ $postfix = '';
+ $isMaxPreview = ($width === $this->maxPreviewWidth
+ && $height === $this->maxPreviewHeight) ? true : false;
+ if ($isMaxPreview) {
+ $postfix = '-max';
+ }
+ if ($this->keepAspect && !$isMaxPreview) {
+ $postfix = '-with-aspect';
+ }
+
+ return $postfix;
+ }
+
+ private function getSmallerThanMaxPreview($fileId, $previewWidth, $previewHeight) {
+ $preview = $this->createPreview($previewWidth, $previewHeight);
+
+ $image = $preview->getPreview();
+ $this->assertNotSame(false, $image);
- $thumbCacheFile = '/' . $this->user . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/' . $x . '-' . $y . '.png';
+ // A thumbnail of the asked dimensions should also have been created (within the constraints of the max preview)
+ list($limitedPreviewWidth, $limitedPreviewHeight) =
+ $this->simulatePreviewDimensions($previewWidth, $previewHeight);
- $this->assertEquals($this->rootView->file_exists($thumbCacheFile), true);
+ $this->assertEquals($limitedPreviewWidth, $image->width());
+ $this->assertEquals($limitedPreviewHeight, $image->height());
+ // And it should be cached
+ $this->checkCache($fileId, $limitedPreviewWidth, $limitedPreviewHeight);
- // create bigger previews - with scale up
- $preview = new \OC\Preview($this->user, 'files/', 'test.txt', 250, 250);
+ $this->cachedBigger[] = $preview->isCached($fileId);
+ }
+
+ private function createThumbnailFromBiggerCachedPreview($fileId, $width, $height) {
+ $preview = $this->createPreview($width, $height);
+
+ // A cache query should return a thumbnail of slightly larger dimensions
+ // and with the proper aspect ratio
$isCached = $preview->isCached($fileId);
+ $expectedCachedBigger = $this->getExpectedCachedBigger();
+
+ $this->assertSame($expectedCachedBigger, $isCached);
+
+ $image = $preview->getPreview();
+ $this->assertNotSame(false, $image);
+ }
+
+ /**
+ * Picks the bigger cached preview with the correct aspect ratio or the max preview if it's
+ * smaller than that
+ *
+ * For non-upscaled images, we pick the only picture without aspect ratio
+ *
+ * @return string
+ */
+ private function getExpectedCachedBigger() {
+ $foundPreview = null;
+ $foundWidth = null;
+ $foundHeight = null;
+ $maxPreview = null;
+ $maxWidth = null;
+ $maxHeight = null;
+
+ foreach ($this->cachedBigger as $cached) {
+ $size = explode('-', basename($cached));
+ $width = (int)$size[0];
+ $height = (int)$size[1];
+
+ if (strpos($cached, 'max')) {
+ $maxWidth = $width;
+ $maxHeight = $height;
+ $maxPreview = $cached;
+ continue;
+ }
+
+ // We pick the larger preview with no aspect ratio
+ if (!strpos($cached, 'aspect') && !strpos($cached, 'max')) {
+ $foundPreview = $cached;
+ $foundWidth = $width;
+ $foundHeight = $height;
+ }
+ }
+ if ($foundWidth > $maxWidth && $foundHeight > $maxHeight) {
+ $foundPreview = $maxPreview;
+ }
+
+ return $foundPreview;
+ }
+
+ /**
+ * A small thumbnail of exact dimensions should be in the cache
+ *
+ * @param int $fileId
+ * @param int $width
+ * @param int $height
+ */
+ private function getCachedSmallThumbnail($fileId, $width, $height) {
+ $preview = $this->createPreview($width, $height);
+
+ $isCached = $preview->isCached($fileId);
+ $thumbCacheFile = $this->buildCachePath($fileId, $width, $height);
+
+ $this->assertSame($thumbCacheFile, $isCached, "$thumbCacheFile \n");
+ }
+
+ /**
+ * Builds the complete path to a cached thumbnail starting from the user folder
+ *
+ * @param int $fileId
+ * @param int $width
+ * @param int $height
+ * @param bool $user
+ * @param string $postfix
+ *
+ * @return string
+ */
+ private function buildCachePath($fileId, $width, $height, $user = false, $postfix = '') {
+ $userPath = '';
+ if ($user) {
+ $userPath = '/' . self::TEST_PREVIEW_USER1 . '/';
+ }
+
+ return $userPath . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId
+ . '/' . $width . '-' . $height . $postfix . '.png';
+ }
+
+ /**
+ * Stores the sample in the filesystem and stores it in the $samples array
+ *
+ * @param string $fileName
+ * @param int $sampleWidth
+ * @param int $sampleHeight
+ */
+ private function prepareSample($fileName, $sampleWidth, $sampleHeight) {
+ $imgData = file_get_contents(\OC::$SERVERROOT . '/tests/data/' . $fileName);
+ $imgPath = '/' . self::TEST_PREVIEW_USER1 . '/files/' . $fileName;
+ $this->rootView->file_put_contents($imgPath, $imgData);
+ $fileInfo = $this->rootView->getFileInfo($imgPath);
+
+ list($maxPreviewWidth, $maxPreviewHeight) =
+ $this->setMaxPreview($sampleWidth, $sampleHeight);
+
+ $this->samples[] =
+ [
+ 'sampleFileId' => $fileInfo['fileid'],
+ 'sampleFileName' => $fileName,
+ 'sampleWidth' => $sampleWidth,
+ 'sampleHeight' => $sampleHeight,
+ 'maxPreviewWidth' => $maxPreviewWidth,
+ 'maxPreviewHeight' => $maxPreviewHeight
+ ];
+ }
+
+ /**
+ * Sets the variables used to define the boundaries which need to be respected when using a
+ * specific sample
+ *
+ * @param $sampleId
+ */
+ private function getSample($sampleId) {
+ // Corrects a rounding difference when using the EPS (Imagick converted) sample
+ $filename = $this->samples[$sampleId]['sampleFileName'];
+ $splitFileName = pathinfo($filename);
+ $extension = $splitFileName['extension'];
+ $correction = ($extension === 'eps') ? 1 : 0;
+ $maxPreviewHeight = $this->samples[$sampleId]['maxPreviewHeight'];
+ $maxPreviewHeight = $maxPreviewHeight - $correction;
+
+ $this->sampleFileId = $this->samples[$sampleId]['sampleFileId'];
+ $this->sampleFilename = $this->samples[$sampleId]['sampleFileName'];
+ $this->sampleWidth = $this->samples[$sampleId]['sampleWidth'];
+ $this->sampleHeight = $this->samples[$sampleId]['sampleHeight'];
+ $this->maxPreviewWidth = $this->samples[$sampleId]['maxPreviewWidth'];
+ $this->maxPreviewHeight = $maxPreviewHeight;
+ $ratio = $this->maxPreviewWidth / $this->maxPreviewHeight;
+ $this->maxPreviewRatio = $ratio;
+ }
+
+ /**
+ * Defines the size of the max preview
+ *
+ * @fixme the Imagick previews don't have the exact same size on disk as they're calculated here
+ *
+ * @param int $sampleWidth
+ * @param int $sampleHeight
+ *
+ * @return array
+ */
+ private function setMaxPreview($sampleWidth, $sampleHeight) {
+ // Max previews are never scaled up
+ $this->scalingUp = false;
+ // Max previews always keep the aspect ratio
+ $this->keepAspect = true;
+ // We set this variable in order to be able to calculate the max preview with the proper aspect ratio
+ $this->maxPreviewRatio = $sampleWidth / $sampleHeight;
+ $maxPreviewWidth = min($sampleWidth, $this->configMaxWidth);
+ $maxPreviewHeight = min($sampleHeight, $this->configMaxHeight);
+ list($maxPreviewWidth, $maxPreviewHeight) =
+ $this->applyAspectRatio($maxPreviewWidth, $maxPreviewHeight);
+
+ return [$maxPreviewWidth, $maxPreviewHeight];
+ }
+
+ /**
+ * Calculates the expected dimensions of the preview to be able to assess if we've got the
+ * right result
+ *
+ * @param int $askedWidth
+ * @param int $askedHeight
+ *
+ * @return array
+ */
+ private function simulatePreviewDimensions($askedWidth, $askedHeight) {
+ $askedWidth = min($askedWidth, $this->configMaxWidth);
+ $askedHeight = min($askedHeight, $this->configMaxHeight);
+
+ if ($this->keepAspect) {
+ // Defines the box in which the preview has to fit
+ $scaleFactor = $this->scalingUp ? $this->maxScaleFactor : 1;
+ $newPreviewWidth = min($askedWidth, $this->maxPreviewWidth * $scaleFactor);
+ $newPreviewHeight = min($askedHeight, $this->maxPreviewHeight * $scaleFactor);
+ list($newPreviewWidth, $newPreviewHeight) =
+ $this->applyAspectRatio($newPreviewWidth, $newPreviewHeight);
+ } else {
+ list($newPreviewWidth, $newPreviewHeight) =
+ $this->fixSize($askedWidth, $askedHeight);
+ }
+
+ return [(int)$newPreviewWidth, (int)$newPreviewHeight];
+ }
+
+ /**
+ * Resizes the boundaries to match the aspect ratio
+ *
+ * @param int $askedWidth
+ * @param int $askedHeight
+ *
+ * @return \int[]
+ */
+ private function applyAspectRatio($askedWidth, $askedHeight) {
+ $originalRatio = $this->maxPreviewRatio;
+ if ($askedWidth / $originalRatio < $askedHeight) {
+ $askedHeight = round($askedWidth / $originalRatio);
+ } else {
+ $askedWidth = round($askedHeight * $originalRatio);
+ }
+
+ return [(int)$askedWidth, (int)$askedHeight];
+ }
+
+ /**
+ * Clips or stretches the dimensions so that they fit in the boundaries
+ *
+ * @param int $askedWidth
+ * @param int $askedHeight
+ *
+ * @return array
+ */
+ private function fixSize($askedWidth, $askedHeight) {
+ if ($this->scalingUp) {
+ $askedWidth = min($this->configMaxWidth, $askedWidth);
+ $askedHeight = min($this->configMaxHeight, $askedHeight);
+ }
- $this->assertEquals($this->user . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/150-150.png', $isCached);
+ return [(int)$askedWidth, (int)$askedHeight];
}
- */
}
diff --git a/tests/lib/preview/bitmap.php b/tests/lib/preview/bitmap.php
new file mode 100644
index 00000000000..49112852e29
--- /dev/null
+++ b/tests/lib/preview/bitmap.php
@@ -0,0 +1,36 @@
+<?php
+/**
+ * @author Olivier Paroz <owncloud@interfasys.ch>
+ *
+ * @copyright Copyright (c) 2015, ownCloud, Inc.
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License, version 3,
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+namespace Test\Preview;
+
+class Bitmap extends Provider {
+
+ public function setUp() {
+ parent::setUp();
+
+ $fileName = 'testimage.eps';
+ $this->imgPath = $this->prepareTestFile($fileName, \OC::$SERVERROOT . '/tests/data/' . $fileName);
+ $this->width = 2400;
+ $this->height = 1707;
+ $this->provider = new \OC\Preview\Postscript;
+ }
+
+}
diff --git a/tests/lib/preview/image.php b/tests/lib/preview/image.php
new file mode 100644
index 00000000000..af46f4e4a66
--- /dev/null
+++ b/tests/lib/preview/image.php
@@ -0,0 +1,36 @@
+<?php
+/**
+ * @author Olivier Paroz <owncloud@interfasys.ch>
+ *
+ * @copyright Copyright (c) 2015, ownCloud, Inc.
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License, version 3,
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+namespace Test\Preview;
+
+class Image extends Provider {
+
+ public function setUp() {
+ parent::setUp();
+
+ $fileName = 'testimage.jpg';
+ $this->imgPath = $this->prepareTestFile($fileName, \OC::$SERVERROOT . '/tests/data/' . $fileName);
+ $this->width = 1680;
+ $this->height = 1050;
+ $this->provider = new \OC\Preview\JPEG();
+ }
+
+}
diff --git a/tests/lib/preview/movie.php b/tests/lib/preview/movie.php
new file mode 100644
index 00000000000..c6b0c0f7322
--- /dev/null
+++ b/tests/lib/preview/movie.php
@@ -0,0 +1,46 @@
+<?php
+/**
+ * @author Olivier Paroz <owncloud@interfasys.ch>
+ *
+ * @copyright Copyright (c) 2015, ownCloud, Inc.
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License, version 3,
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+namespace Test\Preview;
+
+class Movie extends Provider {
+
+ public function setUp() {
+ $avconvBinary = \OC_Helper::findBinaryPath('avconv');
+ $ffmpegBinary = ($avconvBinary) ? null : \OC_Helper::findBinaryPath('ffmpeg');
+
+ if ($avconvBinary || $ffmpegBinary) {
+ parent::setUp();
+
+ \OC\Preview\Movie::$avconvBinary = $avconvBinary;
+ \OC\Preview\Movie::$ffmpegBinary = $ffmpegBinary;
+
+ $fileName = 'testimage.mp4';
+ $this->imgPath = $this->prepareTestFile($fileName, \OC::$SERVERROOT . '/tests/data/' . $fileName);
+ $this->width = 560;
+ $this->height = 320;
+ $this->provider = new \OC\Preview\Movie;
+ } else {
+ $this->markTestSkipped('No Movie provider present');
+ }
+ }
+
+}
diff --git a/tests/lib/preview/mp3.php b/tests/lib/preview/mp3.php
new file mode 100644
index 00000000000..ac3ab07a2bd
--- /dev/null
+++ b/tests/lib/preview/mp3.php
@@ -0,0 +1,36 @@
+<?php
+/**
+ * @author Olivier Paroz <owncloud@interfasys.ch>
+ *
+ * @copyright Copyright (c) 2015, ownCloud, Inc.
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License, version 3,
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+namespace Test\Preview;
+
+class MP3 extends Provider {
+
+ public function setUp() {
+ parent::setUp();
+
+ $fileName = 'testimage.mp3';
+ $this->imgPath = $this->prepareTestFile($fileName, \OC::$SERVERROOT . '/tests/data/' . $fileName);
+ $this->width = 200;
+ $this->height = 200;
+ $this->provider = new \OC\Preview\MP3;
+ }
+
+}
diff --git a/tests/lib/preview/office.php b/tests/lib/preview/office.php
new file mode 100644
index 00000000000..22eeb0aed33
--- /dev/null
+++ b/tests/lib/preview/office.php
@@ -0,0 +1,43 @@
+<?php
+/**
+ * @author Olivier Paroz <owncloud@interfasys.ch>
+ *
+ * @copyright Copyright (c) 2015, ownCloud, Inc.
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License, version 3,
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+namespace Test\Preview;
+
+class Office extends Provider {
+
+ public function setUp() {
+ $libreofficeBinary = \OC_Helper::findBinaryPath('libreoffice');
+ $openofficeBinary = ($libreofficeBinary) ? null : \OC_Helper::findBinaryPath('openoffice');
+
+ if ($libreofficeBinary || $openofficeBinary) {
+ parent::setUp();
+
+ $fileName = 'testimage.odt';
+ $this->imgPath = $this->prepareTestFile($fileName, \OC::$SERVERROOT . '/tests/data/' . $fileName);
+ $this->width = 595;
+ $this->height = 842;
+ $this->provider = new \OC\Preview\OpenDocument;
+ } else {
+ $this->markTestSkipped('No Office provider present');
+ }
+ }
+
+}
diff --git a/tests/lib/preview/provider.php b/tests/lib/preview/provider.php
new file mode 100644
index 00000000000..62b5dde5ec6
--- /dev/null
+++ b/tests/lib/preview/provider.php
@@ -0,0 +1,187 @@
+<?php
+/**
+ * @author Olivier Paroz <owncloud@interfasys.ch>
+ *
+ * @copyright Copyright (c) 2015, ownCloud, Inc.
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License, version 3,
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+namespace Test\Preview;
+
+abstract class Provider extends \Test\TestCase {
+
+ /** @var string */
+ protected $imgPath;
+ /** @var int */
+ protected $width;
+ /** @var int */
+ protected $height;
+ /** @var \OC\Preview\Provider */
+ protected $provider;
+ /** @var int */
+ protected $maxWidth = 1024;
+ /** @var int */
+ protected $maxHeight = 1024;
+ /** @var bool */
+ protected $scalingUp = false;
+ /** @var int */
+ protected $userId;
+ /** @var \OC\Files\View */
+ protected $rootView;
+ /** @var \OC\Files\Storage\Storage */
+ protected $storage;
+
+ protected function setUp() {
+ parent::setUp();
+
+ $userManager = \OC::$server->getUserManager();
+ $userManager->clearBackends();
+ $backend = new \OC_User_Dummy();
+ $userManager->registerBackend($backend);
+
+ $userId = $this->getUniqueID();
+ $backend->createUser($userId, $userId);
+ $this->loginAsUser($userId);
+
+ $this->storage = new \OC\Files\Storage\Temporary([]);
+ \OC\Files\Filesystem::mount($this->storage, [], '/' . $userId . '/');
+
+ $this->rootView = new \OC\Files\View('');
+ $this->rootView->mkdir('/' . $userId);
+ $this->rootView->mkdir('/' . $userId . '/files');
+
+ $this->userId = $userId;
+ }
+
+ protected function tearDown() {
+ $this->logout();
+
+ parent::tearDown();
+ }
+
+ public static function dimensionsDataProvider() {
+ return [
+ [-rand(5, 100), -rand(5, 100)],
+ [rand(5, 100), rand(5, 100)],
+ [-rand(5, 100), rand(5, 100)],
+ [rand(5, 100), -rand(5, 100)],
+ ];
+ }
+
+ /**
+ * Launches all the tests we have
+ *
+ * @dataProvider dimensionsDataProvider
+ *
+ * @param int $widthAdjustment
+ * @param int $heightAdjustment
+ */
+ public function testGetThumbnail($widthAdjustment, $heightAdjustment) {
+ $ratio = round($this->width / $this->height, 2);
+ $this->maxWidth = $this->width - $widthAdjustment;
+ $this->maxHeight = $this->height - $heightAdjustment;
+
+ // Testing code
+ /*print_r("w $this->width ");
+ print_r("h $this->height ");
+ print_r("r $ratio ");*/
+
+ $preview = $this->getPreview($this->provider);
+ // The TXT provider uses the max dimensions to create its canvas,
+ // so the ratio will always be the one of the max dimension canvas
+ if (!$this->provider instanceof \OC\Preview\TXT) {
+ $this->doesRatioMatch($preview, $ratio);
+ }
+ $this->doesPreviewFit($preview);
+ }
+
+ /**
+ * Adds the test file to the filesystem
+ *
+ * @param string $fileName name of the file to create
+ * @param string $fileContent path to file to use for test
+ *
+ * @return string
+ */
+ protected function prepareTestFile($fileName, $fileContent) {
+ $imgData = file_get_contents($fileContent);
+ $imgPath = '/' . $this->userId . '/files/' . $fileName;
+ $this->rootView->file_put_contents($imgPath, $imgData);
+
+ $scanner = $this->storage->getScanner();
+ $scanner->scan('');
+
+ return $imgPath;
+ }
+
+ /**
+ * Retrieves a max size thumbnail can be created
+ *
+ * @param \OC\Preview\Provider $provider
+ *
+ * @return bool|\OCP\IImage
+ */
+ private function getPreview($provider) {
+ $preview = $provider->getThumbnail($this->imgPath, $this->maxWidth, $this->maxHeight, $this->scalingUp, $this->rootView);
+
+ $this->assertNotEquals(false, $preview);
+ $this->assertEquals(true, $preview->valid());
+
+ return $preview;
+ }
+
+ /**
+ * Checks if the preview ratio matches the original ratio
+ *
+ * @param \OCP\IImage $preview
+ * @param int $ratio
+ */
+ private function doesRatioMatch($preview, $ratio) {
+ $previewRatio = round($preview->width() / $preview->height(), 2);
+ $this->assertEquals($ratio, $previewRatio);
+ }
+
+ /**
+ * Tests if a max size preview of smaller dimensions can be created
+ *
+ * @param \OCP\IImage $preview
+ */
+ private function doesPreviewFit($preview) {
+ $maxDimRatio = round($this->maxWidth / $this->maxHeight, 2);
+ $previewRatio = round($preview->width() / $preview->height(), 2);
+
+ // Testing code
+ /*print_r("mw $this->maxWidth ");
+ print_r("mh $this->maxHeight ");
+ print_r("mr $maxDimRatio ");
+ $pw = $preview->width();
+ $ph = $preview->height();
+ print_r("pw $pw ");
+ print_r("ph $ph ");
+ print_r("pr $previewRatio ");*/
+
+ if ($maxDimRatio < $previewRatio) {
+ $this->assertLessThanOrEqual($this->maxWidth, $preview->width());
+ $this->assertLessThan($this->maxHeight, $preview->height());
+ } elseif ($maxDimRatio > $previewRatio) {
+ $this->assertLessThan($this->maxWidth, $preview->width());
+ $this->assertLessThanOrEqual($this->maxHeight, $preview->height());
+ } else { // Original had to be resized
+ $this->assertLessThanOrEqual($this->maxWidth, $preview->width());
+ $this->assertLessThanOrEqual($this->maxHeight, $preview->height());
+ }
+ }
+}
diff --git a/tests/lib/preview/svg.php b/tests/lib/preview/svg.php
new file mode 100644
index 00000000000..768569c72ed
--- /dev/null
+++ b/tests/lib/preview/svg.php
@@ -0,0 +1,41 @@
+<?php
+/**
+ * @author Olivier Paroz <owncloud@interfasys.ch>
+ *
+ * @copyright Copyright (c) 2015, ownCloud, Inc.
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License, version 3,
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+namespace Test\Preview;
+
+class SVG extends Provider {
+
+ public function setUp() {
+ $checkImagick = new \Imagick();
+ if (count($checkImagick->queryFormats('SVG')) === 1) {
+ parent::setUp();
+
+ $fileName = 'testimagelarge.svg';
+ $this->imgPath = $this->prepareTestFile($fileName, \OC::$SERVERROOT . '/tests/data/' . $fileName);
+ $this->width = 3000;
+ $this->height = 2000;
+ $this->provider = new \OC\Preview\SVG;
+ } else {
+ $this->markTestSkipped('No SVG provider present');
+ }
+ }
+
+}
diff --git a/tests/lib/preview/txt.php b/tests/lib/preview/txt.php
new file mode 100644
index 00000000000..8bda86f25e3
--- /dev/null
+++ b/tests/lib/preview/txt.php
@@ -0,0 +1,37 @@
+<?php
+/**
+ * @author Olivier Paroz <owncloud@interfasys.ch>
+ *
+ * @copyright Copyright (c) 2015, ownCloud, Inc.
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License, version 3,
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+namespace Test\Preview;
+
+class TXT extends Provider {
+
+ public function setUp() {
+ parent::setUp();
+
+ $fileName = 'lorem-big.txt';
+ $this->imgPath = $this->prepareTestFile($fileName, \OC::$SERVERROOT . '/tests/data/' . $fileName);
+ // Arbitrary width and length which won't be used to calculate the ratio
+ $this->width = 500;
+ $this->height = 200;
+ $this->provider = new \OC\Preview\TXT;
+ }
+
+}