14514f5e3Sopenharmony_ci/* 24514f5e3Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License. 54514f5e3Sopenharmony_ci * You may obtain a copy of the License at 64514f5e3Sopenharmony_ci * 74514f5e3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 84514f5e3Sopenharmony_ci * 94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and 134514f5e3Sopenharmony_ci * limitations under the License. 144514f5e3Sopenharmony_ci */ 154514f5e3Sopenharmony_ci 164514f5e3Sopenharmony_ci#include "ecmascript/builtins/builtins_locale.h" 174514f5e3Sopenharmony_ci 184514f5e3Sopenharmony_ci#include "ecmascript/global_env.h" 194514f5e3Sopenharmony_ci#include "ecmascript/js_locale.h" 204514f5e3Sopenharmony_ci#include "ecmascript/tests/test_helper.h" 214514f5e3Sopenharmony_ci 224514f5e3Sopenharmony_ciusing namespace panda::ecmascript; 234514f5e3Sopenharmony_ciusing namespace panda::ecmascript::builtins; 244514f5e3Sopenharmony_ci 254514f5e3Sopenharmony_cinamespace panda::test { 264514f5e3Sopenharmony_ciclass BuiltinsLocaleTest : public BaseTestWithScope<true> { 274514f5e3Sopenharmony_ci}; 284514f5e3Sopenharmony_ci 294514f5e3Sopenharmony_ci// new locale( [ options ] ) 304514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsLocaleTest, LocaleConstructor) 314514f5e3Sopenharmony_ci{ 324514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 334514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv(); 344514f5e3Sopenharmony_ci JSHandle<JSFunction> newTarget(env->GetLocaleFunction()); 354514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> objFun = env->GetObjectFunction(); 364514f5e3Sopenharmony_ci 374514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> languageKey = thread->GlobalConstants()->GetHandledLanguageString(); 384514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> regionKey = thread->GlobalConstants()->GetHandledRegionString(); 394514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> numericKey = thread->GlobalConstants()->GetHandledNumericString(); 404514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> scriptKey = thread->GlobalConstants()->GetHandledScriptString(); 414514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> languageValue(factory->NewFromASCII("cn")); 424514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> regionValue(factory->NewFromASCII("CN")); 434514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> scriptValue(factory->NewFromASCII("Chin")); 444514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> numericValue(thread, JSTaggedValue::True()); 454514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> localeString(factory->NewFromASCII("zh-Hans-CN")); 464514f5e3Sopenharmony_ci 474514f5e3Sopenharmony_ci JSHandle<JSObject> optionsObj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objFun), objFun); 484514f5e3Sopenharmony_ci JSObject::SetProperty(thread, optionsObj, numericKey, numericValue); 494514f5e3Sopenharmony_ci JSObject::SetProperty(thread, optionsObj, regionKey, regionValue); 504514f5e3Sopenharmony_ci JSObject::SetProperty(thread, optionsObj, languageKey, languageValue); 514514f5e3Sopenharmony_ci JSObject::SetProperty(thread, optionsObj, scriptKey, scriptValue); 524514f5e3Sopenharmony_ci 534514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*newTarget), 8); 544514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(newTarget.GetTaggedValue()); 554514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); 564514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(0, localeString.GetTaggedValue()); 574514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(1, optionsObj.GetTaggedValue()); 584514f5e3Sopenharmony_ci 594514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 604514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsLocale::LocaleConstructor(ecmaRuntimeCallInfo); 614514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 624514f5e3Sopenharmony_ci 634514f5e3Sopenharmony_ci EXPECT_TRUE(result.IsJSLocale()); 644514f5e3Sopenharmony_ci} 654514f5e3Sopenharmony_ci 664514f5e3Sopenharmony_cistatic JSTaggedValue JSLocaleCreateWithOptionTest(JSThread *thread) 674514f5e3Sopenharmony_ci{ 684514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 694514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv(); 704514f5e3Sopenharmony_ci JSHandle<JSFunction> newTarget(env->GetLocaleFunction()); 714514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> objFun = env->GetObjectFunction(); 724514f5e3Sopenharmony_ci 734514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> languageKey = thread->GlobalConstants()->GetHandledLanguageString(); 744514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> regionKey = thread->GlobalConstants()->GetHandledRegionString(); 754514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> scriptKey = thread->GlobalConstants()->GetHandledScriptString(); 764514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> languageValue(factory->NewFromASCII("en")); 774514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> regionValue(factory->NewFromASCII("US")); 784514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> scriptValue(factory->NewFromASCII("Latn")); 794514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> locale(factory->NewFromASCII("en-Latn-US")); 804514f5e3Sopenharmony_ci // set option(language, region, script) 814514f5e3Sopenharmony_ci JSHandle<JSObject> optionsObj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objFun), objFun); 824514f5e3Sopenharmony_ci JSObject::SetProperty(thread, optionsObj, languageKey, languageValue); 834514f5e3Sopenharmony_ci JSObject::SetProperty(thread, optionsObj, regionKey, regionValue); 844514f5e3Sopenharmony_ci JSObject::SetProperty(thread, optionsObj, scriptKey, scriptValue); 854514f5e3Sopenharmony_ci 864514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> calendarValue(factory->NewFromASCII("chinese")); 874514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> calendarKey = thread->GlobalConstants()->GetHandledCalendarString(); 884514f5e3Sopenharmony_ci JSObject::SetProperty(thread, optionsObj, calendarKey, calendarValue); // test chinese calendar 894514f5e3Sopenharmony_ci 904514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> hourCycleKey = thread->GlobalConstants()->GetHandledHourCycleString(); 914514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> hourCycleValue(factory->NewFromASCII("h24")); 924514f5e3Sopenharmony_ci JSObject::SetProperty(thread, optionsObj, hourCycleKey, hourCycleValue); // test h24 934514f5e3Sopenharmony_ci 944514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> numericKey = thread->GlobalConstants()->GetHandledNumericString(); 954514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> numericValue(factory->NewFromASCII("true")); 964514f5e3Sopenharmony_ci JSObject::SetProperty(thread, optionsObj, numericKey, numericValue); // test true 974514f5e3Sopenharmony_ci 984514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> numberingSystemKey = thread->GlobalConstants()->GetHandledNumberingSystemString(); 994514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> numberingSystemValue(factory->NewFromASCII("mong")); 1004514f5e3Sopenharmony_ci JSObject::SetProperty(thread, optionsObj, numberingSystemKey, numberingSystemValue); // test mong 1014514f5e3Sopenharmony_ci 1024514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> collationKey = thread->GlobalConstants()->GetHandledCollationString(); 1034514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> collationValue(factory->NewFromASCII("compat")); 1044514f5e3Sopenharmony_ci JSObject::SetProperty(thread, optionsObj, collationKey, collationValue); // test compat 1054514f5e3Sopenharmony_ci 1064514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*newTarget), 8); 1074514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(newTarget.GetTaggedValue()); 1084514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); 1094514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(0, locale.GetTaggedValue()); 1104514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(1, optionsObj.GetTaggedValue()); 1114514f5e3Sopenharmony_ci 1124514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 1134514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsLocale::LocaleConstructor(ecmaRuntimeCallInfo); 1144514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 1154514f5e3Sopenharmony_ci return result; 1164514f5e3Sopenharmony_ci} 1174514f5e3Sopenharmony_ci 1184514f5e3Sopenharmony_cienum class AlgorithmType { 1194514f5e3Sopenharmony_ci TO_STRING, 1204514f5e3Sopenharmony_ci GET_BASE_NAME, 1214514f5e3Sopenharmony_ci GET_HOUR_CYCLE, 1224514f5e3Sopenharmony_ci GET_CALENDAR, 1234514f5e3Sopenharmony_ci GET_CASE_FIRST, 1244514f5e3Sopenharmony_ci GET_COLLATION, 1254514f5e3Sopenharmony_ci GET_NUMERIC, 1264514f5e3Sopenharmony_ci GET_NUMBERING_SYSTEM, 1274514f5e3Sopenharmony_ci GET_LANGUAGE, 1284514f5e3Sopenharmony_ci GET_SCRIPT, 1294514f5e3Sopenharmony_ci GET_REGION, 1304514f5e3Sopenharmony_ci MAXI_MIZE, 1314514f5e3Sopenharmony_ci MINI_MIZE, 1324514f5e3Sopenharmony_ci}; 1334514f5e3Sopenharmony_ci 1344514f5e3Sopenharmony_ciJSTaggedValue AlgorithmOther(EcmaRuntimeCallInfo *ecmaRuntimeCallInfo, AlgorithmType type) 1354514f5e3Sopenharmony_ci{ 1364514f5e3Sopenharmony_ci switch (type) { 1374514f5e3Sopenharmony_ci case AlgorithmType::TO_STRING: 1384514f5e3Sopenharmony_ci return BuiltinsLocale::ToString(ecmaRuntimeCallInfo); 1394514f5e3Sopenharmony_ci case AlgorithmType::MAXI_MIZE: 1404514f5e3Sopenharmony_ci return BuiltinsLocale::Maximize(ecmaRuntimeCallInfo); 1414514f5e3Sopenharmony_ci case AlgorithmType::MINI_MIZE: 1424514f5e3Sopenharmony_ci return BuiltinsLocale::Minimize(ecmaRuntimeCallInfo); 1434514f5e3Sopenharmony_ci default: 1444514f5e3Sopenharmony_ci return JSTaggedValue(); 1454514f5e3Sopenharmony_ci } 1464514f5e3Sopenharmony_ci} 1474514f5e3Sopenharmony_ci 1484514f5e3Sopenharmony_ciJSTaggedValue LocaleAlgorithm(JSThread *thread, JSTaggedValue thisArg, std::vector<JSTaggedValue>& args, 1494514f5e3Sopenharmony_ci int32_t argLen, AlgorithmType type) 1504514f5e3Sopenharmony_ci{ 1514514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), argLen); 1524514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); 1534514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(thisArg); 1544514f5e3Sopenharmony_ci for (size_t i = 0; i < args.size(); i++) { 1554514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(i, args[i]); 1564514f5e3Sopenharmony_ci } 1574514f5e3Sopenharmony_ci auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 1584514f5e3Sopenharmony_ci JSTaggedValue result; 1594514f5e3Sopenharmony_ci switch (type) { 1604514f5e3Sopenharmony_ci case AlgorithmType::GET_BASE_NAME: 1614514f5e3Sopenharmony_ci result = BuiltinsLocale::GetBaseName(ecmaRuntimeCallInfo); 1624514f5e3Sopenharmony_ci break; 1634514f5e3Sopenharmony_ci case AlgorithmType::GET_HOUR_CYCLE: 1644514f5e3Sopenharmony_ci result = BuiltinsLocale::GetHourCycle(ecmaRuntimeCallInfo); 1654514f5e3Sopenharmony_ci break; 1664514f5e3Sopenharmony_ci case AlgorithmType::GET_CALENDAR: 1674514f5e3Sopenharmony_ci result = BuiltinsLocale::GetCalendar(ecmaRuntimeCallInfo); 1684514f5e3Sopenharmony_ci break; 1694514f5e3Sopenharmony_ci case AlgorithmType::GET_CASE_FIRST: 1704514f5e3Sopenharmony_ci result = BuiltinsLocale::GetCaseFirst(ecmaRuntimeCallInfo); 1714514f5e3Sopenharmony_ci break; 1724514f5e3Sopenharmony_ci case AlgorithmType::GET_COLLATION: 1734514f5e3Sopenharmony_ci result = BuiltinsLocale::GetCollation(ecmaRuntimeCallInfo); 1744514f5e3Sopenharmony_ci break; 1754514f5e3Sopenharmony_ci case AlgorithmType::GET_NUMERIC: 1764514f5e3Sopenharmony_ci result = BuiltinsLocale::GetNumeric(ecmaRuntimeCallInfo); 1774514f5e3Sopenharmony_ci break; 1784514f5e3Sopenharmony_ci case AlgorithmType::GET_NUMBERING_SYSTEM: 1794514f5e3Sopenharmony_ci result = BuiltinsLocale::GetNumberingSystem(ecmaRuntimeCallInfo); 1804514f5e3Sopenharmony_ci break; 1814514f5e3Sopenharmony_ci case AlgorithmType::GET_LANGUAGE: 1824514f5e3Sopenharmony_ci result = BuiltinsLocale::GetLanguage(ecmaRuntimeCallInfo); 1834514f5e3Sopenharmony_ci break; 1844514f5e3Sopenharmony_ci case AlgorithmType::GET_SCRIPT: 1854514f5e3Sopenharmony_ci result = BuiltinsLocale::GetScript(ecmaRuntimeCallInfo); 1864514f5e3Sopenharmony_ci break; 1874514f5e3Sopenharmony_ci case AlgorithmType::GET_REGION: 1884514f5e3Sopenharmony_ci result = BuiltinsLocale::GetRegion(ecmaRuntimeCallInfo); 1894514f5e3Sopenharmony_ci break; 1904514f5e3Sopenharmony_ci default: 1914514f5e3Sopenharmony_ci result = AlgorithmOther(ecmaRuntimeCallInfo, type); 1924514f5e3Sopenharmony_ci break; 1934514f5e3Sopenharmony_ci } 1944514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 1954514f5e3Sopenharmony_ci return result; 1964514f5e3Sopenharmony_ci} 1974514f5e3Sopenharmony_ci 1984514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsLocaleTest, ToString) 1994514f5e3Sopenharmony_ci{ 2004514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 2014514f5e3Sopenharmony_ci JSHandle<JSLocale> jsLocale = JSHandle<JSLocale>(thread, JSLocaleCreateWithOptionTest(thread)); 2024514f5e3Sopenharmony_ci std::vector<JSTaggedValue> args{}; 2034514f5e3Sopenharmony_ci auto result = LocaleAlgorithm(thread, jsLocale.GetTaggedValue(), args, 4, AlgorithmType::TO_STRING); // 4 : arg len 2044514f5e3Sopenharmony_ci 2054514f5e3Sopenharmony_ci EXPECT_TRUE(result.IsString()); 2064514f5e3Sopenharmony_ci JSHandle<EcmaString> handleEcmaStr(thread, result); 2074514f5e3Sopenharmony_ci JSHandle<EcmaString> resultStr( 2084514f5e3Sopenharmony_ci factory->NewFromASCII("en-Latn-US-u-ca-chinese-co-compat-hc-h24-kn-nu-mong")); 2094514f5e3Sopenharmony_ci EXPECT_EQ(EcmaStringAccessor::Compare(instance, handleEcmaStr, resultStr), 0); 2104514f5e3Sopenharmony_ci} 2114514f5e3Sopenharmony_ci 2124514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsLocaleTest, GetBaseName) 2134514f5e3Sopenharmony_ci{ 2144514f5e3Sopenharmony_ci JSHandle<JSLocale> jsLocale = JSHandle<JSLocale>(thread, JSLocaleCreateWithOptionTest(thread)); 2154514f5e3Sopenharmony_ci 2164514f5e3Sopenharmony_ci std::vector<JSTaggedValue> args{}; 2174514f5e3Sopenharmony_ci auto result = 2184514f5e3Sopenharmony_ci LocaleAlgorithm(thread, jsLocale.GetTaggedValue(), args, 4, AlgorithmType::GET_BASE_NAME); // 4 : arg len 2194514f5e3Sopenharmony_ci 2204514f5e3Sopenharmony_ci EXPECT_TRUE(result.IsString()); 2214514f5e3Sopenharmony_ci JSHandle<EcmaString> handleEcmaStr(thread, result); 2224514f5e3Sopenharmony_ci EXPECT_STREQ("en-Latn-US", EcmaStringAccessor(handleEcmaStr).ToCString().c_str()); 2234514f5e3Sopenharmony_ci} 2244514f5e3Sopenharmony_ci 2254514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsLocaleTest, GetHourCycle) 2264514f5e3Sopenharmony_ci{ 2274514f5e3Sopenharmony_ci JSHandle<JSLocale> jsLocale = JSHandle<JSLocale>(thread, JSLocaleCreateWithOptionTest(thread)); 2284514f5e3Sopenharmony_ci 2294514f5e3Sopenharmony_ci std::vector<JSTaggedValue> args{}; 2304514f5e3Sopenharmony_ci auto result = 2314514f5e3Sopenharmony_ci LocaleAlgorithm(thread, jsLocale.GetTaggedValue(), args, 4, AlgorithmType::GET_HOUR_CYCLE); // 4 : arg len 2324514f5e3Sopenharmony_ci 2334514f5e3Sopenharmony_ci EXPECT_TRUE(result.IsString()); 2344514f5e3Sopenharmony_ci JSHandle<EcmaString> handleEcmaStr(thread, result); 2354514f5e3Sopenharmony_ci EXPECT_STREQ("h24", EcmaStringAccessor(handleEcmaStr).ToCString().c_str()); 2364514f5e3Sopenharmony_ci} 2374514f5e3Sopenharmony_ci 2384514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsLocaleTest, GetCalendar) 2394514f5e3Sopenharmony_ci{ 2404514f5e3Sopenharmony_ci JSHandle<JSLocale> jsLocale = JSHandle<JSLocale>(thread, JSLocaleCreateWithOptionTest(thread)); 2414514f5e3Sopenharmony_ci std::vector<JSTaggedValue> args{}; 2424514f5e3Sopenharmony_ci auto result = 2434514f5e3Sopenharmony_ci LocaleAlgorithm(thread, jsLocale.GetTaggedValue(), args, 4, AlgorithmType::GET_CALENDAR); // 4 : arg len 2444514f5e3Sopenharmony_ci 2454514f5e3Sopenharmony_ci EXPECT_TRUE(result.IsString()); 2464514f5e3Sopenharmony_ci JSHandle<EcmaString> handleEcmaStr(thread, result); 2474514f5e3Sopenharmony_ci EXPECT_STREQ("chinese", EcmaStringAccessor(handleEcmaStr).ToCString().c_str()); 2484514f5e3Sopenharmony_ci} 2494514f5e3Sopenharmony_ci 2504514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsLocaleTest, GetCaseFirst) 2514514f5e3Sopenharmony_ci{ 2524514f5e3Sopenharmony_ci JSHandle<JSLocale> jsLocale = JSHandle<JSLocale>(thread, JSLocaleCreateWithOptionTest(thread)); 2534514f5e3Sopenharmony_ci 2544514f5e3Sopenharmony_ci std::vector<JSTaggedValue> args{}; 2554514f5e3Sopenharmony_ci auto result = 2564514f5e3Sopenharmony_ci LocaleAlgorithm(thread, jsLocale.GetTaggedValue(), args, 4, AlgorithmType::GET_CASE_FIRST); // 4 : arg len 2574514f5e3Sopenharmony_ci 2584514f5e3Sopenharmony_ci EXPECT_TRUE(result.IsUndefined()); 2594514f5e3Sopenharmony_ci} 2604514f5e3Sopenharmony_ci 2614514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsLocaleTest, GetCollation) 2624514f5e3Sopenharmony_ci{ 2634514f5e3Sopenharmony_ci JSHandle<JSLocale> jsLocale = JSHandle<JSLocale>(thread, JSLocaleCreateWithOptionTest(thread)); 2644514f5e3Sopenharmony_ci 2654514f5e3Sopenharmony_ci std::vector<JSTaggedValue> args{}; 2664514f5e3Sopenharmony_ci auto result = 2674514f5e3Sopenharmony_ci LocaleAlgorithm(thread, jsLocale.GetTaggedValue(), args, 4, AlgorithmType::GET_COLLATION); // 4 : arg len 2684514f5e3Sopenharmony_ci 2694514f5e3Sopenharmony_ci EXPECT_TRUE(result.IsString()); 2704514f5e3Sopenharmony_ci JSHandle<EcmaString> handleEcmaStr(thread, result); 2714514f5e3Sopenharmony_ci EXPECT_STREQ("compat", EcmaStringAccessor(handleEcmaStr).ToCString().c_str()); 2724514f5e3Sopenharmony_ci} 2734514f5e3Sopenharmony_ci 2744514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsLocaleTest, GetNumeric) 2754514f5e3Sopenharmony_ci{ 2764514f5e3Sopenharmony_ci JSHandle<JSLocale> jsLocale = JSHandle<JSLocale>(thread, JSLocaleCreateWithOptionTest(thread)); 2774514f5e3Sopenharmony_ci 2784514f5e3Sopenharmony_ci std::vector<JSTaggedValue> args{}; 2794514f5e3Sopenharmony_ci auto result = 2804514f5e3Sopenharmony_ci LocaleAlgorithm(thread, jsLocale.GetTaggedValue(), args, 4, AlgorithmType::GET_NUMERIC); // 4 : arg len 2814514f5e3Sopenharmony_ci 2824514f5e3Sopenharmony_ci EXPECT_EQ(result.GetRawData(), JSTaggedValue::True().GetRawData()); 2834514f5e3Sopenharmony_ci} 2844514f5e3Sopenharmony_ci 2854514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsLocaleTest, GetNumberingSystem) 2864514f5e3Sopenharmony_ci{ 2874514f5e3Sopenharmony_ci JSHandle<JSLocale> jsLocale = JSHandle<JSLocale>(thread, JSLocaleCreateWithOptionTest(thread)); 2884514f5e3Sopenharmony_ci 2894514f5e3Sopenharmony_ci std::vector<JSTaggedValue> args{}; 2904514f5e3Sopenharmony_ci auto result = LocaleAlgorithm(thread, jsLocale.GetTaggedValue(), args, 4, 2914514f5e3Sopenharmony_ci AlgorithmType::GET_NUMBERING_SYSTEM); // 4 : arg len 2924514f5e3Sopenharmony_ci 2934514f5e3Sopenharmony_ci EXPECT_TRUE(result.IsString()); 2944514f5e3Sopenharmony_ci JSHandle<EcmaString> handleEcmaStr(thread, result); 2954514f5e3Sopenharmony_ci EXPECT_STREQ("mong", EcmaStringAccessor(handleEcmaStr).ToCString().c_str()); 2964514f5e3Sopenharmony_ci} 2974514f5e3Sopenharmony_ci 2984514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsLocaleTest, GetLanguage) 2994514f5e3Sopenharmony_ci{ 3004514f5e3Sopenharmony_ci JSHandle<JSLocale> jsLocale = JSHandle<JSLocale>(thread, JSLocaleCreateWithOptionTest(thread)); 3014514f5e3Sopenharmony_ci 3024514f5e3Sopenharmony_ci std::vector<JSTaggedValue> args{}; 3034514f5e3Sopenharmony_ci auto result = 3044514f5e3Sopenharmony_ci LocaleAlgorithm(thread, jsLocale.GetTaggedValue(), args, 4, AlgorithmType::GET_LANGUAGE); // 4 : arg len 3054514f5e3Sopenharmony_ci 3064514f5e3Sopenharmony_ci EXPECT_TRUE(result.IsString()); 3074514f5e3Sopenharmony_ci JSHandle<EcmaString> handleEcmaStr(thread, result); 3084514f5e3Sopenharmony_ci EXPECT_STREQ("en", EcmaStringAccessor(handleEcmaStr).ToCString().c_str()); 3094514f5e3Sopenharmony_ci} 3104514f5e3Sopenharmony_ci 3114514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsLocaleTest, GetScript) 3124514f5e3Sopenharmony_ci{ 3134514f5e3Sopenharmony_ci JSHandle<JSLocale> jsLocale = JSHandle<JSLocale>(thread, JSLocaleCreateWithOptionTest(thread)); 3144514f5e3Sopenharmony_ci 3154514f5e3Sopenharmony_ci std::vector<JSTaggedValue> args{}; 3164514f5e3Sopenharmony_ci auto result = LocaleAlgorithm(thread, jsLocale.GetTaggedValue(), args, 4, AlgorithmType::GET_SCRIPT); // 4 : arg len 3174514f5e3Sopenharmony_ci 3184514f5e3Sopenharmony_ci EXPECT_TRUE(result.IsString()); 3194514f5e3Sopenharmony_ci JSHandle<EcmaString> handleEcmaStr(thread, result); 3204514f5e3Sopenharmony_ci EXPECT_STREQ("Latn", EcmaStringAccessor(handleEcmaStr).ToCString().c_str()); 3214514f5e3Sopenharmony_ci} 3224514f5e3Sopenharmony_ci 3234514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsLocaleTest, GetRegion) 3244514f5e3Sopenharmony_ci{ 3254514f5e3Sopenharmony_ci JSHandle<JSLocale> jsLocale = JSHandle<JSLocale>(thread, JSLocaleCreateWithOptionTest(thread)); 3264514f5e3Sopenharmony_ci 3274514f5e3Sopenharmony_ci std::vector<JSTaggedValue> args{}; 3284514f5e3Sopenharmony_ci auto result = LocaleAlgorithm(thread, jsLocale.GetTaggedValue(), args, 4, AlgorithmType::GET_REGION); // 4 : arg len 3294514f5e3Sopenharmony_ci 3304514f5e3Sopenharmony_ci EXPECT_TRUE(result.IsString()); 3314514f5e3Sopenharmony_ci JSHandle<EcmaString> handleEcmaStr(thread, result); 3324514f5e3Sopenharmony_ci EXPECT_STREQ("US", EcmaStringAccessor(handleEcmaStr).ToCString().c_str()); 3334514f5e3Sopenharmony_ci} 3344514f5e3Sopenharmony_ci 3354514f5e3Sopenharmony_cistatic JSTaggedValue JSLocaleCreateWithOptionsTagsTest(JSThread *thread, JSHandle<JSTaggedValue> &value) 3364514f5e3Sopenharmony_ci{ 3374514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 3384514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv(); 3394514f5e3Sopenharmony_ci JSHandle<JSFunction> newTarget(env->GetLocaleFunction()); 3404514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> objFun = env->GetObjectFunction(); 3414514f5e3Sopenharmony_ci 3424514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> languageKey = thread->GlobalConstants()->GetHandledLanguageString(); 3434514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> languageValue = JSHandle<JSTaggedValue>(value); 3444514f5e3Sopenharmony_ci JSHandle<EcmaString> locale = factory->NewFromASCII("zh"); 3454514f5e3Sopenharmony_ci // set option(language) 3464514f5e3Sopenharmony_ci JSHandle<JSObject> optionsObj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objFun), objFun); 3474514f5e3Sopenharmony_ci JSObject::SetProperty(thread, optionsObj, languageKey, languageValue); 3484514f5e3Sopenharmony_ci 3494514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*newTarget), 8); 3504514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(newTarget.GetTaggedValue()); 3514514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); 3524514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(0, locale.GetTaggedValue()); 3534514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(1, optionsObj.GetTaggedValue()); 3544514f5e3Sopenharmony_ci 3554514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 3564514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsLocale::LocaleConstructor(ecmaRuntimeCallInfo); 3574514f5e3Sopenharmony_ci EXPECT_TRUE(result.IsJSLocale()); 3584514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 3594514f5e3Sopenharmony_ci return result; 3604514f5e3Sopenharmony_ci} 3614514f5e3Sopenharmony_ci 3624514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsLocaleTest, Maximize_001) 3634514f5e3Sopenharmony_ci{ 3644514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 3654514f5e3Sopenharmony_ci // set language,then call Maximize function get language,script and region 3664514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> languageValue(factory->NewFromASCII("zh")); 3674514f5e3Sopenharmony_ci JSHandle<JSLocale> jsLocale = JSHandle<JSLocale>(thread, JSLocaleCreateWithOptionsTagsTest(thread, languageValue)); 3684514f5e3Sopenharmony_ci 3694514f5e3Sopenharmony_ci std::vector<JSTaggedValue> args{}; 3704514f5e3Sopenharmony_ci auto resultObj = 3714514f5e3Sopenharmony_ci LocaleAlgorithm(thread, jsLocale.GetTaggedValue(), args, 4, AlgorithmType::MAXI_MIZE); // 4 : arg len 3724514f5e3Sopenharmony_ci 3734514f5e3Sopenharmony_ci JSHandle<JSLocale> resultLocale(thread, resultObj); 3744514f5e3Sopenharmony_ci auto result = 3754514f5e3Sopenharmony_ci LocaleAlgorithm(thread, resultLocale.GetTaggedValue(), args, 4, AlgorithmType::GET_BASE_NAME); // 4 : arg len 3764514f5e3Sopenharmony_ci 3774514f5e3Sopenharmony_ci EXPECT_TRUE(result.IsString()); 3784514f5e3Sopenharmony_ci JSHandle<EcmaString> handleEcmaStr(thread, result); 3794514f5e3Sopenharmony_ci EXPECT_STREQ("zh-Hans-CN", EcmaStringAccessor(handleEcmaStr).ToCString().c_str()); 3804514f5e3Sopenharmony_ci} 3814514f5e3Sopenharmony_ci 3824514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsLocaleTest, Maximize_002) 3834514f5e3Sopenharmony_ci{ 3844514f5e3Sopenharmony_ci // set language,script,region and numeric and it's maximized 3854514f5e3Sopenharmony_ci JSHandle<JSLocale> jsLocale = JSHandle<JSLocale>(thread, JSLocaleCreateWithOptionTest(thread)); 3864514f5e3Sopenharmony_ci 3874514f5e3Sopenharmony_ci std::vector<JSTaggedValue> args{}; 3884514f5e3Sopenharmony_ci auto resultObj = 3894514f5e3Sopenharmony_ci LocaleAlgorithm(thread, jsLocale.GetTaggedValue(), args, 4, AlgorithmType::MAXI_MIZE); // 4 : arg len 3904514f5e3Sopenharmony_ci JSHandle<JSLocale> resultLocale(thread, resultObj); 3914514f5e3Sopenharmony_ci auto result = 3924514f5e3Sopenharmony_ci LocaleAlgorithm(thread, resultLocale.GetTaggedValue(), args, 4, AlgorithmType::GET_BASE_NAME); // 4 : arg len 3934514f5e3Sopenharmony_ci 3944514f5e3Sopenharmony_ci EXPECT_TRUE(result.IsString()); 3954514f5e3Sopenharmony_ci JSHandle<EcmaString> handleEcmaStr(thread, result); 3964514f5e3Sopenharmony_ci EXPECT_STREQ("en-Latn-US", EcmaStringAccessor(handleEcmaStr).ToCString().c_str()); 3974514f5e3Sopenharmony_ci} 3984514f5e3Sopenharmony_ci 3994514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsLocaleTest, Minimize_001) 4004514f5e3Sopenharmony_ci{ 4014514f5e3Sopenharmony_ci // set language, script, region, and it's maximized,then call Minimize function get language 4024514f5e3Sopenharmony_ci JSHandle<JSLocale> jsLocale = JSHandle<JSLocale>(thread, JSLocaleCreateWithOptionTest(thread)); 4034514f5e3Sopenharmony_ci 4044514f5e3Sopenharmony_ci std::vector<JSTaggedValue> args{}; 4054514f5e3Sopenharmony_ci auto resultObj = 4064514f5e3Sopenharmony_ci LocaleAlgorithm(thread, jsLocale.GetTaggedValue(), args, 4, AlgorithmType::MINI_MIZE); // 4 : arg len 4074514f5e3Sopenharmony_ci 4084514f5e3Sopenharmony_ci JSHandle<JSLocale> resultLocale(thread, resultObj); 4094514f5e3Sopenharmony_ci auto result = 4104514f5e3Sopenharmony_ci LocaleAlgorithm(thread, resultLocale.GetTaggedValue(), args, 4, AlgorithmType::GET_BASE_NAME); // 4 : arg len 4114514f5e3Sopenharmony_ci 4124514f5e3Sopenharmony_ci EXPECT_TRUE(result.IsString()); 4134514f5e3Sopenharmony_ci JSHandle<EcmaString> handleEcmaStr(thread, result); 4144514f5e3Sopenharmony_ci EXPECT_STREQ("en", EcmaStringAccessor(handleEcmaStr).ToCString().c_str()); 4154514f5e3Sopenharmony_ci} 4164514f5e3Sopenharmony_ci 4174514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsLocaleTest, Minimize_002) 4184514f5e3Sopenharmony_ci{ 4194514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 4204514f5e3Sopenharmony_ci // set language and it's minimized 4214514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> languageValue(factory->NewFromASCII("zh")); 4224514f5e3Sopenharmony_ci JSHandle<JSLocale> jsLocale = JSHandle<JSLocale>(thread, JSLocaleCreateWithOptionsTagsTest(thread, languageValue)); 4234514f5e3Sopenharmony_ci 4244514f5e3Sopenharmony_ci std::vector<JSTaggedValue> args{}; 4254514f5e3Sopenharmony_ci auto resultObj = 4264514f5e3Sopenharmony_ci LocaleAlgorithm(thread, jsLocale.GetTaggedValue(), args, 4, AlgorithmType::MINI_MIZE); // 4 : arg len 4274514f5e3Sopenharmony_ci 4284514f5e3Sopenharmony_ci JSHandle<JSLocale> resultLocale(thread, resultObj); 4294514f5e3Sopenharmony_ci auto result = 4304514f5e3Sopenharmony_ci LocaleAlgorithm(thread, resultLocale.GetTaggedValue(), args, 4, AlgorithmType::TO_STRING); // 4 : arg len 4314514f5e3Sopenharmony_ci 4324514f5e3Sopenharmony_ci EXPECT_TRUE(result.IsString()); 4334514f5e3Sopenharmony_ci JSHandle<EcmaString> handleEcmaStr(thread, result); 4344514f5e3Sopenharmony_ci EXPECT_STREQ("zh", EcmaStringAccessor(handleEcmaStr).ToCString().c_str()); 4354514f5e3Sopenharmony_ci} 4364514f5e3Sopenharmony_ci 4374514f5e3Sopenharmony_ci// test NormalizeKeywordValue(kf is not setted "yes") 4384514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsLocaleTest, NormalizeKeywordValue) 4394514f5e3Sopenharmony_ci{ 4404514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 4414514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv(); 4424514f5e3Sopenharmony_ci JSHandle<JSFunction> newTarget(env->GetLocaleFunction()); 4434514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> objFun = env->GetObjectFunction(); 4444514f5e3Sopenharmony_ci 4454514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> caseFirstKey = thread->GlobalConstants()->GetHandledCaseFirstString(); 4464514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> caseFirstValue(factory->NewFromASCII("false")); 4474514f5e3Sopenharmony_ci JSHandle<EcmaString> locale = factory->NewFromASCII("en-US"); 4484514f5e3Sopenharmony_ci 4494514f5e3Sopenharmony_ci JSHandle<JSObject> optionsObj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objFun), objFun); 4504514f5e3Sopenharmony_ci JSObject::SetProperty(thread, optionsObj, caseFirstKey, caseFirstValue); 4514514f5e3Sopenharmony_ci 4524514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*newTarget), 8); 4534514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(newTarget.GetTaggedValue()); 4544514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); 4554514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(0, locale.GetTaggedValue()); 4564514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(1, optionsObj.GetTaggedValue()); 4574514f5e3Sopenharmony_ci 4584514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 4594514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsLocale::LocaleConstructor(ecmaRuntimeCallInfo); 4604514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 4614514f5e3Sopenharmony_ci 4624514f5e3Sopenharmony_ci JSHandle<JSLocale> jsInitLocale(thread, result); 4634514f5e3Sopenharmony_ci JSTaggedValue keyWords = JSLocale::NormalizeKeywordValue(thread, jsInitLocale, "kf"); 4644514f5e3Sopenharmony_ci EXPECT_STREQ("false", EcmaStringAccessor(keyWords).ToCString().c_str()); 4654514f5e3Sopenharmony_ci} 4664514f5e3Sopenharmony_ci} // namespace panda::test 467