content/base/test/TestPlainTextSerializer.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/base/test/TestPlainTextSerializer.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,156 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "TestHarness.h"
    1.10 +
    1.11 +#include "nsServiceManagerUtils.h"
    1.12 +#include "nsStringGlue.h"
    1.13 +#include "nsIDocumentEncoder.h"
    1.14 +#include "nsCRT.h"
    1.15 +#include "nsIParserUtils.h"
    1.16 +
    1.17 +void
    1.18 +ConvertBufToPlainText(nsString &aConBuf, int aFlag)
    1.19 +{
    1.20 +  nsCOMPtr<nsIParserUtils> utils =
    1.21 +    do_GetService(NS_PARSERUTILS_CONTRACTID);
    1.22 +  utils->ConvertToPlainText(aConBuf, aFlag, 72, aConBuf);
    1.23 +}
    1.24 +
    1.25 +// Test for ASCII with format=flowed; delsp=yes
    1.26 +nsresult
    1.27 +TestASCIIWithFlowedDelSp()
    1.28 +{
    1.29 +  nsString test;
    1.30 +  nsString result;
    1.31 +
    1.32 +  test.AssignLiteral("<html><body>"
    1.33 +                     "Firefox Firefox Firefox Firefox "
    1.34 +                     "Firefox Firefox Firefox Firefox "
    1.35 +                     "Firefox Firefox Firefox Firefox"
    1.36 +                     "</body></html>");
    1.37 +
    1.38 +  ConvertBufToPlainText(test, nsIDocumentEncoder::OutputFormatted |
    1.39 +                              nsIDocumentEncoder::OutputCRLineBreak |
    1.40 +                              nsIDocumentEncoder::OutputLFLineBreak |
    1.41 +                              nsIDocumentEncoder::OutputFormatFlowed |
    1.42 +                              nsIDocumentEncoder::OutputFormatDelSp);
    1.43 +
    1.44 +  // create result case
    1.45 +  result.AssignLiteral("Firefox Firefox Firefox Firefox "
    1.46 +                       "Firefox Firefox Firefox Firefox "
    1.47 +                       "Firefox  \r\nFirefox Firefox Firefox\r\n");
    1.48 +
    1.49 +  if (!test.Equals(result)) {
    1.50 +    fail("Wrong HTML to ASCII text serialization with format=flowed; delsp=yes");
    1.51 +    return NS_ERROR_FAILURE;
    1.52 +  }
    1.53 +
    1.54 +  passed("HTML to ASCII text serialization with format=flowed; delsp=yes");
    1.55 +
    1.56 +  return NS_OK;
    1.57 +}
    1.58 +
    1.59 +// Test for CJK with format=flowed; delsp=yes
    1.60 +nsresult
    1.61 +TestCJKWithFlowedDelSp()
    1.62 +{
    1.63 +  nsString test;
    1.64 +  nsString result;
    1.65 +
    1.66 +  test.AssignLiteral("<html><body>");
    1.67 +  for (uint32_t i = 0; i < 40; i++) {
    1.68 +    // Insert Kanji (U+5341)
    1.69 +    test.Append(0x5341);
    1.70 +  }
    1.71 +  test.AppendLiteral("</body></html>");
    1.72 +
    1.73 +  ConvertBufToPlainText(test, nsIDocumentEncoder::OutputFormatted |
    1.74 +                              nsIDocumentEncoder::OutputCRLineBreak |
    1.75 +                              nsIDocumentEncoder::OutputLFLineBreak |
    1.76 +                              nsIDocumentEncoder::OutputFormatFlowed |
    1.77 +                              nsIDocumentEncoder::OutputFormatDelSp);
    1.78 +
    1.79 +  // create result case
    1.80 +  for (uint32_t i = 0; i < 36; i++) {
    1.81 +    result.Append(0x5341);
    1.82 +  }
    1.83 +  result.Append(NS_LITERAL_STRING(" \r\n"));
    1.84 +  for (uint32_t i = 0; i < 4; i++) {
    1.85 +    result.Append(0x5341);
    1.86 +  }
    1.87 +  result.Append(NS_LITERAL_STRING("\r\n"));
    1.88 +
    1.89 +  if (!test.Equals(result)) {
    1.90 +    fail("Wrong HTML to CJK text serialization with format=flowed; delsp=yes");
    1.91 +    return NS_ERROR_FAILURE;
    1.92 +  }
    1.93 +
    1.94 +  passed("HTML to CJK text serialization with format=flowed; delsp=yes");
    1.95 +
    1.96 +  return NS_OK;
    1.97 +}
    1.98 +
    1.99 +nsresult
   1.100 +TestPrettyPrintedHtml()
   1.101 +{
   1.102 +  nsString test;
   1.103 +  test.AppendLiteral(
   1.104 +    "<html>" NS_LINEBREAK
   1.105 +    "<body>" NS_LINEBREAK
   1.106 +    "  first<br>" NS_LINEBREAK
   1.107 +    "  second<br>" NS_LINEBREAK
   1.108 +    "</body>" NS_LINEBREAK "</html>");
   1.109 +
   1.110 +  ConvertBufToPlainText(test, 0);
   1.111 +  if (!test.EqualsLiteral("first" NS_LINEBREAK "second" NS_LINEBREAK)) {
   1.112 +    fail("Wrong prettyprinted html to text serialization");
   1.113 +    return NS_ERROR_FAILURE;
   1.114 +  }
   1.115 +
   1.116 +  passed("prettyprinted HTML to text serialization test");
   1.117 +  return NS_OK;
   1.118 +}
   1.119 +
   1.120 +nsresult
   1.121 +TestPlainTextSerializer()
   1.122 +{
   1.123 +  nsString test;
   1.124 +  test.AppendLiteral("<html><base>base</base><head><span>span</span></head>"
   1.125 +                     "<body>body</body></html>");
   1.126 +  ConvertBufToPlainText(test, 0);
   1.127 +  if (!test.EqualsLiteral("basespanbody")) {
   1.128 +    fail("Wrong html to text serialization");
   1.129 +    return NS_ERROR_FAILURE;
   1.130 +  }
   1.131 +
   1.132 +  passed("HTML to text serialization test");
   1.133 +
   1.134 +  nsresult rv = TestASCIIWithFlowedDelSp();
   1.135 +  NS_ENSURE_SUCCESS(rv, rv);
   1.136 +
   1.137 +  rv = TestCJKWithFlowedDelSp();
   1.138 +  NS_ENSURE_SUCCESS(rv, rv);
   1.139 +
   1.140 +  rv = TestPrettyPrintedHtml();
   1.141 +  NS_ENSURE_SUCCESS(rv, rv);
   1.142 +
   1.143 +  // Add new tests here...
   1.144 +  return NS_OK;
   1.145 +}
   1.146 +
   1.147 +int main(int argc, char** argv)
   1.148 +{
   1.149 +  ScopedXPCOM xpcom("PlainTextSerializer");
   1.150 +  if (xpcom.failed())
   1.151 +    return 1;
   1.152 +
   1.153 +  int retval = 0;
   1.154 +  if (NS_FAILED(TestPlainTextSerializer())) {
   1.155 +    retval = 1;
   1.156 +  }
   1.157 +
   1.158 +  return retval;
   1.159 +}

mercurial