1 /* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.text.cts; 18 19 import android.graphics.Typeface; 20 import android.test.AndroidTestCase; 21 import android.text.Html; 22 import android.text.Spannable; 23 import android.text.SpannableString; 24 import android.text.Spanned; 25 import android.text.Html.ImageGetter; 26 import android.text.Html.TagHandler; 27 import android.text.style.ForegroundColorSpan; 28 import android.text.style.QuoteSpan; 29 import android.text.style.StrikethroughSpan; 30 import android.text.style.StyleSpan; 31 import android.text.style.SubscriptSpan; 32 import android.text.style.SuperscriptSpan; 33 import android.text.style.TypefaceSpan; 34 import android.text.style.URLSpan; 35 import android.text.style.UnderlineSpan; 36 37 public class HtmlTest extends AndroidTestCase { 38 private final static int SPAN_EXCLUSIVE_INCLUSIVE = Spannable.SPAN_EXCLUSIVE_INCLUSIVE; 39 testSingleTagOnWhileString()40 public void testSingleTagOnWhileString() { 41 final String source = "<b>hello</b>"; 42 43 Spanned spanned = Html.fromHtml(source); 44 assertSingleTagOnWhileString(spanned); 45 spanned = Html.fromHtml(source, null, null); 46 assertSingleTagOnWhileString(spanned); 47 } 48 assertSingleTagOnWhileString(Spanned spanned)49 private void assertSingleTagOnWhileString(Spanned spanned) { 50 final int expectStart = 0; 51 final int expectEnd = 5; 52 final int expectLen = 1; 53 final int start = -1; 54 final int end = 100; 55 56 Object[] spans = spanned.getSpans(start, end, Object.class); 57 assertEquals(expectLen, spans.length); 58 assertEquals(expectStart, spanned.getSpanStart(spans[0])); 59 assertEquals(expectEnd, spanned.getSpanEnd(spans[0])); 60 } 61 testBadHtml()62 public void testBadHtml() { 63 final String source = "Hello <b>b<i>bi</b>i</i>"; 64 65 Spanned spanned = Html.fromHtml(source); 66 assertBadHtml(spanned); 67 spanned = Html.fromHtml(source, null, null); 68 assertBadHtml(spanned); 69 } 70 assertBadHtml(Spanned spanned)71 private void assertBadHtml(Spanned spanned) { 72 final int start = 0; 73 final int end = 100; 74 final int spansLen = 3; 75 76 Object[] spans = spanned.getSpans(start, end, Object.class); 77 assertEquals(spansLen, spans.length); 78 } 79 testSymbols()80 public void testSymbols() { 81 final String source = "© > <"; 82 final String expected = "\u00a9 > <"; 83 84 String spanned = Html.fromHtml(source).toString(); 85 assertEquals(expected, spanned); 86 spanned = Html.fromHtml(source, null, null).toString(); 87 assertEquals(expected, spanned); 88 } 89 testColor()90 public void testColor() throws Exception { 91 final int start = 0; 92 93 Class<ForegroundColorSpan> type = ForegroundColorSpan.class; 94 ForegroundColorSpan[] colors; 95 Spanned s = Html.fromHtml("<font color=\"#00FF00\">something</font>"); 96 int end = s.length(); 97 colors = s.getSpans(start, end, type); 98 int expectedColor = 0xFF00FF00; 99 assertEquals(expectedColor, colors[0].getForegroundColor()); 100 101 s = Html.fromHtml("<font color=\"navy\">something</font>"); 102 end = s.length(); 103 colors = s.getSpans(start, end, type); 104 expectedColor = 0xFF000080; 105 assertEquals(0xFF000080, colors[0].getForegroundColor()); 106 107 s = Html.fromHtml("<font color=\"gibberish\">something</font>"); 108 end = s.length(); 109 colors = s.getSpans(start, end, type); 110 assertEquals(0, colors.length); 111 } 112 testParagraphs()113 public void testParagraphs() throws Exception { 114 SpannableString s = new SpannableString("Hello world"); 115 assertEquals("<p dir=\"ltr\">Hello world</p>\n", Html.toHtml(s)); 116 117 s = new SpannableString("Hello world\nor something"); 118 assertEquals("<p dir=\"ltr\">Hello world<br>\nor something</p>\n", Html.toHtml(s)); 119 120 s = new SpannableString("Hello world\n\nor something"); 121 assertEquals("<p dir=\"ltr\">Hello world</p>\n<p dir=\"ltr\">or something</p>\n", 122 Html.toHtml(s)); 123 124 s = new SpannableString("Hello world\n\n\nor something"); 125 assertEquals("<p dir=\"ltr\">Hello world<br></p>\n<p dir=\"ltr\">or something</p>\n", 126 Html.toHtml(s)); 127 } 128 testBlockquote()129 public void testBlockquote() throws Exception { 130 final int start = 0; 131 132 SpannableString s = new SpannableString("Hello world"); 133 int end = s.length(); 134 s.setSpan(new QuoteSpan(), start, end, Spannable.SPAN_PARAGRAPH); 135 assertEquals("<blockquote><p dir=\"ltr\">Hello world</p>\n</blockquote>\n", Html.toHtml(s)); 136 137 s = new SpannableString("Hello\n\nworld"); 138 end = 7; 139 s.setSpan(new QuoteSpan(), start, end, Spannable.SPAN_PARAGRAPH); 140 assertEquals("<blockquote><p dir=\"ltr\">Hello</p>\n</blockquote>\n" + 141 "<p dir=\"ltr\">world</p>\n", Html.toHtml(s)); 142 } 143 testEntities()144 public void testEntities() throws Exception { 145 SpannableString s = new SpannableString("Hello <&> world"); 146 assertEquals("<p dir=\"ltr\">Hello <&> world</p>\n", Html.toHtml(s)); 147 148 s = new SpannableString("Hello \u03D5 world"); 149 assertEquals("<p dir=\"ltr\">Hello ϕ world</p>\n", Html.toHtml(s)); 150 151 s = new SpannableString("Hello world"); 152 assertEquals("<p dir=\"ltr\">Hello world</p>\n", Html.toHtml(s)); 153 } 154 testMarkup()155 public void testMarkup() throws Exception { 156 final int start = 6; 157 158 SpannableString s = new SpannableString("Hello bold world"); 159 int end = s.length() - start; 160 s.setSpan(new StyleSpan(Typeface.BOLD), start, end, SPAN_EXCLUSIVE_INCLUSIVE); 161 assertEquals("<p dir=\"ltr\">Hello <b>bold</b> world</p>\n", Html.toHtml(s)); 162 163 s = new SpannableString("Hello italic world"); 164 end = s.length() - start; 165 s.setSpan(new StyleSpan(Typeface.ITALIC), start, end, SPAN_EXCLUSIVE_INCLUSIVE); 166 assertEquals("<p dir=\"ltr\">Hello <i>italic</i> world</p>\n", Html.toHtml(s)); 167 168 s = new SpannableString("Hello monospace world"); 169 end = s.length() - start; 170 s.setSpan(new TypefaceSpan("monospace"), start, end, SPAN_EXCLUSIVE_INCLUSIVE); 171 assertEquals("<p dir=\"ltr\">Hello <tt>monospace</tt> world</p>\n", Html.toHtml(s)); 172 173 s = new SpannableString("Hello superscript world"); 174 end = s.length() - start; 175 s.setSpan(new SuperscriptSpan(), start, end, SPAN_EXCLUSIVE_INCLUSIVE); 176 assertEquals("<p dir=\"ltr\">Hello <sup>superscript</sup> world</p>\n", Html.toHtml(s)); 177 178 s = new SpannableString("Hello subscript world"); 179 end = s.length() - start; 180 s.setSpan(new SubscriptSpan(), start, end, SPAN_EXCLUSIVE_INCLUSIVE); 181 assertEquals("<p dir=\"ltr\">Hello <sub>subscript</sub> world</p>\n", Html.toHtml(s)); 182 183 s = new SpannableString("Hello underline world"); 184 end = s.length() - start; 185 s.setSpan(new UnderlineSpan(), start, end, SPAN_EXCLUSIVE_INCLUSIVE); 186 assertEquals("<p dir=\"ltr\">Hello <u>underline</u> world</p>\n", Html.toHtml(s)); 187 188 s = new SpannableString("Hello struck world"); 189 end = s.length() - start; 190 s.setSpan(new StrikethroughSpan(), start, end, SPAN_EXCLUSIVE_INCLUSIVE); 191 assertEquals("<p dir=\"ltr\">Hello <strike>struck</strike> world</p>\n", Html.toHtml(s)); 192 193 s = new SpannableString("Hello linky world"); 194 end = s.length() - start; 195 s.setSpan(new URLSpan("http://www.google.com"), start, end, SPAN_EXCLUSIVE_INCLUSIVE); 196 String ret = Html.toHtml(s); 197 assertEquals("<p dir=\"ltr\">Hello <a href=\"http://www.google.com\">linky</a> world</p>\n", 198 ret); 199 } 200 testImg()201 public void testImg() throws Exception { 202 Spanned s = Html.fromHtml("yes<img src=\"http://example.com/foo.gif\">no"); 203 assertEquals("<p dir=\"ltr\">yes<img src=\"http://example.com/foo.gif\">no</p>\n", 204 Html.toHtml(s)); 205 } 206 testUtf8()207 public void testUtf8() throws Exception { 208 Spanned s = Html.fromHtml("<p>\u0124\u00eb\u0142\u0142o, world!</p>"); 209 assertEquals("<p dir=\"ltr\">Ĥëłło, world!</p>\n", Html.toHtml(s)); 210 } 211 testSurrogates()212 public void testSurrogates() throws Exception { 213 Spanned s = Html.fromHtml("\ud83d\udc31"); 214 assertEquals("<p dir=\"ltr\">🐱</p>\n", Html.toHtml(s)); 215 } 216 testBadSurrogates()217 public void testBadSurrogates() throws Exception { 218 Spanned s = Html.fromHtml("\udc31\ud83d"); 219 assertEquals("<p dir=\"ltr\"></p>\n", Html.toHtml(s)); 220 } 221 } 222