14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2022 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/js_displaynames.h"
174514f5e3Sopenharmony_ci
184514f5e3Sopenharmony_ci#include "ecmascript/intl/locale_helper.h"
194514f5e3Sopenharmony_ci#include "ecmascript/tests/test_helper.h"
204514f5e3Sopenharmony_ci
214514f5e3Sopenharmony_ciusing namespace panda;
224514f5e3Sopenharmony_ciusing namespace panda::ecmascript;
234514f5e3Sopenharmony_ci
244514f5e3Sopenharmony_cinamespace panda::test {
254514f5e3Sopenharmony_ciclass JSDisplayNamesTest : public BaseTestWithScope<true> {
264514f5e3Sopenharmony_ci};
274514f5e3Sopenharmony_ci
284514f5e3Sopenharmony_ci/**
294514f5e3Sopenharmony_ci * @tc.name: GetIcuLocaleDisplayNames
304514f5e3Sopenharmony_ci * @tc.desc: Call "SetIcuLocaleDisplayNames" function Set IcuLocale DisplayNames,check whether the IcuLocale
314514f5e3Sopenharmony_ci *           DisplayNames through "GetIcuLocaleDisplayNames" function is within expectations then call "getLocale"
324514f5e3Sopenharmony_ci *           function display locale and check the locale is within expectations.
334514f5e3Sopenharmony_ci * @tc.type: FUNC
344514f5e3Sopenharmony_ci * @tc.require:
354514f5e3Sopenharmony_ci */
364514f5e3Sopenharmony_ciHWTEST_F_L0(JSDisplayNamesTest, GetIcuLocaleDisplayNames)
374514f5e3Sopenharmony_ci{
384514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
394514f5e3Sopenharmony_ci    JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
404514f5e3Sopenharmony_ci
414514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> ctor = env->GetDisplayNamesFunction();
424514f5e3Sopenharmony_ci    JSHandle<JSDisplayNames> displayNames =
434514f5e3Sopenharmony_ci        JSHandle<JSDisplayNames>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(ctor), ctor));
444514f5e3Sopenharmony_ci
454514f5e3Sopenharmony_ci    icu::Locale icuLocale("en");
464514f5e3Sopenharmony_ci    UDisplayContext display_context[] = {UDISPCTX_LENGTH_SHORT};
474514f5e3Sopenharmony_ci    icu::LocaleDisplayNames* iculocaledisplaynames =
484514f5e3Sopenharmony_ci        icu::LocaleDisplayNames::createInstance(icuLocale, display_context, 1);
494514f5e3Sopenharmony_ci    EXPECT_TRUE(iculocaledisplaynames != nullptr);
504514f5e3Sopenharmony_ci    JSDisplayNames::SetIcuLocaleDisplayNames(
514514f5e3Sopenharmony_ci        thread, displayNames, iculocaledisplaynames, JSDisplayNames::FreeIcuLocaleDisplayNames);
524514f5e3Sopenharmony_ci    icu::LocaleDisplayNames *resultIculocaledisplaynames = displayNames->GetIcuLocaleDisplayNames();
534514f5e3Sopenharmony_ci    EXPECT_TRUE(iculocaledisplaynames == resultIculocaledisplaynames);
544514f5e3Sopenharmony_ci    JSHandle<EcmaString> localeStr =
554514f5e3Sopenharmony_ci        intl::LocaleHelper::ToLanguageTag(thread, resultIculocaledisplaynames->getLocale());
564514f5e3Sopenharmony_ci    EXPECT_STREQ(EcmaStringAccessor(localeStr).ToCString().c_str(), "en");
574514f5e3Sopenharmony_ci}
584514f5e3Sopenharmony_ci
594514f5e3Sopenharmony_ci/**
604514f5e3Sopenharmony_ci * @tc.name: GetAvailableLocales
614514f5e3Sopenharmony_ci * @tc.desc: Call function "GetAvailableLocales" to obtain the available locale from the ICU library and
624514f5e3Sopenharmony_ci *           check whether the obtained locale is empty.
634514f5e3Sopenharmony_ci * @tc.type: FUNC
644514f5e3Sopenharmony_ci * @tc.require:
654514f5e3Sopenharmony_ci */
664514f5e3Sopenharmony_ciHWTEST_F_L0(JSDisplayNamesTest, GetAvailableLocales)
674514f5e3Sopenharmony_ci{
684514f5e3Sopenharmony_ci    JSHandle<TaggedArray> displayLocale = JSDisplayNames::GetAvailableLocales(thread);
694514f5e3Sopenharmony_ci    uint32_t localeLen = displayLocale->GetLength();
704514f5e3Sopenharmony_ci    EXPECT_NE(localeLen, 0U);
714514f5e3Sopenharmony_ci
724514f5e3Sopenharmony_ci    for (uint32_t i = 0; i < localeLen; i++) {
734514f5e3Sopenharmony_ci        EXPECT_FALSE(displayLocale->Get(i).IsHole());
744514f5e3Sopenharmony_ci    }
754514f5e3Sopenharmony_ci}
764514f5e3Sopenharmony_ci
774514f5e3Sopenharmony_civoid SetOptionProperties(JSThread *thread, JSHandle<JSObject> &optionsObj,
784514f5e3Sopenharmony_ci                         std::map<std::string, std::string> &displayOptions)
794514f5e3Sopenharmony_ci{
804514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
814514f5e3Sopenharmony_ci    auto globalConst = thread->GlobalConstants();
824514f5e3Sopenharmony_ci    // display options keys
834514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> styleKey = globalConst->GetHandledStyleString();
844514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> typeKey = globalConst->GetHandledTypeString();
854514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> fallBackKey = globalConst->GetHandledFallbackString();
864514f5e3Sopenharmony_ci    // display options value
874514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> styleValue(factory->NewFromASCII(displayOptions["style"].c_str()));
884514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> typeValue(factory->NewFromASCII(displayOptions["type"].c_str()));
894514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> fallBackValue(factory->NewFromASCII(displayOptions["fallback"].c_str()));
904514f5e3Sopenharmony_ci    JSObject::SetProperty(thread, optionsObj, styleKey, styleValue);
914514f5e3Sopenharmony_ci    JSObject::SetProperty(thread, optionsObj, typeKey, typeValue);
924514f5e3Sopenharmony_ci    JSObject::SetProperty(thread, optionsObj, fallBackKey, fallBackValue);
934514f5e3Sopenharmony_ci}
944514f5e3Sopenharmony_ci
954514f5e3Sopenharmony_ci/**
964514f5e3Sopenharmony_ci * @tc.name: InitializeDisplayNames
974514f5e3Sopenharmony_ci * @tc.desc: Call function "InitializeDisplayNames" to initialize the jsdisplaynames class object and check whether the
984514f5e3Sopenharmony_ci *           properties of the object is within expectations.
994514f5e3Sopenharmony_ci * @tc.type: FUNC
1004514f5e3Sopenharmony_ci * @tc.require:
1014514f5e3Sopenharmony_ci */
1024514f5e3Sopenharmony_ciHWTEST_F_L0(JSDisplayNamesTest, InitializeDisplayNames)
1034514f5e3Sopenharmony_ci{
1044514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1054514f5e3Sopenharmony_ci    JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
1064514f5e3Sopenharmony_ci
1074514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> ctor = env->GetDisplayNamesFunction();
1084514f5e3Sopenharmony_ci    JSHandle<JSDisplayNames> displayNames =
1094514f5e3Sopenharmony_ci        JSHandle<JSDisplayNames>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(ctor), ctor));
1104514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> objFun = env->GetObjectFunction();
1114514f5e3Sopenharmony_ci    JSHandle<JSObject> displayOptions = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objFun), objFun);
1124514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> localeStr(factory->NewFromASCII("en-US"));
1134514f5e3Sopenharmony_ci    // options is empty
1144514f5e3Sopenharmony_ci    JSDisplayNames::InitializeDisplayNames(thread, displayNames, localeStr, JSHandle<JSTaggedValue>(displayOptions));
1154514f5e3Sopenharmony_ci    // Default attribute value and type is undefiend throw a expection
1164514f5e3Sopenharmony_ci    EXPECT_EQ(displayNames->GetStyle(), StyOption::LONG);
1174514f5e3Sopenharmony_ci    EXPECT_EQ(displayNames->GetType(), TypednsOption::EXCEPTION);
1184514f5e3Sopenharmony_ci    EXPECT_EQ(displayNames->GetFallback(), FallbackOption::EXCEPTION);
1194514f5e3Sopenharmony_ci    EXPECT_TRUE(thread->HasPendingException());
1204514f5e3Sopenharmony_ci    thread->ClearException();
1214514f5e3Sopenharmony_ci    // options of the key and value
1224514f5e3Sopenharmony_ci    std::map<std::string, std::string> displayOptionsProperty {
1234514f5e3Sopenharmony_ci        { "style", "short" },
1244514f5e3Sopenharmony_ci        { "type", "script" },
1254514f5e3Sopenharmony_ci        { "fallback", "none" },
1264514f5e3Sopenharmony_ci    };
1274514f5e3Sopenharmony_ci    SetOptionProperties(thread, displayOptions, displayOptionsProperty);
1284514f5e3Sopenharmony_ci    // options is not empty
1294514f5e3Sopenharmony_ci    JSDisplayNames::InitializeDisplayNames(thread, displayNames, localeStr, JSHandle<JSTaggedValue>(displayOptions));
1304514f5e3Sopenharmony_ci    JSHandle<EcmaString> setlocale(thread, displayNames->GetLocale());
1314514f5e3Sopenharmony_ci    EXPECT_EQ(displayNames->GetStyle(), StyOption::SHORT);
1324514f5e3Sopenharmony_ci    EXPECT_EQ(displayNames->GetType(), TypednsOption::SCRIPT);
1334514f5e3Sopenharmony_ci    EXPECT_EQ(displayNames->GetFallback(), FallbackOption::NONE);
1344514f5e3Sopenharmony_ci    EXPECT_STREQ(EcmaStringAccessor(setlocale).ToCString().c_str(), "en-US");
1354514f5e3Sopenharmony_ci    EXPECT_TRUE(displayNames->GetIcuLocaleDisplayNames() != nullptr);
1364514f5e3Sopenharmony_ci}
1374514f5e3Sopenharmony_ci
1384514f5e3Sopenharmony_ci/**
1394514f5e3Sopenharmony_ci * @tc.name: CanonicalCodeForDisplayNames
1404514f5e3Sopenharmony_ci * @tc.desc: Display the language region and script of the locale according to the display configuration of
1414514f5e3Sopenharmony_ci *           different regions.
1424514f5e3Sopenharmony_ci * @tc.type: FUNC
1434514f5e3Sopenharmony_ci * @tc.require:
1444514f5e3Sopenharmony_ci */
1454514f5e3Sopenharmony_ciHWTEST_F_L0(JSDisplayNamesTest, CanonicalCodeForDisplayNames)
1464514f5e3Sopenharmony_ci{
1474514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1484514f5e3Sopenharmony_ci    JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
1494514f5e3Sopenharmony_ci
1504514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> ctor = env->GetDisplayNamesFunction();
1514514f5e3Sopenharmony_ci    JSHandle<JSDisplayNames> displayNames =
1524514f5e3Sopenharmony_ci        JSHandle<JSDisplayNames>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(ctor), ctor));
1534514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> objFun = env->GetObjectFunction();
1544514f5e3Sopenharmony_ci    JSHandle<JSObject> displayOptions = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objFun), objFun);
1554514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> locale(factory->NewFromASCII("zh-Hant"));
1564514f5e3Sopenharmony_ci    // test UDISPCTX_LENGTH_SHORT
1574514f5e3Sopenharmony_ci    std::map<std::string, std::string> displayOptionsProperty {
1584514f5e3Sopenharmony_ci        { "style", "narrow" },
1594514f5e3Sopenharmony_ci        { "type", "script" },
1604514f5e3Sopenharmony_ci        { "fallback", "none" },
1614514f5e3Sopenharmony_ci    };
1624514f5e3Sopenharmony_ci    SetOptionProperties(thread, displayOptions, displayOptionsProperty);
1634514f5e3Sopenharmony_ci    JSHandle<JSDisplayNames> initDisplayNames =
1644514f5e3Sopenharmony_ci        JSDisplayNames::InitializeDisplayNames(thread, displayNames, locale, JSHandle<JSTaggedValue>(displayOptions));
1654514f5e3Sopenharmony_ci    // CanonicalCode for script
1664514f5e3Sopenharmony_ci    JSHandle<EcmaString> code = factory->NewFromASCII("Kana");
1674514f5e3Sopenharmony_ci    JSHandle<EcmaString> resultDisplay =
1684514f5e3Sopenharmony_ci        JSDisplayNames::CanonicalCodeForDisplayNames(thread, initDisplayNames, initDisplayNames->GetType(), code);
1694514f5e3Sopenharmony_ci    EXPECT_STREQ(EcmaStringAccessor(resultDisplay).ToCString().c_str(), "片假名");
1704514f5e3Sopenharmony_ci    // CanonicalCode for languege
1714514f5e3Sopenharmony_ci    code = factory->NewFromASCII("fr");
1724514f5e3Sopenharmony_ci    initDisplayNames->SetType(TypednsOption::LANGUAGE);
1734514f5e3Sopenharmony_ci    resultDisplay =
1744514f5e3Sopenharmony_ci        JSDisplayNames::CanonicalCodeForDisplayNames(thread, initDisplayNames, initDisplayNames->GetType(), code);
1754514f5e3Sopenharmony_ci    EXPECT_STREQ(EcmaStringAccessor(resultDisplay).ToCString().c_str(), "法文");
1764514f5e3Sopenharmony_ci    // CanonicalCode for region
1774514f5e3Sopenharmony_ci    code = factory->NewFromASCII("US");
1784514f5e3Sopenharmony_ci    initDisplayNames->SetType(TypednsOption::REGION);
1794514f5e3Sopenharmony_ci    resultDisplay =
1804514f5e3Sopenharmony_ci        JSDisplayNames::CanonicalCodeForDisplayNames(thread, initDisplayNames, initDisplayNames->GetType(), code);
1814514f5e3Sopenharmony_ci    EXPECT_STREQ(EcmaStringAccessor(resultDisplay).ToCString().c_str(), "美國");
1824514f5e3Sopenharmony_ci}
1834514f5e3Sopenharmony_ci
1844514f5e3Sopenharmony_ci/**
1854514f5e3Sopenharmony_ci * @tc.name: ResolvedOptions
1864514f5e3Sopenharmony_ci * @tc.desc: Call function "InitializeDisplayNames" to initialize the jsdisplaynames class object and Copy the
1874514f5e3Sopenharmony_ci *           properties of jsdisplaynames class to a new object.
1884514f5e3Sopenharmony_ci * @tc.type: FUNC
1894514f5e3Sopenharmony_ci * @tc.require:
1904514f5e3Sopenharmony_ci */
1914514f5e3Sopenharmony_ciHWTEST_F_L0(JSDisplayNamesTest, ResolvedOptions)
1924514f5e3Sopenharmony_ci{
1934514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1944514f5e3Sopenharmony_ci    JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
1954514f5e3Sopenharmony_ci    auto globalConst = thread->GlobalConstants();
1964514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> localeKey = globalConst->GetHandledLocaleString();
1974514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> styleKey = globalConst->GetHandledStyleString();
1984514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> typeKey = globalConst->GetHandledTypeString();
1994514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> fallBackKey = globalConst->GetHandledFallbackString();
2004514f5e3Sopenharmony_ci
2014514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> ctor = env->GetDisplayNamesFunction();
2024514f5e3Sopenharmony_ci    JSHandle<JSDisplayNames> displayNames =
2034514f5e3Sopenharmony_ci        JSHandle<JSDisplayNames>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(ctor), ctor));
2044514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> objFun = env->GetObjectFunction();
2054514f5e3Sopenharmony_ci    JSHandle<JSObject> displayOptions = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objFun), objFun);
2064514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> locale(factory->NewFromASCII("zh-Hant"));
2074514f5e3Sopenharmony_ci
2084514f5e3Sopenharmony_ci    std::map<std::string, std::string> displayOptionsProperty {
2094514f5e3Sopenharmony_ci        { "style", "short" },
2104514f5e3Sopenharmony_ci        { "type", "region" },
2114514f5e3Sopenharmony_ci        { "fallback", "code" },
2124514f5e3Sopenharmony_ci    };
2134514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> styleValue(factory->NewFromASCII(displayOptionsProperty["style"].c_str()));
2144514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> typeValue(factory->NewFromASCII(displayOptionsProperty["type"].c_str()));
2154514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> fallBackValue(factory->NewFromASCII(displayOptionsProperty["fallback"].c_str()));
2164514f5e3Sopenharmony_ci    SetOptionProperties(thread, displayOptions, displayOptionsProperty);
2174514f5e3Sopenharmony_ci    JSHandle<JSDisplayNames> initDisplayNames =
2184514f5e3Sopenharmony_ci        JSDisplayNames::InitializeDisplayNames(thread, displayNames, locale, JSHandle<JSTaggedValue>(displayOptions));
2194514f5e3Sopenharmony_ci
2204514f5e3Sopenharmony_ci    JSDisplayNames::ResolvedOptions(thread, initDisplayNames, displayOptions);
2214514f5e3Sopenharmony_ci    EXPECT_EQ(JSTaggedValue::SameValue(
2224514f5e3Sopenharmony_ci        JSObject::GetProperty(thread, displayOptions, styleKey).GetValue(), styleValue), true);
2234514f5e3Sopenharmony_ci    EXPECT_EQ(JSTaggedValue::SameValue(
2244514f5e3Sopenharmony_ci        JSObject::GetProperty(thread, displayOptions, localeKey).GetValue(), locale), true);
2254514f5e3Sopenharmony_ci    EXPECT_EQ(JSTaggedValue::SameValue(
2264514f5e3Sopenharmony_ci        JSObject::GetProperty(thread, displayOptions, fallBackKey).GetValue(), fallBackValue), true);
2274514f5e3Sopenharmony_ci    EXPECT_EQ(JSTaggedValue::SameValue(
2284514f5e3Sopenharmony_ci        JSObject::GetProperty(thread, displayOptions, typeKey).GetValue(), typeValue), true);
2294514f5e3Sopenharmony_ci}
2304514f5e3Sopenharmony_ci}  // namespace panda::test
231