tor-browser

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

TestL10nOverlays.cpp (2668B)


      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/NullPrincipal.h"
      8 #include "mozilla/dom/Document.h"
      9 #include "mozilla/dom/Element.h"
     10 #include "mozilla/dom/L10nOverlays.h"
     11 #include "mozilla/dom/L10nOverlaysBinding.h"
     12 #include "nsNetUtil.h"
     13 
     14 using mozilla::NullPrincipal;
     15 using namespace mozilla::dom;
     16 
     17 static already_AddRefed<Document> SetUpDocument() {
     18  nsCOMPtr<nsIURI> uri;
     19  NS_NewURI(getter_AddRefs(uri), "about:blank");
     20  nsCOMPtr<nsIPrincipal> principal =
     21      NullPrincipal::CreateWithoutOriginAttributes();
     22  nsCOMPtr<Document> document;
     23  nsresult rv =
     24      NS_NewDOMDocument(getter_AddRefs(document),
     25                        u""_ns,   // aNamespaceURI
     26                        u""_ns,   // aQualifiedName
     27                        nullptr,  // aDoctype
     28                        uri, uri, principal,
     29                        mozilla::dom::LoadedAsData::No,  // aLoadedAsData
     30                        nullptr,                         // aEventObject
     31                        DocumentFlavor::HTML);
     32 
     33  if (NS_WARN_IF(NS_FAILED(rv))) {
     34    return nullptr;
     35  }
     36  return document.forget();
     37 }
     38 
     39 /**
     40 * This test verifies that the basic C++ DOM L10nOverlays API
     41 * works correctly.
     42 */
     43 TEST(DOM_L10n_Overlays, Initial)
     44 {
     45  mozilla::ErrorResult rv;
     46 
     47  // 1. Set up an HTML document.
     48  nsCOMPtr<Document> doc = SetUpDocument();
     49 
     50  // 2. Create a simple Element with a child.
     51  //
     52  //   <div>
     53  //     <a data-l10n-name="link" href="https://www.mozilla.org"></a>
     54  //   </div>
     55  //
     56  RefPtr<Element> elem = doc->CreateHTMLElement(nsGkAtoms::div);
     57  RefPtr<Element> span = doc->CreateHTMLElement(nsGkAtoms::a);
     58  span->SetAttribute(u"data-l10n-name"_ns, u"link"_ns, rv);
     59  span->SetAttribute(u"href"_ns, u"https://www.mozilla.org"_ns, rv);
     60  elem->AppendChild(*span, rv);
     61 
     62  // 3. Create an L10nMessage with a translation for the element.
     63  L10nMessage translation;
     64  translation.mValue.AssignLiteral(
     65      "Hello <a data-l10n-name=\"link\">World</a>.");
     66 
     67  // 4. Translate the element.
     68  nsTArray<L10nOverlaysError> errors;
     69  L10nOverlays::TranslateElement(*elem, translation, errors, rv);
     70 
     71  nsAutoString textContent;
     72  elem->GetInnerHTML(textContent, rv);
     73 
     74  // 5. Verify that the innerHTML matches the expectations.
     75  ASSERT_STREQ(NS_ConvertUTF16toUTF8(textContent).get(),
     76               "Hello <a data-l10n-name=\"link\" "
     77               "href=\"https://www.mozilla.org\">World</a>.");
     78 }