/* * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "ecmascript/builtins/builtins_displaynames.h" #include "ecmascript/global_env.h" #include "ecmascript/js_displaynames.h" #include "ecmascript/tests/test_helper.h" using namespace panda::ecmascript; using namespace panda::ecmascript::builtins; namespace panda::test { class BuiltinsDisplayNamesTest : public BaseTestWithScope { }; static JSTaggedValue JSDisplayNamesCreateWithOptionTest(JSThread *thread, JSHandle &locale, JSHandle &typeValue) { ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); JSHandle env = thread->GetEcmaVM()->GetGlobalEnv(); JSHandle newTarget(env->GetDisplayNamesFunction()); JSHandle objFun = env->GetObjectFunction(); JSHandle optionsObj = factory->NewJSObjectByConstructor(JSHandle(objFun), objFun); JSHandle typeKey = thread->GlobalConstants()->GetHandledTypeString(); JSObject::SetProperty(thread, optionsObj, typeKey, typeValue); JSHandle localesString = locale; std::vector args{localesString.GetTaggedValue(), optionsObj.GetTaggedValue()}; auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, newTarget, args, 8); [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); JSTaggedValue result = BuiltinsDisplayNames::DisplayNamesConstructor(ecmaRuntimeCallInfo); TestHelper::TearDownFrame(thread, prev); EXPECT_TRUE(result.IsJSDisplayNames()); return result; } // new DisplayNames(locales, options) HWTEST_F_L0(BuiltinsDisplayNamesTest, DisplayNamesConstructor) { ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); JSHandle env = thread->GetEcmaVM()->GetGlobalEnv(); JSHandle newTarget(env->GetDisplayNamesFunction()); JSHandle objFun = env->GetObjectFunction(); JSHandle typeKey = thread->GlobalConstants()->GetHandledTypeString(); JSHandle styleKey = thread->GlobalConstants()->GetHandledStyleString(); JSHandle fallbackKey = thread->GlobalConstants()->GetHandledFallbackString(); JSHandle localeString(factory->NewFromASCII("en")); JSHandle typeValue(factory->NewFromASCII("language")); JSHandle styleValue(factory->NewFromASCII("narrow")); JSHandle fallbackValue(factory->NewFromASCII("code")); JSHandle optionsObj = factory->NewJSObjectByConstructor(JSHandle(objFun), objFun); JSObject::SetProperty(thread, optionsObj, typeKey, typeValue); JSObject::SetProperty(thread, optionsObj, styleKey, styleValue); JSObject::SetProperty(thread, optionsObj, fallbackKey, fallbackValue); std::vector args{localeString.GetTaggedValue(), optionsObj.GetTaggedValue()}; auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, newTarget, args, 8); [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); JSTaggedValue result = BuiltinsDisplayNames::DisplayNamesConstructor(ecmaRuntimeCallInfo); TestHelper::TearDownFrame(thread, prev); EXPECT_TRUE(result.IsJSDisplayNames()); } // Of(fr, type(language)) HWTEST_F_L0(BuiltinsDisplayNamesTest, Of_001) { ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); JSHandle locale(factory->NewFromASCII("en")); JSHandle typeValue(factory->NewFromASCII("language")); JSHandle jsDisplayNames = JSHandle(thread, JSDisplayNamesCreateWithOptionTest(thread, locale, typeValue)); JSHandle stringValue(factory->NewFromASCII("fr")); auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); ecmaRuntimeCallInfo->SetThis(jsDisplayNames.GetTaggedValue()); ecmaRuntimeCallInfo->SetCallArg(0, stringValue.GetTaggedValue()); [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); JSTaggedValue result = BuiltinsDisplayNames::Of(ecmaRuntimeCallInfo); TestHelper::TearDownFrame(thread, prev); EXPECT_TRUE(result.IsString()); JSHandle handleEcmaStr(thread, result); EXPECT_STREQ("French", EcmaStringAccessor(handleEcmaStr).ToCString().c_str()); } // Of(419, type(region)) HWTEST_F_L0(BuiltinsDisplayNamesTest, Of_002) { ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); JSHandle locale(factory->NewFromASCII("en")); JSHandle typeValue(factory->NewFromASCII("region")); JSHandle jsDisplayNames = JSHandle(thread, JSDisplayNamesCreateWithOptionTest(thread, locale, typeValue)); JSHandle stringValue(factory->NewFromASCII("419")); auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); ecmaRuntimeCallInfo->SetThis(jsDisplayNames.GetTaggedValue()); ecmaRuntimeCallInfo->SetCallArg(0, stringValue.GetTaggedValue()); [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); JSTaggedValue result = BuiltinsDisplayNames::Of(ecmaRuntimeCallInfo); TestHelper::TearDownFrame(thread, prev); EXPECT_TRUE(result.IsString()); JSHandle handleEcmaStr(thread, result); EXPECT_STREQ("Latin America", EcmaStringAccessor(handleEcmaStr).ToCString().c_str()); } // Of(EUR, type(currency)) HWTEST_F_L0(BuiltinsDisplayNamesTest, Of_003) { ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); JSHandle locale(factory->NewFromASCII("en")); JSHandle typeValue(factory->NewFromASCII("currency")); JSHandle jsDisplayNames = JSHandle(thread, JSDisplayNamesCreateWithOptionTest(thread, locale, typeValue)); JSHandle stringValue(factory->NewFromASCII("EUR")); auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); ecmaRuntimeCallInfo->SetThis(jsDisplayNames.GetTaggedValue()); ecmaRuntimeCallInfo->SetCallArg(0, stringValue.GetTaggedValue()); [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); JSTaggedValue result = BuiltinsDisplayNames::Of(ecmaRuntimeCallInfo); TestHelper::TearDownFrame(thread, prev); EXPECT_TRUE(result.IsString()); JSHandle handleEcmaStr(thread, result); EXPECT_STREQ("Euro", EcmaStringAccessor(handleEcmaStr).ToCString().c_str()); } // Of(Code Cover) HWTEST_F_L0(BuiltinsDisplayNamesTest, Of_004) { // IsNotJSDisplayNames auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue::Undefined()); [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); JSTaggedValue result = BuiltinsDisplayNames::Of(ecmaRuntimeCallInfo); TestHelper::TearDownFrame(thread, prev); EXPECT_TRUE(thread->HasPendingException()); EXPECT_EQ(result, JSTaggedValue::Exception()); thread->ClearException(); } // SupportedLocalesOf("best fit") HWTEST_F_L0(BuiltinsDisplayNamesTest, SupportedLocalesOf_001) { ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); JSHandle locale(factory->NewFromASCII("id-u-co-pinyin-de-ID")); auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8); ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); ecmaRuntimeCallInfo->SetCallArg(0, locale.GetTaggedValue()); // set the tag is default value ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue::Undefined()); [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); JSTaggedValue resultArr = BuiltinsDisplayNames::SupportedLocalesOf(ecmaRuntimeCallInfo); TestHelper::TearDownFrame(thread, prev); JSHandle resultHandle(thread, resultArr); JSHandle elements(thread, resultHandle->GetElements()); EXPECT_EQ(elements->GetLength(), 1U); JSHandle handleEcmaStr(thread, elements->Get(0)); EXPECT_STREQ("id-u-co-pinyin-de-id", EcmaStringAccessor(handleEcmaStr).ToCString().c_str()); } // SupportedLocalesOf("look up") HWTEST_F_L0(BuiltinsDisplayNamesTest, SupportedLocalesOf_002) { ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); JSHandle env = thread->GetEcmaVM()->GetGlobalEnv(); JSHandle objFun = env->GetObjectFunction(); JSHandle localeMatcherKey = thread->GlobalConstants()->GetHandledLocaleMatcherString(); JSHandle localeMatcherValue(factory->NewFromASCII("lookup")); JSHandle optionsObj = factory->NewJSObjectByConstructor(JSHandle(objFun), objFun); JSObject::SetProperty(thread, optionsObj, localeMatcherKey, localeMatcherValue); JSHandle locale(factory->NewFromASCII("de-DE-u-co-phonebk")); auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8); ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); ecmaRuntimeCallInfo->SetCallArg(0, locale.GetTaggedValue()); ecmaRuntimeCallInfo->SetCallArg(1, optionsObj.GetTaggedValue()); [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); JSTaggedValue resultArr = BuiltinsDisplayNames::SupportedLocalesOf(ecmaRuntimeCallInfo); TestHelper::TearDownFrame(thread, prev); JSHandle resultHandle(thread, resultArr); JSHandle elements(thread, resultHandle->GetElements()); EXPECT_EQ(elements->GetLength(), 1U); JSHandle handleEcmaStr(thread, elements->Get(0)); EXPECT_STREQ("de-DE-u-co-phonebk", EcmaStringAccessor(handleEcmaStr).ToCString().c_str()); } HWTEST_F_L0(BuiltinsDisplayNamesTest, ResolvedOptions) { ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); auto globalConst = thread->GlobalConstants(); JSHandle locale(factory->NewFromASCII("de-DE")); JSHandle typeValue(factory->NewFromASCII("currency")); JSHandle jsDisplayNames = JSHandle(thread, JSDisplayNamesCreateWithOptionTest( thread, locale, typeValue)); std::vector args{}; auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, args, 4, jsDisplayNames.GetTaggedValue()); [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); JSTaggedValue result = BuiltinsDisplayNames::ResolvedOptions(ecmaRuntimeCallInfo); TestHelper::TearDownFrame(thread, prev); JSHandle resultObj = JSHandle(thread, JSTaggedValue(static_cast(result.GetRawData()))); // judge whether the properties of the object are the same as those of jsdatetimeformat tag JSHandle localeKey = globalConst->GetHandledLocaleString(); JSHandle localeValue(factory->NewFromASCII("de-DE")); EXPECT_EQ(JSTaggedValue::SameValue( JSObject::GetProperty(thread, resultObj, localeKey).GetValue(), localeValue), true); JSHandle typeKey = globalConst->GetHandledTypeString(); EXPECT_EQ(JSTaggedValue::SameValue( JSObject::GetProperty(thread, resultObj, typeKey).GetValue(), typeValue), true); } }