UtilTest.php 7.6 KB

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