content/base/test/TestPlainTextSerializer.cpp

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:2e9452ccd14f
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 #include "TestHarness.h"
7
8 #include "nsServiceManagerUtils.h"
9 #include "nsStringGlue.h"
10 #include "nsIDocumentEncoder.h"
11 #include "nsCRT.h"
12 #include "nsIParserUtils.h"
13
14 void
15 ConvertBufToPlainText(nsString &aConBuf, int aFlag)
16 {
17 nsCOMPtr<nsIParserUtils> utils =
18 do_GetService(NS_PARSERUTILS_CONTRACTID);
19 utils->ConvertToPlainText(aConBuf, aFlag, 72, aConBuf);
20 }
21
22 // Test for ASCII with format=flowed; delsp=yes
23 nsresult
24 TestASCIIWithFlowedDelSp()
25 {
26 nsString test;
27 nsString result;
28
29 test.AssignLiteral("<html><body>"
30 "Firefox Firefox Firefox Firefox "
31 "Firefox Firefox Firefox Firefox "
32 "Firefox Firefox Firefox Firefox"
33 "</body></html>");
34
35 ConvertBufToPlainText(test, nsIDocumentEncoder::OutputFormatted |
36 nsIDocumentEncoder::OutputCRLineBreak |
37 nsIDocumentEncoder::OutputLFLineBreak |
38 nsIDocumentEncoder::OutputFormatFlowed |
39 nsIDocumentEncoder::OutputFormatDelSp);
40
41 // create result case
42 result.AssignLiteral("Firefox Firefox Firefox Firefox "
43 "Firefox Firefox Firefox Firefox "
44 "Firefox \r\nFirefox Firefox Firefox\r\n");
45
46 if (!test.Equals(result)) {
47 fail("Wrong HTML to ASCII text serialization with format=flowed; delsp=yes");
48 return NS_ERROR_FAILURE;
49 }
50
51 passed("HTML to ASCII text serialization with format=flowed; delsp=yes");
52
53 return NS_OK;
54 }
55
56 // Test for CJK with format=flowed; delsp=yes
57 nsresult
58 TestCJKWithFlowedDelSp()
59 {
60 nsString test;
61 nsString result;
62
63 test.AssignLiteral("<html><body>");
64 for (uint32_t i = 0; i < 40; i++) {
65 // Insert Kanji (U+5341)
66 test.Append(0x5341);
67 }
68 test.AppendLiteral("</body></html>");
69
70 ConvertBufToPlainText(test, nsIDocumentEncoder::OutputFormatted |
71 nsIDocumentEncoder::OutputCRLineBreak |
72 nsIDocumentEncoder::OutputLFLineBreak |
73 nsIDocumentEncoder::OutputFormatFlowed |
74 nsIDocumentEncoder::OutputFormatDelSp);
75
76 // create result case
77 for (uint32_t i = 0; i < 36; i++) {
78 result.Append(0x5341);
79 }
80 result.Append(NS_LITERAL_STRING(" \r\n"));
81 for (uint32_t i = 0; i < 4; i++) {
82 result.Append(0x5341);
83 }
84 result.Append(NS_LITERAL_STRING("\r\n"));
85
86 if (!test.Equals(result)) {
87 fail("Wrong HTML to CJK text serialization with format=flowed; delsp=yes");
88 return NS_ERROR_FAILURE;
89 }
90
91 passed("HTML to CJK text serialization with format=flowed; delsp=yes");
92
93 return NS_OK;
94 }
95
96 nsresult
97 TestPrettyPrintedHtml()
98 {
99 nsString test;
100 test.AppendLiteral(
101 "<html>" NS_LINEBREAK
102 "<body>" NS_LINEBREAK
103 " first<br>" NS_LINEBREAK
104 " second<br>" NS_LINEBREAK
105 "</body>" NS_LINEBREAK "</html>");
106
107 ConvertBufToPlainText(test, 0);
108 if (!test.EqualsLiteral("first" NS_LINEBREAK "second" NS_LINEBREAK)) {
109 fail("Wrong prettyprinted html to text serialization");
110 return NS_ERROR_FAILURE;
111 }
112
113 passed("prettyprinted HTML to text serialization test");
114 return NS_OK;
115 }
116
117 nsresult
118 TestPlainTextSerializer()
119 {
120 nsString test;
121 test.AppendLiteral("<html><base>base</base><head><span>span</span></head>"
122 "<body>body</body></html>");
123 ConvertBufToPlainText(test, 0);
124 if (!test.EqualsLiteral("basespanbody")) {
125 fail("Wrong html to text serialization");
126 return NS_ERROR_FAILURE;
127 }
128
129 passed("HTML to text serialization test");
130
131 nsresult rv = TestASCIIWithFlowedDelSp();
132 NS_ENSURE_SUCCESS(rv, rv);
133
134 rv = TestCJKWithFlowedDelSp();
135 NS_ENSURE_SUCCESS(rv, rv);
136
137 rv = TestPrettyPrintedHtml();
138 NS_ENSURE_SUCCESS(rv, rv);
139
140 // Add new tests here...
141 return NS_OK;
142 }
143
144 int main(int argc, char** argv)
145 {
146 ScopedXPCOM xpcom("PlainTextSerializer");
147 if (xpcom.failed())
148 return 1;
149
150 int retval = 0;
151 if (NS_FAILED(TestPlainTextSerializer())) {
152 retval = 1;
153 }
154
155 return retval;
156 }

mercurial