Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/base/test/gtest/TestPlainTextSerializer.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#include "gtest/gtest.h"
8
9
#include "nsServiceManagerUtils.h"
10
#include "nsString.h"
11
#include "nsIDocumentEncoder.h"
12
#include "nsCRT.h"
13
#include "nsIParserUtils.h"
14
15
void
16
ConvertBufToPlainText(nsString &aConBuf, int aFlag)
17
0
{
18
0
  nsCOMPtr<nsIParserUtils> utils = do_GetService(NS_PARSERUTILS_CONTRACTID);
19
0
  utils->ConvertToPlainText(aConBuf, aFlag, 72, aConBuf);
20
0
}
21
22
// Test for ASCII with format=flowed; delsp=yes
23
TEST(PlainTextSerializer, ASCIIWithFlowedDelSp)
24
0
{
25
0
  nsString test;
26
0
  nsString result;
27
0
28
0
  test.AssignLiteral("<html><body>"
29
0
                     "Firefox Firefox Firefox Firefox "
30
0
                     "Firefox Firefox Firefox Firefox "
31
0
                     "Firefox Firefox Firefox Firefox"
32
0
                     "</body></html>");
33
0
34
0
  ConvertBufToPlainText(test, nsIDocumentEncoder::OutputFormatted |
35
0
                              nsIDocumentEncoder::OutputCRLineBreak |
36
0
                              nsIDocumentEncoder::OutputLFLineBreak |
37
0
                              nsIDocumentEncoder::OutputFormatFlowed |
38
0
                              nsIDocumentEncoder::OutputFormatDelSp);
39
0
40
0
  // create result case
41
0
  result.AssignLiteral("Firefox Firefox Firefox Firefox "
42
0
                       "Firefox Firefox Firefox Firefox "
43
0
                       "Firefox  \r\nFirefox Firefox Firefox\r\n");
44
0
45
0
  ASSERT_TRUE(test.Equals(result)) <<
46
0
    "Wrong HTML to ASCII text serialization with format=flowed; delsp=yes";
47
0
}
48
49
// Test for CJK with format=flowed; delsp=yes
50
TEST(PlainTextSerializer, CJKWithFlowedDelSp)
51
0
{
52
0
  nsString test;
53
0
  nsString result;
54
0
55
0
  test.AssignLiteral("<html><body>");
56
0
  for (uint32_t i = 0; i < 40; i++) {
57
0
    // Insert Kanji (U+5341)
58
0
    test.Append(0x5341);
59
0
  }
60
0
  test.AppendLiteral("</body></html>");
61
0
62
0
  ConvertBufToPlainText(test, nsIDocumentEncoder::OutputFormatted |
63
0
                              nsIDocumentEncoder::OutputCRLineBreak |
64
0
                              nsIDocumentEncoder::OutputLFLineBreak |
65
0
                              nsIDocumentEncoder::OutputFormatFlowed |
66
0
                              nsIDocumentEncoder::OutputFormatDelSp);
67
0
68
0
  // create result case
69
0
  for (uint32_t i = 0; i < 36; i++) {
70
0
    result.Append(0x5341);
71
0
  }
72
0
  result.AppendLiteral(" \r\n");
73
0
  for (uint32_t i = 0; i < 4; i++) {
74
0
    result.Append(0x5341);
75
0
  }
76
0
  result.AppendLiteral("\r\n");
77
0
78
0
  ASSERT_TRUE(test.Equals(result)) <<
79
0
    "Wrong HTML to CJK text serialization with format=flowed; delsp=yes";
80
0
}
81
82
// Test for CJK with DisallowLineBreaking
83
TEST(PlainTextSerializer, CJKWithDisallowLineBreaking)
84
0
{
85
0
  nsString test;
86
0
  nsString result;
87
0
88
0
  test.AssignLiteral("<html><body>");
89
0
  for (uint32_t i = 0; i < 400; i++) {
90
0
    // Insert Kanji (U+5341)
91
0
    test.Append(0x5341);
92
0
  }
93
0
  test.AppendLiteral("</body></html>");
94
0
95
0
  ConvertBufToPlainText(test, nsIDocumentEncoder::OutputFormatted |
96
0
                              nsIDocumentEncoder::OutputCRLineBreak |
97
0
                              nsIDocumentEncoder::OutputLFLineBreak |
98
0
                              nsIDocumentEncoder::OutputFormatFlowed |
99
0
                              nsIDocumentEncoder::OutputDisallowLineBreaking);
100
0
101
0
  // create result case
102
0
  for (uint32_t i = 0; i < 400; i++) {
103
0
    result.Append(0x5341);
104
0
  }
105
0
  result.AppendLiteral("\r\n");
106
0
107
0
  ASSERT_TRUE(test.Equals(result)) <<
108
0
    "Wrong HTML to CJK text serialization with OutputDisallowLineBreaking";
109
0
}
110
111
// Test for ASCII with format=flowed; and quoted lines in preformatted span.
112
TEST(PlainTextSerializer, PreformatFlowedQuotes)
113
0
{
114
0
  nsString test;
115
0
  nsString result;
116
0
117
0
  test.AssignLiteral("<html><body>"
118
0
                     "<span style=\"white-space: pre-wrap;\">"
119
0
                     "&gt; Firefox Firefox Firefox Firefox <br>"
120
0
                     "&gt; Firefox Firefox Firefox Firefox<br>"
121
0
                     "&gt;<br>"
122
0
                     "&gt;&gt; Firefox Firefox Firefox Firefox <br>"
123
0
                     "&gt;&gt; Firefox Firefox Firefox Firefox<br>"
124
0
                     "</span></body></html>");
125
0
126
0
  ConvertBufToPlainText(test, nsIDocumentEncoder::OutputFormatted |
127
0
                              nsIDocumentEncoder::OutputCRLineBreak |
128
0
                              nsIDocumentEncoder::OutputLFLineBreak |
129
0
                              nsIDocumentEncoder::OutputFormatFlowed);
130
0
131
0
  // create result case
132
0
  result.AssignLiteral("> Firefox Firefox Firefox Firefox \r\n"
133
0
                       "> Firefox Firefox Firefox Firefox\r\n"
134
0
                       ">\r\n"
135
0
                       ">> Firefox Firefox Firefox Firefox \r\n"
136
0
                       ">> Firefox Firefox Firefox Firefox\r\n");
137
0
138
0
  ASSERT_TRUE(test.Equals(result)) <<
139
0
    "Wrong HTML to ASCII text serialization with format=flowed; and quoted "
140
0
    "lines";
141
0
}
142
143
TEST(PlainTextSerializer, PrettyPrintedHtml)
144
0
{
145
0
  nsString test;
146
0
  test.AppendLiteral(
147
0
    "<html>" NS_LINEBREAK
148
0
    "<body>" NS_LINEBREAK
149
0
    "  first<br>" NS_LINEBREAK
150
0
    "  second<br>" NS_LINEBREAK
151
0
    "</body>" NS_LINEBREAK "</html>");
152
0
153
0
  ConvertBufToPlainText(test, 0);
154
0
  ASSERT_TRUE(test.EqualsLiteral("first" NS_LINEBREAK "second" NS_LINEBREAK)) <<
155
0
    "Wrong prettyprinted html to text serialization";
156
0
}
157
158
TEST(PlainTextSerializer, PreElement)
159
0
{
160
0
  nsString test;
161
0
  test.AppendLiteral(
162
0
    "<html>" NS_LINEBREAK
163
0
    "<body>" NS_LINEBREAK
164
0
    "<pre>" NS_LINEBREAK
165
0
    "  first" NS_LINEBREAK
166
0
    "  second" NS_LINEBREAK
167
0
    "</pre>" NS_LINEBREAK
168
0
    "</body>" NS_LINEBREAK "</html>");
169
0
170
0
  ConvertBufToPlainText(test, 0);
171
0
  ASSERT_TRUE(test.EqualsLiteral("  first" NS_LINEBREAK
172
0
                                 "  second" NS_LINEBREAK NS_LINEBREAK)) <<
173
0
    "Wrong prettyprinted html to text serialization";
174
0
}
175
176
TEST(PlainTextSerializer, BlockElement)
177
0
{
178
0
  nsString test;
179
0
  test.AppendLiteral(
180
0
    "<html>" NS_LINEBREAK
181
0
    "<body>" NS_LINEBREAK
182
0
    "<div>" NS_LINEBREAK
183
0
    "  first" NS_LINEBREAK
184
0
    "</div>" NS_LINEBREAK
185
0
    "<div>" NS_LINEBREAK
186
0
    "  second" NS_LINEBREAK
187
0
    "</div>" NS_LINEBREAK
188
0
    "</body>" NS_LINEBREAK "</html>");
189
0
190
0
  ConvertBufToPlainText(test, 0);
191
0
  ASSERT_TRUE(test.EqualsLiteral("first" NS_LINEBREAK "second" NS_LINEBREAK)) <<
192
0
    "Wrong prettyprinted html to text serialization";
193
0
}
194
195
TEST(PlainTextSerializer, PreWrapElementForThunderbird)
196
0
{
197
0
  // This test examines the magic pre-wrap setup that Thunderbird relies on.
198
0
  nsString test;
199
0
  test.AppendLiteral(
200
0
    "<html>" NS_LINEBREAK
201
0
    "<body style=\"white-space: pre-wrap; width: 10ch;\">" NS_LINEBREAK
202
0
    "<pre>" NS_LINEBREAK
203
0
    "  first line is too long" NS_LINEBREAK
204
0
    "  second line is even loooonger  " NS_LINEBREAK
205
0
    "</pre>" NS_LINEBREAK
206
0
    "</body>" NS_LINEBREAK "</html>");
207
0
208
0
  ConvertBufToPlainText(test, nsIDocumentEncoder::OutputWrap);
209
0
  // "\n\n  first\nline is\ntoo long\n  second\nline is\neven\nloooonger\n\n\n"
210
0
  ASSERT_TRUE(test.EqualsLiteral(NS_LINEBREAK NS_LINEBREAK
211
0
                                 "  first" NS_LINEBREAK
212
0
                                 "line is" NS_LINEBREAK
213
0
                                 "too long" NS_LINEBREAK
214
0
                                 "  second" NS_LINEBREAK
215
0
                                 "line is" NS_LINEBREAK
216
0
                                 "even" NS_LINEBREAK
217
0
                                 "loooonger" NS_LINEBREAK
218
0
                                 NS_LINEBREAK NS_LINEBREAK)) <<
219
0
    "Wrong prettyprinted html to text serialization";
220
0
}
221
222
TEST(PlainTextSerializer, Simple)
223
0
{
224
0
  nsString test;
225
0
  test.AppendLiteral("<html><base>base</base><head><span>span</span></head>"
226
0
                     "<body>body</body></html>");
227
0
  ConvertBufToPlainText(test, 0);
228
0
  ASSERT_TRUE(test.EqualsLiteral("basespanbody")) <<
229
0
    "Wrong html to text serialization";
230
0
}
231