UtilTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 testElementColorDefault() {
  97. $elementColor = $this->util->elementColor("#000000");
  98. $this->assertEquals('#000000', $elementColor);
  99. }
  100. public function testElementColorOnDarkBackground() {
  101. $elementColor = $this->util->elementColor("#000000", false);
  102. $this->assertEquals('#555555', $elementColor);
  103. }
  104. public function testElementColorOnBrightBackground() {
  105. $elementColor = $this->util->elementColor('#ffffff');
  106. $this->assertEquals('#aaaaaa', $elementColor);
  107. }
  108. public function testGenerateRadioButtonWhite() {
  109. $button = $this->util->generateRadioButton('#ffffff');
  110. $expected = 'PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIGhlaWdodD0iMTYiIHdpZHRoPSIxNiI+PHBhdGggZD0iTTggMWE3IDcgMCAwIDAtNyA3IDcgNyAwIDAgMCA3IDcgNyA3IDAgMCAwIDctNyA3IDcgMCAwIDAtNy03em0wIDFhNiA2IDAgMCAxIDYgNiA2IDYgMCAwIDEtNiA2IDYgNiAwIDAgMS02LTYgNiA2IDAgMCAxIDYtNnptMCAyYTQgNCAwIDEgMCAwIDggNCA0IDAgMCAwIDAtOHoiIGZpbGw9IiNmZmZmZmYiLz48L3N2Zz4=';
  111. $this->assertEquals($expected, $button);
  112. }
  113. public function testGenerateRadioButtonBlack() {
  114. $button = $this->util->generateRadioButton('#000000');
  115. $expected = 'PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIGhlaWdodD0iMTYiIHdpZHRoPSIxNiI+PHBhdGggZD0iTTggMWE3IDcgMCAwIDAtNyA3IDcgNyAwIDAgMCA3IDcgNyA3IDAgMCAwIDctNyA3IDcgMCAwIDAtNy03em0wIDFhNiA2IDAgMCAxIDYgNiA2IDYgMCAwIDEtNiA2IDYgNiAwIDAgMS02LTYgNiA2IDAgMCAxIDYtNnptMCAyYTQgNCAwIDEgMCAwIDggNCA0IDAgMCAwIDAtOHoiIGZpbGw9IiMwMDAwMDAiLz48L3N2Zz4=';
  116. $this->assertEquals($expected, $button);
  117. }
  118. /**
  119. * @dataProvider dataGetAppIcon
  120. */
  121. public function testGetAppIcon($app, $expected) {
  122. $this->appData->expects($this->any())
  123. ->method('getFolder')
  124. ->with('images')
  125. ->willThrowException(new NotFoundException());
  126. $this->appManager->expects($this->once())
  127. ->method('getAppPath')
  128. ->with($app)
  129. ->willReturn(\OC_App::getAppPath($app));
  130. $icon = $this->util->getAppIcon($app);
  131. $this->assertEquals($expected, $icon);
  132. }
  133. public function dataGetAppIcon() {
  134. return [
  135. ['user_ldap', \OC_App::getAppPath('user_ldap') . '/img/app.svg'],
  136. ['noapplikethis', \OC::$SERVERROOT . '/core/img/logo/logo.svg'],
  137. ['comments', \OC_App::getAppPath('comments') . '/img/comments.svg'],
  138. ];
  139. }
  140. public function testGetAppIconThemed() {
  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('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) {
  158. if ($app !== 'core') {
  159. $this->appManager->expects($this->once())
  160. ->method('getAppPath')
  161. ->with($app)
  162. ->willReturn(\OC_App::getAppPath($app));
  163. }
  164. $this->assertEquals($expected, $this->util->getAppImage($app, $image));
  165. }
  166. public function dataGetAppImage() {
  167. return [
  168. ['core', 'logo/logo.svg', \OC::$SERVERROOT . '/core/img/logo/logo.svg'],
  169. ['files', 'external', \OC::$SERVERROOT . '/apps/files/img/external.svg'],
  170. ['files', 'external.svg', \OC::$SERVERROOT . '/apps/files/img/external.svg'],
  171. ['noapplikethis', 'foobar.svg', false],
  172. ];
  173. }
  174. public function testColorizeSvg() {
  175. $input = "#0082c9 #0082C9 #000000 #FFFFFF";
  176. $expected = "#AAAAAA #AAAAAA #000000 #FFFFFF";
  177. $result = $this->util->colorizeSvg($input, '#AAAAAA');
  178. $this->assertEquals($expected, $result);
  179. }
  180. public function testIsAlreadyThemedFalse() {
  181. $this->config->expects($this->once())
  182. ->method('getSystemValue')
  183. ->with('theme', '')
  184. ->willReturn('');
  185. $actual = $this->util->isAlreadyThemed();
  186. $this->assertFalse($actual);
  187. }
  188. public function testIsAlreadyThemedTrue() {
  189. $this->config->expects($this->once())
  190. ->method('getSystemValue')
  191. ->with('theme', '')
  192. ->willReturn('example');
  193. $actual = $this->util->isAlreadyThemed();
  194. $this->assertTrue($actual);
  195. }
  196. public function dataIsBackgroundThemed() {
  197. return [
  198. ['', false],
  199. ['png', true],
  200. ['backgroundColor', false],
  201. ];
  202. }
  203. /**
  204. * @dataProvider dataIsBackgroundThemed
  205. */
  206. public function testIsBackgroundThemed($backgroundMime, $expected) {
  207. $this->config->expects($this->once())
  208. ->method('getAppValue')
  209. ->with('theming', 'backgroundMime', '')
  210. ->willReturn($backgroundMime);
  211. $this->assertEquals($expected, $this->util->isBackgroundThemed());
  212. }
  213. }