UtilTest.php 7.5 KB

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