1
0

UtilTest.php 7.6 KB

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