UtilTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 OCA\Theming\ImageManager;
  8. use OCA\Theming\Util;
  9. use OCP\App\IAppManager;
  10. use OCP\Files\IAppData;
  11. use OCP\Files\NotFoundException;
  12. use OCP\Files\SimpleFS\ISimpleFile;
  13. use OCP\Files\SimpleFS\ISimpleFolder;
  14. use OCP\IConfig;
  15. use PHPUnit\Framework\MockObject\MockObject;
  16. use Test\TestCase;
  17. class UtilTest extends TestCase {
  18. /** @var Util */
  19. protected $util;
  20. /** @var IConfig|MockObject */
  21. protected $config;
  22. /** @var IAppData|MockObject */
  23. protected $appData;
  24. /** @var IAppManager|MockObject */
  25. protected $appManager;
  26. /** @var ImageManager|MockObject */
  27. protected $imageManager;
  28. protected function setUp(): void {
  29. parent::setUp();
  30. $this->config = $this->createMock(IConfig::class);
  31. $this->appData = $this->createMock(IAppData::class);
  32. $this->appManager = $this->createMock(IAppManager::class);
  33. $this->imageManager = $this->createMock(ImageManager::class);
  34. $this->util = new Util($this->config, $this->appManager, $this->appData, $this->imageManager);
  35. }
  36. public function dataColorContrast() {
  37. return [
  38. ['#ffffff', '#FFFFFF', 1],
  39. ['#000000', '#000000', 1],
  40. ['#ffffff', '#000000', 21],
  41. ['#000000', '#FFFFFF', 21],
  42. ['#9E9E9E', '#353535', 4.578],
  43. ['#353535', '#9E9E9E', 4.578],
  44. ];
  45. }
  46. /**
  47. * @dataProvider dataColorContrast
  48. */
  49. public function testColorContrast(string $color1, string $color2, $contrast) {
  50. $this->assertEqualsWithDelta($contrast, $this->util->colorContrast($color1, $color2), .001);
  51. }
  52. public function dataInvertTextColor() {
  53. return [
  54. ['#ffffff', true],
  55. ['#000000', false],
  56. ['#00679e', false],
  57. ['#ffff00', true],
  58. ];
  59. }
  60. /**
  61. * @dataProvider dataInvertTextColor
  62. */
  63. public function testInvertTextColor($color, $expected) {
  64. $invert = $this->util->invertTextColor($color);
  65. $this->assertEquals($expected, $invert);
  66. }
  67. public function testCalculateLuminanceLight() {
  68. $luminance = $this->util->calculateLuminance('#ffffff');
  69. $this->assertEquals(1, $luminance);
  70. }
  71. public function testCalculateLuminanceDark() {
  72. $luminance = $this->util->calculateLuminance('#000000');
  73. $this->assertEquals(0, $luminance);
  74. }
  75. public function testCalculateLuminanceLightShorthand() {
  76. $luminance = $this->util->calculateLuminance('#fff');
  77. $this->assertEquals(1, $luminance);
  78. }
  79. public function testCalculateLuminanceDarkShorthand() {
  80. $luminance = $this->util->calculateLuminance('#000');
  81. $this->assertEquals(0, $luminance);
  82. }
  83. public function testInvertTextColorInvalid() {
  84. $this->expectException(\Exception::class);
  85. $this->util->invertTextColor('aaabbbcccddd123');
  86. }
  87. public function testInvertTextColorEmpty() {
  88. $this->expectException(\Exception::class);
  89. $this->util->invertTextColor('');
  90. }
  91. public function testElementColorDefaultBlack() {
  92. $elementColor = $this->util->elementColor("#000000");
  93. $this->assertEquals('#4d4d4d', $elementColor);
  94. }
  95. public function testElementColorDefaultWhite() {
  96. $elementColor = $this->util->elementColor("#ffffff");
  97. $this->assertEquals('#b3b3b3', $elementColor);
  98. }
  99. public function testElementColorBlackOnDarkBackground() {
  100. $elementColor = $this->util->elementColor("#000000", false);
  101. $this->assertEquals('#4d4d4d', $elementColor);
  102. }
  103. public function testElementColorBlackOnBrightBackground() {
  104. $elementColor = $this->util->elementColor("#000000", true);
  105. $this->assertEquals('#000000', $elementColor);
  106. }
  107. public function testElementColorWhiteOnBrightBackground() {
  108. $elementColor = $this->util->elementColor('#ffffff', true);
  109. $this->assertEquals('#b3b3b3', $elementColor);
  110. }
  111. public function testElementColorWhiteOnDarkBackground() {
  112. $elementColor = $this->util->elementColor('#ffffff', false);
  113. $this->assertEquals('#ffffff', $elementColor);
  114. }
  115. public function testGenerateRadioButtonWhite() {
  116. $button = $this->util->generateRadioButton('#ffffff');
  117. $expected = 'PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIGhlaWdodD0iMTYiIHdpZHRoPSIxNiI+PHBhdGggZD0iTTggMWE3IDcgMCAwIDAtNyA3IDcgNyAwIDAgMCA3IDcgNyA3IDAgMCAwIDctNyA3IDcgMCAwIDAtNy03em0wIDFhNiA2IDAgMCAxIDYgNiA2IDYgMCAwIDEtNiA2IDYgNiAwIDAgMS02LTYgNiA2IDAgMCAxIDYtNnptMCAyYTQgNCAwIDEgMCAwIDggNCA0IDAgMCAwIDAtOHoiIGZpbGw9IiNmZmZmZmYiLz48L3N2Zz4=';
  118. $this->assertEquals($expected, $button);
  119. }
  120. public function testGenerateRadioButtonBlack() {
  121. $button = $this->util->generateRadioButton('#000000');
  122. $expected = 'PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIGhlaWdodD0iMTYiIHdpZHRoPSIxNiI+PHBhdGggZD0iTTggMWE3IDcgMCAwIDAtNyA3IDcgNyAwIDAgMCA3IDcgNyA3IDAgMCAwIDctNyA3IDcgMCAwIDAtNy03em0wIDFhNiA2IDAgMCAxIDYgNiA2IDYgMCAwIDEtNiA2IDYgNiAwIDAgMS02LTYgNiA2IDAgMCAxIDYtNnptMCAyYTQgNCAwIDEgMCAwIDggNCA0IDAgMCAwIDAtOHoiIGZpbGw9IiMwMDAwMDAiLz48L3N2Zz4=';
  123. $this->assertEquals($expected, $button);
  124. }
  125. /**
  126. * @dataProvider dataGetAppIcon
  127. */
  128. public function testGetAppIcon($app, $expected) {
  129. $this->appData->expects($this->any())
  130. ->method('getFolder')
  131. ->with('global/images')
  132. ->willThrowException(new NotFoundException());
  133. $this->appManager->expects($this->once())
  134. ->method('getAppPath')
  135. ->with($app)
  136. ->willReturn(\OC_App::getAppPath($app));
  137. $icon = $this->util->getAppIcon($app);
  138. $this->assertEquals($expected, $icon);
  139. }
  140. public function dataGetAppIcon() {
  141. return [
  142. ['user_ldap', \OC_App::getAppPath('user_ldap') . '/img/app.svg'],
  143. ['noapplikethis', \OC::$SERVERROOT . '/core/img/logo/logo.svg'],
  144. ['comments', \OC_App::getAppPath('comments') . '/img/comments.svg'],
  145. ];
  146. }
  147. public function testGetAppIconThemed() {
  148. $file = $this->createMock(ISimpleFile::class);
  149. $folder = $this->createMock(ISimpleFolder::class);
  150. $folder->expects($this->once())
  151. ->method('getFile')
  152. ->with('logo')
  153. ->willReturn($file);
  154. $this->appData->expects($this->once())
  155. ->method('getFolder')
  156. ->with('global/images')
  157. ->willReturn($folder);
  158. $icon = $this->util->getAppIcon('noapplikethis');
  159. $this->assertEquals($file, $icon);
  160. }
  161. /**
  162. * @dataProvider dataGetAppImage
  163. */
  164. public function testGetAppImage($app, $image, $expected) {
  165. if ($app !== 'core') {
  166. $this->appManager->expects($this->once())
  167. ->method('getAppPath')
  168. ->with($app)
  169. ->willReturn(\OC_App::getAppPath($app));
  170. }
  171. $this->assertEquals($expected, $this->util->getAppImage($app, $image));
  172. }
  173. public function dataGetAppImage() {
  174. return [
  175. ['core', 'logo/logo.svg', \OC::$SERVERROOT . '/core/img/logo/logo.svg'],
  176. ['files', 'folder', \OC::$SERVERROOT . '/apps/files/img/folder.svg'],
  177. ['files', 'folder.svg', \OC::$SERVERROOT . '/apps/files/img/folder.svg'],
  178. ['noapplikethis', 'foobar.svg', false],
  179. ];
  180. }
  181. public function testColorizeSvg() {
  182. $input = "#0082c9 #0082C9 #000000 #FFFFFF";
  183. $expected = "#AAAAAA #AAAAAA #000000 #FFFFFF";
  184. $result = $this->util->colorizeSvg($input, '#AAAAAA');
  185. $this->assertEquals($expected, $result);
  186. }
  187. public function testIsAlreadyThemedFalse() {
  188. $this->config->expects($this->once())
  189. ->method('getSystemValue')
  190. ->with('theme', '')
  191. ->willReturn('');
  192. $actual = $this->util->isAlreadyThemed();
  193. $this->assertFalse($actual);
  194. }
  195. public function testIsAlreadyThemedTrue() {
  196. $this->config->expects($this->once())
  197. ->method('getSystemValue')
  198. ->with('theme', '')
  199. ->willReturn('example');
  200. $actual = $this->util->isAlreadyThemed();
  201. $this->assertTrue($actual);
  202. }
  203. public function dataIsBackgroundThemed() {
  204. return [
  205. ['', false],
  206. ['png', true],
  207. ['backgroundColor', false],
  208. ];
  209. }
  210. /**
  211. * @dataProvider dataIsBackgroundThemed
  212. */
  213. public function testIsBackgroundThemed($backgroundMime, $expected) {
  214. $this->config->expects($this->once())
  215. ->method('getAppValue')
  216. ->with('theming', 'backgroundMime', '')
  217. ->willReturn($backgroundMime);
  218. $this->assertEquals($expected, $this->util->isBackgroundThemed());
  219. }
  220. }