/* * 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/js_relative_time_format.h" #include "ecmascript/intl/locale_helper.h" #include "ecmascript/base/number_helper.h" #include "ecmascript/global_env.h" #include "ecmascript/napi/jsnapi_helper.h" #include "ecmascript/tests/test_helper.h" using namespace panda::ecmascript; using namespace panda::ecmascript::base; namespace panda::test { class JSRelativeTimeFormatTest : public BaseTestWithScope { }; HWTEST_F_L0(JSRelativeTimeFormatTest, InitializeRelativeTimeFormat) { ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); JSHandle env = thread->GetEcmaVM()->GetGlobalEnv(); JSHandle ctor = env->GetRelativeTimeFormatFunction(); JSHandle relativeTimeFormat = JSHandle::Cast(factory->NewJSObjectByConstructor(JSHandle(ctor), ctor)); EXPECT_TRUE(*relativeTimeFormat != nullptr); JSHandle locales(factory->NewFromASCII("en")); JSHandle undefinedOptions(thread, JSTaggedValue::Undefined()); JSRelativeTimeFormat::InitializeRelativeTimeFormat(thread, relativeTimeFormat, locales, undefinedOptions); // Initialize attribute comparison JSHandle numberingSystemStr(thread, relativeTimeFormat->GetNumberingSystem(thread).GetTaggedObject()); EXPECT_STREQ("latn", EcmaStringAccessor(numberingSystemStr).ToCString(thread).c_str()); JSHandle localeStr(thread, relativeTimeFormat->GetLocale(thread).GetTaggedObject()); EXPECT_STREQ("en", EcmaStringAccessor(localeStr).ToCString(thread).c_str()); EXPECT_EQ(NumericOption::ALWAYS, relativeTimeFormat->GetNumeric()); EXPECT_EQ(RelativeStyleOption::LONG, relativeTimeFormat->GetStyle()); } HWTEST_F_L0(JSRelativeTimeFormatTest, GetIcuRTFFormatter) { double value = base::NAN_VALUE; ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); JSHandle env = thread->GetEcmaVM()->GetGlobalEnv(); JSHandle ctor = env->GetRelativeTimeFormatFunction(); JSHandle relativeTimeFormat = JSHandle::Cast(factory->NewJSObjectByConstructor(JSHandle(ctor), ctor)); // Create Icu RelativeDateTimeFormatter icu::Locale icuLocale("en", "US"); UErrorCode status = U_ZERO_ERROR; icu::NumberFormat *icuNumberFormat = icu::NumberFormat::createInstance(icuLocale, UNUM_DECIMAL, status); icu::RelativeDateTimeFormatter rtfFormatter(icuLocale, icuNumberFormat, UDAT_STYLE_LONG, UDISPCTX_CAPITALIZATION_NONE, status); icu::UnicodeString result1 = rtfFormatter.formatNumericToValue(value, UDAT_REL_UNIT_YEAR, status).toString(status); JSHandle stringValue1 = intl::LocaleHelper::UStringToString(thread, result1); // Set Icu RelativeDateTimeFormatter to Icu Field factory->NewJSIntlIcuData(relativeTimeFormat, rtfFormatter, JSRelativeTimeFormat::FreeIcuRTFFormatter); // Get Icu Field icu::RelativeDateTimeFormatter *resultRelativeDateTimeFormatter = relativeTimeFormat->GetIcuRTFFormatter(thread); icu::UnicodeString result2 = resultRelativeDateTimeFormatter->formatNumericToValue(value, UDAT_REL_UNIT_YEAR, status).toString(status); JSHandle stringValue2 = intl::LocaleHelper::UStringToString(thread, result2); EXPECT_EQ(EcmaStringAccessor::StringsAreEqual(thread, *stringValue1, *stringValue2), true); } HWTEST_F_L0(JSRelativeTimeFormatTest, UnwrapRelativeTimeFormat) { ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); JSHandle env = thread->GetEcmaVM()->GetGlobalEnv(); EcmaVM *vm = thread->GetEcmaVM(); JSHandle relativeTimeFormatFunc = env->GetRelativeTimeFormatFunction(); JSHandle relativeTimeFormat( factory->NewJSObjectByConstructor(JSHandle(relativeTimeFormatFunc), relativeTimeFormatFunc)); Local relativeTimeFormatLocal = JSNApiHelper::ToLocal(relativeTimeFormatFunc); JSHandle disPlayNamesFunc = env->GetDisplayNamesFunction(); Local disPlayNamesLocal = JSNApiHelper::ToLocal(disPlayNamesFunc); // displaynames Inherit relativeTimeFormat disPlayNamesLocal->Inherit(vm, relativeTimeFormatLocal); JSHandle disPlayNamesHandle = JSNApiHelper::ToJSHandle(disPlayNamesLocal); JSHandle disPlayNamesObj( factory->NewJSObjectByConstructor(JSHandle::Cast(disPlayNamesHandle), disPlayNamesHandle)); // object has no Instance JSHandle unwrapNumberFormat1 = JSRelativeTimeFormat::UnwrapRelativeTimeFormat(thread, relativeTimeFormat); EXPECT_TRUE(JSTaggedValue::SameValue(thread, relativeTimeFormat, unwrapNumberFormat1)); // object has Instance JSHandle unwrapNumberFormat2 = JSRelativeTimeFormat::UnwrapRelativeTimeFormat(thread, disPlayNamesObj); EXPECT_TRUE(unwrapNumberFormat2->IsUndefined()); } } // namespace panda::test