aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/test/java/com/vaadin/tests/event/ShortcutActionTest.java
blob: 40bb0537291eee4e91455609866e1722dc102ab0 (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
package com.vaadin.tests.event;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.HashSet;

import org.junit.Test;

import com.vaadin.event.ShortcutAction;
import com.vaadin.shared.util.SharedUtil;
import com.vaadin.tests.design.DesignFormatterTest;

/**
 * Tests various things about shortcut actions.
 *
 * @since 7.4
 * @author Vaadin Ltd
 */
public class ShortcutActionTest {

    private static final String[] KEYS = "a b c d e f g h i j k l m n o p q r s t u v w x y z"
            .split("\\s+");

    @Test
    public void testHashCodeUniqueness() {
        HashSet<ShortcutAction> set = new HashSet<>();
        for (String modifier : new String[] { "^", "&", "_", "&^", "&_", "_^",
                "&^_" }) {
            for (String key : KEYS) {
                ShortcutAction action = new ShortcutAction(modifier + key);
                for (ShortcutAction other : set) {
                    assertFalse(equals(action, other));
                }
                set.add(action);
            }
        }
    }

    @Test
    public void testModifierOrderIrrelevant() {
        for (String key : KEYS) {
            // two modifiers
            for (String modifier : new String[] { "&^", "&_", "_^" }) {
                ShortcutAction action1 = new ShortcutAction(modifier + key);
                ShortcutAction action2 = new ShortcutAction(
                        modifier.substring(1) + modifier.substring(0, 1) + key);
                assertTrue(modifier + key, equals(action1, action2));
            }
            // three modifiers
            ShortcutAction action1 = new ShortcutAction("&^_" + key);
            for (String modifier : new String[] { "&_^", "^&_", "^_&", "_^&",
                    "_&^" }) {
                ShortcutAction action2 = new ShortcutAction(modifier + key);
                assertTrue(modifier + key, equals(action1, action2));

            }
        }
    }

    @Test
    public void testSameKeycodeDifferentCaptions() {
        ShortcutAction act1 = new ShortcutAction("E&xit");
        ShortcutAction act2 = new ShortcutAction("Lu&xtorpeda - Autystyczny");
        assertFalse(equals(act1, act2));
    }

    /**
     * A static method to allow comparison two different actions.
     *
     * @see DesignFormatterTest
     *
     * @param act
     *            One action to compare.
     * @param other
     *            Second action to compare.
     * @return <b>true</b> when both actions are the same (caption, icon, and
     *         key combination).
     */
    public static final boolean equals(ShortcutAction act,
            ShortcutAction other) {
        if (SharedUtil.equals(other.getCaption(), act.getCaption())
                && SharedUtil.equals(other.getIcon(), act.getIcon())
                && act.getKeyCode() == other.getKeyCode()
                && act.getModifiers().length == other.getModifiers().length) {
            HashSet<Integer> thisSet = new HashSet<>(act.getModifiers().length);
            // this is a bit tricky comparison, but there is no nice way of
            // making int[] into a Set
            for (int mod : act.getModifiers()) {
                thisSet.add(mod);
            }
            for (int mod : other.getModifiers()) {
                thisSet.remove(mod);
            }
            return thisSet.isEmpty();
        }
        return false;
    }

}