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