tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

TestParser.cpp (1811B)


      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/. */
      5 
      6 #include "gtest/gtest.h"
      7 #include "mozilla/ErrorResult.h"
      8 #include "mozilla/dom/DOMParser.h"
      9 #include "mozilla/dom/Document.h"
     10 #include "nsCOMPtr.h"
     11 #include "nsIDocumentEncoder.h"
     12 #include "nsString.h"
     13 
     14 // This is a test for mozilla::dom::DOMParser::CreateWithoutGlobal() which was
     15 // implemented for use in Thunderbird's MailNews module.
     16 
     17 // int main(int argc, char** argv)
     18 TEST(TestParser, TestParserMain)
     19 {
     20  bool allTestsPassed = false;
     21  constexpr auto htmlInput =
     22      u"<html><head>"
     23      "<meta http-equiv=\"content-type\" content=\"text/html; charset=\">"
     24      "</head><body>Hello <b>Thunderbird!</b></body></html>"_ns;
     25 
     26  do {
     27    // Parse the HTML source.
     28    mozilla::IgnoredErrorResult rv2;
     29    RefPtr<mozilla::dom::DOMParser> parser =
     30        mozilla::dom::DOMParser::CreateWithoutGlobal(rv2);
     31    if (rv2.Failed()) break;
     32    nsCOMPtr<mozilla::dom::Document> document = parser->ParseFromStringInternal(
     33        htmlInput, mozilla::dom::SupportedType::Text_html, rv2);
     34    if (rv2.Failed()) break;
     35 
     36    // Serialize it back to HTML source again.
     37    nsCOMPtr<nsIDocumentEncoder> encoder =
     38        do_createDocumentEncoder("text/html");
     39    if (!encoder) break;
     40    nsresult rv =
     41        encoder->Init(document, u"text/html"_ns, nsIDocumentEncoder::OutputRaw);
     42    if (NS_FAILED(rv)) break;
     43    nsString parsed;
     44    rv = encoder->EncodeToString(parsed);
     45    if (NS_FAILED(rv)) break;
     46 
     47    EXPECT_TRUE(parsed.Equals(htmlInput));
     48    allTestsPassed = true;
     49  } while (false);
     50 
     51  EXPECT_TRUE(allTestsPassed);
     52 }