UtilTest.php 7.6 KB

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