The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 com.android.unit_tests; |
| 18 | |
| 19 | import android.graphics.Typeface; |
| 20 | import android.test.suitebuilder.annotation.MediumTest; |
| 21 | import android.test.suitebuilder.annotation.SmallTest; |
| 22 | import android.text.*; |
| 23 | import android.text.style.*; |
| 24 | |
| 25 | import junit.framework.TestCase; |
| 26 | |
| 27 | /** |
| 28 | * HtmlTest tests the Spanned-to-HTML converter |
| 29 | */ |
| 30 | public class HtmlTest extends TestCase { |
| 31 | @MediumTest |
| 32 | public void testColor() throws Exception { |
| 33 | Spanned s; |
| 34 | ForegroundColorSpan[] colors; |
| 35 | |
| 36 | s = Html.fromHtml("<font color=\"#00FF00\">something</font>"); |
| 37 | colors = s.getSpans(0, s.length(), ForegroundColorSpan.class); |
| 38 | assertEquals(colors[0].getForegroundColor(), 0xFF00FF00); |
| 39 | |
| 40 | s = Html.fromHtml("<font color=\"navy\">something</font>"); |
| 41 | colors = s.getSpans(0, s.length(), ForegroundColorSpan.class); |
| 42 | assertEquals(colors[0].getForegroundColor(), 0xFF000080); |
| 43 | |
| 44 | s = Html.fromHtml("<font color=\"gibberish\">something</font>"); |
| 45 | colors = s.getSpans(0, s.length(), ForegroundColorSpan.class); |
| 46 | assertEquals(colors.length, 0); |
| 47 | } |
| 48 | |
| 49 | @SmallTest |
| 50 | public void testParagraphs() throws Exception { |
| 51 | SpannableString s; |
| 52 | |
| 53 | s = new SpannableString("Hello world"); |
| 54 | assertEquals(Html.toHtml(s), "<p>Hello world</p>\n"); |
| 55 | |
| 56 | s = new SpannableString("Hello world\nor something"); |
| 57 | assertEquals(Html.toHtml(s), "<p>Hello world<br>\nor something</p>\n"); |
| 58 | |
| 59 | s = new SpannableString("Hello world\n\nor something"); |
| 60 | assertEquals(Html.toHtml(s), "<p>Hello world</p>\n<p>or something</p>\n"); |
| 61 | |
| 62 | s = new SpannableString("Hello world\n\n\nor something"); |
| 63 | assertEquals(Html.toHtml(s), "<p>Hello world<br></p>\n<p>or something</p>\n"); |
The Android Open Source Project | d24b818 | 2009-02-10 15:44:00 -0800 | [diff] [blame^] | 64 | |
| 65 | assertEquals("foo\nbar", Html.fromHtml("foo<br>bar").toString()); |
| 66 | assertEquals("foo\nbar", Html.fromHtml("foo<br>\nbar").toString()); |
| 67 | assertEquals("foo\nbar", Html.fromHtml("foo<br>\n \nbar").toString()); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | @SmallTest |
| 71 | public void testBlockquote() throws Exception { |
| 72 | SpannableString s; |
| 73 | |
| 74 | s = new SpannableString("Hello world"); |
| 75 | s.setSpan(new QuoteSpan(), 0, s.length(), Spannable.SPAN_PARAGRAPH); |
| 76 | assertEquals(Html.toHtml(s), "<blockquote><p>Hello world</p>\n</blockquote>\n"); |
| 77 | |
| 78 | s = new SpannableString("Hello\n\nworld"); |
| 79 | s.setSpan(new QuoteSpan(), 0, 7, Spannable.SPAN_PARAGRAPH); |
| 80 | assertEquals(Html.toHtml(s), "<blockquote><p>Hello</p>\n</blockquote>\n<p>world</p>\n"); |
| 81 | } |
| 82 | |
| 83 | @SmallTest |
| 84 | public void testEntities() throws Exception { |
| 85 | SpannableString s; |
| 86 | |
| 87 | s = new SpannableString("Hello <&> world"); |
| 88 | assertEquals(Html.toHtml(s), "<p>Hello <&> world</p>\n"); |
| 89 | |
| 90 | s = new SpannableString("Hello \u03D5 world"); |
| 91 | assertEquals(Html.toHtml(s), "<p>Hello ϕ world</p>\n"); |
| 92 | |
| 93 | s = new SpannableString("Hello world"); |
| 94 | assertEquals(Html.toHtml(s), "<p>Hello world</p>\n"); |
| 95 | } |
| 96 | |
| 97 | @SmallTest |
| 98 | public void testMarkup() throws Exception { |
| 99 | SpannableString s; |
| 100 | |
| 101 | s = new SpannableString("Hello bold world"); |
| 102 | s.setSpan(new StyleSpan(Typeface.BOLD), 6, s.length() - 6, |
| 103 | Spannable.SPAN_EXCLUSIVE_INCLUSIVE); |
| 104 | assertEquals(Html.toHtml(s), "<p>Hello <b>bold</b> world</p>\n"); |
| 105 | |
| 106 | s = new SpannableString("Hello italic world"); |
| 107 | s.setSpan(new StyleSpan(Typeface.ITALIC), 6, s.length() - 6, |
| 108 | Spannable.SPAN_EXCLUSIVE_INCLUSIVE); |
| 109 | assertEquals(Html.toHtml(s), "<p>Hello <i>italic</i> world</p>\n"); |
| 110 | |
| 111 | s = new SpannableString("Hello monospace world"); |
| 112 | s.setSpan(new TypefaceSpan("monospace"), 6, s.length() - 6, |
| 113 | Spannable.SPAN_EXCLUSIVE_INCLUSIVE); |
| 114 | assertEquals(Html.toHtml(s), "<p>Hello <tt>monospace</tt> world</p>\n"); |
| 115 | |
| 116 | s = new SpannableString("Hello superscript world"); |
| 117 | s.setSpan(new SuperscriptSpan(), 6, s.length() - 6, |
| 118 | Spannable.SPAN_EXCLUSIVE_INCLUSIVE); |
| 119 | assertEquals(Html.toHtml(s), "<p>Hello <sup>superscript</sup> world</p>\n"); |
| 120 | |
| 121 | s = new SpannableString("Hello subscript world"); |
| 122 | s.setSpan(new SubscriptSpan(), 6, s.length() - 6, |
| 123 | Spannable.SPAN_EXCLUSIVE_INCLUSIVE); |
| 124 | assertEquals(Html.toHtml(s), "<p>Hello <sub>subscript</sub> world</p>\n"); |
| 125 | |
| 126 | s = new SpannableString("Hello underline world"); |
| 127 | s.setSpan(new UnderlineSpan(), 6, s.length() - 6, |
| 128 | Spannable.SPAN_EXCLUSIVE_INCLUSIVE); |
| 129 | assertEquals(Html.toHtml(s), "<p>Hello <u>underline</u> world</p>\n"); |
| 130 | |
| 131 | s = new SpannableString("Hello struck world"); |
| 132 | s.setSpan(new StrikethroughSpan(), 6, s.length() - 6, |
| 133 | Spannable.SPAN_EXCLUSIVE_INCLUSIVE); |
| 134 | assertEquals(Html.toHtml(s), "<p>Hello <strike>struck</strike> world</p>\n"); |
| 135 | |
| 136 | s = new SpannableString("Hello linky world"); |
| 137 | s.setSpan(new URLSpan("http://www.google.com"), 6, s.length() - 6, |
| 138 | Spannable.SPAN_EXCLUSIVE_INCLUSIVE); |
| 139 | assertEquals(Html.toHtml(s), |
| 140 | "<p>Hello <a href=\"http://www.google.com\">linky</a> world</p>\n"); |
| 141 | } |
| 142 | |
| 143 | @SmallTest |
| 144 | public void testImg() throws Exception { |
| 145 | Spanned s; |
| 146 | |
| 147 | s = Html.fromHtml("yes<img src=\"http://example.com/foo.gif\">no"); |
| 148 | |
| 149 | assertEquals("<p>yes<img src=\"http://example.com/foo.gif\">no</p>\n", |
| 150 | Html.toHtml(s)); |
| 151 | } |
| 152 | |
| 153 | @SmallTest |
| 154 | public void testUtf8() throws Exception { |
| 155 | Spanned s; |
| 156 | |
| 157 | s = Html.fromHtml("<p>\u0124\u00eb\u0142\u0142o, world!</p>"); |
| 158 | assertEquals("<p>Ĥëłło, world!</p>\n", Html.toHtml(s)); |
| 159 | } |
| 160 | } |