1/*
2 * Copyright (C) 2013 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
17package com.android.mail.utils;
18
19import android.test.AndroidTestCase;
20import android.test.suitebuilder.annotation.SmallTest;
21
22import com.android.mail.utils.NotificationUtils.MailMessagePlainTextConverter;
23import com.google.android.mail.common.html.parser.HtmlTree;
24
25@SmallTest
26public class NotificationUtilsTest extends AndroidTestCase {
27    /**
28     * Verifies that we strip out <style /> tags.
29     */
30    public void testMailMessagePlainTextConverterStyles() {
31        final String expectedText = "This test passed!";
32        final String html = "<body style=3D=22margin:0; padding:0;=22>"
33                + "<style type=3D=22text/css=22>=20"
34                + "=2EReadMsgBody =7B width: 100%;=7D"
35                + "       img =7Bdisplay: block;=7D"
36                + "       html =7B -webkit-text-size-adjust:none; =7D</style>"
37                + expectedText + "</body>";
38
39        // Get the html "tree" for this message body
40        final HtmlTree htmlTree = Utils.getHtmlTree(html);
41        htmlTree.setConverterFactory(new HtmlTree.ConverterFactory() {
42            @Override
43            public HtmlTree.Converter<String> createInstance() {
44                return new MailMessagePlainTextConverter();
45            }
46        });
47
48        final String resultText = htmlTree.getPlainText();
49
50        assertEquals(expectedText, resultText);
51    }
52
53    /**
54     * Verifies that we strip out nested <style /> tags.
55     */
56    public void testMailMessagePlainTextConverterNestedStyles() {
57        final String expectedText = "This test passed!";
58        final String html = "<body style=3D=22margin:0; padding:0;=22>"
59                + "<style type=3D=22text/css=22>=20"
60                + "=2EReadMsgBody =7B width: 100%;=7D"
61                + "       img =7Bdisplay: block;=7D"
62                + "       html =7B -webkit-text-size-adjust:none; =7D"
63                + "       <style>html =7B -webkit-text-size-adjust:none; =7D</style></style>"
64                + expectedText + "</body>";
65
66        // Get the html "tree" for this message body
67        final HtmlTree htmlTree = Utils.getHtmlTree(html);
68        htmlTree.setConverterFactory(new HtmlTree.ConverterFactory() {
69            @Override
70            public HtmlTree.Converter<String> createInstance() {
71                return new MailMessagePlainTextConverter();
72            }
73        });
74
75        final String resultText = htmlTree.getPlainText();
76
77        assertEquals(expectedText, resultText);
78    }
79}
80