michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "TestHarness.h" michael@0: michael@0: #include "nsServiceManagerUtils.h" michael@0: #include "nsStringGlue.h" michael@0: #include "nsIDocumentEncoder.h" michael@0: #include "nsCRT.h" michael@0: #include "nsIParserUtils.h" michael@0: michael@0: void michael@0: ConvertBufToPlainText(nsString &aConBuf, int aFlag) michael@0: { michael@0: nsCOMPtr utils = michael@0: do_GetService(NS_PARSERUTILS_CONTRACTID); michael@0: utils->ConvertToPlainText(aConBuf, aFlag, 72, aConBuf); michael@0: } michael@0: michael@0: // Test for ASCII with format=flowed; delsp=yes michael@0: nsresult michael@0: TestASCIIWithFlowedDelSp() michael@0: { michael@0: nsString test; michael@0: nsString result; michael@0: michael@0: test.AssignLiteral("" michael@0: "Firefox Firefox Firefox Firefox " michael@0: "Firefox Firefox Firefox Firefox " michael@0: "Firefox Firefox Firefox Firefox" michael@0: ""); michael@0: michael@0: ConvertBufToPlainText(test, nsIDocumentEncoder::OutputFormatted | michael@0: nsIDocumentEncoder::OutputCRLineBreak | michael@0: nsIDocumentEncoder::OutputLFLineBreak | michael@0: nsIDocumentEncoder::OutputFormatFlowed | michael@0: nsIDocumentEncoder::OutputFormatDelSp); michael@0: michael@0: // create result case michael@0: result.AssignLiteral("Firefox Firefox Firefox Firefox " michael@0: "Firefox Firefox Firefox Firefox " michael@0: "Firefox \r\nFirefox Firefox Firefox\r\n"); michael@0: michael@0: if (!test.Equals(result)) { michael@0: fail("Wrong HTML to ASCII text serialization with format=flowed; delsp=yes"); michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: passed("HTML to ASCII text serialization with format=flowed; delsp=yes"); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: // Test for CJK with format=flowed; delsp=yes michael@0: nsresult michael@0: TestCJKWithFlowedDelSp() michael@0: { michael@0: nsString test; michael@0: nsString result; michael@0: michael@0: test.AssignLiteral(""); michael@0: for (uint32_t i = 0; i < 40; i++) { michael@0: // Insert Kanji (U+5341) michael@0: test.Append(0x5341); michael@0: } michael@0: test.AppendLiteral(""); michael@0: michael@0: ConvertBufToPlainText(test, nsIDocumentEncoder::OutputFormatted | michael@0: nsIDocumentEncoder::OutputCRLineBreak | michael@0: nsIDocumentEncoder::OutputLFLineBreak | michael@0: nsIDocumentEncoder::OutputFormatFlowed | michael@0: nsIDocumentEncoder::OutputFormatDelSp); michael@0: michael@0: // create result case michael@0: for (uint32_t i = 0; i < 36; i++) { michael@0: result.Append(0x5341); michael@0: } michael@0: result.Append(NS_LITERAL_STRING(" \r\n")); michael@0: for (uint32_t i = 0; i < 4; i++) { michael@0: result.Append(0x5341); michael@0: } michael@0: result.Append(NS_LITERAL_STRING("\r\n")); michael@0: michael@0: if (!test.Equals(result)) { michael@0: fail("Wrong HTML to CJK text serialization with format=flowed; delsp=yes"); michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: passed("HTML to CJK text serialization with format=flowed; delsp=yes"); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: TestPrettyPrintedHtml() michael@0: { michael@0: nsString test; michael@0: test.AppendLiteral( michael@0: "" NS_LINEBREAK michael@0: "" NS_LINEBREAK michael@0: " first
" NS_LINEBREAK michael@0: " second
" NS_LINEBREAK michael@0: "" NS_LINEBREAK ""); michael@0: michael@0: ConvertBufToPlainText(test, 0); michael@0: if (!test.EqualsLiteral("first" NS_LINEBREAK "second" NS_LINEBREAK)) { michael@0: fail("Wrong prettyprinted html to text serialization"); michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: passed("prettyprinted HTML to text serialization test"); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: TestPlainTextSerializer() michael@0: { michael@0: nsString test; michael@0: test.AppendLiteral("basespan" michael@0: "body"); michael@0: ConvertBufToPlainText(test, 0); michael@0: if (!test.EqualsLiteral("basespanbody")) { michael@0: fail("Wrong html to text serialization"); michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: passed("HTML to text serialization test"); michael@0: michael@0: nsresult rv = TestASCIIWithFlowedDelSp(); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: rv = TestCJKWithFlowedDelSp(); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: rv = TestPrettyPrintedHtml(); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: // Add new tests here... michael@0: return NS_OK; michael@0: } michael@0: michael@0: int main(int argc, char** argv) michael@0: { michael@0: ScopedXPCOM xpcom("PlainTextSerializer"); michael@0: if (xpcom.failed()) michael@0: return 1; michael@0: michael@0: int retval = 0; michael@0: if (NS_FAILED(TestPlainTextSerializer())) { michael@0: retval = 1; michael@0: } michael@0: michael@0: return retval; michael@0: }