1
0

IconBuilderTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\Theming\Tests;
  7. use OC\Files\AppData\AppData;
  8. use OCA\Theming\IconBuilder;
  9. use OCA\Theming\ImageManager;
  10. use OCA\Theming\ThemingDefaults;
  11. use OCA\Theming\Util;
  12. use OCP\App\IAppManager;
  13. use OCP\Files\NotFoundException;
  14. use OCP\IConfig;
  15. use OCP\ServerVersion;
  16. use PHPUnit\Framework\Error\Warning;
  17. use Test\TestCase;
  18. class IconBuilderTest extends TestCase {
  19. /** @var IConfig */
  20. protected $config;
  21. /** @var AppData */
  22. protected $appData;
  23. /** @var ThemingDefaults */
  24. protected $themingDefaults;
  25. /** @var Util */
  26. protected $util;
  27. /** @var ImageManager */
  28. protected $imageManager;
  29. /** @var IconBuilder */
  30. protected $iconBuilder;
  31. /** @var IAppManager */
  32. protected $appManager;
  33. protected function setUp(): void {
  34. parent::setUp();
  35. $this->config = $this->createMock(IConfig::class);
  36. $this->appData = $this->createMock(AppData::class);
  37. $this->themingDefaults = $this->createMock(ThemingDefaults::class);
  38. $this->appManager = $this->createMock(IAppManager::class);
  39. $this->imageManager = $this->createMock(ImageManager::class);
  40. $this->util = new Util($this->createMock(ServerVersion::class), $this->config, $this->appManager, $this->appData, $this->imageManager);
  41. $this->iconBuilder = new IconBuilder($this->themingDefaults, $this->util, $this->imageManager);
  42. }
  43. private function checkImagick() {
  44. if (!extension_loaded('imagick')) {
  45. $this->markTestSkipped('Imagemagick is required for dynamic icon generation.');
  46. }
  47. $checkImagick = new \Imagick();
  48. if (count($checkImagick->queryFormats('SVG')) < 1) {
  49. $this->markTestSkipped('No SVG provider present.');
  50. }
  51. if (count($checkImagick->queryFormats('PNG')) < 1) {
  52. $this->markTestSkipped('No PNG provider present.');
  53. }
  54. }
  55. public function dataRenderAppIcon() {
  56. return [
  57. ['core', '#0082c9', 'touch-original.png'],
  58. ['core', '#FF0000', 'touch-core-red.png'],
  59. ['testing', '#FF0000', 'touch-testing-red.png'],
  60. ['comments', '#0082c9', 'touch-comments.png'],
  61. ['core', '#0082c9', 'touch-original-png.png'],
  62. ];
  63. }
  64. /**
  65. * @dataProvider dataRenderAppIcon
  66. * @param $app
  67. * @param $color
  68. * @param $file
  69. */
  70. public function testRenderAppIcon($app, $color, $file): void {
  71. $this->checkImagick();
  72. $this->themingDefaults->expects($this->once())
  73. ->method('getColorPrimary')
  74. ->willReturn($color);
  75. $this->appData->expects($this->once())
  76. ->method('getFolder')
  77. ->with('global/images')
  78. ->willThrowException(new NotFoundException());
  79. $expectedIcon = new \Imagick(realpath(__DIR__) . '/data/' . $file);
  80. $icon = $this->iconBuilder->renderAppIcon($app, 512);
  81. $this->assertEquals(true, $icon->valid());
  82. $this->assertEquals(512, $icon->getImageWidth());
  83. $this->assertEquals(512, $icon->getImageHeight());
  84. $this->assertEquals($icon, $expectedIcon);
  85. $icon->destroy();
  86. $expectedIcon->destroy();
  87. // FIXME: We may need some comparison of the generated and the test images
  88. // cloud be something like $expectedIcon->compareImages($icon, Imagick::METRIC_MEANABSOLUTEERROR)[1])
  89. }
  90. /**
  91. * @dataProvider dataRenderAppIcon
  92. * @param $app
  93. * @param $color
  94. * @param $file
  95. */
  96. public function testGetTouchIcon($app, $color, $file): void {
  97. $this->checkImagick();
  98. $this->themingDefaults->expects($this->once())
  99. ->method('getColorPrimary')
  100. ->willReturn($color);
  101. $this->appData->expects($this->once())
  102. ->method('getFolder')
  103. ->with('global/images')
  104. ->willThrowException(new NotFoundException());
  105. $expectedIcon = new \Imagick(realpath(__DIR__) . '/data/' . $file);
  106. $icon = new \Imagick();
  107. $icon->readImageBlob($this->iconBuilder->getTouchIcon($app));
  108. $this->assertEquals(true, $icon->valid());
  109. $this->assertEquals(512, $icon->getImageWidth());
  110. $this->assertEquals(512, $icon->getImageHeight());
  111. $this->assertEquals($icon, $expectedIcon);
  112. $icon->destroy();
  113. $expectedIcon->destroy();
  114. // FIXME: We may need some comparison of the generated and the test images
  115. // cloud be something like $expectedIcon->compareImages($icon, Imagick::METRIC_MEANABSOLUTEERROR)[1])
  116. }
  117. /**
  118. * @dataProvider dataRenderAppIcon
  119. * @param $app
  120. * @param $color
  121. * @param $file
  122. */
  123. public function testGetFavicon($app, $color, $file): void {
  124. $this->checkImagick();
  125. $this->imageManager->expects($this->once())
  126. ->method('shouldReplaceIcons')
  127. ->willReturn(true);
  128. $this->themingDefaults->expects($this->once())
  129. ->method('getColorPrimary')
  130. ->willReturn($color);
  131. $this->appData->expects($this->once())
  132. ->method('getFolder')
  133. ->with('global/images')
  134. ->willThrowException(new NotFoundException());
  135. $expectedIcon = new \Imagick(realpath(__DIR__) . '/data/' . $file);
  136. $actualIcon = $this->iconBuilder->getFavicon($app);
  137. $icon = new \Imagick();
  138. $icon->setFormat('ico');
  139. $icon->readImageBlob($actualIcon);
  140. $this->assertEquals(true, $icon->valid());
  141. $this->assertEquals(128, $icon->getImageWidth());
  142. $this->assertEquals(128, $icon->getImageHeight());
  143. $icon->destroy();
  144. $expectedIcon->destroy();
  145. // FIXME: We may need some comparison of the generated and the test images
  146. // cloud be something like $expectedIcon->compareImages($icon, Imagick::METRIC_MEANABSOLUTEERROR)[1])
  147. }
  148. public function testGetFaviconNotFound(): void {
  149. $this->checkImagick();
  150. $this->expectWarning(Warning::class);
  151. $util = $this->getMockBuilder(Util::class)->disableOriginalConstructor()->getMock();
  152. $iconBuilder = new IconBuilder($this->themingDefaults, $util, $this->imageManager);
  153. $this->imageManager->expects($this->once())
  154. ->method('shouldReplaceIcons')
  155. ->willReturn(true);
  156. $util->expects($this->once())
  157. ->method('getAppIcon')
  158. ->willReturn('notexistingfile');
  159. $this->assertFalse($iconBuilder->getFavicon('noapp'));
  160. }
  161. public function testGetTouchIconNotFound(): void {
  162. $this->checkImagick();
  163. $this->expectWarning(Warning::class);
  164. $util = $this->getMockBuilder(Util::class)->disableOriginalConstructor()->getMock();
  165. $iconBuilder = new IconBuilder($this->themingDefaults, $util, $this->imageManager);
  166. $util->expects($this->once())
  167. ->method('getAppIcon')
  168. ->willReturn('notexistingfile');
  169. $this->assertFalse($iconBuilder->getTouchIcon('noapp'));
  170. }
  171. public function testColorSvgNotFound(): void {
  172. $this->checkImagick();
  173. $this->expectWarning(Warning::class);
  174. $util = $this->getMockBuilder(Util::class)->disableOriginalConstructor()->getMock();
  175. $iconBuilder = new IconBuilder($this->themingDefaults, $util, $this->imageManager);
  176. $util->expects($this->once())
  177. ->method('getAppImage')
  178. ->willReturn('notexistingfile');
  179. $this->assertFalse($iconBuilder->colorSvg('noapp', 'noimage'));
  180. }
  181. }