tor-browser

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

TestLocalization.cpp (3664B)


      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/intl/Localization.h"
      8 
      9 using namespace mozilla;
     10 using namespace mozilla::dom;
     11 using namespace mozilla::intl;
     12 
     13 TEST(Intl_Localization, FormatValueSyncMissing)
     14 {
     15  nsTArray<nsCString> resIds = {
     16      "toolkit/global/handlerDialog.ftl"_ns,
     17  };
     18  RefPtr<Localization> l10n = Localization::Create(resIds, true);
     19 
     20  auto l10nId = "non-existing-l10n-id"_ns;
     21  IgnoredErrorResult rv;
     22  nsAutoCString result;
     23 
     24  l10n->FormatValueSync(l10nId, {}, result, rv);
     25  ASSERT_FALSE(rv.Failed());
     26  ASSERT_TRUE(result.IsEmpty());
     27 }
     28 
     29 TEST(Intl_Localization, FormatValueSync)
     30 {
     31  nsTArray<nsCString> resIds = {
     32      "toolkit/global/handlerDialog.ftl"_ns,
     33  };
     34  RefPtr<Localization> l10n = Localization::Create(resIds, true);
     35 
     36  auto l10nId = "permission-dialog-unset-description"_ns;
     37  IgnoredErrorResult rv;
     38  nsAutoCString result;
     39 
     40  l10n->FormatValueSync(l10nId, {}, result, rv);
     41  ASSERT_FALSE(rv.Failed());
     42  ASSERT_FALSE(result.IsEmpty());
     43 }
     44 
     45 TEST(Intl_Localization, FormatValueSyncWithArgs)
     46 {
     47  nsTArray<nsCString> resIds = {
     48      "toolkit/global/handlerDialog.ftl"_ns,
     49  };
     50  RefPtr<Localization> l10n = Localization::Create(resIds, true);
     51 
     52  auto l10nId = "permission-dialog-description"_ns;
     53 
     54  auto l10nArgs = dom::Optional<intl::L10nArgs>();
     55  l10nArgs.Construct();
     56 
     57  auto dirArg = l10nArgs.Value().Entries().AppendElement();
     58  dirArg->mKey = "scheme"_ns;
     59  dirArg->mValue.SetValue().SetAsUTF8String().Assign("Foo"_ns);
     60 
     61  IgnoredErrorResult rv;
     62  nsAutoCString result;
     63 
     64  l10n->FormatValueSync(l10nId, l10nArgs, result, rv);
     65  ASSERT_FALSE(rv.Failed());
     66  ASSERT_TRUE(result.Find("Foo"_ns) > -1);
     67 }
     68 
     69 TEST(Intl_Localization, FormatMessagesSync)
     70 {
     71  nsTArray<nsCString> resIds = {
     72      "toolkit/global/handlerDialog.ftl"_ns,
     73  };
     74  RefPtr<Localization> l10n = Localization::Create(resIds, true);
     75 
     76  dom::Sequence<dom::OwningUTF8StringOrL10nIdArgs> l10nIds;
     77  auto* l10nId = l10nIds.AppendElement(fallible);
     78  ASSERT_TRUE(l10nId);
     79  l10nId->SetAsUTF8String().Assign("permission-dialog-unset-description"_ns);
     80 
     81  IgnoredErrorResult rv;
     82  nsTArray<dom::Nullable<dom::L10nMessage>> result;
     83 
     84  l10n->FormatMessagesSync(l10nIds, result, rv);
     85  ASSERT_FALSE(rv.Failed());
     86  ASSERT_FALSE(result.IsEmpty());
     87 }
     88 
     89 TEST(Intl_Localization, FormatMessagesSyncWithArgs)
     90 {
     91  nsTArray<nsCString> resIds = {
     92      "toolkit/global/handlerDialog.ftl"_ns,
     93  };
     94  RefPtr<Localization> l10n = Localization::Create(resIds, true);
     95 
     96  dom::Sequence<dom::OwningUTF8StringOrL10nIdArgs> l10nIds;
     97  L10nIdArgs& key0 = l10nIds.AppendElement(fallible)->SetAsL10nIdArgs();
     98  key0.mId.Assign("permission-dialog-description"_ns);
     99  auto arg = key0.mArgs.SetValue().Entries().AppendElement();
    100  arg->mKey = "scheme"_ns;
    101  arg->mValue.SetValue().SetAsUTF8String().Assign("Foo"_ns);
    102 
    103  L10nIdArgs& key1 = l10nIds.AppendElement(fallible)->SetAsL10nIdArgs();
    104  key1.mId.Assign("chooser-window"_ns);
    105 
    106  IgnoredErrorResult rv;
    107  nsTArray<dom::Nullable<dom::L10nMessage>> result;
    108 
    109  l10n->FormatMessagesSync(l10nIds, result, rv);
    110  ASSERT_FALSE(rv.Failed());
    111  ASSERT_TRUE(result.Length() == 2);
    112  ASSERT_TRUE(result.ElementAt(0).Value().mValue.Find("Foo"_ns) > -1);
    113 
    114  auto fmtAttr = result.ElementAt(1).Value().mAttributes.Value();
    115  ASSERT_TRUE(fmtAttr.Length() == 2);
    116  ASSERT_FALSE(fmtAttr.ElementAt(0).mName.IsEmpty());
    117  ASSERT_FALSE(fmtAttr.ElementAt(0).mValue.IsEmpty());
    118 }