• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017, 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 package com.android.managedprovisioning.common;
17 
18 import static org.hamcrest.CoreMatchers.equalTo;
19 import static org.hamcrest.MatcherAssert.assertThat;
20 
21 import android.graphics.Color;
22 import android.text.Spanned;
23 
24 import androidx.test.InstrumentationRegistry;
25 
26 import com.android.managedprovisioning.preprovisioning.WebActivity;
27 
28 import org.junit.Before;
29 import org.junit.Test;
30 
31 public class HtmlToSpannedParserTest {
32     private static final int SAMPLE_COLOR = Color.MAGENTA;
33     private HtmlToSpannedParser mHtmlToSpannedParser;
34 
35     @Before
setUp()36     public void setUp() throws Exception {
37         mHtmlToSpannedParser =
38                 new HtmlToSpannedParser(new ClickableSpanFactory(SAMPLE_COLOR),
39                         url -> WebActivity.createIntent(InstrumentationRegistry.getTargetContext(),
40                                 url, SAMPLE_COLOR));
41     }
42 
43     @Test(expected = IllegalArgumentException.class)
throwsExceptionForEmptyInputs1()44     public void throwsExceptionForEmptyInputs1() {
45         mHtmlToSpannedParser.parseHtml(null);
46     }
47 
48     @Test(expected = IllegalArgumentException.class)
throwsExceptionForEmptyInputs2()49     public void throwsExceptionForEmptyInputs2() {
50         mHtmlToSpannedParser.parseHtml("");
51     }
52 
53     @Test
handlesSimpleText()54     public void handlesSimpleText() {
55         String inputHtml = "bb\n\ncc\ndd";
56         String textRaw = "bb cc dd"; // whitespace stripped out in the process of HTML conversion
57 
58         assertRawTextCorrect(inputHtml, textRaw);
59     }
60 
61     @Test
handlesComplexHtml()62     public void handlesComplexHtml() {
63         String inputHtml = "a <b> b </b> <h1> ch1 </h1> <ol> <li> i1 </li> </ol> e";
64         String textRaw = "a b \nch1 \ni1 \ne";
65 
66         assertRawTextCorrect(inputHtml, textRaw);
67         // TODO: add testing of formatting
68     }
69 
assertRawTextCorrect(String inputHtml, String textRaw)70     private void assertRawTextCorrect(String inputHtml, String textRaw) {
71         Spanned spanned = mHtmlToSpannedParser.parseHtml(inputHtml);
72         assertThat(spanned.toString(), equalTo(textRaw));
73     }
74 }