1
0

IconBuilderTest.php 6.5 KB

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