• 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/global_env.h"
17 #include "ecmascript/js_arraybuffer.h"
18 #include "ecmascript/js_dataview.h"
19 #include "ecmascript/object_factory.h"
20 #include "ecmascript/tests/test_helper.h"
21 
22 using namespace panda::ecmascript;
23 
24 namespace panda::test {
25 class JSDataViewTest : 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(instance, thread, scope);
40     }
41 
TearDown()42     void TearDown() override
43     {
44         TestHelper::DestroyEcmaVMWithScope(instance, scope);
45     }
46     EcmaVM *instance {nullptr};
47     ecmascript::EcmaHandleScope *scope {nullptr};
48     JSThread *thread {nullptr};
49 };
50 
51 /*
52  * Feature: JSDataView
53  * Function: GetElementSize
54  * SubFunction: N/A
55  * FunctionPoints: Get ElementSize
56  * CaseDescription: Check whether the returned value through "GetElementSize" function is within expectations.
57  */
HWTEST_F_L0(JSDataViewTest,GetElementSize)58 HWTEST_F_L0(JSDataViewTest, GetElementSize)
59 {
60     EXPECT_EQ(JSDataView::GetElementSize(DataViewType::INT8), 1U);
61     EXPECT_EQ(JSDataView::GetElementSize(DataViewType::UINT8), 1U);
62     EXPECT_EQ(JSDataView::GetElementSize(DataViewType::UINT8_CLAMPED), 1U);
63     EXPECT_EQ(JSDataView::GetElementSize(DataViewType::INT16), 2U);
64     EXPECT_EQ(JSDataView::GetElementSize(DataViewType::UINT16), 2U);
65     EXPECT_EQ(JSDataView::GetElementSize(DataViewType::INT32), 4U);
66     EXPECT_EQ(JSDataView::GetElementSize(DataViewType::UINT32), 4U);
67     EXPECT_EQ(JSDataView::GetElementSize(DataViewType::FLOAT32), 4U);
68     EXPECT_EQ(JSDataView::GetElementSize(DataViewType::FLOAT64), 8U);
69 }
70 
71 /*
72  * Feature: JSDataView
73  * Function: SetDataView
74  * SubFunction: GetDataView
75  * FunctionPoints: Set DataView
76  * CaseDescription: Check whether the returned value through "GetDataView" function is within expectations after
77  *                  calling "SetDataView" function.
78  */
HWTEST_F_L0(JSDataViewTest,SetDataView)79 HWTEST_F_L0(JSDataViewTest, SetDataView)
80 {
81     EcmaVM *ecmaVMPtr = thread->GetEcmaVM();
82     ObjectFactory *factory = ecmaVMPtr->GetFactory();
83     JSHandle<GlobalEnv> handleGlobalEnv = ecmaVMPtr->GetGlobalEnv();
84 
85     uint32_t lengthDataArrayBuf = 8;
86     uint32_t offsetDataView = 4;
87     uint32_t lengthDataView = 4;
88     JSHandle<JSFunction> handleFuncArrayBuf(handleGlobalEnv->GetArrayBufferFunction());
89     JSHandle<JSTaggedValue> handleTagValFuncArrayBuf(handleFuncArrayBuf);
90     JSHandle<JSArrayBuffer> handleArrayBuf(
91         factory->NewJSObjectByConstructor(handleFuncArrayBuf, handleTagValFuncArrayBuf));
92     handleArrayBuf->SetArrayBufferByteLength(lengthDataArrayBuf);
93 
94     // Call "SetDataView" function through "NewJSDataView" function of "object_factory.cpp"
95     JSHandle<JSDataView> handleDataView = factory->NewJSDataView(handleArrayBuf, offsetDataView,
96         lengthDataView);
97     EXPECT_TRUE(handleDataView->GetDataView().IsTrue());
98 
99     // Call "SetDataView" function in this HWTEST_F_L0.
100     handleDataView->SetDataView(thread, JSTaggedValue::False());
101     EXPECT_TRUE(handleDataView->GetDataView().IsFalse());
102     handleDataView->SetDataView(thread, JSTaggedValue::True());
103     EXPECT_TRUE(handleDataView->GetDataView().IsTrue());
104 }
105 
106 /*
107  * Feature: JSDataView
108  * Function: SetViewedArrayBuffer
109  * SubFunction: GetViewedArrayBuffer
110  * FunctionPoints: Set ViewedArrayBuffer
111  * CaseDescription: Check whether the returned value through "GetViewedArrayBuffer" function is within expectations
112  *                  after calling "SetViewedArrayBuffer" function.
113  */
HWTEST_F_L0(JSDataViewTest,SetViewedArrayBuffer)114 HWTEST_F_L0(JSDataViewTest, SetViewedArrayBuffer)
115 {
116     EcmaVM *ecmaVMPtr = thread->GetEcmaVM();
117     ObjectFactory *factory = ecmaVMPtr->GetFactory();
118     JSHandle<JSFunction> handleFuncArrayBuf(ecmaVMPtr->GetGlobalEnv()->GetArrayBufferFunction());
119     JSHandle<JSTaggedValue> handleTagValFuncArrayBuf(handleFuncArrayBuf);
120 
121     uint32_t lengthDataArrayBuf1 = 8;
122     uint32_t lengthDataArrayBuf2 = 16;
123     uint32_t offsetDataView = 4;
124     uint32_t lengthDataView = 4;
125     JSHandle<JSArrayBuffer> handleArrayBuf1(
126         factory->NewJSObjectByConstructor(handleFuncArrayBuf, handleTagValFuncArrayBuf));
127     JSHandle<JSArrayBuffer> handleArrayBuf2(
128         factory->NewJSObjectByConstructor(handleFuncArrayBuf, handleTagValFuncArrayBuf));
129     handleArrayBuf1->SetArrayBufferByteLength(lengthDataArrayBuf1);
130     handleArrayBuf2->SetArrayBufferByteLength(lengthDataArrayBuf2);
131 
132     // Call "SetViewedArrayBuffer" function through "NewJSDataView" function of "object_factory.cpp"
133     JSHandle<JSDataView> handleDataView = factory->NewJSDataView(handleArrayBuf1, offsetDataView,
134         lengthDataView);
135     JSHandle<JSTaggedValue> handleTagValDataViewFrom1(thread, handleArrayBuf1.GetTaggedValue());
136     JSHandle<JSTaggedValue> handleTagValDataViewTo1(thread, handleDataView->GetViewedArrayBuffer());
137     EXPECT_TRUE(JSTaggedValue::Equal(thread, handleTagValDataViewFrom1, handleTagValDataViewTo1));
138 
139     // Call "SetViewedArrayBuffer" function in this HWTEST_F_L0.
140     handleDataView->SetViewedArrayBuffer(thread, handleArrayBuf2.GetTaggedValue());
141     JSHandle<JSTaggedValue> handleTagValDataViewFrom2(thread, handleArrayBuf2.GetTaggedValue());
142     JSHandle<JSTaggedValue> handleTagValDataViewTo2(thread, handleDataView->GetViewedArrayBuffer());
143     EXPECT_TRUE(JSTaggedValue::Equal(thread, handleTagValDataViewFrom2, handleTagValDataViewTo2));
144     EXPECT_FALSE(JSTaggedValue::Equal(thread, handleTagValDataViewFrom1, handleTagValDataViewFrom2));
145 }
146 
147 /*
148  * Feature: JSDataView
149  * Function: SetByteLength
150  * SubFunction: GetByteLength
151  * FunctionPoints: Set ByteLength
152  * CaseDescription: Check whether the returned value through "GetByteLength" function is within expectations after
153  *                  calling "SetByteLength" function.
154  */
HWTEST_F_L0(JSDataViewTest,SetByteLength)155 HWTEST_F_L0(JSDataViewTest, SetByteLength)
156 {
157     EcmaVM *ecmaVMPtr = thread->GetEcmaVM();
158     ObjectFactory *factory = ecmaVMPtr->GetFactory();
159     JSHandle<JSFunction> handleFuncArrayBuf(ecmaVMPtr->GetGlobalEnv()->GetArrayBufferFunction());
160     JSHandle<JSTaggedValue> handleTagValFuncArrayBuf(handleFuncArrayBuf);
161 
162     uint32_t lengthDataArrayBuf = 8;
163     uint32_t offsetDataView = 4;
164     uint32_t lengthDataView1 = 4;
165     uint32_t lengthDataView2 = 2;
166     JSHandle<JSArrayBuffer> handleArrayBuf(
167         factory->NewJSObjectByConstructor(handleFuncArrayBuf, handleTagValFuncArrayBuf));
168     handleArrayBuf->SetArrayBufferByteLength(lengthDataArrayBuf);
169 
170     // Call "SetByteLength" function through "NewJSDataView" function of "object_factory.cpp"
171     JSHandle<JSDataView> handleDataView = factory->NewJSDataView(handleArrayBuf, offsetDataView,
172         lengthDataView1);
173     EXPECT_EQ(handleDataView->GetByteLength(), lengthDataView1);
174 
175     // Call "SetByteLength" function in this HWTEST_F_L0.
176     handleDataView->SetByteLength(lengthDataView2);
177     EXPECT_EQ(handleDataView->GetByteLength(), lengthDataView2);
178 }
179 
180 /*
181  * Feature: JSDataView
182  * Function: SetByteOffset
183  * SubFunction: GetByteOffset
184  * FunctionPoints: Set ByteOffset
185  * CaseDescription: Check whether the returned value through "GetByteOffset" function is within expectations after
186  *                  calling "SetByteOffset" function.
187  */
HWTEST_F_L0(JSDataViewTest,SetByteOffset)188 HWTEST_F_L0(JSDataViewTest, SetByteOffset)
189 {
190     EcmaVM *ecmaVMPtr = thread->GetEcmaVM();
191     ObjectFactory *factory = ecmaVMPtr->GetFactory();
192     JSHandle<JSFunction> handleFuncArrayBuf1(ecmaVMPtr->GetGlobalEnv()->GetArrayBufferFunction());
193     JSHandle<JSTaggedValue> handleTagValFuncArrayBuf1(handleFuncArrayBuf1);
194 
195     uint32_t lengthDataArrayBuf = 8;
196     uint32_t offsetDataView1 = 4;
197     uint32_t offsetDataView2 = 6;
198     uint32_t lengthDataView = 2;
199     JSHandle<JSArrayBuffer> handleArrayBuf(
200         factory->NewJSObjectByConstructor(handleFuncArrayBuf1, handleTagValFuncArrayBuf1));
201     handleArrayBuf->SetArrayBufferByteLength(lengthDataArrayBuf);
202 
203     // Call "SetByteOffset" function through "NewJSDataView" function of "object_factory.cpp"
204     JSHandle<JSDataView> handleDataView = factory->NewJSDataView(handleArrayBuf, offsetDataView1,
205         lengthDataView);
206     EXPECT_EQ(handleDataView->GetByteOffset(), offsetDataView1);
207 
208     // Call "SetByteOffset" function in this HWTEST_F_L0.
209     handleDataView->SetByteOffset(offsetDataView2);
210     EXPECT_EQ(handleDataView->GetByteOffset(), offsetDataView2);
211 }
212 }  // namespace panda::test
213