content/base/test/TestPlainTextSerializer.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial