• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "ecmascript/builtins/builtins_intl.h"
17 
18 #include "ecmascript/js_array.h"
19 #include "ecmascript/global_env.h"
20 #include "ecmascript/tests/test_helper.h"
21 
22 using namespace panda::ecmascript;
23 using namespace panda::ecmascript::builtins;
24 
25 namespace panda::test {
26 class BuiltinsIntlTest : public testing::Test {
27 public:
SetUpTestCase()28     static void SetUpTestCase()
29     {
30         GTEST_LOG_(INFO) << "SetUpTestCase";
31     }
32 
TearDownTestCase()33     static void TearDownTestCase()
34     {
35         GTEST_LOG_(INFO) << "TearDownCase";
36     }
37 
SetUp()38     void SetUp() override
39     {
40         JSRuntimeOptions options;
41 #if PANDA_TARGET_LINUX
42         // for consistency requirement, use ohos_icu4j/data as icu-data-path
43         options.SetIcuDataPath(ICU_PATH);
44 #endif
45         options.SetEnableForceGC(true);
46         instance = JSNApi::CreateEcmaVM(options);
47         instance->SetEnableForceGC(true);
48         ASSERT_TRUE(instance != nullptr) << "Cannot create EcmaVM";
49         thread = instance->GetJSThread();
50         scope = new EcmaHandleScope(thread);
51     }
52 
TearDown()53     void TearDown() override
54     {
55         TestHelper::DestroyEcmaVMWithScope(instance, scope);
56     }
57 
58     EcmaVM *instance {nullptr};
59     EcmaHandleScope *scope {nullptr};
60     JSThread *thread {nullptr};
61 };
62 
HWTEST_F_L0(BuiltinsIntlTest,GetCanonicalLocales_001)63 HWTEST_F_L0(BuiltinsIntlTest, GetCanonicalLocales_001)
64 {
65     auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
66     ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
67     ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
68     ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue::Undefined());
69 
70     [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
71     JSTaggedValue resultObj = BuiltinsIntl::GetCanonicalLocales(ecmaRuntimeCallInfo);
72     JSHandle<JSArray> resultHandle(thread, resultObj);
73     EXPECT_EQ(resultHandle->GetArrayLength(), 0U);
74 }
75 
HWTEST_F_L0(BuiltinsIntlTest,GetCanonicalLocales_002)76 HWTEST_F_L0(BuiltinsIntlTest, GetCanonicalLocales_002)
77 {
78     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
79     JSHandle<EcmaString> handleStr = factory->NewFromASCII("ko-kore-kr");
80 
81     auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
82     ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
83     ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
84     ecmaRuntimeCallInfo->SetCallArg(0, handleStr.GetTaggedValue());
85 
86     [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
87     JSTaggedValue resultObj = BuiltinsIntl::GetCanonicalLocales(ecmaRuntimeCallInfo);
88     JSHandle<JSArray> resultHandle(thread, resultObj);
89 
90     JSHandle<TaggedArray> elements(thread, resultHandle->GetElements());
91     EXPECT_EQ(elements->GetLength(), 1U);
92     JSHandle<EcmaString> handleEcmaStr(thread, elements->Get(0));
93     EXPECT_STREQ("ko-Kore-KR", EcmaStringAccessor(handleEcmaStr).ToCString().c_str());
94 }
95 
HWTEST_F_L0(BuiltinsIntlTest,GetCanonicalLocales_003)96 HWTEST_F_L0(BuiltinsIntlTest, GetCanonicalLocales_003)
97 {
98     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
99 
100     JSArray *arr = JSArray::ArrayCreate(thread, JSTaggedNumber(0)).GetObject<JSArray>();
101     JSHandle<JSTaggedValue> obj(thread, arr);
102 
103     JSHandle<JSTaggedValue> handleStr(factory->NewFromASCII("zh-Hans-Cn"));
104     PropertyDescriptor desc(thread, handleStr, true, true, true);
105     JSHandle<JSTaggedValue> key(factory->NewFromASCII("1"));
106     JSArray::DefineOwnProperty(thread, JSHandle<JSObject>(obj), key, desc);
107 
108     auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
109     ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
110     ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
111     ecmaRuntimeCallInfo->SetCallArg(0, obj.GetTaggedValue());
112 
113     [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
114     JSTaggedValue resultObj = BuiltinsIntl::GetCanonicalLocales(ecmaRuntimeCallInfo);
115     JSHandle<JSArray> resultHandle(thread, resultObj);
116 
117     JSHandle<TaggedArray> elements(thread, resultHandle->GetElements());
118     EXPECT_EQ(elements->GetLength(), 1U);
119     JSHandle<EcmaString> handleEcmaStr(thread, elements->Get(0));
120     EXPECT_STREQ("zh-Hans-CN", EcmaStringAccessor(handleEcmaStr).ToCString().c_str());
121 }
122 } // namespace panda::test
123