1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
17 : * The Initial Developer of the Original Code is Mozilla Foundation.
18 : * Portions created by the Initial Developer are Copyright (C) 2008
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : * Olli Pettay <Olli.Pettay@helsinki.fi> (Original Author)
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either of the GNU General Public License Version 2 or later (the "GPL"),
26 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #include "TestHarness.h"
39 :
40 : #include "nsServiceManagerUtils.h"
41 : #include "nsStringGlue.h"
42 : #include "nsIDocumentEncoder.h"
43 : #include "nsCRT.h"
44 : #include "nsIParserUtils.h"
45 :
46 : void
47 4 : ConvertBufToPlainText(nsString &aConBuf, int aFlag)
48 : {
49 : nsCOMPtr<nsIParserUtils> utils =
50 8 : do_GetService(NS_PARSERUTILS_CONTRACTID);
51 4 : utils->ConvertToPlainText(aConBuf, aFlag, 72, aConBuf);
52 4 : }
53 :
54 : // Test for ASCII with format=flowed; delsp=yes
55 : nsresult
56 1 : TestASCIIWithFlowedDelSp()
57 : {
58 2 : nsString test;
59 2 : nsString result;
60 :
61 : test.AssignLiteral("<html><body>"
62 : "Firefox Firefox Firefox Firefox "
63 : "Firefox Firefox Firefox Firefox "
64 : "Firefox Firefox Firefox Firefox"
65 1 : "</body></html>");
66 :
67 : ConvertBufToPlainText(test, nsIDocumentEncoder::OutputFormatted |
68 : nsIDocumentEncoder::OutputCRLineBreak |
69 : nsIDocumentEncoder::OutputLFLineBreak |
70 : nsIDocumentEncoder::OutputFormatFlowed |
71 1 : nsIDocumentEncoder::OutputFormatDelSp);
72 :
73 : // create result case
74 : result.AssignLiteral("Firefox Firefox Firefox Firefox "
75 : "Firefox Firefox Firefox Firefox "
76 1 : "Firefox \r\nFirefox Firefox Firefox\r\n");
77 :
78 1 : if (!test.Equals(result)) {
79 0 : fail("Wrong HTML to ASCII text serialization with format=flowed; delsp=yes");
80 0 : return NS_ERROR_FAILURE;
81 : }
82 :
83 1 : passed("HTML to ASCII text serialization with format=flowed; delsp=yes");
84 :
85 1 : return NS_OK;
86 : }
87 :
88 : // Test for CJK with format=flowed; delsp=yes
89 : nsresult
90 1 : TestCJKWithFlowedDelSp()
91 : {
92 2 : nsString test;
93 2 : nsString result;
94 :
95 1 : test.AssignLiteral("<html><body>");
96 41 : for (PRUint32 i = 0; i < 40; i++) {
97 : // Insert Kanji (U+5341)
98 40 : test.Append(0x5341);
99 : }
100 1 : test.AppendLiteral("</body></html>");
101 :
102 : ConvertBufToPlainText(test, nsIDocumentEncoder::OutputFormatted |
103 : nsIDocumentEncoder::OutputCRLineBreak |
104 : nsIDocumentEncoder::OutputLFLineBreak |
105 : nsIDocumentEncoder::OutputFormatFlowed |
106 1 : nsIDocumentEncoder::OutputFormatDelSp);
107 :
108 : // create result case
109 37 : for (PRUint32 i = 0; i < 36; i++) {
110 36 : result.Append(0x5341);
111 : }
112 1 : result.Append(NS_LITERAL_STRING(" \r\n"));
113 5 : for (PRUint32 i = 0; i < 4; i++) {
114 4 : result.Append(0x5341);
115 : }
116 1 : result.Append(NS_LITERAL_STRING("\r\n"));
117 :
118 1 : if (!test.Equals(result)) {
119 0 : fail("Wrong HTML to CJK text serialization with format=flowed; delsp=yes");
120 0 : return NS_ERROR_FAILURE;
121 : }
122 :
123 1 : passed("HTML to CJK text serialization with format=flowed; delsp=yes");
124 :
125 1 : return NS_OK;
126 : }
127 :
128 : nsresult
129 1 : TestPrettyPrintedHtml()
130 : {
131 2 : nsString test;
132 : test.AppendLiteral(
133 : "<html>" NS_LINEBREAK
134 : "<body>" NS_LINEBREAK
135 : " first<br>" NS_LINEBREAK
136 : " second<br>" NS_LINEBREAK
137 1 : "</body>" NS_LINEBREAK "</html>");
138 :
139 1 : ConvertBufToPlainText(test, 0);
140 1 : if (!test.EqualsLiteral("first" NS_LINEBREAK "second" NS_LINEBREAK)) {
141 0 : fail("Wrong prettyprinted html to text serialization");
142 0 : return NS_ERROR_FAILURE;
143 : }
144 :
145 1 : passed("prettyprinted HTML to text serialization test");
146 1 : return NS_OK;
147 : }
148 :
149 : nsresult
150 1 : TestPlainTextSerializer()
151 : {
152 2 : nsString test;
153 : test.AppendLiteral("<html><base>base</base><head><span>span</span></head>"
154 1 : "<body>body</body></html>");
155 1 : ConvertBufToPlainText(test, 0);
156 1 : if (!test.EqualsLiteral("basespanbody")) {
157 0 : fail("Wrong html to text serialization");
158 0 : return NS_ERROR_FAILURE;
159 : }
160 :
161 1 : passed("HTML to text serialization test");
162 :
163 1 : nsresult rv = TestASCIIWithFlowedDelSp();
164 1 : NS_ENSURE_SUCCESS(rv, rv);
165 :
166 1 : rv = TestCJKWithFlowedDelSp();
167 1 : NS_ENSURE_SUCCESS(rv, rv);
168 :
169 1 : rv = TestPrettyPrintedHtml();
170 1 : NS_ENSURE_SUCCESS(rv, rv);
171 :
172 : // Add new tests here...
173 1 : return NS_OK;
174 : }
175 :
176 1 : int main(int argc, char** argv)
177 : {
178 2 : ScopedXPCOM xpcom("PlainTextSerializer");
179 1 : if (xpcom.failed())
180 0 : return 1;
181 :
182 1 : int retval = 0;
183 1 : if (NS_FAILED(TestPlainTextSerializer())) {
184 0 : retval = 1;
185 : }
186 :
187 1 : return retval;
188 : }
|