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.

     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/. */
     6 #include "TestHarness.h"
     8 #include "nsServiceManagerUtils.h"
     9 #include "nsStringGlue.h"
    10 #include "nsIDocumentEncoder.h"
    11 #include "nsCRT.h"
    12 #include "nsIParserUtils.h"
    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 }
    22 // Test for ASCII with format=flowed; delsp=yes
    23 nsresult
    24 TestASCIIWithFlowedDelSp()
    25 {
    26   nsString test;
    27   nsString result;
    29   test.AssignLiteral("<html><body>"
    30                      "Firefox Firefox Firefox Firefox "
    31                      "Firefox Firefox Firefox Firefox "
    32                      "Firefox Firefox Firefox Firefox"
    33                      "</body></html>");
    35   ConvertBufToPlainText(test, nsIDocumentEncoder::OutputFormatted |
    36                               nsIDocumentEncoder::OutputCRLineBreak |
    37                               nsIDocumentEncoder::OutputLFLineBreak |
    38                               nsIDocumentEncoder::OutputFormatFlowed |
    39                               nsIDocumentEncoder::OutputFormatDelSp);
    41   // create result case
    42   result.AssignLiteral("Firefox Firefox Firefox Firefox "
    43                        "Firefox Firefox Firefox Firefox "
    44                        "Firefox  \r\nFirefox Firefox Firefox\r\n");
    46   if (!test.Equals(result)) {
    47     fail("Wrong HTML to ASCII text serialization with format=flowed; delsp=yes");
    48     return NS_ERROR_FAILURE;
    49   }
    51   passed("HTML to ASCII text serialization with format=flowed; delsp=yes");
    53   return NS_OK;
    54 }
    56 // Test for CJK with format=flowed; delsp=yes
    57 nsresult
    58 TestCJKWithFlowedDelSp()
    59 {
    60   nsString test;
    61   nsString result;
    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>");
    70   ConvertBufToPlainText(test, nsIDocumentEncoder::OutputFormatted |
    71                               nsIDocumentEncoder::OutputCRLineBreak |
    72                               nsIDocumentEncoder::OutputLFLineBreak |
    73                               nsIDocumentEncoder::OutputFormatFlowed |
    74                               nsIDocumentEncoder::OutputFormatDelSp);
    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"));
    86   if (!test.Equals(result)) {
    87     fail("Wrong HTML to CJK text serialization with format=flowed; delsp=yes");
    88     return NS_ERROR_FAILURE;
    89   }
    91   passed("HTML to CJK text serialization with format=flowed; delsp=yes");
    93   return NS_OK;
    94 }
    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>");
   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   }
   113   passed("prettyprinted HTML to text serialization test");
   114   return NS_OK;
   115 }
   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   }
   129   passed("HTML to text serialization test");
   131   nsresult rv = TestASCIIWithFlowedDelSp();
   132   NS_ENSURE_SUCCESS(rv, rv);
   134   rv = TestCJKWithFlowedDelSp();
   135   NS_ENSURE_SUCCESS(rv, rv);
   137   rv = TestPrettyPrintedHtml();
   138   NS_ENSURE_SUCCESS(rv, rv);
   140   // Add new tests here...
   141   return NS_OK;
   142 }
   144 int main(int argc, char** argv)
   145 {
   146   ScopedXPCOM xpcom("PlainTextSerializer");
   147   if (xpcom.failed())
   148     return 1;
   150   int retval = 0;
   151   if (NS_FAILED(TestPlainTextSerializer())) {
   152     retval = 1;
   153   }
   155   return retval;
   156 }

mercurial