• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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/ecma_context.h"
17 #include "ecmascript/ecma_string_table.h"
18 #include "ecmascript/object_factory.h"
19 #include "ecmascript/tests/test_helper.h"
20 #include "ecmascript/global_env.h"
21 
22 using namespace panda::ecmascript;
23 using namespace panda::ecmascript::base;
24 namespace panda::test {
25 class EcmaContextTest : public testing::Test {
26 public:
SetUpTestCase()27     static void SetUpTestCase()
28     {
29         GTEST_LOG_(INFO) << "SetUpTestCase";
30     }
31 
TearDownTestCase()32     static void TearDownTestCase()
33     {
34         GTEST_LOG_(INFO) << "TearDownCase";
35     }
36 
SetUp()37     void SetUp() override
38     {
39         TestHelper::CreateEcmaVMWithScope(ecmaVMPtr, thread, scope);
40     }
41 
TearDown()42     void TearDown() override
43     {
44         TestHelper::DestroyEcmaVMWithScope(ecmaVMPtr, scope);
45     }
46 
47     EcmaVM *ecmaVMPtr {nullptr};
48     ecmascript::EcmaHandleScope *scope {nullptr};
49     JSThread *thread {nullptr};
50 };
51 
HWTEST_F_L0(EcmaContextTest,Create)52 HWTEST_F_L0(EcmaContextTest, Create)
53 {
54     auto context = EcmaContext::CreateAndInitialize(thread);
55     CVector<EcmaContext *> Cv1 = thread->GetEcmaContexts();
56     EXPECT_EQ(Cv1.size(), 2);  // 2: size of contexts.
57     auto context2 = EcmaContext::CreateAndInitialize(thread);
58     Cv1 = thread->GetEcmaContexts();
59     EXPECT_EQ(Cv1.size(), 3);  // 3: size of contexts.
60     EcmaContext::CheckAndDestroy(thread, context);
61     Cv1 = thread->GetEcmaContexts();
62     EXPECT_EQ(Cv1.size(), 2);  // 2: size of contexts.
63     EcmaContext::CheckAndDestroy(thread, context2);
64 }
65 
HWTEST_F_L0(EcmaContextTest,GetRegExpCache)66 HWTEST_F_L0(EcmaContextTest, GetRegExpCache)
67 {
68     EcmaVM *vm = thread->GetEcmaVM();
69     ObjectFactory *factory = vm->GetFactory();
70     auto context = EcmaContext::CreateAndInitialize(thread);
71     JSHandle<EcmaString> regexp = factory->NewFromASCII("\\g");
72     JSHandle<JSTaggedValue> value2(regexp);
73     context->SetRegExpCache(value2.GetTaggedValue());
74     JSHandle<JSTaggedValue> res2 = context->GetRegExpCache();
75     EXPECT_EQ(res2.GetTaggedValue(), value2.GetTaggedValue());
76     EcmaContext::CheckAndDestroy(thread, context);
77 }
78 
HWTEST_F_L0(EcmaContextTest,AllowAtomicWait)79 HWTEST_F_L0(EcmaContextTest, AllowAtomicWait)
80 {
81     auto context = EcmaContext::CreateAndInitialize(thread);
82     bool value = context->GetAllowAtomicWait();
83     EXPECT_TRUE(value);
84     context->SetAllowAtomicWait(false);
85     bool value2 = context->GetAllowAtomicWait();
86     EXPECT_FALSE(value2);
87     EcmaContext::CheckAndDestroy(thread, context);
88 }
89 
HWTEST_F_L0(EcmaContextTest,SwitchCurrentContext)90 HWTEST_F_L0(EcmaContextTest, SwitchCurrentContext)
91 {
92     auto context = EcmaContext::CreateAndInitialize(thread);
93     auto context1 = EcmaContext::CreateAndInitialize(thread);
94 
95     CVector<EcmaContext *> contextVector = thread->GetEcmaContexts();
96     EXPECT_EQ(contextVector.size(), 3);  // 3: size of contexts.
97 
98     thread->SwitchCurrentContext(context);
99 
100     EcmaContext::CheckAndDestroy(thread, context);
101     contextVector = thread->GetEcmaContexts();
102     EXPECT_EQ(contextVector.size(), 2);  // 3: size of contexts.
103     EcmaContext::CheckAndDestroy(thread, context1);
104 }
105 }  // namespace panda::test