1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "ecmascript/js_displaynames.h"
17
18 #include "ecmascript/intl/locale_helper.h"
19 #include "ecmascript/tests/test_helper.h"
20
21 using namespace panda;
22 using namespace panda::ecmascript;
23
24 namespace panda::test {
25 class JSDisplayNamesTest : public BaseTestWithScope<true> {
26 };
27
28 /**
29 * @tc.name: GetIcuLocaleDisplayNames
30 * @tc.desc: Call "SetIcuLocaleDisplayNames" function Set IcuLocale DisplayNames,check whether the IcuLocale
31 * DisplayNames through "GetIcuLocaleDisplayNames" function is within expectations then call "getLocale"
32 * function display locale and check the locale is within expectations.
33 * @tc.type: FUNC
34 * @tc.require:
35 */
HWTEST_F_L0(JSDisplayNamesTest,GetIcuLocaleDisplayNames)36 HWTEST_F_L0(JSDisplayNamesTest, GetIcuLocaleDisplayNames)
37 {
38 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
39 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
40
41 JSHandle<JSTaggedValue> ctor = env->GetDisplayNamesFunction();
42 JSHandle<JSDisplayNames> displayNames =
43 JSHandle<JSDisplayNames>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(ctor), ctor));
44
45 icu::Locale icuLocale("en");
46 UDisplayContext display_context[] = {UDISPCTX_LENGTH_SHORT};
47 icu::LocaleDisplayNames* iculocaledisplaynames =
48 icu::LocaleDisplayNames::createInstance(icuLocale, display_context, 1);
49 EXPECT_TRUE(iculocaledisplaynames != nullptr);
50 JSDisplayNames::SetIcuLocaleDisplayNames(
51 thread, displayNames, iculocaledisplaynames, JSDisplayNames::FreeIcuLocaleDisplayNames);
52 icu::LocaleDisplayNames *resultIculocaledisplaynames = displayNames->GetIcuLocaleDisplayNames(thread);
53 EXPECT_TRUE(iculocaledisplaynames == resultIculocaledisplaynames);
54 JSHandle<EcmaString> localeStr =
55 intl::LocaleHelper::ToLanguageTag(thread, resultIculocaledisplaynames->getLocale());
56 EXPECT_STREQ(EcmaStringAccessor(localeStr).ToCString(thread).c_str(), "en");
57 }
58
59 /**
60 * @tc.name: GetAvailableLocales
61 * @tc.desc: Call function "GetAvailableLocales" to obtain the available locale from the ICU library and
62 * check whether the obtained locale is empty.
63 * @tc.type: FUNC
64 * @tc.require:
65 */
HWTEST_F_L0(JSDisplayNamesTest,GetAvailableLocales)66 HWTEST_F_L0(JSDisplayNamesTest, GetAvailableLocales)
67 {
68 JSHandle<TaggedArray> displayLocale = JSDisplayNames::GetAvailableLocales(thread);
69 uint32_t localeLen = displayLocale->GetLength();
70 EXPECT_NE(localeLen, 0U);
71
72 for (uint32_t i = 0; i < localeLen; i++) {
73 EXPECT_FALSE(displayLocale->Get(thread, i).IsHole());
74 }
75 }
76
SetOptionProperties(JSThread * thread,JSHandle<JSObject> & optionsObj,std::map<std::string,std::string> & displayOptions)77 void SetOptionProperties(JSThread *thread, JSHandle<JSObject> &optionsObj,
78 std::map<std::string, std::string> &displayOptions)
79 {
80 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
81 auto globalConst = thread->GlobalConstants();
82 // display options keys
83 JSHandle<JSTaggedValue> styleKey = globalConst->GetHandledStyleString();
84 JSHandle<JSTaggedValue> typeKey = globalConst->GetHandledTypeString();
85 JSHandle<JSTaggedValue> fallBackKey = globalConst->GetHandledFallbackString();
86 // display options value
87 JSHandle<JSTaggedValue> styleValue(factory->NewFromASCII(displayOptions["style"].c_str()));
88 JSHandle<JSTaggedValue> typeValue(factory->NewFromASCII(displayOptions["type"].c_str()));
89 JSHandle<JSTaggedValue> fallBackValue(factory->NewFromASCII(displayOptions["fallback"].c_str()));
90 JSObject::SetProperty(thread, optionsObj, styleKey, styleValue);
91 JSObject::SetProperty(thread, optionsObj, typeKey, typeValue);
92 JSObject::SetProperty(thread, optionsObj, fallBackKey, fallBackValue);
93 }
94
95 /**
96 * @tc.name: InitializeDisplayNames
97 * @tc.desc: Call function "InitializeDisplayNames" to initialize the jsdisplaynames class object and check whether the
98 * properties of the object is within expectations.
99 * @tc.type: FUNC
100 * @tc.require:
101 */
HWTEST_F_L0(JSDisplayNamesTest,InitializeDisplayNames)102 HWTEST_F_L0(JSDisplayNamesTest, InitializeDisplayNames)
103 {
104 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
105 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
106
107 JSHandle<JSTaggedValue> ctor = env->GetDisplayNamesFunction();
108 JSHandle<JSDisplayNames> displayNames =
109 JSHandle<JSDisplayNames>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(ctor), ctor));
110 JSHandle<JSTaggedValue> objFun = env->GetObjectFunction();
111 JSHandle<JSObject> displayOptions = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objFun), objFun);
112 JSHandle<JSTaggedValue> localeStr(factory->NewFromASCII("en-US"));
113 // options is empty
114 JSDisplayNames::InitializeDisplayNames(thread, displayNames, localeStr, JSHandle<JSTaggedValue>(displayOptions));
115 // Default attribute value and type is undefiend throw a expection
116 EXPECT_EQ(displayNames->GetStyle(), StyOption::LONG);
117 EXPECT_EQ(displayNames->GetType(), TypednsOption::EXCEPTION);
118 EXPECT_EQ(displayNames->GetFallback(), FallbackOption::EXCEPTION);
119 EXPECT_TRUE(thread->HasPendingException());
120 thread->ClearException();
121 // options of the key and value
122 std::map<std::string, std::string> displayOptionsProperty {
123 { "style", "short" },
124 { "type", "script" },
125 { "fallback", "none" },
126 };
127 SetOptionProperties(thread, displayOptions, displayOptionsProperty);
128 // options is not empty
129 JSDisplayNames::InitializeDisplayNames(thread, displayNames, localeStr, JSHandle<JSTaggedValue>(displayOptions));
130 JSHandle<EcmaString> setlocale(thread, displayNames->GetLocale(thread));
131 EXPECT_EQ(displayNames->GetStyle(), StyOption::SHORT);
132 EXPECT_EQ(displayNames->GetType(), TypednsOption::SCRIPT);
133 EXPECT_EQ(displayNames->GetFallback(), FallbackOption::NONE);
134 EXPECT_STREQ(EcmaStringAccessor(setlocale).ToCString(thread).c_str(), "en-US");
135 EXPECT_TRUE(displayNames->GetIcuLocaleDisplayNames(thread) != nullptr);
136 }
137
138 /**
139 * @tc.name: CanonicalCodeForDisplayNames
140 * @tc.desc: Display the language region and script of the locale according to the display configuration of
141 * different regions.
142 * @tc.type: FUNC
143 * @tc.require:
144 */
HWTEST_F_L0(JSDisplayNamesTest,CanonicalCodeForDisplayNames)145 HWTEST_F_L0(JSDisplayNamesTest, CanonicalCodeForDisplayNames)
146 {
147 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
148 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
149
150 JSHandle<JSTaggedValue> ctor = env->GetDisplayNamesFunction();
151 JSHandle<JSDisplayNames> displayNames =
152 JSHandle<JSDisplayNames>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(ctor), ctor));
153 JSHandle<JSTaggedValue> objFun = env->GetObjectFunction();
154 JSHandle<JSObject> displayOptions = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objFun), objFun);
155 JSHandle<JSTaggedValue> locale(factory->NewFromASCII("zh-Hant"));
156 // test UDISPCTX_LENGTH_SHORT
157 std::map<std::string, std::string> displayOptionsProperty {
158 { "style", "narrow" },
159 { "type", "script" },
160 { "fallback", "none" },
161 };
162 SetOptionProperties(thread, displayOptions, displayOptionsProperty);
163 JSHandle<JSDisplayNames> initDisplayNames =
164 JSDisplayNames::InitializeDisplayNames(thread, displayNames, locale, JSHandle<JSTaggedValue>(displayOptions));
165 // CanonicalCode for script
166 JSHandle<EcmaString> code = factory->NewFromASCII("Kana");
167 JSHandle<EcmaString> resultDisplay =
168 JSDisplayNames::CanonicalCodeForDisplayNames(thread, initDisplayNames, initDisplayNames->GetType(), code);
169 EXPECT_STREQ(EcmaStringAccessor(resultDisplay).ToCString(thread).c_str(), "片假名");
170 // CanonicalCode for languege
171 code = factory->NewFromASCII("fr");
172 initDisplayNames->SetType(TypednsOption::LANGUAGE);
173 resultDisplay =
174 JSDisplayNames::CanonicalCodeForDisplayNames(thread, initDisplayNames, initDisplayNames->GetType(), code);
175 EXPECT_STREQ(EcmaStringAccessor(resultDisplay).ToCString(thread).c_str(), "法文");
176 // CanonicalCode for region
177 code = factory->NewFromASCII("US");
178 initDisplayNames->SetType(TypednsOption::REGION);
179 resultDisplay =
180 JSDisplayNames::CanonicalCodeForDisplayNames(thread, initDisplayNames, initDisplayNames->GetType(), code);
181 EXPECT_STREQ(EcmaStringAccessor(resultDisplay).ToCString(thread).c_str(), "美國");
182 }
183
184 /**
185 * @tc.name: ResolvedOptions
186 * @tc.desc: Call function "InitializeDisplayNames" to initialize the jsdisplaynames class object and Copy the
187 * properties of jsdisplaynames class to a new object.
188 * @tc.type: FUNC
189 * @tc.require:
190 */
HWTEST_F_L0(JSDisplayNamesTest,ResolvedOptions)191 HWTEST_F_L0(JSDisplayNamesTest, ResolvedOptions)
192 {
193 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
194 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
195 auto globalConst = thread->GlobalConstants();
196 JSHandle<JSTaggedValue> localeKey = globalConst->GetHandledLocaleString();
197 JSHandle<JSTaggedValue> styleKey = globalConst->GetHandledStyleString();
198 JSHandle<JSTaggedValue> typeKey = globalConst->GetHandledTypeString();
199 JSHandle<JSTaggedValue> fallBackKey = globalConst->GetHandledFallbackString();
200
201 JSHandle<JSTaggedValue> ctor = env->GetDisplayNamesFunction();
202 JSHandle<JSDisplayNames> displayNames =
203 JSHandle<JSDisplayNames>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(ctor), ctor));
204 JSHandle<JSTaggedValue> objFun = env->GetObjectFunction();
205 JSHandle<JSObject> displayOptions = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objFun), objFun);
206 JSHandle<JSTaggedValue> locale(factory->NewFromASCII("zh-Hant"));
207
208 std::map<std::string, std::string> displayOptionsProperty {
209 { "style", "short" },
210 { "type", "region" },
211 { "fallback", "code" },
212 };
213 JSHandle<JSTaggedValue> styleValue(factory->NewFromASCII(displayOptionsProperty["style"].c_str()));
214 JSHandle<JSTaggedValue> typeValue(factory->NewFromASCII(displayOptionsProperty["type"].c_str()));
215 JSHandle<JSTaggedValue> fallBackValue(factory->NewFromASCII(displayOptionsProperty["fallback"].c_str()));
216 SetOptionProperties(thread, displayOptions, displayOptionsProperty);
217 JSHandle<JSDisplayNames> initDisplayNames =
218 JSDisplayNames::InitializeDisplayNames(thread, displayNames, locale, JSHandle<JSTaggedValue>(displayOptions));
219
220 JSDisplayNames::ResolvedOptions(thread, initDisplayNames, displayOptions);
221 EXPECT_EQ(JSTaggedValue::SameValue(thread,
222 JSObject::GetProperty(thread, displayOptions, styleKey).GetValue(), styleValue), true);
223 EXPECT_EQ(JSTaggedValue::SameValue(thread,
224 JSObject::GetProperty(thread, displayOptions, localeKey).GetValue(), locale), true);
225 EXPECT_EQ(JSTaggedValue::SameValue(thread,
226 JSObject::GetProperty(thread, displayOptions, fallBackKey).GetValue(), fallBackValue), true);
227 EXPECT_EQ(JSTaggedValue::SameValue(thread,
228 JSObject::GetProperty(thread, displayOptions, typeKey).GetValue(), typeValue), true);
229 }
230 } // namespace panda::test
231