• 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_global.h"
17 
18 #include "ecmascript/js_hclass.h"
19 #include "ecmascript/ecma_runtime_call_info.h"
20 #include "ecmascript/ecma_vm.h"
21 #include "ecmascript/global_env.h"
22 #include "ecmascript/js_tagged_value-inl.h"
23 #include "ecmascript/js_thread.h"
24 #include "ecmascript/object_factory.h"
25 #include "ecmascript/tests/test_helper.h"
26 
27 using namespace panda::ecmascript;
28 using namespace panda::ecmascript::builtins;
29 using BuiltinsBase = panda::ecmascript::base::BuiltinsBase;
30 
31 namespace panda::test {
32 class BuiltinsGlobalTest : public testing::Test {
33 public:
SetUpTestCase()34     static void SetUpTestCase()
35     {
36         GTEST_LOG_(INFO) << "SetUpTestCase";
37     }
38 
TearDownTestCase()39     static void TearDownTestCase()
40     {
41         GTEST_LOG_(INFO) << "TearDownCase";
42     }
43 
SetUp()44     void SetUp() override
45     {
46         TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
47     }
48 
TearDown()49     void TearDown() override
50     {
51         TestHelper::DestroyEcmaVMWithScope(instance, scope);
52     }
53 
54     EcmaVM *instance {nullptr};
55     EcmaHandleScope *scope {nullptr};
56     JSThread *thread {nullptr};
57 
NewJSObject(JSThread * thread,ObjectFactory * factory,JSHandle<GlobalEnv> globalEnv)58     static JSHandle<JSObject> NewJSObject(JSThread *thread, ObjectFactory *factory, JSHandle<GlobalEnv> globalEnv)
59     {
60     JSFunction *jsFunc = globalEnv->GetObjectFunction().GetObject<JSFunction>();
61     JSHandle<JSTaggedValue> jsFunc1(thread, jsFunc);
62     JSHandle<JSObject> jsObj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(jsFunc1), jsFunc1);
63     return jsObj;
64     }
65 };
66 
HWTEST_F_L0(BuiltinsGlobalTest,DecodeURIComponent)67 HWTEST_F_L0(BuiltinsGlobalTest, DecodeURIComponent)
68 {
69     auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
70     ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
71     ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
72     ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(123));
73 
74     [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
75     JSTaggedValue result = BuiltinsGlobal::DecodeURIComponent(ecmaRuntimeCallInfo);
76     TestHelper::TearDownFrame(thread, prev);
77     EXPECT_TRUE(result.IsString());
78     JSHandle<EcmaString> ecmaStrHandle(thread, result);
79     EXPECT_STREQ("123", EcmaStringAccessor(ecmaStrHandle).ToCString().c_str());
80 }
81 
HWTEST_F_L0(BuiltinsGlobalTest,EncodeURIComponent)82 HWTEST_F_L0(BuiltinsGlobalTest, EncodeURIComponent)
83 {
84     auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
85     ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
86     ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
87     ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(123));
88 
89     [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
90     JSTaggedValue result = BuiltinsGlobal::EncodeURIComponent(ecmaRuntimeCallInfo);
91     TestHelper::TearDownFrame(thread, prev);
92     EXPECT_TRUE(result.IsString());
93     JSHandle<EcmaString> ecmaStrHandle(thread, result);
94     EXPECT_STREQ("123", EcmaStringAccessor(ecmaStrHandle).ToCString().c_str());
95 }
96 
HWTEST_F_L0(BuiltinsGlobalTest,PrintEntrypointAndPrintString)97 HWTEST_F_L0(BuiltinsGlobalTest, PrintEntrypointAndPrintString)
98 {
99     // msg == nullptr
100     JSTaggedValue result = BuiltinsGlobal::PrintEntrypoint(nullptr);
101     EXPECT_EQ(result, JSTaggedValue::Undefined());
102 }
103 
HWTEST_F_L0(BuiltinsGlobalTest,CallJsProxy)104 HWTEST_F_L0(BuiltinsGlobalTest, CallJsProxy)
105 {
106     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
107     auto ecmaVM = thread->GetEcmaVM();
108     JSHandle<GlobalEnv> env = ecmaVM->GetGlobalEnv();
109     JSHandle<JSTaggedValue> emptyObj(thread, NewJSObject(thread, factory, env).GetTaggedValue());
110     JSHandle<JSProxy> proxy = factory->NewJSProxy(emptyObj, emptyObj);
111     proxy->SetCallable(false);
112     auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
113     ecmaRuntimeCallInfo->SetFunction(proxy.GetTaggedValue());
114     ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
115 
116     [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
117     JSTaggedValue result = BuiltinsGlobal::CallJsProxy(ecmaRuntimeCallInfo);
118     TestHelper::TearDownFrame(thread, prev);
119     EXPECT_TRUE(thread->HasPendingException());
120     EXPECT_EQ(result, JSTaggedValue::Undefined());
121     thread->ClearException();
122 }
123 }  // namespace panda::test
124