UtilTest.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Julius Härtl <jus@bitgrid.net>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Julius Haertl <jus@bitgrid.net>
  8. * @author Julius Härtl <jus@bitgrid.net>
  9. * @author Michael Weimann <mail@michael-weimann.eu>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. *
  12. * @license GNU AGPL version 3 or any later version
  13. *
  14. * This program is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License as
  16. * published by the Free Software Foundation, either version 3 of the
  17. * License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  26. *
  27. */
  28. namespace OCA\Theming\Tests;
  29. use OCA\Theming\ImageManager;
  30. use OCA\Theming\Util;
  31. use OCP\App\IAppManager;
  32. use OCP\Files\IAppData;
  33. use OCP\Files\NotFoundException;
  34. use OCP\Files\SimpleFS\ISimpleFile;
  35. use OCP\Files\SimpleFS\ISimpleFolder;
  36. use OCP\IConfig;
  37. use PHPUnit\Framework\MockObject\MockObject;
  38. use Test\TestCase;
  39. class UtilTest extends TestCase {
  40. /** @var Util */
  41. protected $util;
  42. /** @var IConfig|MockObject */
  43. protected $config;
  44. /** @var IAppData|MockObject */
  45. protected $appData;
  46. /** @var IAppManager|MockObject */
  47. protected $appManager;
  48. /** @var ImageManager|MockObject */
  49. protected $imageManager;
  50. protected function setUp(): void {
  51. parent::setUp();
  52. $this->config = $this->createMock(IConfig::class);
  53. $this->appData = $this->createMock(IAppData::class);
  54. $this->appManager = $this->createMock(IAppManager::class);
  55. $this->imageManager = $this->createMock(ImageManager::class);
  56. $this->util = new Util($this->config, $this->appManager, $this->appData, $this->imageManager);
  57. }
  58. public function dataColorContrast() {
  59. return [
  60. ['#ffffff', '#FFFFFF', 1],
  61. ['#000000', '#000000', 1],
  62. ['#ffffff', '#000000', 21],
  63. ['#000000', '#FFFFFF', 21],
  64. ['#9E9E9E', '#353535', 4.578],
  65. ['#353535', '#9E9E9E', 4.578],
  66. ];
  67. }
  68. /**
  69. * @dataProvider dataColorContrast
  70. */
  71. public function testColorContrast(string $color1, string $color2, $contrast) {
  72. $this->assertEqualsWithDelta($contrast, $this->util->colorContrast($color1, $color2), .001);
  73. }
  74. public function dataInvertTextColor() {
  75. return [
  76. ['#ffffff', true],
  77. ['#000000', false],
  78. ['#00679e', false],
  79. ['#ffff00', true],
  80. ];
  81. }
  82. /**
  83. * @dataProvider dataInvertTextColor
  84. */
  85. public function testInvertTextColor($color, $expected) {
  86. $invert = $this->util->invertTextColor($color);
  87. $this->assertEquals($expected, $invert);
  88. }
  89. public function testCalculateLuminanceLight() {
  90. $luminance = $this->util->calculateLuminance('#ffffff');
  91. $this->assertEquals(1, $luminance);
  92. }
  93. public function testCalculateLuminanceDark() {
  94. $luminance = $this->util->calculateLuminance('#000000');
  95. $this->assertEquals(0, $luminance);
  96. }
  97. public function testCalculateLuminanceLightShorthand() {
  98. $luminance = $this->util->calculateLuminance('#fff');
  99. $this->assertEquals(1, $luminance);
  100. }
  101. public function testCalculateLuminanceDarkShorthand() {
  102. $luminance = $this->util->calculateLuminance('#000');
  103. $this->assertEquals(0, $luminance);
  104. }
  105. public function testInvertTextColorInvalid() {
  106. $this->expectException(\Exception::class);
  107. $this->util->invertTextColor('aaabbbcccddd123');
  108. }
  109. public function testInvertTextColorEmpty() {
  110. $this->expectException(\Exception::class);
  111. $this->util->invertTextColor('');
  112. }
  113. public function testElementColorDefaultBlack() {
  114. $elementColor = $this->util->elementColor("#000000");
  115. $this->assertEquals('#4d4d4d', $elementColor);
  116. }
  117. public function testElementColorDefaultWhite() {
  118. $elementColor = $this->util->elementColor("#ffffff");
  119. $this->assertEquals('#b3b3b3', $elementColor);
  120. }
  121. public function testElementColorBlackOnDarkBackground() {
  122. $elementColor = $this->util->elementColor("#000000", false);
  123. $this->assertEquals('#4d4d4d', $elementColor);
  124. }
  125. public function testElementColorBlackOnBrightBackground() {
  126. $elementColor = $this->util->elementColor("#000000", true);
  127. $this->assertEquals('#000000', $elementColor);
  128. }
  129. public function testElementColorWhiteOnBrightBackground() {
  130. $elementColor = $this->util->elementColor('#ffffff', true);
  131. $this->assertEquals('#b3b3b3', $elementColor);
  132. }
  133. public function testElementColorWhiteOnDarkBackground() {
  134. $elementColor = $this->util->elementColor('#ffffff', false);
  135. $this->assertEquals('#ffffff', $elementColor);
  136. }
  137. public function testGenerateRadioButtonWhite() {
  138. $button = $this->util->generateRadioButton('#ffffff');
  139. $expected = 'PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIGhlaWdodD0iMTYiIHdpZHRoPSIxNiI+PHBhdGggZD0iTTggMWE3IDcgMCAwIDAtNyA3IDcgNyAwIDAgMCA3IDcgNyA3IDAgMCAwIDctNyA3IDcgMCAwIDAtNy03em0wIDFhNiA2IDAgMCAxIDYgNiA2IDYgMCAwIDEtNiA2IDYgNiAwIDAgMS02LTYgNiA2IDAgMCAxIDYtNnptMCAyYTQgNCAwIDEgMCAwIDggNCA0IDAgMCAwIDAtOHoiIGZpbGw9IiNmZmZmZmYiLz48L3N2Zz4=';
  140. $this->assertEquals($expected, $button);
  141. }
  142. public function testGenerateRadioButtonBlack() {
  143. $button = $this->util->generateRadioButton('#000000');
  144. $expected = 'PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIGhlaWdodD0iMTYiIHdpZHRoPSIxNiI+PHBhdGggZD0iTTggMWE3IDcgMCAwIDAtNyA3IDcgNyAwIDAgMCA3IDcgNyA3IDAgMCAwIDctNyA3IDcgMCAwIDAtNy03em0wIDFhNiA2IDAgMCAxIDYgNiA2IDYgMCAwIDEtNiA2IDYgNiAwIDAgMS02LTYgNiA2IDAgMCAxIDYtNnptMCAyYTQgNCAwIDEgMCAwIDggNCA0IDAgMCAwIDAtOHoiIGZpbGw9IiMwMDAwMDAiLz48L3N2Zz4=';
  145. $this->assertEquals($expected, $button);
  146. }
  147. /**
  148. * @dataProvider dataGetAppIcon
  149. */
  150. public function testGetAppIcon($app, $expected) {
  151. $this->appData->expects($this->any())
  152. ->method('getFolder')
  153. ->with('global/images')
  154. ->willThrowException(new NotFoundException());
  155. $this->appManager->expects($this->once())
  156. ->method('getAppPath')
  157. ->with($app)
  158. ->willReturn(\OC_App::getAppPath($app));
  159. $icon = $this->util->getAppIcon($app);
  160. $this->assertEquals($expected, $icon);
  161. }
  162. public function dataGetAppIcon() {
  163. return [
  164. ['user_ldap', \OC_App::getAppPath('user_ldap') . '/img/app.svg'],
  165. ['noapplikethis', \OC::$SERVERROOT . '/core/img/logo/logo.svg'],
  166. ['comments', \OC_App::getAppPath('comments') . '/img/comments.svg'],
  167. ];
  168. }
  169. public function testGetAppIconThemed() {
  170. $file = $this->createMock(ISimpleFile::class);
  171. $folder = $this->createMock(ISimpleFolder::class);
  172. $folder->expects($this->once())
  173. ->method('getFile')
  174. ->with('logo')
  175. ->willReturn($file);
  176. $this->appData->expects($this->once())
  177. ->method('getFolder')
  178. ->with('global/images')
  179. ->willReturn($folder);
  180. $icon = $this->util->getAppIcon('noapplikethis');
  181. $this->assertEquals($file, $icon);
  182. }
  183. /**
  184. * @dataProvider dataGetAppImage
  185. */
  186. public function testGetAppImage($app, $image, $expected) {
  187. if ($app !== 'core') {
  188. $this->appManager->expects($this->once())
  189. ->method('getAppPath')
  190. ->with($app)
  191. ->willReturn(\OC_App::getAppPath($app));
  192. }
  193. $this->assertEquals($expected, $this->util->getAppImage($app, $image));
  194. }
  195. public function dataGetAppImage() {
  196. return [
  197. ['core', 'logo/logo.svg', \OC::$SERVERROOT . '/core/img/logo/logo.svg'],
  198. ['files', 'folder', \OC::$SERVERROOT . '/apps/files/img/folder.svg'],
  199. ['files', 'folder.svg', \OC::$SERVERROOT . '/apps/files/img/folder.svg'],
  200. ['noapplikethis', 'foobar.svg', false],
  201. ];
  202. }
  203. public function testColorizeSvg() {
  204. $input = "#0082c9 #0082C9 #000000 #FFFFFF";
  205. $expected = "#AAAAAA #AAAAAA #000000 #FFFFFF";
  206. $result = $this->util->colorizeSvg($input, '#AAAAAA');
  207. $this->assertEquals($expected, $result);
  208. }
  209. public function testIsAlreadyThemedFalse() {
  210. $this->config->expects($this->once())
  211. ->method('getSystemValue')
  212. ->with('theme', '')
  213. ->willReturn('');
  214. $actual = $this->util->isAlreadyThemed();
  215. $this->assertFalse($actual);
  216. }
  217. public function testIsAlreadyThemedTrue() {
  218. $this->config->expects($this->once())
  219. ->method('getSystemValue')
  220. ->with('theme', '')
  221. ->willReturn('example');
  222. $actual = $this->util->isAlreadyThemed();
  223. $this->assertTrue($actual);
  224. }
  225. public function dataIsBackgroundThemed() {
  226. return [
  227. ['', false],
  228. ['png', true],
  229. ['backgroundColor', false],
  230. ];
  231. }
  232. /**
  233. * @dataProvider dataIsBackgroundThemed
  234. */
  235. public function testIsBackgroundThemed($backgroundMime, $expected) {
  236. $this->config->expects($this->once())
  237. ->method('getAppValue')
  238. ->with('theming', 'backgroundMime', '')
  239. ->willReturn($backgroundMime);
  240. $this->assertEquals($expected, $this->util->isBackgroundThemed());
  241. }
  242. }