Welcome to mirror list, hosted at ThFree Co, Russian Federation.

UtilTest.php « tests « theming « apps - github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 94e4c9cbcc84bd8ec1a91a8c368b4c3490afb5b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
/**
 * @copyright Copyright (c) 2016 Julius Härtl <jus@bitgrid.net>
 *
 * @author Joas Schilling <coding@schilljs.com>
 * @author Julius Haertl <jus@bitgrid.net>
 * @author Julius Härtl <jus@bitgrid.net>
 *
 * @license GNU AGPL version 3 or any later version
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
namespace OCA\Theming\Tests;

use OCA\Theming\Util;
use OCP\App\IAppManager;
use OCP\Files\IAppData;
use OCP\Files\NotFoundException;
use OCP\Files\SimpleFS\ISimpleFile;
use OCP\Files\SimpleFS\ISimpleFolder;
use OCP\IConfig;
use OCP\Files\IRootFolder;
use Test\TestCase;

class UtilTest extends TestCase {

	/** @var Util */
	protected $util;
	/** @var IConfig */
	protected $config;
	/** @var IAppData */
	protected $appData;
	/** @var IAppManager */
	protected $appManager;

	protected function setUp() {
		parent::setUp();
		$this->config = $this->createMock(IConfig::class);
		$this->appData = $this->createMock(IAppData::class);
		$this->appManager = $this->createMock(IAppManager::class);
		$this->util = new Util($this->config, $this->appManager, $this->appData);
	}

	public function testInvertTextColorLight() {
		$invert = $this->util->invertTextColor('#ffffff');
		$this->assertEquals(true, $invert);
	}

	public function testInvertTextColorDark() {
		$invert = $this->util->invertTextColor('#000000');
		$this->assertEquals(false, $invert);
	}

	public function testCalculateLuminanceLight() {
		$luminance = $this->util->calculateLuminance('#ffffff');
		$this->assertEquals(1, $luminance);
	}

	public function testCalculateLuminanceDark() {
		$luminance = $this->util->calculateLuminance('#000000');
		$this->assertEquals(0, $luminance);
	}

	public function testCalculateLuminanceLightShorthand() {
		$luminance = $this->util->calculateLuminance('#fff');
		$this->assertEquals(1, $luminance);
	}

	public function testCalculateLuminanceDarkShorthand() {
		$luminance = $this->util->calculateLuminance('#000');
		$this->assertEquals(0, $luminance);
	}
	public function testInvertTextColorInvalid() {
		$invert = $this->util->invertTextColor('aaabbbcccddd123');
		$this->assertEquals(false, $invert);
	}
	
	public function testInvertTextColorEmpty() {
		$invert = $this->util->invertTextColor('');
		$this->assertEquals(false, $invert);
	}

	public function testElementColorDefault() {
		$elementColor = $this->util->elementColor("#000000");
		$this->assertEquals('#000000', $elementColor);
	}

	public function testElementColorOnBrightBackground() {
		$elementColor = $this->util->elementColor('#ffffff');
		$this->assertEquals('#555555', $elementColor);
	}

	public function testGenerateRadioButtonWhite() {
		$button = $this->util->generateRadioButton('#ffffff');
		$expected = 'PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIGhlaWdodD0iMTYiIHdpZHRoPSIxNiI+PHBhdGggZD0iTTggMWE3IDcgMCAwIDAtNyA3IDcgNyAwIDAgMCA3IDcgNyA3IDAgMCAwIDctNyA3IDcgMCAwIDAtNy03em0wIDFhNiA2IDAgMCAxIDYgNiA2IDYgMCAwIDEtNiA2IDYgNiAwIDAgMS02LTYgNiA2IDAgMCAxIDYtNnptMCAyYTQgNCAwIDEgMCAwIDggNCA0IDAgMCAwIDAtOHoiIGZpbGw9IiNmZmZmZmYiLz48L3N2Zz4=';
		$this->assertEquals($expected, $button);
	}

	public function testGenerateRadioButtonBlack() {
		$button = $this->util->generateRadioButton('#000000');
		$expected = 'PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIGhlaWdodD0iMTYiIHdpZHRoPSIxNiI+PHBhdGggZD0iTTggMWE3IDcgMCAwIDAtNyA3IDcgNyAwIDAgMCA3IDcgNyA3IDAgMCAwIDctNyA3IDcgMCAwIDAtNy03em0wIDFhNiA2IDAgMCAxIDYgNiA2IDYgMCAwIDEtNiA2IDYgNiAwIDAgMS02LTYgNiA2IDAgMCAxIDYtNnptMCAyYTQgNCAwIDEgMCAwIDggNCA0IDAgMCAwIDAtOHoiIGZpbGw9IiMwMDAwMDAiLz48L3N2Zz4=';
		$this->assertEquals($expected, $button);
	}

	/**
	 * @dataProvider dataGetAppIcon
	 */
	public function testGetAppIcon($app, $expected) {
		$this->appData->expects($this->any())
			->method('getFolder')
			->with('images')
			->willThrowException(new NotFoundException());
		$this->appManager->expects($this->once())
			->method('getAppPath')
			->with($app)
			->willReturn(\OC_App::getAppPath($app));
		$icon = $this->util->getAppIcon($app);
		$this->assertEquals($expected, $icon);
	}

	public function dataGetAppIcon() {
		return [
			['user_ldap', \OC_App::getAppPath('user_ldap') . '/img/app.svg'],
			['noapplikethis', \OC::$SERVERROOT . '/core/img/logo.svg'],
			['comments', \OC_App::getAppPath('comments') . '/img/comments.svg'],
		];
	}

	public function testGetAppIconThemed() {
		$file = $this->createMock(ISimpleFile::class);
		$folder = $this->createMock(ISimpleFolder::class);
		$folder->expects($this->once())
			->method('getFile')
			->with('logo')
			->willReturn($file);
		$this->appData->expects($this->once())
			->method('getFolder')
			->with('images')
			->willReturn($folder);
		$icon = $this->util->getAppIcon('noapplikethis');
		$this->assertEquals($file, $icon);
	}

	/**
	 * @dataProvider dataGetAppImage
	 */
	public function testGetAppImage($app, $image, $expected) {
		if($app !== 'core') {
			$this->appManager->expects($this->once())
				->method('getAppPath')
				->with($app)
				->willReturn(\OC_App::getAppPath($app));
		}
		$this->assertEquals($expected, $this->util->getAppImage($app, $image));
	}

	public function dataGetAppImage() {
		return [
			['core', 'logo.svg', \OC::$SERVERROOT . '/core/img/logo.svg'],
			['files', 'external', \OC::$SERVERROOT . '/apps/files/img/external.svg'],
			['files', 'external.svg', \OC::$SERVERROOT . '/apps/files/img/external.svg'],
			['noapplikethis', 'foobar.svg', false],
		];
	}

	public function testColorizeSvg() {
		$input = "#0082c9 #0082C9 #000000 #FFFFFF";
		$expected = "#AAAAAA #AAAAAA #000000 #FFFFFF";
		$result = $this->util->colorizeSvg($input, '#AAAAAA');
		$this->assertEquals($expected, $result);
	}

	public function testIsAlreadyThemedFalse() {
		$theme = $this->config->getSystemValue('theme', '');
		$this->config->expects($this->once())
			->method('getSystemValue')
			->with('theme', '')
			->willReturn('');
		$actual = $this->util->isAlreadyThemed();
		$this->assertFalse($actual);
	}

	public function testIsAlreadyThemedTrue() {
		$theme = $this->config->getSystemValue('theme', '');
		$this->config->expects($this->once())
			->method('getSystemValue')
			->with('theme', '')
			->willReturn('example');
		$actual = $this->util->isAlreadyThemed();
		$this->assertTrue($actual);
	}

}