1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 #include "gtest/gtest.h"
5
6 #include "mozilla/intl/Calendar.h"
7 #include "mozilla/Span.h"
8 #include "./TestBuffer.h"
9
10 namespace mozilla::intl {
11
12 // Firefox 1.0 release date.
13 const double CALENDAR_DATE = 1032800850000.0;
14
TEST(IntlCalendar,GetLegacyKeywordValuesForLocale)15 TEST(IntlCalendar, GetLegacyKeywordValuesForLocale)
16 {
17 bool hasGregorian = false;
18 bool hasIslamic = false;
19 auto gregorian = MakeStringSpan("gregorian");
20 auto islamic = MakeStringSpan("islamic");
21 auto keywords = Calendar::GetLegacyKeywordValuesForLocale("en-US").unwrap();
22 for (auto name : keywords) {
23 // Check a few keywords, as this list may not be stable between ICU updates.
24 if (name.unwrap() == gregorian) {
25 hasGregorian = true;
26 }
27 if (name.unwrap() == islamic) {
28 hasIslamic = true;
29 }
30 }
31 ASSERT_TRUE(hasGregorian);
32 ASSERT_TRUE(hasIslamic);
33 }
34
TEST(IntlCalendar,GetBcp47KeywordValuesForLocale)35 TEST(IntlCalendar, GetBcp47KeywordValuesForLocale)
36 {
37 bool hasGregory = false;
38 bool hasIslamic = false;
39 auto gregory = MakeStringSpan("gregory");
40 auto islamic = MakeStringSpan("islamic");
41 auto keywords = Calendar::GetBcp47KeywordValuesForLocale("en-US").unwrap();
42 for (auto name : keywords) {
43 // Check a few keywords, as this list may not be stable between ICU updates.
44 if (name.unwrap() == gregory) {
45 hasGregory = true;
46 }
47 if (name.unwrap() == islamic) {
48 hasIslamic = true;
49 }
50 }
51 ASSERT_TRUE(hasGregory);
52 ASSERT_TRUE(hasIslamic);
53 }
54
TEST(IntlCalendar,GetBcp47Type)55 TEST(IntlCalendar, GetBcp47Type)
56 {
57 auto calendar =
58 Calendar::TryCreate("en-US", Some(MakeStringSpan(u"GMT+3"))).unwrap();
59 ASSERT_STREQ(calendar->GetBcp47Type().unwrap(), "gregory");
60 }
61
62 // These tests are dependent on the machine that this test is being run on.
63 // Unwrap the results to ensure it doesn't fail, but don't check the values.
TEST(IntlCalendar,SystemDependentTests)64 TEST(IntlCalendar, SystemDependentTests)
65 {
66 auto calendar =
67 Calendar::TryCreate("en-US", Some(MakeStringSpan(u"GMT+3"))).unwrap();
68 TestBuffer<char16_t> buffer;
69 // e.g. For America/Chicago: 1000 * 60 * 60 * -6
70 calendar->GetDefaultTimeZoneOffsetMs().unwrap();
71
72 // e.g. "America/Chicago"
73 Calendar::GetDefaultTimeZone(buffer).unwrap();
74
75 // This isn't system dependent, but currently there is no way to verify the
76 // results.
77 calendar->SetTimeInMs(CALENDAR_DATE).unwrap();
78 }
79
TEST(IntlCalendar,CloneFrom)80 TEST(IntlCalendar, CloneFrom)
81 {
82 auto dtFormat =
83 DateTimeFormat::TryCreateFromStyle(
84 MakeStringSpan("en-US"), DateTimeStyle::Medium, DateTimeStyle::Medium,
85 Some(MakeStringSpan(u"America/Chicago")))
86 .unwrap();
87
88 dtFormat->CloneCalendar(CALENDAR_DATE).unwrap();
89 }
90
TEST(IntlCalendar,GetCanonicalTimeZoneID)91 TEST(IntlCalendar, GetCanonicalTimeZoneID)
92 {
93 TestBuffer<char16_t> buffer;
94
95 // Providing a canonical time zone results in the same string at the end.
96 Calendar::GetCanonicalTimeZoneID(MakeStringSpan(u"America/Chicago"), buffer)
97 .unwrap();
98 ASSERT_EQ(buffer.get_string_view<char16_t>(), u"America/Chicago");
99
100 // Providing an alias will result in the canonical representation.
101 Calendar::GetCanonicalTimeZoneID(MakeStringSpan(u"Europe/Belfast"), buffer)
102 .unwrap();
103 ASSERT_EQ(buffer.get_string_view<char16_t>(), u"Europe/London");
104
105 // An unknown time zone results in an error.
106 ASSERT_TRUE(Calendar::GetCanonicalTimeZoneID(
107 MakeStringSpan(u"Not a time zone"), buffer)
108 .isErr());
109 }
110
111 } // namespace mozilla::intl
112