summaryrefslogtreecommitdiffstats
path: root/intl/l10n/test/gtest/TestLocalization.cpp
blob: 804c228c989fd3478a3bf091df3eb10af3f9fa94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "gtest/gtest.h"
#include "mozilla/intl/Localization.h"

using namespace mozilla;
using namespace mozilla::dom;
using namespace mozilla::intl;

TEST(Intl_Localization, FormatValueSyncMissing)
{
  nsTArray<nsCString> resIds = {
      "toolkit/global/handlerDialog.ftl"_ns,
  };
  RefPtr<Localization> l10n = Localization::Create(resIds, true);

  auto l10nId = "non-existing-l10n-id"_ns;
  IgnoredErrorResult rv;
  nsAutoCString result;

  l10n->FormatValueSync(l10nId, {}, result, rv);
  ASSERT_FALSE(rv.Failed());
  ASSERT_TRUE(result.IsEmpty());
}

TEST(Intl_Localization, FormatValueSync)
{
  nsTArray<nsCString> resIds = {
      "toolkit/global/handlerDialog.ftl"_ns,
  };
  RefPtr<Localization> l10n = Localization::Create(resIds, true);

  auto l10nId = "permission-dialog-unset-description"_ns;
  IgnoredErrorResult rv;
  nsAutoCString result;

  l10n->FormatValueSync(l10nId, {}, result, rv);
  ASSERT_FALSE(rv.Failed());
  ASSERT_FALSE(result.IsEmpty());
}

TEST(Intl_Localization, FormatValueSyncWithArgs)
{
  nsTArray<nsCString> resIds = {
      "toolkit/global/handlerDialog.ftl"_ns,
  };
  RefPtr<Localization> l10n = Localization::Create(resIds, true);

  auto l10nId = "permission-dialog-description"_ns;

  auto l10nArgs = dom::Optional<intl::L10nArgs>();
  l10nArgs.Construct();

  auto dirArg = l10nArgs.Value().Entries().AppendElement();
  dirArg->mKey = "scheme"_ns;
  dirArg->mValue.SetValue().SetAsUTF8String().Assign("Foo"_ns);

  IgnoredErrorResult rv;
  nsAutoCString result;

  l10n->FormatValueSync(l10nId, l10nArgs, result, rv);
  ASSERT_FALSE(rv.Failed());
  ASSERT_TRUE(result.Find("Foo"_ns) > -1);
}

TEST(Intl_Localization, FormatMessagesSync)
{
  nsTArray<nsCString> resIds = {
      "toolkit/global/handlerDialog.ftl"_ns,
  };
  RefPtr<Localization> l10n = Localization::Create(resIds, true);

  dom::Sequence<dom::OwningUTF8StringOrL10nIdArgs> l10nIds;
  auto* l10nId = l10nIds.AppendElement(fallible);
  ASSERT_TRUE(l10nId);
  l10nId->SetAsUTF8String().Assign("permission-dialog-unset-description"_ns);

  IgnoredErrorResult rv;
  nsTArray<dom::Nullable<dom::L10nMessage>> result;

  l10n->FormatMessagesSync(l10nIds, result, rv);
  ASSERT_FALSE(rv.Failed());
  ASSERT_FALSE(result.IsEmpty());
}

TEST(Intl_Localization, FormatMessagesSyncWithArgs)
{
  nsTArray<nsCString> resIds = {
      "toolkit/global/handlerDialog.ftl"_ns,
  };
  RefPtr<Localization> l10n = Localization::Create(resIds, true);

  dom::Sequence<dom::OwningUTF8StringOrL10nIdArgs> l10nIds;
  L10nIdArgs& key0 = l10nIds.AppendElement(fallible)->SetAsL10nIdArgs();
  key0.mId.Assign("permission-dialog-description"_ns);
  auto arg = key0.mArgs.SetValue().Entries().AppendElement();
  arg->mKey = "scheme"_ns;
  arg->mValue.SetValue().SetAsUTF8String().Assign("Foo"_ns);

  L10nIdArgs& key1 = l10nIds.AppendElement(fallible)->SetAsL10nIdArgs();
  key1.mId.Assign("chooser-window"_ns);

  IgnoredErrorResult rv;
  nsTArray<dom::Nullable<dom::L10nMessage>> result;

  l10n->FormatMessagesSync(l10nIds, result, rv);
  ASSERT_FALSE(rv.Failed());
  ASSERT_TRUE(result.Length() == 2);
  ASSERT_TRUE(result.ElementAt(0).Value().mValue.Find("Foo"_ns) > -1);

  auto fmtAttr = result.ElementAt(1).Value().mAttributes.Value();
  ASSERT_TRUE(fmtAttr.Length() == 2);
  ASSERT_FALSE(fmtAttr.ElementAt(0).mName.IsEmpty());
  ASSERT_FALSE(fmtAttr.ElementAt(0).mValue.IsEmpty());
}