Provider.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * @author Olivier Paroz <owncloud@interfasys.ch>
  4. *
  5. * @copyright Copyright (c) 2015, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace Test\Preview;
  22. abstract class Provider extends \Test\TestCase {
  23. /** @var string */
  24. protected $imgPath;
  25. /** @var int */
  26. protected $width;
  27. /** @var int */
  28. protected $height;
  29. /** @var \OC\Preview\Provider */
  30. protected $provider;
  31. /** @var int */
  32. protected $maxWidth = 1024;
  33. /** @var int */
  34. protected $maxHeight = 1024;
  35. /** @var bool */
  36. protected $scalingUp = false;
  37. /** @var int */
  38. protected $userId;
  39. /** @var \OC\Files\View */
  40. protected $rootView;
  41. /** @var \OC\Files\Storage\Storage */
  42. protected $storage;
  43. protected function setUp() {
  44. parent::setUp();
  45. $userManager = \OC::$server->getUserManager();
  46. $userManager->clearBackends();
  47. $backend = new \Test\Util\User\Dummy();
  48. $userManager->registerBackend($backend);
  49. $userId = $this->getUniqueID();
  50. $backend->createUser($userId, $userId);
  51. $this->loginAsUser($userId);
  52. $this->storage = new \OC\Files\Storage\Temporary([]);
  53. \OC\Files\Filesystem::mount($this->storage, [], '/' . $userId . '/');
  54. $this->rootView = new \OC\Files\View('');
  55. $this->rootView->mkdir('/' . $userId);
  56. $this->rootView->mkdir('/' . $userId . '/files');
  57. $this->userId = $userId;
  58. }
  59. protected function tearDown() {
  60. $this->logout();
  61. parent::tearDown();
  62. }
  63. public static function dimensionsDataProvider() {
  64. return [
  65. [-rand(5, 100), -rand(5, 100)],
  66. [rand(5, 100), rand(5, 100)],
  67. [-rand(5, 100), rand(5, 100)],
  68. [rand(5, 100), -rand(5, 100)],
  69. ];
  70. }
  71. /**
  72. * Launches all the tests we have
  73. *
  74. * @dataProvider dimensionsDataProvider
  75. * @requires extension imagick
  76. *
  77. * @param int $widthAdjustment
  78. * @param int $heightAdjustment
  79. */
  80. public function testGetThumbnail($widthAdjustment, $heightAdjustment) {
  81. $ratio = round($this->width / $this->height, 2);
  82. $this->maxWidth = $this->width - $widthAdjustment;
  83. $this->maxHeight = $this->height - $heightAdjustment;
  84. // Testing code
  85. /*print_r("w $this->width ");
  86. print_r("h $this->height ");
  87. print_r("r $ratio ");*/
  88. $preview = $this->getPreview($this->provider);
  89. // The TXT provider uses the max dimensions to create its canvas,
  90. // so the ratio will always be the one of the max dimension canvas
  91. if (!$this->provider instanceof \OC\Preview\TXT) {
  92. $this->doesRatioMatch($preview, $ratio);
  93. }
  94. $this->doesPreviewFit($preview);
  95. }
  96. /**
  97. * Adds the test file to the filesystem
  98. *
  99. * @param string $fileName name of the file to create
  100. * @param string $fileContent path to file to use for test
  101. *
  102. * @return string
  103. */
  104. protected function prepareTestFile($fileName, $fileContent) {
  105. $imgData = file_get_contents($fileContent);
  106. $imgPath = '/' . $this->userId . '/files/' . $fileName;
  107. $this->rootView->file_put_contents($imgPath, $imgData);
  108. $scanner = $this->storage->getScanner();
  109. $scanner->scan('');
  110. return $imgPath;
  111. }
  112. /**
  113. * Retrieves a max size thumbnail can be created
  114. *
  115. * @param \OC\Preview\Provider $provider
  116. *
  117. * @return bool|\OCP\IImage
  118. */
  119. private function getPreview($provider) {
  120. $preview = $provider->getThumbnail($this->imgPath, $this->maxWidth, $this->maxHeight, $this->scalingUp, $this->rootView);
  121. $this->assertNotEquals(false, $preview);
  122. $this->assertEquals(true, $preview->valid());
  123. return $preview;
  124. }
  125. /**
  126. * Checks if the preview ratio matches the original ratio
  127. *
  128. * @param \OCP\IImage $preview
  129. * @param int $ratio
  130. */
  131. private function doesRatioMatch($preview, $ratio) {
  132. $previewRatio = round($preview->width() / $preview->height(), 2);
  133. $this->assertEquals($ratio, $previewRatio);
  134. }
  135. /**
  136. * Tests if a max size preview of smaller dimensions can be created
  137. *
  138. * @param \OCP\IImage $preview
  139. */
  140. private function doesPreviewFit($preview) {
  141. $maxDimRatio = round($this->maxWidth / $this->maxHeight, 2);
  142. $previewRatio = round($preview->width() / $preview->height(), 2);
  143. // Testing code
  144. /*print_r("mw $this->maxWidth ");
  145. print_r("mh $this->maxHeight ");
  146. print_r("mr $maxDimRatio ");
  147. $pw = $preview->width();
  148. $ph = $preview->height();
  149. print_r("pw $pw ");
  150. print_r("ph $ph ");
  151. print_r("pr $previewRatio ");*/
  152. if ($maxDimRatio < $previewRatio) {
  153. $this->assertLessThanOrEqual($this->maxWidth, $preview->width());
  154. $this->assertLessThan($this->maxHeight, $preview->height());
  155. } elseif ($maxDimRatio > $previewRatio) {
  156. $this->assertLessThan($this->maxWidth, $preview->width());
  157. $this->assertLessThanOrEqual($this->maxHeight, $preview->height());
  158. } else { // Original had to be resized
  159. $this->assertLessThanOrEqual($this->maxWidth, $preview->width());
  160. $this->assertLessThanOrEqual($this->maxHeight, $preview->height());
  161. }
  162. }
  163. }