You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

338 lines
10 KiB

  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Roeland Jago Douma <roeland@famdouma.nl>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace Test\Preview;
  24. use OC\Preview\Generator;
  25. use OC\Preview\GeneratorHelper;
  26. use OCP\Files\File;
  27. use OCP\Files\IAppData;
  28. use OCP\Files\NotFoundException;
  29. use OCP\Files\SimpleFS\ISimpleFile;
  30. use OCP\Files\SimpleFS\ISimpleFolder;
  31. use OCP\IConfig;
  32. use OCP\IImage;
  33. use OCP\IPreview;
  34. use OCP\Preview\IProvider;
  35. class GeneratorTest extends \Test\TestCase {
  36. /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
  37. private $config;
  38. /** @var IPreview|\PHPUnit_Framework_MockObject_MockObject */
  39. private $previewManager;
  40. /** @var IAppData|\PHPUnit_Framework_MockObject_MockObject */
  41. private $appData;
  42. /** @var GeneratorHelper|\PHPUnit_Framework_MockObject_MockObject */
  43. private $helper;
  44. /** @var Generator */
  45. private $generator;
  46. public function setUp() {
  47. parent::setUp();
  48. $this->config = $this->createMock(IConfig::class);
  49. $this->previewManager = $this->createMock(IPreview::class);
  50. $this->appData = $this->createMock(IAppData::class);
  51. $this->helper = $this->createMock(GeneratorHelper::class);
  52. $this->generator = new Generator(
  53. $this->config,
  54. $this->previewManager,
  55. $this->appData,
  56. $this->helper
  57. );
  58. }
  59. public function testGetCachedPreview() {
  60. $file = $this->createMock(File::class);
  61. $file->method('getMimeType')
  62. ->willReturn('myMimeType');
  63. $file->method('getId')
  64. ->willReturn(42);
  65. $this->previewManager->method('isMimeSupported')
  66. ->with($this->equalTo('myMimeType'))
  67. ->willReturn(true);
  68. $previewFolder = $this->createMock(ISimpleFolder::class);
  69. $this->appData->method('getFolder')
  70. ->with($this->equalTo(42))
  71. ->willReturn($previewFolder);
  72. $maxPreview = $this->createMock(ISimpleFile::class);
  73. $maxPreview->method('getName')
  74. ->willReturn('1000-1000-max.png');
  75. $previewFolder->method('getDirectoryListing')
  76. ->willReturn([$maxPreview]);
  77. $previewFile = $this->createMock(ISimpleFile::class);
  78. $previewFolder->method('getFile')
  79. ->with($this->equalTo('128-128.png'))
  80. ->willReturn($previewFile);
  81. $result = $this->generator->getPreview($file, 100, 100);
  82. $this->assertSame($previewFile, $result);
  83. }
  84. public function testGetNewPreview() {
  85. $file = $this->createMock(File::class);
  86. $file->method('getMimeType')
  87. ->willReturn('myMimeType');
  88. $file->method('getId')
  89. ->willReturn(42);
  90. $this->previewManager->method('isMimeSupported')
  91. ->with($this->equalTo('myMimeType'))
  92. ->willReturn(true);
  93. $previewFolder = $this->createMock(ISimpleFolder::class);
  94. $this->appData->method('getFolder')
  95. ->with($this->equalTo(42))
  96. ->willThrowException(new NotFoundException());
  97. $this->appData->method('newFolder')
  98. ->with($this->equalTo(42))
  99. ->willReturn($previewFolder);
  100. $this->config->method('getSystemValue')
  101. ->will($this->returnCallback(function($key, $defult) {
  102. return $defult;
  103. }));
  104. $invalidProvider = $this->createMock(IProvider::class);
  105. $validProvider = $this->createMock(IProvider::class);
  106. $this->previewManager->method('getProviders')
  107. ->willReturn([
  108. '/image\/png/' => ['wrongProvider'],
  109. '/myMimeType/' => ['brokenProvider', 'invalidProvider', 'validProvider'],
  110. ]);
  111. $this->helper->method('getProvider')
  112. ->will($this->returnCallback(function($provider) use ($invalidProvider, $validProvider) {
  113. if ($provider === 'wrongProvider') {
  114. $this->fail('Wrongprovider should not be constructed!');
  115. } else if ($provider === 'brokenProvider') {
  116. return false;
  117. } else if ($provider === 'invalidProvider') {
  118. return $invalidProvider;
  119. } else if ($provider === 'validProvider') {
  120. return $validProvider;
  121. }
  122. $this->fail('Unexpected provider requested');
  123. }));
  124. $image = $this->createMock(IImage::class);
  125. $image->method('width')->willReturn(2048);
  126. $image->method('height')->willReturn(2048);
  127. $this->helper->method('getThumbnail')
  128. ->will($this->returnCallback(function ($provider, $file, $x, $y) use ($invalidProvider, $validProvider, $image) {
  129. if ($provider === $validProvider) {
  130. return $image;
  131. } else {
  132. return false;
  133. }
  134. }));
  135. $image->method('data')
  136. ->willReturn('my data');
  137. $maxPreview = $this->createMock(ISimpleFile::class);
  138. $maxPreview->method('getName')->willReturn('2048-2048-max.png');
  139. $previewFile = $this->createMock(ISimpleFile::class);
  140. $previewFolder->method('getDirectoryListing')
  141. ->willReturn([]);
  142. $previewFolder->method('newFile')
  143. ->will($this->returnCallback(function($filename) use ($maxPreview, $previewFile) {
  144. if ($filename === '2048-2048-max.png') {
  145. return $maxPreview;
  146. } else if ($filename === '128-128.png') {
  147. return $previewFile;
  148. }
  149. $this->fail('Unexpected file');
  150. }));
  151. $maxPreview->expects($this->once())
  152. ->method('putContent')
  153. ->with($this->equalTo('my data'));
  154. $previewFolder->method('getFile')
  155. ->with($this->equalTo('128-128.png'))
  156. ->willThrowException(new NotFoundException());
  157. $image = $this->createMock(IImage::class);
  158. $this->helper->method('getImage')
  159. ->with($this->equalTo($maxPreview))
  160. ->willReturn($image);
  161. $image->expects($this->once())
  162. ->method('resize')
  163. ->with(128);
  164. $image->method('data')
  165. ->willReturn('my resized data');
  166. $previewFile->expects($this->once())
  167. ->method('putContent')
  168. ->with('my resized data');
  169. $result = $this->generator->getPreview($file, 100, 100);
  170. $this->assertSame($previewFile, $result);
  171. }
  172. public function testInvalidMimeType() {
  173. $this->expectException(NotFoundException::class);
  174. $file = $this->createMock(File::class);
  175. $this->previewManager->method('isMimeSupported')
  176. ->with('invalidType')
  177. ->willReturn(false);
  178. $this->generator->getPreview($file, 0, 0, true, IPreview::MODE_COVER, 'invalidType');
  179. }
  180. public function testNoProvider() {
  181. $file = $this->createMock(File::class);
  182. $file->method('getMimeType')
  183. ->willReturn('myMimeType');
  184. $file->method('getId')
  185. ->willReturn(42);
  186. $this->previewManager->method('isMimeSupported')
  187. ->with($this->equalTo('myMimeType'))
  188. ->willReturn(true);
  189. $previewFolder = $this->createMock(ISimpleFolder::class);
  190. $this->appData->method('getFolder')
  191. ->with($this->equalTo(42))
  192. ->willReturn($previewFolder);
  193. $previewFolder->method('getDirectoryListing')
  194. ->willReturn([]);
  195. $this->previewManager->method('getProviders')
  196. ->willReturn([]);
  197. $this->expectException(NotFoundException::class);
  198. $this->generator->getPreview($file, 100, 100);
  199. }
  200. public function dataSize() {
  201. return [
  202. [1024, 2048, 512, 512, false, IPreview::MODE_FILL, 256, 512],
  203. [1024, 2048, 512, 512, false, IPreview::MODE_COVER, 512, 1024],
  204. [1024, 2048, 512, 512, true, IPreview::MODE_FILL, 512, 512],
  205. [1024, 2048, 512, 512, true, IPreview::MODE_COVER, 512, 512],
  206. [1024, 2048, -1, 512, false, IPreview::MODE_COVER, 256, 512],
  207. [1024, 2048, 512, -1, false, IPreview::MODE_FILL, 512, 1024],
  208. [1024, 2048, 250, 1100, true, IPreview::MODE_COVER, 256, 1126],
  209. [1024, 1100, 250, 1100, true, IPreview::MODE_COVER, 250, 1100],
  210. [1024, 2048, 4096, 2048, false, IPreview::MODE_FILL, 1024, 2048],
  211. [1024, 2048, 4096, 2048, false, IPreview::MODE_COVER, 1024, 2048],
  212. [2048, 1024, 512, 512, false, IPreview::MODE_FILL, 512, 256],
  213. [2048, 1024, 512, 512, false, IPreview::MODE_COVER, 1024, 512],
  214. [2048, 1024, 512, 512, true, IPreview::MODE_FILL, 512, 512],
  215. [2048, 1024, 512, 512, true, IPreview::MODE_COVER, 512, 512],
  216. [2048, 1024, -1, 512, false, IPreview::MODE_FILL, 1024, 512],
  217. [2048, 1024, 512, -1, false, IPreview::MODE_COVER, 512, 256],
  218. [2048, 1024, 4096, 1024, true, IPreview::MODE_FILL, 2048, 512],
  219. [2048, 1024, 4096, 1024, true, IPreview::MODE_COVER, 2048, 512],
  220. ];
  221. }
  222. /**
  223. * @dataProvider dataSize
  224. *
  225. * @param int $maxX
  226. * @param int $maxY
  227. * @param int $reqX
  228. * @param int $reqY
  229. * @param bool $crop
  230. * @param string $mode
  231. * @param int $expectedX
  232. * @param int $expectedY
  233. */
  234. public function testCorrectSize($maxX, $maxY, $reqX, $reqY, $crop, $mode, $expectedX, $expectedY) {
  235. $file = $this->createMock(File::class);
  236. $file->method('getMimeType')
  237. ->willReturn('myMimeType');
  238. $file->method('getId')
  239. ->willReturn(42);
  240. $this->previewManager->method('isMimeSupported')
  241. ->with($this->equalTo('myMimeType'))
  242. ->willReturn(true);
  243. $previewFolder = $this->createMock(ISimpleFolder::class);
  244. $this->appData->method('getFolder')
  245. ->with($this->equalTo(42))
  246. ->willReturn($previewFolder);
  247. $maxPreview = $this->createMock(ISimpleFile::class);
  248. $maxPreview->method('getName')
  249. ->willReturn($maxX . '-' . $maxY . '-max.png');
  250. $previewFolder->method('getDirectoryListing')
  251. ->willReturn([$maxPreview]);
  252. $filename = $expectedX . '-' . $expectedY;
  253. if ($crop) {
  254. $filename .= '-crop';
  255. }
  256. $filename .= '.png';
  257. $previewFolder->method('getFile')
  258. ->with($this->equalTo($filename))
  259. ->willThrowException(new NotFoundException());
  260. $image = $this->createMock(IImage::class);
  261. $this->helper->method('getImage')
  262. ->with($this->equalTo($maxPreview))
  263. ->willReturn($image);
  264. $image->method('height')->willReturn($maxY);
  265. $image->method('width')->willReturn($maxX);
  266. $preview = $this->createMock(ISimpleFile::class);
  267. $previewFolder->method('newFile')
  268. ->with($this->equalTo($filename))
  269. ->willReturn($preview);
  270. $result = $this->generator->getPreview($file, $reqX, $reqY, $crop, $mode);
  271. $this->assertSame($preview, $result);
  272. }
  273. }