summaryrefslogtreecommitdiff
path: root/java/com/android/contacts/common/format/testing/SpannedTestUtils.java
blob: 293d9d5adf57f6c2341913e9b82aab3ada6aad4e (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
/*
 * Copyright (C) 2011 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 com.android.contacts.common.format.testing;

import android.test.suitebuilder.annotation.SmallTest;
import android.text.Html;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.StyleSpan;
import android.widget.TextView;
import junit.framework.Assert;

/** Utility class to check the value of spanned text in text views. */
@SmallTest
public class SpannedTestUtils {

  /**
   * Checks that the text contained in the text view matches the given HTML text.
   *
   * @param expectedHtmlText the expected text to be in the text view
   * @param textView the text view from which to get the text
   */
  public static void checkHtmlText(String expectedHtmlText, TextView textView) {
    String actualHtmlText = Html.toHtml((Spanned) textView.getText());
    if (TextUtils.isEmpty(expectedHtmlText)) {
      // If the text is empty, it does not add the <p></p> bits to it.
      Assert.assertEquals("", actualHtmlText);
    } else {
      Assert.assertEquals("<p dir=ltr>" + expectedHtmlText + "</p>\n", actualHtmlText);
    }
  }

  /**
   * Assert span exists in the correct location.
   *
   * @param seq The spannable string to check.
   * @param start The starting index.
   * @param end The ending index.
   */
  public static void assertPrefixSpan(CharSequence seq, int start, int end) {
    Assert.assertTrue(seq instanceof Spanned);
    Spanned spannable = (Spanned) seq;

    if (start > 0) {
      Assert.assertEquals(0, getNumForegroundColorSpansBetween(spannable, 0, start - 1));
    }
    Assert.assertEquals(1, getNumForegroundColorSpansBetween(spannable, start, end));
    Assert.assertEquals(
        0, getNumForegroundColorSpansBetween(spannable, end + 1, spannable.length() - 1));
  }

  private static int getNumForegroundColorSpansBetween(Spanned value, int start, int end) {
    return value.getSpans(start, end, StyleSpan.class).length;
  }

  /**
   * Asserts that the given character sequence is not a Spanned object and text is correct.
   *
   * @param seq The sequence to check.
   * @param expected The expected text.
   */
  public static void assertNotSpanned(CharSequence seq, String expected) {
    Assert.assertFalse(seq instanceof Spanned);
    Assert.assertEquals(expected, seq);
  }

  public static int getNextTransition(SpannableString seq, int start) {
    return seq.nextSpanTransition(start, seq.length(), StyleSpan.class);
  }
}