1
0

UtilTest.php 7.7 KB

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