aboutsummaryrefslogtreecommitdiff
path: root/libs/editor/example/src/test/java/org/wordpress/android/editor/UtilsTest.java
blob: b7bc70fe884fa81b4db59c1d301393dde3230084 (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
package org.wordpress.android.editor;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.wordpress.android.editor.Utils.buildMapFromKeyValuePairs;
import static org.wordpress.android.editor.Utils.decodeHtml;
import static org.wordpress.android.editor.Utils.escapeHtml;
import static org.wordpress.android.editor.Utils.getChangeMapFromSets;
import static org.wordpress.android.editor.Utils.splitDelimitedString;
import static org.wordpress.android.editor.Utils.splitValuePairDelimitedString;
import static org.wordpress.android.editor.Utils.getUrlFromClipboard;

@Config(sdk = 18)
@RunWith(RobolectricTestRunner.class)
public class UtilsTest {

    @Test
    public void testEscapeHtml() {
        // Test null
        assertEquals(null, escapeHtml(null));
    }

    @Test
    public void testDecodeHtml() {
        // Test null
        assertEquals(null, decodeHtml(null));

        // Test normal usage
        assertEquals("http://www.wordpress.com/", decodeHtml("http%3A%2F%2Fwww.wordpress.com%2F"));
    }

    @Test
    public void testSplitDelimitedString() {
        Set<String> splitString = new HashSet<>();

        // Test normal usage
        splitString.add("p");
        splitString.add("bold");
        splitString.add("justifyLeft");

        assertEquals(splitString, splitDelimitedString("p~bold~justifyLeft", "~"));

        // Test empty string
        assertEquals(Collections.emptySet(), splitDelimitedString("", "~"));
    }

    @Test
    public void testSplitValuePairDelimitedString() {
        // Test usage with a URL containing the delimiter
        Set<String> keyValueSet = new HashSet<>();
        keyValueSet.add("url=http://www.wordpress.com/~user");
        keyValueSet.add("title=I'm a link!");

        List<String> identifiers = new ArrayList<>();
        identifiers.add("url");
        identifiers.add("title");

        assertEquals(keyValueSet, splitValuePairDelimitedString(
                "url=http://www.wordpress.com/~user~title=I'm a link!", "~", identifiers));

        // Test usage with a matching identifier but no delimiters
        keyValueSet.clear();
        keyValueSet.add("url=http://www.wordpress.com/");

        assertEquals(keyValueSet, splitValuePairDelimitedString("url=http://www.wordpress.com/", "~", identifiers));

        // Test usage with no matching identifier and no delimiters
        keyValueSet.clear();
        keyValueSet.add("something=something else");

        assertEquals(keyValueSet, splitValuePairDelimitedString("something=something else", "~", identifiers));
    }

    @Test
    public void testBuildMapFromKeyValuePairs() {
        Set<String> keyValueSet = new HashSet<>();
        Map<String, String> expectedMap = new HashMap<>();

        // Test normal usage
        keyValueSet.add("id=test");
        keyValueSet.add("name=example");

        expectedMap.put("id", "test");
        expectedMap.put("name", "example");

        assertEquals(expectedMap, buildMapFromKeyValuePairs(keyValueSet));

        // Test mixed valid and invalid entries
        keyValueSet.clear();
        keyValueSet.add("test");
        keyValueSet.add("name=example");

        expectedMap.clear();
        expectedMap.put("name", "example");

        assertEquals(expectedMap, buildMapFromKeyValuePairs(keyValueSet));

        // Test multiple '=' (should split at the first `=` and treat the rest of them as part of the string)
        keyValueSet.clear();
        keyValueSet.add("id=test");
        keyValueSet.add("contents=some text\n<a href=\"http://wordpress.com\">WordPress</a>");

        expectedMap.clear();
        expectedMap.put("id", "test");
        expectedMap.put("contents", "some text\n<a href=\"http://wordpress.com\">WordPress</a>");

        assertEquals(expectedMap, buildMapFromKeyValuePairs(keyValueSet));

        // Test invalid entry
        keyValueSet.clear();
        keyValueSet.add("test");

        assertEquals(Collections.emptyMap(), buildMapFromKeyValuePairs(keyValueSet));

        // Test empty sets
        assertEquals(Collections.emptyMap(), buildMapFromKeyValuePairs(Collections.<String>emptySet()));
    }

    @Test
    public void testGetChangeMapFromSets() {
        Set<String> oldSet = new HashSet<>();
        Set<String> newSet = new HashSet<>();
        Map<String, Boolean> expectedMap = new HashMap<>();

        // Test normal usage
        oldSet.add("p");
        oldSet.add("bold");
        oldSet.add("justifyLeft");

        newSet.add("p");
        newSet.add("justifyRight");

        expectedMap.put("bold", false);
        expectedMap.put("justifyLeft", false);
        expectedMap.put("justifyRight", true);

        assertEquals(expectedMap, getChangeMapFromSets(oldSet, newSet));

        // Test no changes
        oldSet.clear();
        oldSet.add("p");
        oldSet.add("bold");

        newSet.clear();
        newSet.add("p");
        newSet.add("bold");

        assertEquals(Collections.emptyMap(), getChangeMapFromSets(oldSet, newSet));

        // Test empty sets
        assertEquals(Collections.emptyMap(), getChangeMapFromSets(Collections.emptySet(), Collections.emptySet()));
    }

    @Test
    public void testClipboardUrlWithNullContext() {
        assertNull(getUrlFromClipboard(null));
    }

    @Test
    public void testClipboardUrlWithNoClipData() {
        assertNull(getClipboardUrlHelper(0, null));
    }

    @Test
    public void testClipboardUrlWithNonUriData() {
        assertNull(getClipboardUrlHelper(1, "not a URL"));
    }

    @Test
    public void testClipboardUrlWithLocalUriData() {
        assertNull(getClipboardUrlHelper(1, "file://test.png"));
    }

    @Test
    public void testClipboardWithUrlData() {
        String testUrl = "google.com";
        assertEquals(testUrl, getClipboardUrlHelper(1, testUrl));
    }

    private String getClipboardUrlHelper(int itemCount, String clipText) {
        ClipData.Item mockItem = mock(ClipData.Item.class);
        when(mockItem.getText()).thenReturn(clipText);

        ClipData mockPrimary = mock(ClipData.class);
        when(mockPrimary.getItemCount()).thenReturn(itemCount);
        when(mockPrimary.getItemAt(0)).thenReturn(mockItem);

        ClipboardManager mockManager = mock(ClipboardManager.class);
        when(mockManager.getPrimaryClip()).thenReturn(mockPrimary);

        Context mockContext = mock(Context.class);
        when(mockContext.getSystemService(Context.CLIPBOARD_SERVICE)).thenReturn(mockManager);

        return getUrlFromClipboard(mockContext);
    }
}