1
0

UtilTest.php 7.6 KB

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