forked from pockethub/PocketHub
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHtmlUtilsTest.java
More file actions
229 lines (202 loc) · 6.54 KB
/
HtmlUtilsTest.java
File metadata and controls
229 lines (202 loc) · 6.54 KB
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
/*
* Copyright 2012 GitHub Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.mobile.tests.util;
import static android.os.Build.VERSION.SDK_INT;
import static android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH;
import android.test.AndroidTestCase;
import com.github.mobile.util.HtmlUtils;
/**
* Unit tests of HTML conversions done when rendering markdown
*/
public class HtmlUtilsTest extends AndroidTestCase {
private String format(String html) {
CharSequence formatted = HtmlUtils.format(html);
assertNotNull(formatted);
return formatted.toString().replace("<githubroot>", "")
.replace("</githubroot>", "");
}
/**
* Single email toggle span is removed
*/
public void testToggleRemoved() {
String html = "before <span class=\"email-hidden-toggle\"><a href=\"#\">…</a></span>after";
assertEquals("before after", format(html));
}
/**
* Multiple email toggle spans are removed
*/
public void testTogglesRemoved() {
String html = "before <span class=\"email-hidden-toggle\"><a href=\"#\">…</a></span>after<span class=\"email-hidden-toggle\"><a href=\"#\">…</a></span>";
assertEquals("before after", format(html));
}
/**
* Email div is transformed into block quote
*/
public void testEmailQuoted() {
String html = "before <div class=\"email-quoted-reply\">quoted</div> after";
assertEquals("before <blockquote>quoted</blockquote> after",
format(html));
}
/**
* Email fragment div is removed and newlines are replaced with br tags
*/
public void testEmailFragment() {
String html = "before <div class=\"email-fragment\">in\nside</div> after";
assertEquals("before in<br>side after", format(html));
}
/**
* Email fragment div is removed and newlines are replaced with br tags
*/
public void testEmailFragments() {
String html = "before <div class=\"email-fragment\">in\nside</div> after <div class=\"email-fragment\">out\nside</div>";
assertEquals("before in<br>side after out<br>side", format(html));
}
/**
* Email fragment div is removed and newlines are replaced with br tags
*/
public void testTrailingEmailFragment() {
String html = "before <div class=\"email-fragment\">in\nside</div>";
assertEquals("before in<br>side", format(html));
}
/**
* Leading break is removed
*/
public void testLeadingBreak() {
String html = "<br>content";
assertEquals("content", format(html));
}
/**
* Trailing break is removed
*/
public void testTrailingBreak() {
String html = "content<br>";
assertEquals("content", format(html));
}
/**
* Leading & trailing breaks are removed
*/
public void testWrappedBreaks() {
String html = "<br>content<br>";
assertEquals("content", format(html));
}
/**
* Leading & trailing breaks are removed
*/
public void testWrappedParagraphs() {
String html = "<p>content</p>";
assertEquals("content", format(html));
}
/**
* Paragraph replaced with break
*/
public void testParagraphReplacedWithBreak() {
String html = "line1<p>line2</p>";
assertEquals("line1<br>line2", format(html));
}
/**
* em tags replaced with i tags
*/
public void testEmReplacedWithI() {
String html = "a<em>b</em>c";
if (SDK_INT < ICE_CREAM_SANDWICH)
assertEquals("a<i>b</i>c", format(html));
else
assertEquals(html, format(html));
}
/**
* strong tags replaced with b tags
*/
public void testStrongReplacedWithB() {
String html = "<strong>a</strong>";
if (SDK_INT < ICE_CREAM_SANDWICH)
assertEquals("<b>a</b>", format(html));
else
assertEquals(html, format(html));
}
/**
* Leading whitespace is removed
*/
public void testLeadingWhitespace() {
String html = " content";
assertEquals("content", format(html));
}
/**
* Trailing whitespace is removed
*/
public void testTrailingWhitespace() {
String html = "content ";
assertEquals("content", format(html));
}
/**
* Leading & trailing whitespace is removed
*/
public void testWrappedWhitetspace() {
String html = " content ";
assertEquals("content", format(html));
}
/**
* Pre untouched
*/
public void testPreWithNoWhitespace() {
String html = "a<pre>b</pre> c";
assertEquals("a<pre>b</pre> c", format(html));
}
/**
* Pre space escaped
*/
public void testPreWithSpaces() {
String html = "a<pre> b</pre> c";
assertEquals("a<pre> b</pre> c", format(html));
}
/**
* Pre tab escaped
*/
public void testPreWithTabs() {
String html = "a<pre>\tb</pre> c";
assertEquals("a<pre> b</pre> c", format(html));
}
/**
* Pre newline escaped
*/
public void testPreWithNewline() {
String html = "a<pre>\nb</pre> c";
assertEquals("a<pre><br>b</pre> c", format(html));
}
/**
* Pre space, tab, and newline escaped
*/
public void testPreWithAllWhitepsace() {
String html = "a<pre>\nb\tc </pre>d";
assertEquals("a<pre><br>b c </pre>d",
format(html));
}
/**
* Multiple pre elements escaped
*/
public void testMultiplePresEscaped() {
String html = "a<pre> c </pre>d<pre>\te\t</pre>";
assertEquals(
"a<pre> c </pre>d<pre> e </pre>",
format(html));
}
/**
* Single code element inside a pre element
*/
public void testFormatPreCodeOnly() {
String html = "<pre><code>a\nb\nc\n</code></pre>";
assertEquals("<pre><code>a<br>b<br>c</code></pre>", format(html));
}
}