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_intl.h"
174514f5e3Sopenharmony_ci
184514f5e3Sopenharmony_ci#include "ecmascript/js_array.h"
194514f5e3Sopenharmony_ci#include "ecmascript/global_env.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 BuiltinsIntlTest : public BaseTestWithScope<true> {
274514f5e3Sopenharmony_ci};
284514f5e3Sopenharmony_ci
294514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsIntlTest, GetCanonicalLocales_001)
304514f5e3Sopenharmony_ci{
314514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
324514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
334514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
344514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue::Undefined());
354514f5e3Sopenharmony_ci
364514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
374514f5e3Sopenharmony_ci    JSTaggedValue resultObj = BuiltinsIntl::GetCanonicalLocales(ecmaRuntimeCallInfo);
384514f5e3Sopenharmony_ci    JSHandle<JSArray> resultHandle(thread, resultObj);
394514f5e3Sopenharmony_ci    EXPECT_EQ(resultHandle->GetArrayLength(), 0U);
404514f5e3Sopenharmony_ci}
414514f5e3Sopenharmony_ci
424514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsIntlTest, GetCanonicalLocales_002)
434514f5e3Sopenharmony_ci{
444514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
454514f5e3Sopenharmony_ci    JSHandle<EcmaString> handleStr = factory->NewFromASCII("ko-kore-kr");
464514f5e3Sopenharmony_ci
474514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
484514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
494514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
504514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetCallArg(0, handleStr.GetTaggedValue());
514514f5e3Sopenharmony_ci
524514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
534514f5e3Sopenharmony_ci    JSTaggedValue resultObj = BuiltinsIntl::GetCanonicalLocales(ecmaRuntimeCallInfo);
544514f5e3Sopenharmony_ci    JSHandle<JSArray> resultHandle(thread, resultObj);
554514f5e3Sopenharmony_ci
564514f5e3Sopenharmony_ci    JSHandle<TaggedArray> elements(thread, resultHandle->GetElements());
574514f5e3Sopenharmony_ci    EXPECT_EQ(elements->GetLength(), 1U);
584514f5e3Sopenharmony_ci    JSHandle<EcmaString> handleEcmaStr(thread, elements->Get(0));
594514f5e3Sopenharmony_ci    EXPECT_STREQ("ko-Kore-KR", EcmaStringAccessor(handleEcmaStr).ToCString().c_str());
604514f5e3Sopenharmony_ci}
614514f5e3Sopenharmony_ci
624514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsIntlTest, GetCanonicalLocales_003)
634514f5e3Sopenharmony_ci{
644514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
654514f5e3Sopenharmony_ci
664514f5e3Sopenharmony_ci    JSArray *arr = JSArray::ArrayCreate(thread, JSTaggedNumber(0)).GetObject<JSArray>();
674514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> obj(thread, arr);
684514f5e3Sopenharmony_ci
694514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> handleStr(factory->NewFromASCII("zh-Hans-Cn"));
704514f5e3Sopenharmony_ci    PropertyDescriptor desc(thread, handleStr, true, true, true);
714514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> key(factory->NewFromASCII("1"));
724514f5e3Sopenharmony_ci    JSArray::DefineOwnProperty(thread, JSHandle<JSObject>(obj), key, desc);
734514f5e3Sopenharmony_ci
744514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
754514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
764514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
774514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetCallArg(0, obj.GetTaggedValue());
784514f5e3Sopenharmony_ci
794514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
804514f5e3Sopenharmony_ci    JSTaggedValue resultObj = BuiltinsIntl::GetCanonicalLocales(ecmaRuntimeCallInfo);
814514f5e3Sopenharmony_ci    JSHandle<JSArray> resultHandle(thread, resultObj);
824514f5e3Sopenharmony_ci
834514f5e3Sopenharmony_ci    JSHandle<TaggedArray> elements(thread, resultHandle->GetElements());
844514f5e3Sopenharmony_ci    EXPECT_EQ(elements->GetLength(), 1U);
854514f5e3Sopenharmony_ci    JSHandle<EcmaString> handleEcmaStr(thread, elements->Get(0));
864514f5e3Sopenharmony_ci    EXPECT_STREQ("zh-Hans-CN", EcmaStringAccessor(handleEcmaStr).ToCString().c_str());
874514f5e3Sopenharmony_ci}
884514f5e3Sopenharmony_ci} // namespace panda::test
89