• 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 #include "jsvmtest.h"
17 
18 static std::string defineFunction = R"JS(
19     var x1 = 0;
20     var x2 = 0;
21     function f1(x) {
22         x1 = x;
23         return x + 1;
24     }
25     function f2(x) {
26         x2 = x;
27         return x + 1;
28     }
29 )JS";
30 
31 static std::string init = R"JS(
32     x1 = 0;
33     x2 = 0;
34 )JS";
35 
TEST(PromiseThen)36 TEST(PromiseThen)
37 {
38     JSVM_Value promise = nullptr;
39     JSVM_Deferred deffered;
40 
41     JSVMTEST_CALL(OH_JSVM_CreatePromise(env, &deffered, &promise));
42     jsvm::Run(defineFunction.c_str());
43 
44     JSVM_Value f1 = jsvm::GetProperty(jsvm::Global(), "f1");
45 
46     JSVMTEST_CALL(OH_JSVM_PromiseRegisterHandler(env, promise, f1, nullptr, nullptr));
47 
48     auto x1 = jsvm::GetProperty(jsvm::Global(), "x1");
49     auto x2 = jsvm::GetProperty(jsvm::Global(), "x2");
50     CHECK(jsvm::Equals(x1, jsvm::Int32(0)));
51     CHECK(jsvm::Equals(x2, jsvm::Int32(0)));
52 
53     OH_JSVM_ResolveDeferred(env, deffered, jsvm::Int32(2));
54     deffered = nullptr;
55 
56     x1 = jsvm::GetProperty(jsvm::Global(), "x1");
57     x2 = jsvm::GetProperty(jsvm::Global(), "x2");
58     CHECK(jsvm::Equals(x1, jsvm::Int32(2)));
59     CHECK(jsvm::Equals(x2, jsvm::Int32(0)));
60 }
61 
TEST(PromiseThen2)62 TEST(PromiseThen2)
63 {
64     jsvm::Run(defineFunction.c_str());
65 
66     JSVM_Value f1 = jsvm::GetProperty(jsvm::Global(), "f1");
67     auto f2 = jsvm::GetProperty(jsvm::Global(), "f2");
68 
69     JSVM_Value promise;
70     JSVM_Deferred deffered;
71 
72     // Resolve
73     JSVMTEST_CALL(OH_JSVM_CreatePromise(env, &deffered, &promise));
74     JSVMTEST_CALL(OH_JSVM_PromiseRegisterHandler(env, promise, f1, f2, nullptr));
75 
76     auto x1 = jsvm::GetProperty(jsvm::Global(), "x1");
77     auto x2 = jsvm::GetProperty(jsvm::Global(), "x2");
78     CHECK(jsvm::Equals(x1, jsvm::Int32(0)));
79     CHECK(jsvm::Equals(x2, jsvm::Int32(0)));
80 
81     OH_JSVM_ResolveDeferred(env, deffered, jsvm::Int32(2));
82     deffered = nullptr;
83 
84     x1 = jsvm::GetProperty(jsvm::Global(), "x1");
85     x2 = jsvm::GetProperty(jsvm::Global(), "x2");
86     CHECK(jsvm::Equals(x1, jsvm::Int32(2)));
87     CHECK(jsvm::Equals(x2, jsvm::Int32(0)));
88 
89     jsvm::Run(init.c_str());
90     // Reject
91     JSVMTEST_CALL(OH_JSVM_CreatePromise(env, &deffered, &promise));
92     JSVMTEST_CALL(OH_JSVM_PromiseRegisterHandler(env, promise, f1, f2, nullptr));
93 
94     x1 = jsvm::GetProperty(jsvm::Global(), "x1");
95     x2 = jsvm::GetProperty(jsvm::Global(), "x2");
96     CHECK(jsvm::Equals(x1, jsvm::Int32(0)));
97     CHECK(jsvm::Equals(x2, jsvm::Int32(0)));
98 
99     OH_JSVM_RejectDeferred(env, deffered, jsvm::Int32(3));
100     deffered = nullptr;
101 
102     x1 = jsvm::GetProperty(jsvm::Global(), "x1");
103     x2 = jsvm::GetProperty(jsvm::Global(), "x2");
104     CHECK(jsvm::Equals(x1, jsvm::Int32(0)));
105     CHECK(jsvm::Equals(x2, jsvm::Int32(3)));
106 }
107 
TEST(PromiseThenChain)108 TEST(PromiseThenChain)
109 {
110     jsvm::Run(defineFunction.c_str());
111 
112     JSVM_Value f1 = jsvm::GetProperty(jsvm::Global(), "f1");
113     auto f2 = jsvm::GetProperty(jsvm::Global(), "f2");
114 
115     JSVM_Value promise, promise1, x1, x2;
116     JSVM_Deferred deffered = nullptr;
117 
118     // Resolve
119     JSVMTEST_CALL(OH_JSVM_CreatePromise(env, &deffered, &promise));
120     JSVMTEST_CALL(OH_JSVM_PromiseRegisterHandler(env, promise, f1, nullptr, &promise1));
121     JSVMTEST_CALL(OH_JSVM_PromiseRegisterHandler(env, promise1, f2, nullptr, nullptr));
122 
123     x1 = jsvm::GetProperty(jsvm::Global(), "x1");
124     x2 = jsvm::GetProperty(jsvm::Global(), "x2");
125     CHECK(jsvm::Equals(x1, jsvm::Int32(0)));
126     CHECK(jsvm::Equals(x2, jsvm::Int32(0)));
127 
128     OH_JSVM_ResolveDeferred(env, deffered, jsvm::Int32(2));
129     deffered = nullptr;
130 
131     x1 = jsvm::GetProperty(jsvm::Global(), "x1");
132     x2 = jsvm::GetProperty(jsvm::Global(), "x2");
133     CHECK(jsvm::Equals(x1, jsvm::Int32(2)));
134     CHECK(jsvm::Equals(x2, jsvm::Int32(3)));
135 }
136 
TEST(PromiseCatch)137 TEST(PromiseCatch)
138 {
139     jsvm::Run(defineFunction.c_str());
140 
141     JSVM_Value f1 = jsvm::GetProperty(jsvm::Global(), "f1");
142 
143     JSVM_Value promise;
144     JSVM_Value promise1;
145     JSVM_Deferred deffered = nullptr;
146 
147     // Resolve
148     JSVMTEST_CALL(OH_JSVM_CreatePromise(env, &deffered, &promise));
149     JSVMTEST_CALL(OH_JSVM_PromiseRegisterHandler(env, promise, nullptr, f1, &promise1));
150 
151     auto x1 = jsvm::GetProperty(jsvm::Global(), "x1");
152     auto x2 = jsvm::GetProperty(jsvm::Global(), "x2");
153     CHECK(jsvm::Equals(x1, jsvm::Int32(0)));
154     CHECK(jsvm::Equals(x2, jsvm::Int32(0)));
155 
156     JSVMTEST_CALL(OH_JSVM_RejectDeferred(env, deffered, jsvm::Int32(2)));
157     deffered = nullptr;
158 
159     x1 = jsvm::GetProperty(jsvm::Global(), "x1");
160     x2 = jsvm::GetProperty(jsvm::Global(), "x2");
161     CHECK(jsvm::Equals(x1, jsvm::Int32(2)));
162     CHECK(jsvm::Equals(x2, jsvm::Int32(0)));
163 }
164