UtilTest.php 8.1 KB

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