• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 /*
17  * Copyright (c) 2024 Huawei Device Co., Ltd.
18  * Licensed under the Apache License, Version 2.0 (the "License");
19  * you may not use this file except in compliance with the License.
20  * You may obtain a copy of the License at
21  *
22  *     http://www.apache.org/licenses/LICENSE-2.0
23  *
24  * Unless required by applicable law or agreed to in writing, software
25  * distributed under the License is distributed on an "AS IS" BASIS,
26  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27  * See the License for the specific language governing permissions and
28  * limitations under the License.
29  */
30 
31 #include "jsvmtest.h"
32 
TEST(CreateProxy)33 TEST(CreateProxy)
34 {
35     std::string registerObject = R"JS(
36       target = {
37         message1: "hello",
38         message2: "everyone",
39       };
40 
41       handler1 = {};
42       handler2 = {
43         get(target, prop, receiver) {
44           return "world";
45         },
46       };
47     )JS";
48 
49     jsvm::Run(registerObject.c_str());
50     auto obj = jsvm::GetProperty(jsvm::Global(), "target");
51     auto handler1 = jsvm::GetProperty(jsvm::Global(), "handler1");
52     auto handler2 = jsvm::GetProperty(jsvm::Global(), "handler2");
53 
54     JSVM_Value proxy1 = nullptr;
55     JSVMTEST_CALL(OH_JSVM_CreateProxy(env, obj, handler1, &proxy1));
56 
57     JSVM_Value proxy2 = nullptr;
58     JSVMTEST_CALL(OH_JSVM_CreateProxy(env, obj, handler2, &proxy2));
59 
60     jsvm::SetProperty(jsvm::Global(), "proxy1", proxy1);
61     jsvm::SetProperty(jsvm::Global(), "proxy2", proxy2);
62 
63     std::string checkProxy1 = R"JS(
64       proxy1.message1 == target.message1 && proxy1.message2 == target.message2;
65     )JS";
66 
67     auto result = jsvm::Run(checkProxy1.c_str());
68     CHECK(jsvm::IsTrue(result));
69 
70     std::string checkProxy2 = R"JS(
71       proxy2.message1 == "world" && proxy2.message2 == "world" &&
72       proxy2.anyProperty == "world";
73     )JS";
74 
75     result = jsvm::Run(checkProxy2.c_str());
76     CHECK(jsvm::IsTrue(result));
77 }
78 
TEST(IsProxy)79 TEST(IsProxy)
80 {
81     jsvm::Run(R"JS(
82       target = {
83         message1: "hello",
84         message2: "everyone",
85       };
86 
87       handler1 = {};
88       proxy1 = new Proxy(target, handler1);
89     )JS");
90 
91     bool isProxy = false;
92 
93     auto value = jsvm::GetProperty(jsvm::Global(), "target");
94     JSVMTEST_CALL(OH_JSVM_IsProxy(env, value, &isProxy));
95     CHECK(!isProxy && "target is not JS Proxy");
96 
97     value = jsvm::GetProperty(jsvm::Global(), "handler1");
98     JSVMTEST_CALL(OH_JSVM_IsProxy(env, value, &isProxy));
99     CHECK(!isProxy && "handler1 is not JS Proxy");
100 
101     value = jsvm::GetProperty(jsvm::Global(), "proxy1");
102     JSVMTEST_CALL(OH_JSVM_IsProxy(env, value, &isProxy));
103     CHECK(isProxy && "proxy1 is JS Proxy");
104 }
105 
TEST(GetProxyTarget)106 TEST(GetProxyTarget)
107 {
108     jsvm::Run(R"JS(
109       target = {
110         message1: "hello",
111         message2: "everyone",
112       };
113 
114       handler1 = {};
115       proxy1 = new Proxy(target, handler1);
116     )JS");
117 
118     auto target = jsvm::GetProperty(jsvm::Global(), "target");
119     auto proxy1 = jsvm::GetProperty(jsvm::Global(), "proxy1");
120 
121     JSVM_Value result = nullptr;
122     JSVMTEST_CALL(OH_JSVM_ProxyGetTarget(env, proxy1, &result));
123 
124     CHECK(jsvm::StrictEquals(target, result));
125 }
126