alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Android example source code file (HtmlTest.java)

This example Android source code file (HtmlTest.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Android by Example" TM.

Java - Android tags/keywords

android, colorstatelist, content, drawing, exception, foregroundcolorspan, hello, mediumtest, object, quotespan, smalltest, spannablestring, spanned, style, stylespan, test, testcase, text, typefacespan, urlspan

The HtmlTest.java Android example source code

/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * 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 android.text;

import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.test.suitebuilder.annotation.MediumTest;
import android.test.suitebuilder.annotation.SmallTest;
import android.text.style.ForegroundColorSpan;
import android.text.style.QuoteSpan;
import android.text.style.StrikethroughSpan;
import android.text.style.StyleSpan;
import android.text.style.SubscriptSpan;
import android.text.style.SuperscriptSpan;
import android.text.style.TextAppearanceSpan;
import android.text.style.TypefaceSpan;
import android.text.style.URLSpan;
import android.text.style.UnderlineSpan;

import junit.framework.TestCase;

public class HtmlTest extends TestCase {

    @MediumTest
    public void testSingleTagOnWhileString() {
        Spanned spanned = Html.fromHtml("<b>hello");
        Object[] spans = spanned.getSpans(-1, 100, Object.class);
        assertEquals(1, spans.length);
        Object span = spans[0];
        assertEquals(0, spanned.getSpanStart(span));
        assertEquals(5, spanned.getSpanEnd(span));
    }

    @MediumTest
    public void testEmptyFontTag() {
        Spanned spanned = Html.fromHtml("Hello <font color=\"#ff00ff00\">");
        Object[] spans = spanned.getSpans(0, 100, Object.class);
        // TODO: figure out what the spans should be after the crashes are fixed and assert them.
    }

    /** Tests that the parser can handle mal-formed HTML. */
    @MediumTest
    public void testBadHtml() {
        Spanned spanned = Html.fromHtml("Hello <b>bbii");
        Object[] spans = spanned.getSpans(0, 100, Object.class);
        assertEquals(Typeface.ITALIC, ((StyleSpan) spans[0]).getStyle());
        assertEquals(7, spanned.getSpanStart(spans[0]));
        assertEquals(9, spanned.getSpanEnd(spans[0]));
        assertEquals(Typeface.BOLD, ((StyleSpan) spans[1]).getStyle());
        assertEquals(6, spanned.getSpanStart(spans[1]));
        assertEquals(9, spanned.getSpanEnd(spans[1]));
        assertEquals(Typeface.ITALIC, ((StyleSpan) spans[2]).getStyle());
        assertEquals(9, spanned.getSpanStart(spans[2]));
        assertEquals(10, spanned.getSpanEnd(spans[2]));
    }

    @MediumTest
    public void testSymbols() {
        String spanned = Html.fromHtml("© > <").toString();
        assertEquals("\u00a9 > <", spanned);
    }
    
    @MediumTest
    public void testColor() throws Exception {
        Spanned s;
        ForegroundColorSpan[] colors;

        s = Html.fromHtml("<font color=\"#00FF00\">something");
        colors = s.getSpans(0, s.length(), ForegroundColorSpan.class);
        assertEquals(1, colors.length);
        assertEquals(0xFF00FF00, colors[0].getForegroundColor());

        s = Html.fromHtml("<font color=\"navy\">something");
        colors = s.getSpans(0, s.length(), ForegroundColorSpan.class);
        assertEquals(1, colors.length);
        assertEquals(0xFF000080, colors[0].getForegroundColor());

        s = Html.fromHtml("<font color=\"gibberish\">something");
        colors = s.getSpans(0, s.length(), ForegroundColorSpan.class);
        assertEquals(0, colors.length);
    }

    @MediumTest
    public void testResourceColor() throws Exception {
        ColorStateList c =
                Resources.getSystem().getColorStateList(android.R.color.primary_text_dark);
        Spanned s;
        TextAppearanceSpan[] colors;

        s = Html.fromHtml("<font color=\"@android:color/primary_text_dark\">something");
        colors = s.getSpans(0, s.length(), TextAppearanceSpan.class);
        assertEquals(1, colors.length);
        assertEquals(c.toString(), colors[0].getTextColor().toString());

        s = Html.fromHtml("<font color=\"@android:primary_text_dark\">something");
        colors = s.getSpans(0, s.length(), TextAppearanceSpan.class);
        assertEquals(1, colors.length);
        assertEquals(c.toString(), colors[0].getTextColor().toString());

        s = Html.fromHtml("<font color=\"@color/primary_text_dark\">something");
        colors = s.getSpans(0, s.length(), TextAppearanceSpan.class);
        assertEquals(1, colors.length);
        assertEquals(c.toString(), colors[0].getTextColor().toString());

        s = Html.fromHtml("<font color=\"@primary_text_dark\">something");
        colors = s.getSpans(0, s.length(), TextAppearanceSpan.class);
        assertEquals(1, colors.length);
        assertEquals(c.toString(), colors[0].getTextColor().toString());

        s = Html.fromHtml("<font color=\"@" + android.R.color.primary_text_dark
                + "\">something</font>");
        colors = s.getSpans(0, s.length(), TextAppearanceSpan.class);
        assertEquals(1, colors.length);
        assertEquals(c.toString(), colors[0].getTextColor().toString());

        s = Html.fromHtml("<font color=\"gibberish\">something");
        colors = s.getSpans(0, s.length(), TextAppearanceSpan.class);
        assertEquals(colors.length, 0);
    }

    @SmallTest
    public void testParagraphs() throws Exception {
        SpannableString s;

        s = new SpannableString("Hello world");
        assertEquals(Html.toHtml(s), "<p>Hello world

\n"); s = new SpannableString("Hello world\nor something"); assertEquals(Html.toHtml(s), "<p>Hello world
\nor something

\n"); s = new SpannableString("Hello world\n\nor something"); assertEquals(Html.toHtml(s), "<p>Hello world

\n

or something

\n"); s = new SpannableString("Hello world\n\n\nor something"); assertEquals(Html.toHtml(s), "<p>Hello world

\n

or something

\n"); assertEquals("foo\nbar", Html.fromHtml("foo<br>bar").toString()); assertEquals("foo\nbar", Html.fromHtml("foo<br>\nbar").toString()); assertEquals("foo\nbar", Html.fromHtml("foo<br>\n \nbar").toString()); } @SmallTest public void testBlockquote() throws Exception { SpannableString s; s = new SpannableString("Hello world"); s.setSpan(new QuoteSpan(), 0, s.length(), Spannable.SPAN_PARAGRAPH); assertEquals(Html.toHtml(s), "<blockquote>

Hello world

\n\n"); s = new SpannableString("Hello\n\nworld"); s.setSpan(new QuoteSpan(), 0, 7, Spannable.SPAN_PARAGRAPH); assertEquals(Html.toHtml(s), "<blockquote>

Hello

\n\n

world

\n"); } @SmallTest public void testEntities() throws Exception { SpannableString s; s = new SpannableString("Hello <&> world"); assertEquals(Html.toHtml(s), "<p>Hello <&> world

\n"); s = new SpannableString("Hello \u03D5 world"); assertEquals(Html.toHtml(s), "<p>Hello ϕ world

\n"); s = new SpannableString("Hello world"); assertEquals(Html.toHtml(s), "<p>Hello  world

\n"); } @SmallTest public void testMarkup() throws Exception { SpannableString s; s = new SpannableString("Hello bold world"); s.setSpan(new StyleSpan(Typeface.BOLD), 6, s.length() - 6, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); assertEquals(Html.toHtml(s), "<p>Hello bold world

\n"); s = new SpannableString("Hello italic world"); s.setSpan(new StyleSpan(Typeface.ITALIC), 6, s.length() - 6, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); assertEquals(Html.toHtml(s), "<p>Hello italic world

\n"); s = new SpannableString("Hello monospace world"); s.setSpan(new TypefaceSpan("monospace"), 6, s.length() - 6, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); assertEquals(Html.toHtml(s), "<p>Hello monospace world

\n"); s = new SpannableString("Hello superscript world"); s.setSpan(new SuperscriptSpan(), 6, s.length() - 6, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); assertEquals(Html.toHtml(s), "<p>Hello superscript world

\n"); s = new SpannableString("Hello subscript world"); s.setSpan(new SubscriptSpan(), 6, s.length() - 6, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); assertEquals(Html.toHtml(s), "<p>Hello subscript world

\n"); s = new SpannableString("Hello underline world"); s.setSpan(new UnderlineSpan(), 6, s.length() - 6, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); assertEquals(Html.toHtml(s), "<p>Hello underline world

\n"); s = new SpannableString("Hello struck world"); s.setSpan(new StrikethroughSpan(), 6, s.length() - 6, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); assertEquals(Html.toHtml(s), "<p>Hello struck world

\n"); s = new SpannableString("Hello linky world"); s.setSpan(new URLSpan("http://www.google.com"), 6, s.length() - 6, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); assertEquals(Html.toHtml(s), "<p>Hello linky world

\n"); } @SmallTest public void testImg() throws Exception { Spanned s; s = Html.fromHtml("yes<img src=\"http://example.com/foo.gif\">no"); assertEquals("<p>yesno

\n", Html.toHtml(s)); } @SmallTest public void testUtf8() throws Exception { Spanned s; s = Html.fromHtml("<p>\u0124\u00eb\u0142\u0142o, world!

"); assertEquals("<p>Ĥëłło, world!

\n", Html.toHtml(s)); } }

Other Android examples (source code examples)

Here is a short list of links related to this Android HtmlTest.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.