aboutsummaryrefslogtreecommitdiffstats
path: root/documentation/articles/VisuallyDistinguishPrimaryActions.asciidoc
blob: e7fe9ee987fca18b39596a2ef2c9fc4f68d4d074 (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
[[visually-distinguish-primary-actions]]
Visually distinguish primary actions
------------------------------------

Most forms and dialogs have at least two actions that can be performed,
such as _Submit/Cancel_, _Save/Revert_ or _Yes/No_. Quite often, there
are more, such as a login form with the actions _“Sign in”_,
_“Register”_, and _“Forgot password”_. Usually, one of these actions is
by far the most commonly used, and as such, the most likely one the user
is going to be looking for.

If all actions are represented by identical buttons (save for the
caption), identifying the primary button can be quite slow, and the risk
of selecting the wrong action by mistake (especially when in a hurry) is
substantial:

image:img/sign%20in%20-%20no%20distinction.png[Sign in - no distinction]

By visually distinguishing primary actions, e.g. by color, size or
shape, the user can quickly and accurately find them even in a crowded,
cluttered UI. A typical approach is to use a stronger (more saturated)
color with greater contrast for the primary actions, and a grayer, lower
contrast color for the secondary actions:

image:img/sign%20in%20-%20primary%20distinguished.png[Sign in - distinguished]

Sometimes a view can have more than one primary action simultaneously
available, although usually in different parts of the view. Google
handles this quite elegantly by systematically styling _creation_
primary buttons (such as _Compose_ in Gmail and _Create_ in Drive) in
*red*, and other primary buttons (such as search) in *blue*, while
leaving secondary buttons *gray*:

image:img/google%20drive.png[Google drive]

Choose colors wisely, though – red, for instance, means _“no”_, _"stop"_
or _“danger”_ in most cultures, so using that for _“Yes”_ or _“Submit”_
might send the user mixed signals. You might also want to take into
account the effects of color blindness (affecting approximately 10% of
men and 1% of women), especially if your user base is going to be tens
of thousands of people.

Setting a different visual style for primary action buttons is very easy
to do in Vaadin by using the *BUTTON_DEFAULT* stylename in any of the
built-in themes like Reindeer or Chameleon:

[source,java]
....
Button btnSignIn = new Button("Sign in");
btnSignIn.addStyleName(Reindeer.BUTTON_DEFAULT);
....

Another common approach, mainly used on the web, is to use text links
instead of buttons for secondary or tertiary actions. This has a
significantly stronger effect than color or size, and should only be
used for significantly less common actions, such as a password reset
request, not for the _“No”_ option in a _Yes/No_ dialog, for instance:

image:img/sign%20in%20-%20all%20different.png[Sign in - all different]

This is just as easy in Vaadin. Just use the *BUTTON_LINK* stylename
defined in the base theme (and inherited in all built in themes), and
your Button will look like a normal text-hyperlink.

[source,java]
....
Button btnForgotPwd = new Button("Forgot password?");
btnForgotPwd.addStyleName(Reindeer.BUTTON_LINK);
....

(Note that the separate *Link* component should not be used for server
actions, since you can't bind a ClickListener to a Link.)

[[consider-binding-the-enter-key-to-the-primary-action]]
Consider binding the Enter key to the primary action
++++++++++++++++++++++++++++++++++++++++++++++++++++

Especially in short, often used forms, such as a login form, it is
usually a good idea to bind the Enter key to the primary action. This
relieves the user from having to move his hand from the keyboard to the
mouse.

[source,java]
....
Button btnSignIn = new Button("Sign in");
btnSignIn.addStyleName(Reindeer.BUTTON_DEFAULT);
btnSignIn.setClickShortcut(KeyCode.ENTER);
....

If the primary action is something that really mustn’t be invoked by
mistake or without properly thinking about it first, however, it’s
probably better not to bind it to a keyboard shortcut, to avoid
accidental invocations. Another reason to abstain from a keyboard
shortcut is if the form contains an input field in which Enter can be
used for something, such as a multi-line text area (where Enter creates
a line break).
alue='backport/47339/stable28'>backport/47339/stable28 Nextcloud server, a safe home for all your data: https://github.com/nextcloud/serverwww-data
aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/files/filesystem.php
blob: 7bf59315d77d0de6ac6edaf3948526691d023180 (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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
<?php
/**
 * ownCloud
 *
 * @author Robin Appelman
 * @copyright 2012 Robin Appelman icewind@owncloud.com
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or any later version.
 *
 * This library 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 library.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

namespace Test\Files;

class Filesystem extends \Test\TestCase {
	/**
	 * @var array tmpDirs
	 */
	private $tmpDirs = array();

	/** @var \OC\Files\Storage\Storage */
	private $originalStorage;

	/**
	 * @return array
	 */
	private function getStorageData() {
		$dir = \OC_Helper::tmpFolder();
		$this->tmpDirs[] = $dir;
		return array('datadir' => $dir);
	}

	protected function setUp() {
		parent::setUp();

		$this->originalStorage = \OC\Files\Filesystem::getStorage('/');
		\OC_User::setUserId('');
		\OC\Files\Filesystem::clearMounts();
	}

	protected function tearDown() {
		foreach ($this->tmpDirs as $dir) {
			\OC_Helper::rmdirr($dir);
		}
		\OC\Files\Filesystem::clearMounts();
		\OC\Files\Filesystem::mount($this->originalStorage, array(), '/');
		\OC_User::setUserId('');

		parent::tearDown();
	}

	public function testMount() {
		\OC\Files\Filesystem::mount('\OC\Files\Storage\Local', self::getStorageData(), '/');
		$this->assertEquals('/', \OC\Files\Filesystem::getMountPoint('/'));
		$this->assertEquals('/', \OC\Files\Filesystem::getMountPoint('/some/folder'));
		list(, $internalPath) = \OC\Files\Filesystem::resolvePath('/');
		$this->assertEquals('', $internalPath);
		list(, $internalPath) = \OC\Files\Filesystem::resolvePath('/some/folder');
		$this->assertEquals('some/folder', $internalPath);

		\OC\Files\Filesystem::mount('\OC\Files\Storage\Local', self::getStorageData(), '/some');
		$this->assertEquals('/', \OC\Files\Filesystem::getMountPoint('/'));
		$this->assertEquals('/some/', \OC\Files\Filesystem::getMountPoint('/some/folder'));
		$this->assertEquals('/some/', \OC\Files\Filesystem::getMountPoint('/some/'));
		$this->assertEquals('/some/', \OC\Files\Filesystem::getMountPoint('/some'));
		list(, $internalPath) = \OC\Files\Filesystem::resolvePath('/some/folder');
		$this->assertEquals('folder', $internalPath);
	}

	public function normalizePathData() {
		return array(
			array('/', ''),
			array('/', '/'),
			array('/', '//'),
			array('/', '/', false),
			array('/', '//', false),

			array('/path', '/path/'),
			array('/path/', '/path/', false),
			array('/path', 'path'),

			array('/foo/bar', '/foo//bar/'),
			array('/foo/bar/', '/foo//bar/', false),
			array('/foo/bar', '/foo////bar'),
			array('/foo/bar', '/foo/////bar'),
			array('/foo/bar', '/foo/bar/.'),
			array('/foo/bar', '/foo/bar/./'),
			array('/foo/bar/', '/foo/bar/./', false),
			array('/foo/bar', '/foo/bar/./.'),
			array('/foo/bar', '/foo/bar/././'),
			array('/foo/bar/', '/foo/bar/././', false),
			array('/foo/bar', '/foo/./bar/'),
			array('/foo/bar/', '/foo/./bar/', false),
			array('/foo/.bar', '/foo/.bar/'),
			array('/foo/.bar/', '/foo/.bar/', false),
			array('/foo/.bar/tee', '/foo/.bar/tee'),

			// Windows paths
			array('/', ''),
			array('/', '\\'),
			array('/', '\\', false),
			array('/', '\\\\'),
			array('/', '\\\\', false),

			array('/path', '\\path'),
			array('/path', '\\path', false),
			array('/path', '\\path\\'),
			array('/path/', '\\path\\', false),

			array('/foo/bar', '\\foo\\\\bar\\'),
			array('/foo/bar/', '\\foo\\\\bar\\', false),
			array('/foo/bar', '\\foo\\\\\\\\bar'),
			array('/foo/bar', '\\foo\\\\\\\\\\bar'),
			array('/foo/bar', '\\foo\\bar\\.'),
			array('/foo/bar', '\\foo\\bar\\.\\'),
			array('/foo/bar/', '\\foo\\bar\\.\\', false),
			array('/foo/bar', '\\foo\\bar\\.\\.'),
			array('/foo/bar', '\\foo\\bar\\.\\.\\'),
			array('/foo/bar/', '\\foo\\bar\\.\\.\\', false),
			array('/foo/bar', '\\foo\\.\\bar\\'),
			array('/foo/bar/', '\\foo\\.\\bar\\', false),
			array('/foo/.bar', '\\foo\\.bar\\'),
			array('/foo/.bar/', '\\foo\\.bar\\', false),
			array('/foo/.bar/tee', '\\foo\\.bar\\tee'),

			// Absolute windows paths NOT marked as absolute
			array('/C:', 'C:\\'),
			array('/C:/', 'C:\\', false),
			array('/C:/tests', 'C:\\tests'),
			array('/C:/tests', 'C:\\tests', false),
			array('/C:/tests', 'C:\\tests\\'),
			array('/C:/tests/', 'C:\\tests\\', false),

			// normalize does not resolve '..' (by design)
			array('/foo/..', '/foo/../'),
			array('/foo/..', '\\foo\\..\\'),
		);
	}

	/**
	 * @dataProvider normalizePathData
	 */
	public function testNormalizePath($expected, $path, $stripTrailingSlash = true) {
		$this->assertEquals($expected, \OC\Files\Filesystem::normalizePath($path, $stripTrailingSlash));
	}

	public function isValidPathData() {
		return array(
			array('/', true),
			array('/path', true),
			array('/foo/bar', true),
			array('/foo//bar/', true),
			array('/foo////bar', true),
			array('/foo//\///bar', true),
			array('/foo/bar/.', true),
			array('/foo/bar/./', true),
			array('/foo/bar/./.', true),
			array('/foo/bar/././', true),
			array('/foo/bar/././..bar', true),
			array('/foo/bar/././..bar/a', true),
			array('/foo/bar/././..', false),
			array('/foo/bar/././../', false),
			array('/foo/bar/.././', false),
			array('/foo/bar/../../', false),
			array('/foo/bar/../..\\', false),
			array('..', false),
			array('../', false),
			array('../foo/bar', false),
			array('..\foo/bar', false),
		);
	}

	/**
	 * @dataProvider isValidPathData
	 */
	public function testIsValidPath($path, $expected) {
		$this->assertSame($expected, \OC\Files\Filesystem::isValidPath($path));
	}

	public function isFileBlacklistedData() {
		return array(
			array('/etc/foo/bar/foo.txt', false),
			array('\etc\foo/bar\foo.txt', false),
			array('.htaccess', true),
			array('.htaccess/', true),
			array('.htaccess\\', true),
			array('/etc/foo\bar/.htaccess\\', true),
			array('/etc/foo\bar/.htaccess/', true),
			array('/etc/foo\bar/.htaccess/foo', false),
			array('//foo//bar/\.htaccess/', true),
			array('\foo\bar\.HTAccess', true),
		);
	}

	/**
	 * @dataProvider isFileBlacklistedData
	 */
	public function testIsFileBlacklisted($path, $expected) {
		$this->assertSame($expected, \OC\Files\Filesystem::isFileBlacklisted($path));
	}

	public function normalizePathWindowsAbsolutePathData() {
		return array(
			array('C:/', 'C:\\'),
			array('C:/', 'C:\\', false),
			array('C:/tests', 'C:\\tests'),
			array('C:/tests', 'C:\\tests', false),
			array('C:/tests', 'C:\\tests\\'),
			array('C:/tests/', 'C:\\tests\\', false),
		);
	}

	/**
	 * @dataProvider normalizePathWindowsAbsolutePathData
	 */
	public function testNormalizePathWindowsAbsolutePath($expected, $path, $stripTrailingSlash = true) {
		if (!\OC_Util::runningOnWindows()) {
			$this->markTestSkipped('This test is Windows only');
		}

		$this->assertEquals($expected, \OC\Files\Filesystem::normalizePath($path, $stripTrailingSlash, true));
	}

	public function testNormalizePathUTF8() {
		if (!class_exists('Patchwork\PHP\Shim\Normalizer')) {
			$this->markTestSkipped('UTF8 normalizer Patchwork was not found');
		}

		$this->assertEquals("/foo/bar\xC3\xBC", \OC\Files\Filesystem::normalizePath("/foo/baru\xCC\x88"));
		$this->assertEquals("/foo/bar\xC3\xBC", \OC\Files\Filesystem::normalizePath("\\foo\\baru\xCC\x88"));
	}

	public function testHooks() {
		if (\OC\Files\Filesystem::getView()) {
			$user = \OC_User::getUser();
		} else {
			$user = $this->getUniqueID();
			\OC\Files\Filesystem::init($user, '/' . $user . '/files');
		}
		\OC_Hook::clear('OC_Filesystem');
		\OC_Hook::connect('OC_Filesystem', 'post_write', $this, 'dummyHook');

		\OC\Files\Filesystem::mount('OC\Files\Storage\Temporary', array(), '/');

		$rootView = new \OC\Files\View('');
		$rootView->mkdir('/' . $user);
		$rootView->mkdir('/' . $user . '/files');

//		\OC\Files\Filesystem::file_put_contents('/foo', 'foo');
		\OC\Files\Filesystem::mkdir('/bar');
//		\OC\Files\Filesystem::file_put_contents('/bar//foo', 'foo');

		$tmpFile = \OC_Helper::tmpFile();
		file_put_contents($tmpFile, 'foo');
		$fh = fopen($tmpFile, 'r');
//		\OC\Files\Filesystem::file_put_contents('/bar//foo', $fh);
	}

	/**
	 * Tests that a local storage mount is used when passed user
	 * does not exist.
	 */
	public function testLocalMountWhenUserDoesNotExist() {
		$datadir = \OC_Config::getValue("datadirectory", \OC::$SERVERROOT . "/data");
		$userId = $this->getUniqueID('user_');

		\OC\Files\Filesystem::initMountPoints($userId);

		$homeMount = \OC\Files\Filesystem::getStorage('/' . $userId . '/');

		$this->assertTrue($homeMount->instanceOfStorage('\OC\Files\Storage\Local'));
		$this->assertEquals('local::' . $datadir . '/' . $userId . '/', $homeMount->getId());
	}

	/**
	 * Tests that the home storage is used for the user's mount point
	 */
	public function testHomeMount() {
		$userId = $this->getUniqueID('user_');

		\OC_User::createUser($userId, $userId);

		\OC\Files\Filesystem::initMountPoints($userId);

		$homeMount = \OC\Files\Filesystem::getStorage('/' . $userId . '/');

		$this->assertTrue($homeMount->instanceOfStorage('\OC\Files\Storage\Home'));
		$this->assertEquals('home::' . $userId, $homeMount->getId());

		\OC_User::deleteUser($userId);
	}

	/**
	 * Tests that the home storage is used in legacy mode
	 * for the user's mount point
	 */
	public function testLegacyHomeMount() {
		$datadir = \OC_Config::getValue("datadirectory", \OC::$SERVERROOT . "/data");
		$userId = $this->getUniqueID('user_');

		// insert storage into DB by constructing it
		// to make initMountsPoint find its existence
		$localStorage = new \OC\Files\Storage\Local(array('datadir' => $datadir . '/' . $userId . '/'));
		// this will trigger the insert
		$cache = $localStorage->getCache();

		\OC_User::createUser($userId, $userId);
		\OC\Files\Filesystem::initMountPoints($userId);

		$homeMount = \OC\Files\Filesystem::getStorage('/' . $userId . '/');

		$this->assertTrue($homeMount->instanceOfStorage('\OC\Files\Storage\Home'));
		$this->assertEquals('local::' . $datadir . '/' . $userId . '/', $homeMount->getId());

		\OC_User::deleteUser($userId);
		// delete storage entry
		$cache->clear();
	}

	public function dummyHook($arguments) {
		$path = $arguments['path'];
		$this->assertEquals($path, \OC\Files\Filesystem::normalizePath($path)); //the path passed to the hook should already be normalized
	}

	/**
	 * Test that the default cache dir is part of the user's home
	 */
	public function testMountDefaultCacheDir() {
		$userId = $this->getUniqueID('user_');
		$oldCachePath = \OC_Config::getValue('cache_path', '');
		// no cache path configured
		\OC_Config::setValue('cache_path', '');

		\OC_User::createUser($userId, $userId);
		\OC\Files\Filesystem::initMountPoints($userId);

		$this->assertEquals(
			'/' . $userId . '/',
			\OC\Files\Filesystem::getMountPoint('/' . $userId . '/cache')
		);
		list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath('/' . $userId . '/cache');
		$this->assertTrue($storage->instanceOfStorage('\OC\Files\Storage\Home'));
		$this->assertEquals('cache', $internalPath);
		\OC_User::deleteUser($userId);

		\OC_Config::setValue('cache_path', $oldCachePath);
	}

	/**
	 * Test that an external cache is mounted into
	 * the user's home
	 */
	public function testMountExternalCacheDir() {
		$userId = $this->getUniqueID('user_');

		$oldCachePath = \OC_Config::getValue('cache_path', '');
		// set cache path to temp dir
		$cachePath = \OC_Helper::tmpFolder() . '/extcache';
		\OC_Config::setValue('cache_path', $cachePath);

		\OC_User::createUser($userId, $userId);
		\OC\Files\Filesystem::initMountPoints($userId);

		$this->assertEquals(
			'/' . $userId . '/cache/',
			\OC\Files\Filesystem::getMountPoint('/' . $userId . '/cache')
		);
		list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath('/' . $userId . '/cache');
		$this->assertTrue($storage->instanceOfStorage('\OC\Files\Storage\Local'));
		$this->assertEquals('', $internalPath);
		\OC_User::deleteUser($userId);

		\OC_Config::setValue('cache_path', $oldCachePath);
	}
}