• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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/builtins/builtins_locale.h"
17 
18 #include "ecmascript/global_env.h"
19 #include "ecmascript/js_locale.h"
20 #include "ecmascript/tests/test_helper.h"
21 
22 using namespace panda::ecmascript;
23 using namespace panda::ecmascript::builtins;
24 
25 namespace panda::test {
26 class BuiltinsLocaleTest : public testing::Test {
27 public:
SetUpTestCase()28     static void SetUpTestCase()
29     {
30         GTEST_LOG_(INFO) << "SetUpTestCase";
31     }
32 
TearDownTestCase()33     static void TearDownTestCase()
34     {
35         GTEST_LOG_(INFO) << "TearDownCase";
36     }
37 
SetUp()38     void SetUp() override
39     {
40         JSRuntimeOptions options;
41 #if PANDA_TARGET_LINUX
42         // for consistency requirement, use ohos_icu4j/data as icu-data-path
43         options.SetIcuDataPath(ICU_PATH);
44 #endif
45         options.SetEnableForceGC(true);
46         instance = JSNApi::CreateEcmaVM(options);
47         instance->SetEnableForceGC(true);
48         ASSERT_TRUE(instance != nullptr) << "Cannot create EcmaVM";
49         thread = instance->GetJSThread();
50         scope = new EcmaHandleScope(thread);
51     }
52 
TearDown()53     void TearDown() override
54     {
55         TestHelper::DestroyEcmaVMWithScope(instance, scope);
56     }
57 
58     EcmaVM *instance {nullptr};
59     EcmaHandleScope *scope {nullptr};
60     JSThread *thread {nullptr};
61 };
62 
63 // new locale( [ options ] )
HWTEST_F_L0(BuiltinsLocaleTest,LocaleConstructor)64 HWTEST_F_L0(BuiltinsLocaleTest, LocaleConstructor)
65 {
66     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
67     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
68     JSHandle<JSFunction> newTarget(env->GetLocaleFunction());
69     JSHandle<JSTaggedValue> objFun = env->GetObjectFunction();
70 
71     JSHandle<JSTaggedValue> languageKey = thread->GlobalConstants()->GetHandledLanguageString();
72     JSHandle<JSTaggedValue> regionKey = thread->GlobalConstants()->GetHandledRegionString();
73     JSHandle<JSTaggedValue> numericKey = thread->GlobalConstants()->GetHandledNumericString();
74     JSHandle<JSTaggedValue> scriptKey = thread->GlobalConstants()->GetHandledScriptString();
75     JSHandle<JSTaggedValue> languageValue(factory->NewFromASCII("cn"));
76     JSHandle<JSTaggedValue> regionValue(factory->NewFromASCII("CN"));
77     JSHandle<JSTaggedValue> scriptValue(factory->NewFromASCII("Chin"));
78     JSHandle<JSTaggedValue> numericValue(thread, JSTaggedValue::True());
79     JSHandle<JSTaggedValue> localeString(factory->NewFromASCII("zh-Hans-CN"));
80 
81     JSHandle<JSObject> optionsObj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objFun), objFun);
82     JSObject::SetProperty(thread, optionsObj, numericKey, numericValue);
83     JSObject::SetProperty(thread, optionsObj, regionKey, regionValue);
84     JSObject::SetProperty(thread, optionsObj, languageKey, languageValue);
85     JSObject::SetProperty(thread, optionsObj, scriptKey, scriptValue);
86 
87     auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*newTarget), 8);
88     ecmaRuntimeCallInfo->SetFunction(newTarget.GetTaggedValue());
89     ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
90     ecmaRuntimeCallInfo->SetCallArg(0, localeString.GetTaggedValue());
91     ecmaRuntimeCallInfo->SetCallArg(1, optionsObj.GetTaggedValue());
92 
93     [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
94     JSTaggedValue result = BuiltinsLocale::LocaleConstructor(ecmaRuntimeCallInfo);
95     TestHelper::TearDownFrame(thread, prev);
96 
97     EXPECT_TRUE(result.IsJSLocale());
98 }
99 
JSLocaleCreateWithOptionTest(JSThread * thread)100 static JSTaggedValue JSLocaleCreateWithOptionTest(JSThread *thread)
101 {
102     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
103     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
104     JSHandle<JSFunction> newTarget(env->GetLocaleFunction());
105     JSHandle<JSTaggedValue> objFun = env->GetObjectFunction();
106 
107     JSHandle<JSTaggedValue> languageKey = thread->GlobalConstants()->GetHandledLanguageString();
108     JSHandle<JSTaggedValue> regionKey = thread->GlobalConstants()->GetHandledRegionString();
109     JSHandle<JSTaggedValue> scriptKey = thread->GlobalConstants()->GetHandledScriptString();
110     JSHandle<JSTaggedValue> languageValue(factory->NewFromASCII("en"));
111     JSHandle<JSTaggedValue> regionValue(factory->NewFromASCII("US"));
112     JSHandle<JSTaggedValue> scriptValue(factory->NewFromASCII("Latn"));
113     JSHandle<JSTaggedValue> locale(factory->NewFromASCII("en-Latn-US"));
114     // set option(language, region, script)
115     JSHandle<JSObject> optionsObj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objFun), objFun);
116     JSObject::SetProperty(thread, optionsObj, languageKey, languageValue);
117     JSObject::SetProperty(thread, optionsObj, regionKey, regionValue);
118     JSObject::SetProperty(thread, optionsObj, scriptKey, scriptValue);
119 
120     JSHandle<JSTaggedValue> calendarValue(factory->NewFromASCII("chinese"));
121     JSHandle<JSTaggedValue> calendarKey = thread->GlobalConstants()->GetHandledCalendarString();
122     JSObject::SetProperty(thread, optionsObj, calendarKey, calendarValue); // test chinese calendar
123 
124     JSHandle<JSTaggedValue> hourCycleKey = thread->GlobalConstants()->GetHandledHourCycleString();
125     JSHandle<JSTaggedValue> hourCycleValue(factory->NewFromASCII("h24"));
126     JSObject::SetProperty(thread, optionsObj, hourCycleKey, hourCycleValue); // test h24
127 
128     JSHandle<JSTaggedValue> numericKey = thread->GlobalConstants()->GetHandledNumericString();
129     JSHandle<JSTaggedValue> numericValue(factory->NewFromASCII("true"));
130     JSObject::SetProperty(thread, optionsObj, numericKey, numericValue); // test true
131 
132     JSHandle<JSTaggedValue> numberingSystemKey = thread->GlobalConstants()->GetHandledNumberingSystemString();
133     JSHandle<JSTaggedValue> numberingSystemValue(factory->NewFromASCII("mong"));
134     JSObject::SetProperty(thread, optionsObj, numberingSystemKey, numberingSystemValue); // test mong
135 
136     JSHandle<JSTaggedValue> collationKey = thread->GlobalConstants()->GetHandledCollationString();
137     JSHandle<JSTaggedValue> collationValue(factory->NewFromASCII("compat"));
138     JSObject::SetProperty(thread, optionsObj, collationKey, collationValue); // test compat
139 
140     auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*newTarget), 8);
141     ecmaRuntimeCallInfo->SetFunction(newTarget.GetTaggedValue());
142     ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
143     ecmaRuntimeCallInfo->SetCallArg(0, locale.GetTaggedValue());
144     ecmaRuntimeCallInfo->SetCallArg(1, optionsObj.GetTaggedValue());
145 
146     [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
147     JSTaggedValue result = BuiltinsLocale::LocaleConstructor(ecmaRuntimeCallInfo);
148     TestHelper::TearDownFrame(thread, prev);
149     return result;
150 }
151 
HWTEST_F_L0(BuiltinsLocaleTest,ToString)152 HWTEST_F_L0(BuiltinsLocaleTest, ToString)
153 {
154     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
155     JSHandle<JSLocale> jsLocale = JSHandle<JSLocale>(thread, JSLocaleCreateWithOptionTest(thread));
156 
157     auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
158     ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
159     ecmaRuntimeCallInfo->SetThis(jsLocale.GetTaggedValue());
160 
161     [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
162     JSTaggedValue result = BuiltinsLocale::ToString(ecmaRuntimeCallInfo);
163     TestHelper::TearDownFrame(thread, prev);
164 
165     EXPECT_TRUE(result.IsString());
166     JSHandle<EcmaString> handleEcmaStr(thread, result);
167     JSHandle<EcmaString> resultStr(
168         factory->NewFromASCII("en-Latn-US-u-ca-chinese-co-compat-hc-h24-kn-nu-mong"));
169     EXPECT_EQ(EcmaStringAccessor::Compare(instance, handleEcmaStr, resultStr), 0);
170 }
171 
HWTEST_F_L0(BuiltinsLocaleTest,GetBaseName)172 HWTEST_F_L0(BuiltinsLocaleTest, GetBaseName)
173 {
174     JSHandle<JSLocale> jsLocale = JSHandle<JSLocale>(thread, JSLocaleCreateWithOptionTest(thread));
175 
176     auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
177     ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
178     ecmaRuntimeCallInfo->SetThis(jsLocale.GetTaggedValue());
179 
180     [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
181     JSTaggedValue result = BuiltinsLocale::GetBaseName(ecmaRuntimeCallInfo);
182     TestHelper::TearDownFrame(thread, prev);
183 
184     EXPECT_TRUE(result.IsString());
185     JSHandle<EcmaString> handleEcmaStr(thread, result);
186     EXPECT_STREQ("en-Latn-US", EcmaStringAccessor(handleEcmaStr).ToCString().c_str());
187 }
188 
HWTEST_F_L0(BuiltinsLocaleTest,GetHourCycle)189 HWTEST_F_L0(BuiltinsLocaleTest, GetHourCycle)
190 {
191     JSHandle<JSLocale> jsLocale = JSHandle<JSLocale>(thread, JSLocaleCreateWithOptionTest(thread));
192 
193     auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
194     ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
195     ecmaRuntimeCallInfo->SetThis(jsLocale.GetTaggedValue());
196 
197     [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
198     JSTaggedValue result = BuiltinsLocale::GetHourCycle(ecmaRuntimeCallInfo);
199     TestHelper::TearDownFrame(thread, prev);
200 
201     EXPECT_TRUE(result.IsString());
202     JSHandle<EcmaString> handleEcmaStr(thread, result);
203     EXPECT_STREQ("h24", EcmaStringAccessor(handleEcmaStr).ToCString().c_str());
204 }
205 
HWTEST_F_L0(BuiltinsLocaleTest,GetCalendar)206 HWTEST_F_L0(BuiltinsLocaleTest, GetCalendar)
207 {
208     JSHandle<JSLocale> jsLocale = JSHandle<JSLocale>(thread, JSLocaleCreateWithOptionTest(thread));
209 
210     auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
211     ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
212     ecmaRuntimeCallInfo->SetThis(jsLocale.GetTaggedValue());
213 
214     [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
215     JSTaggedValue result = BuiltinsLocale::GetCalendar(ecmaRuntimeCallInfo);
216     TestHelper::TearDownFrame(thread, prev);
217 
218     EXPECT_TRUE(result.IsString());
219     JSHandle<EcmaString> handleEcmaStr(thread, result);
220     EXPECT_STREQ("chinese", EcmaStringAccessor(handleEcmaStr).ToCString().c_str());
221 }
222 
HWTEST_F_L0(BuiltinsLocaleTest,GetCaseFirst)223 HWTEST_F_L0(BuiltinsLocaleTest, GetCaseFirst)
224 {
225     JSHandle<JSLocale> jsLocale = JSHandle<JSLocale>(thread, JSLocaleCreateWithOptionTest(thread));
226 
227     auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
228     ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
229     ecmaRuntimeCallInfo->SetThis(jsLocale.GetTaggedValue());
230 
231     [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
232     JSTaggedValue result = BuiltinsLocale::GetCaseFirst(ecmaRuntimeCallInfo);
233     TestHelper::TearDownFrame(thread, prev);
234 
235     EXPECT_TRUE(result.IsString());
236     JSHandle<EcmaString> handleEcmaStr(thread, result);
237     EXPECT_STREQ("undefined", EcmaStringAccessor(handleEcmaStr).ToCString().c_str());
238 }
239 
HWTEST_F_L0(BuiltinsLocaleTest,GetCollation)240 HWTEST_F_L0(BuiltinsLocaleTest, GetCollation)
241 {
242     JSHandle<JSLocale> jsLocale = JSHandle<JSLocale>(thread, JSLocaleCreateWithOptionTest(thread));
243 
244     auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
245     ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
246     ecmaRuntimeCallInfo->SetThis(jsLocale.GetTaggedValue());
247 
248     [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
249     JSTaggedValue result = BuiltinsLocale::GetCollation(ecmaRuntimeCallInfo);
250     TestHelper::TearDownFrame(thread, prev);
251 
252     EXPECT_TRUE(result.IsString());
253     JSHandle<EcmaString> handleEcmaStr(thread, result);
254     EXPECT_STREQ("compat", EcmaStringAccessor(handleEcmaStr).ToCString().c_str());
255 }
256 
HWTEST_F_L0(BuiltinsLocaleTest,GetNumeric)257 HWTEST_F_L0(BuiltinsLocaleTest, GetNumeric)
258 {
259     JSHandle<JSLocale> jsLocale = JSHandle<JSLocale>(thread, JSLocaleCreateWithOptionTest(thread));
260 
261     auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
262     ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
263     ecmaRuntimeCallInfo->SetThis(jsLocale.GetTaggedValue());
264 
265     [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
266     JSTaggedValue result = BuiltinsLocale::GetNumeric(ecmaRuntimeCallInfo);
267     TestHelper::TearDownFrame(thread, prev);
268 
269     EXPECT_EQ(result.GetRawData(), JSTaggedValue::True().GetRawData());
270 }
271 
HWTEST_F_L0(BuiltinsLocaleTest,GetNumberingSystem)272 HWTEST_F_L0(BuiltinsLocaleTest, GetNumberingSystem)
273 {
274     JSHandle<JSLocale> jsLocale = JSHandle<JSLocale>(thread, JSLocaleCreateWithOptionTest(thread));
275 
276     auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
277     ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
278     ecmaRuntimeCallInfo->SetThis(jsLocale.GetTaggedValue());
279 
280     [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
281     JSTaggedValue result = BuiltinsLocale::GetNumberingSystem(ecmaRuntimeCallInfo);
282     TestHelper::TearDownFrame(thread, prev);
283 
284     EXPECT_TRUE(result.IsString());
285     JSHandle<EcmaString> handleEcmaStr(thread, result);
286     EXPECT_STREQ("mong", EcmaStringAccessor(handleEcmaStr).ToCString().c_str());
287 }
288 
HWTEST_F_L0(BuiltinsLocaleTest,GetLanguage)289 HWTEST_F_L0(BuiltinsLocaleTest, GetLanguage)
290 {
291     JSHandle<JSLocale> jsLocale = JSHandle<JSLocale>(thread, JSLocaleCreateWithOptionTest(thread));
292 
293     auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
294     ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
295     ecmaRuntimeCallInfo->SetThis(jsLocale.GetTaggedValue());
296 
297     [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
298     JSTaggedValue result = BuiltinsLocale::GetLanguage(ecmaRuntimeCallInfo);
299     TestHelper::TearDownFrame(thread, prev);
300 
301     EXPECT_TRUE(result.IsString());
302     JSHandle<EcmaString> handleEcmaStr(thread, result);
303     EXPECT_STREQ("en", EcmaStringAccessor(handleEcmaStr).ToCString().c_str());
304 }
305 
HWTEST_F_L0(BuiltinsLocaleTest,GetScript)306 HWTEST_F_L0(BuiltinsLocaleTest, GetScript)
307 {
308     JSHandle<JSLocale> jsLocale = JSHandle<JSLocale>(thread, JSLocaleCreateWithOptionTest(thread));
309 
310     auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
311     ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
312     ecmaRuntimeCallInfo->SetThis(jsLocale.GetTaggedValue());
313 
314     [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
315     JSTaggedValue result = BuiltinsLocale::GetScript(ecmaRuntimeCallInfo);
316     TestHelper::TearDownFrame(thread, prev);
317 
318     EXPECT_TRUE(result.IsString());
319     JSHandle<EcmaString> handleEcmaStr(thread, result);
320     EXPECT_STREQ("Latn", EcmaStringAccessor(handleEcmaStr).ToCString().c_str());
321 }
322 
HWTEST_F_L0(BuiltinsLocaleTest,GetRegion)323 HWTEST_F_L0(BuiltinsLocaleTest, GetRegion)
324 {
325     JSHandle<JSLocale> jsLocale = JSHandle<JSLocale>(thread, JSLocaleCreateWithOptionTest(thread));
326 
327     auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
328     ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
329     ecmaRuntimeCallInfo->SetThis(jsLocale.GetTaggedValue());
330 
331     [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
332     JSTaggedValue result = BuiltinsLocale::GetRegion(ecmaRuntimeCallInfo);
333     TestHelper::TearDownFrame(thread, prev);
334 
335     EXPECT_TRUE(result.IsString());
336     JSHandle<EcmaString> handleEcmaStr(thread, result);
337     EXPECT_STREQ("US", EcmaStringAccessor(handleEcmaStr).ToCString().c_str());
338 }
339 
JSLocaleCreateWithOptionsTagsTest(JSThread * thread,JSHandle<JSTaggedValue> & value)340 static JSTaggedValue JSLocaleCreateWithOptionsTagsTest(JSThread *thread, JSHandle<JSTaggedValue> &value)
341 {
342     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
343     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
344     JSHandle<JSFunction> newTarget(env->GetLocaleFunction());
345     JSHandle<JSTaggedValue> objFun = env->GetObjectFunction();
346 
347     JSHandle<JSTaggedValue> languageKey = thread->GlobalConstants()->GetHandledLanguageString();
348     JSHandle<JSTaggedValue> languageValue = JSHandle<JSTaggedValue>(value);
349     JSHandle<EcmaString> locale = factory->NewFromASCII("zh");
350     // set option(language)
351     JSHandle<JSObject> optionsObj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objFun), objFun);
352     JSObject::SetProperty(thread, optionsObj, languageKey, languageValue);
353 
354     auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*newTarget), 8);
355     ecmaRuntimeCallInfo->SetFunction(newTarget.GetTaggedValue());
356     ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
357     ecmaRuntimeCallInfo->SetCallArg(0, locale.GetTaggedValue());
358     ecmaRuntimeCallInfo->SetCallArg(1, optionsObj.GetTaggedValue());
359 
360     [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
361     JSTaggedValue result = BuiltinsLocale::LocaleConstructor(ecmaRuntimeCallInfo);
362     EXPECT_TRUE(result.IsJSLocale());
363     TestHelper::TearDownFrame(thread, prev);
364     return result;
365 }
366 
HWTEST_F_L0(BuiltinsLocaleTest,Maximize_001)367 HWTEST_F_L0(BuiltinsLocaleTest, Maximize_001)
368 {
369     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
370     // set language,then call Maximize function get language,script and region
371     JSHandle<JSTaggedValue> languageValue(factory->NewFromASCII("zh"));
372     JSHandle<JSLocale> jsLocale = JSHandle<JSLocale>(thread, JSLocaleCreateWithOptionsTagsTest(thread, languageValue));
373 
374     auto ecmaRuntimeCallInfo1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
375     ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined());
376     ecmaRuntimeCallInfo1->SetThis(jsLocale.GetTaggedValue());
377     // test "zh" to "zh-Hans-CN"
378     [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1);
379     JSTaggedValue resultObj = BuiltinsLocale::Maximize(ecmaRuntimeCallInfo1);
380     TestHelper::TearDownFrame(thread, prev);
381     JSHandle<JSLocale> resultLocale(thread, resultObj);
382 
383     auto ecmaRuntimeCallInfo2 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
384     ecmaRuntimeCallInfo2->SetFunction(JSTaggedValue::Undefined());
385     ecmaRuntimeCallInfo2->SetThis(resultLocale.GetTaggedValue());
386 
387     prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo2);
388     JSTaggedValue result = BuiltinsLocale::GetBaseName(ecmaRuntimeCallInfo2);
389     TestHelper::TearDownFrame(thread, prev);
390 
391     EXPECT_TRUE(result.IsString());
392     JSHandle<EcmaString> handleEcmaStr(thread, result);
393     EXPECT_STREQ("zh-Hans-CN", EcmaStringAccessor(handleEcmaStr).ToCString().c_str());
394 }
395 
HWTEST_F_L0(BuiltinsLocaleTest,Maximize_002)396 HWTEST_F_L0(BuiltinsLocaleTest, Maximize_002)
397 {
398     // set language,script,region and numeric and it's maximized
399     JSHandle<JSLocale> jsLocale = JSHandle<JSLocale>(thread, JSLocaleCreateWithOptionTest(thread));
400 
401     auto ecmaRuntimeCallInfo1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
402     ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined());
403     ecmaRuntimeCallInfo1->SetThis(jsLocale.GetTaggedValue());
404     // test "en-Latn-US" to "en-Latn-US"
405     [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1);
406     JSTaggedValue resultObj = BuiltinsLocale::Maximize(ecmaRuntimeCallInfo1);
407     TestHelper::TearDownFrame(thread, prev);
408     JSHandle<JSLocale> resultLocale(thread, resultObj);
409 
410     auto ecmaRuntimeCallInfo2 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
411     ecmaRuntimeCallInfo2->SetFunction(JSTaggedValue::Undefined());
412     ecmaRuntimeCallInfo2->SetThis(resultLocale.GetTaggedValue());
413 
414     prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo2);
415     JSTaggedValue result = BuiltinsLocale::GetBaseName(ecmaRuntimeCallInfo2);
416     TestHelper::TearDownFrame(thread, prev);
417 
418     EXPECT_TRUE(result.IsString());
419     JSHandle<EcmaString> handleEcmaStr(thread, result);
420     EXPECT_STREQ("en-Latn-US", EcmaStringAccessor(handleEcmaStr).ToCString().c_str());
421 }
422 
HWTEST_F_L0(BuiltinsLocaleTest,Minimize_001)423 HWTEST_F_L0(BuiltinsLocaleTest, Minimize_001)
424 {
425     // set language, script, region, and it's maximized,then call Minimize function get language
426     JSHandle<JSLocale> jsLocale = JSHandle<JSLocale>(thread, JSLocaleCreateWithOptionTest(thread));
427 
428     auto ecmaRuntimeCallInfo1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
429     ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined());
430     ecmaRuntimeCallInfo1->SetThis(jsLocale.GetTaggedValue());
431     // test "en-Latn-US" to "en"
432     [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1);
433     JSTaggedValue resultObj = BuiltinsLocale::Minimize(ecmaRuntimeCallInfo1);
434     TestHelper::TearDownFrame(thread, prev);
435     JSHandle<JSLocale> resultLocale(thread, resultObj);
436 
437     auto ecmaRuntimeCallInfo2 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
438     ecmaRuntimeCallInfo2->SetFunction(JSTaggedValue::Undefined());
439     ecmaRuntimeCallInfo2->SetThis(resultLocale.GetTaggedValue());
440 
441     prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo2);
442     JSTaggedValue result = BuiltinsLocale::GetBaseName(ecmaRuntimeCallInfo2);
443     TestHelper::TearDownFrame(thread, prev);
444 
445     EXPECT_TRUE(result.IsString());
446     JSHandle<EcmaString> handleEcmaStr(thread, result);
447     EXPECT_STREQ("en", EcmaStringAccessor(handleEcmaStr).ToCString().c_str());
448 }
449 
HWTEST_F_L0(BuiltinsLocaleTest,Minimize_002)450 HWTEST_F_L0(BuiltinsLocaleTest, Minimize_002)
451 {
452     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
453     // set language and it's minimized
454     JSHandle<JSTaggedValue> languageValue(factory->NewFromASCII("zh"));
455     JSHandle<JSLocale> jsLocale = JSHandle<JSLocale>(thread, JSLocaleCreateWithOptionsTagsTest(thread, languageValue));
456 
457     auto ecmaRuntimeCallInfo1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
458     ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined());
459     ecmaRuntimeCallInfo1->SetThis(jsLocale.GetTaggedValue());
460     // test "zh" to "zh"
461     [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1);
462     JSTaggedValue resultObj = BuiltinsLocale::Minimize(ecmaRuntimeCallInfo1);
463     TestHelper::TearDownFrame(thread, prev);
464     JSHandle<JSLocale> resultLocale(thread, resultObj);
465 
466     auto ecmaRuntimeCallInfo2 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
467     ecmaRuntimeCallInfo2->SetFunction(JSTaggedValue::Undefined());
468     ecmaRuntimeCallInfo2->SetThis(resultLocale.GetTaggedValue());
469 
470     prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo2);
471     JSTaggedValue result = BuiltinsLocale::ToString(ecmaRuntimeCallInfo2);
472     TestHelper::TearDownFrame(thread, prev);
473 
474     EXPECT_TRUE(result.IsString());
475     JSHandle<EcmaString> handleEcmaStr(thread, result);
476     EXPECT_STREQ("zh", EcmaStringAccessor(handleEcmaStr).ToCString().c_str());
477 }
478 
479 // test NormalizeKeywordValue(kf is not setted "yes")
HWTEST_F_L0(BuiltinsLocaleTest,NormalizeKeywordValue)480 HWTEST_F_L0(BuiltinsLocaleTest, NormalizeKeywordValue)
481 {
482     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
483     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
484     JSHandle<JSFunction> newTarget(env->GetLocaleFunction());
485     JSHandle<JSTaggedValue> objFun = env->GetObjectFunction();
486 
487     JSHandle<JSTaggedValue> caseFirstKey = thread->GlobalConstants()->GetHandledCaseFirstString();
488     JSHandle<JSTaggedValue> caseFirstValue(factory->NewFromASCII("false"));
489     JSHandle<EcmaString> locale = factory->NewFromASCII("en-US");
490 
491     JSHandle<JSObject> optionsObj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objFun), objFun);
492     JSObject::SetProperty(thread, optionsObj, caseFirstKey, caseFirstValue);
493 
494     auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*newTarget), 8);
495     ecmaRuntimeCallInfo->SetFunction(newTarget.GetTaggedValue());
496     ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
497     ecmaRuntimeCallInfo->SetCallArg(0, locale.GetTaggedValue());
498     ecmaRuntimeCallInfo->SetCallArg(1, optionsObj.GetTaggedValue());
499 
500     [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
501     JSTaggedValue result = BuiltinsLocale::LocaleConstructor(ecmaRuntimeCallInfo);
502     TestHelper::TearDownFrame(thread, prev);
503 
504     JSHandle<JSLocale> jsInitLocale(thread, result);
505     JSHandle<EcmaString> keyWords = JSLocale::NormalizeKeywordValue(thread, jsInitLocale, "kf");
506     EXPECT_STREQ("false", EcmaStringAccessor(keyWords).ToCString().c_str());
507 }
508 }  // namespace panda::test
509