1425bb815Sopenharmony_ci/* Copyright JS Foundation and other contributors, http://js.foundation
2425bb815Sopenharmony_ci *
3425bb815Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4425bb815Sopenharmony_ci * you may not use this file except in compliance with the License.
5425bb815Sopenharmony_ci * You may obtain a copy of the License at
6425bb815Sopenharmony_ci *
7425bb815Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8425bb815Sopenharmony_ci *
9425bb815Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10425bb815Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS
11425bb815Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12425bb815Sopenharmony_ci * See the License for the specific language governing permissions and
13425bb815Sopenharmony_ci * limitations under the License.
14425bb815Sopenharmony_ci */
15425bb815Sopenharmony_ci
16425bb815Sopenharmony_ci#include "jerryscript.h"
17425bb815Sopenharmony_ci#include "jerryscript-port.h"
18425bb815Sopenharmony_ci#include "jerryscript-port-default.h"
19425bb815Sopenharmony_ci#include "test-common.h"
20425bb815Sopenharmony_ci#include <gtest/gtest.h>
21425bb815Sopenharmony_ci
22425bb815Sopenharmony_cistatic const jerry_char_t test_source[] = TEST_STRING_LITERAL (
23425bb815Sopenharmony_ci  "var p1 = create_promise1();"
24425bb815Sopenharmony_ci  "var p2 = create_promise2();"
25425bb815Sopenharmony_ci  "p1.then(function(x) { "
26425bb815Sopenharmony_ci  "  assert(x==='resolved'); "
27425bb815Sopenharmony_ci  "}); "
28425bb815Sopenharmony_ci  "p2.catch(function(x) { "
29425bb815Sopenharmony_ci  "  assert(x==='rejected'); "
30425bb815Sopenharmony_ci  "}); "
31425bb815Sopenharmony_ci);
32425bb815Sopenharmony_ci
33425bb815Sopenharmony_cistatic int count_in_assert = 0;
34425bb815Sopenharmony_cistatic jerry_value_t my_promise1;
35425bb815Sopenharmony_cistatic jerry_value_t my_promise2;
36425bb815Sopenharmony_cistatic const jerry_char_t s1[] = "resolved";
37425bb815Sopenharmony_cistatic const jerry_char_t s2[] = "rejected";
38425bb815Sopenharmony_ci
39425bb815Sopenharmony_cistatic jerry_value_t
40425bb815Sopenharmony_cicreate_promise1_handler (const jerry_value_t func_obj_val, /**< function object */
41425bb815Sopenharmony_ci                         const jerry_value_t this_val, /**< this value */
42425bb815Sopenharmony_ci                         const jerry_value_t args_p[], /**< arguments list */
43425bb815Sopenharmony_ci                         const jerry_length_t args_cnt) /**< arguments length */
44425bb815Sopenharmony_ci{
45425bb815Sopenharmony_ci  JERRY_UNUSED (func_obj_val);
46425bb815Sopenharmony_ci  JERRY_UNUSED (this_val);
47425bb815Sopenharmony_ci  JERRY_UNUSED (args_p);
48425bb815Sopenharmony_ci  JERRY_UNUSED (args_cnt);
49425bb815Sopenharmony_ci
50425bb815Sopenharmony_ci  jerry_value_t ret =  jerry_create_promise ();
51425bb815Sopenharmony_ci  my_promise1 = jerry_acquire_value (ret);
52425bb815Sopenharmony_ci
53425bb815Sopenharmony_ci  return ret;
54425bb815Sopenharmony_ci} /* create_promise1_handler */
55425bb815Sopenharmony_ci
56425bb815Sopenharmony_cistatic jerry_value_t
57425bb815Sopenharmony_cicreate_promise2_handler (const jerry_value_t func_obj_val, /**< function object */
58425bb815Sopenharmony_ci                         const jerry_value_t this_val, /**< this value */
59425bb815Sopenharmony_ci                         const jerry_value_t args_p[], /**< arguments list */
60425bb815Sopenharmony_ci                         const jerry_length_t args_cnt) /**< arguments length */
61425bb815Sopenharmony_ci{
62425bb815Sopenharmony_ci  JERRY_UNUSED (func_obj_val);
63425bb815Sopenharmony_ci  JERRY_UNUSED (this_val);
64425bb815Sopenharmony_ci  JERRY_UNUSED (args_p);
65425bb815Sopenharmony_ci  JERRY_UNUSED (args_cnt);
66425bb815Sopenharmony_ci
67425bb815Sopenharmony_ci  jerry_value_t ret =  jerry_create_promise ();
68425bb815Sopenharmony_ci  my_promise2 = jerry_acquire_value (ret);
69425bb815Sopenharmony_ci
70425bb815Sopenharmony_ci  return ret;
71425bb815Sopenharmony_ci} /* create_promise2_handler */
72425bb815Sopenharmony_ci
73425bb815Sopenharmony_cistatic jerry_value_t
74425bb815Sopenharmony_ciassert_handler (const jerry_value_t func_obj_val, /**< function object */
75425bb815Sopenharmony_ci                const jerry_value_t this_val, /**< this arg */
76425bb815Sopenharmony_ci                const jerry_value_t args_p[], /**< function arguments */
77425bb815Sopenharmony_ci                const jerry_length_t args_cnt) /**< number of function arguments */
78425bb815Sopenharmony_ci{
79425bb815Sopenharmony_ci  JERRY_UNUSED (func_obj_val);
80425bb815Sopenharmony_ci  JERRY_UNUSED (this_val);
81425bb815Sopenharmony_ci
82425bb815Sopenharmony_ci  count_in_assert++;
83425bb815Sopenharmony_ci
84425bb815Sopenharmony_ci  if (args_cnt == 1
85425bb815Sopenharmony_ci      && jerry_value_is_boolean (args_p[0])
86425bb815Sopenharmony_ci      && jerry_get_boolean_value (args_p[0]))
87425bb815Sopenharmony_ci  {
88425bb815Sopenharmony_ci    return jerry_create_boolean (true);
89425bb815Sopenharmony_ci  }
90425bb815Sopenharmony_ci  else
91425bb815Sopenharmony_ci  {
92425bb815Sopenharmony_ci    TEST_ASSERT (false);
93425bb815Sopenharmony_ci  }
94425bb815Sopenharmony_ci} /* assert_handler */
95425bb815Sopenharmony_ci
96425bb815Sopenharmony_ci/**
97425bb815Sopenharmony_ci * Register a JavaScript function in the global object.
98425bb815Sopenharmony_ci */
99425bb815Sopenharmony_cistatic void
100425bb815Sopenharmony_ciregister_js_function (const char *name_p, /**< name of the function */
101425bb815Sopenharmony_ci                      jerry_external_handler_t handler_p) /**< function callback */
102425bb815Sopenharmony_ci{
103425bb815Sopenharmony_ci  jerry_value_t global_obj_val = jerry_get_global_object ();
104425bb815Sopenharmony_ci
105425bb815Sopenharmony_ci  jerry_value_t function_val = jerry_create_external_function (handler_p);
106425bb815Sopenharmony_ci  jerry_value_t function_name_val = jerry_create_string ((const jerry_char_t *) name_p);
107425bb815Sopenharmony_ci  jerry_value_t result_val = jerry_set_property (global_obj_val, function_name_val, function_val);
108425bb815Sopenharmony_ci
109425bb815Sopenharmony_ci  jerry_release_value (function_name_val);
110425bb815Sopenharmony_ci  jerry_release_value (function_val);
111425bb815Sopenharmony_ci  jerry_release_value (global_obj_val);
112425bb815Sopenharmony_ci
113425bb815Sopenharmony_ci  jerry_release_value (result_val);
114425bb815Sopenharmony_ci} /* register_js_function */
115425bb815Sopenharmony_ci
116425bb815Sopenharmony_ciclass PromiseTest : public testing::Test{
117425bb815Sopenharmony_cipublic:
118425bb815Sopenharmony_ci    static void SetUpTestCase()
119425bb815Sopenharmony_ci    {
120425bb815Sopenharmony_ci        GTEST_LOG_(INFO) << "PromiseTest SetUpTestCase";
121425bb815Sopenharmony_ci    }
122425bb815Sopenharmony_ci
123425bb815Sopenharmony_ci    static void TearDownTestCase()
124425bb815Sopenharmony_ci    {
125425bb815Sopenharmony_ci        GTEST_LOG_(INFO) << "PromiseTest TearDownTestCase";
126425bb815Sopenharmony_ci    }
127425bb815Sopenharmony_ci
128425bb815Sopenharmony_ci    void SetUp() override {}
129425bb815Sopenharmony_ci    void TearDown() override {}
130425bb815Sopenharmony_ci
131425bb815Sopenharmony_ci};
132425bb815Sopenharmony_cistatic constexpr size_t JERRY_SCRIPT_MEM_SIZE = 50 * 1024 * 1024;
133425bb815Sopenharmony_cistatic void* context_alloc_fn(size_t size, void* cb_data)
134425bb815Sopenharmony_ci{
135425bb815Sopenharmony_ci    (void)cb_data;
136425bb815Sopenharmony_ci    size_t newSize = size > JERRY_SCRIPT_MEM_SIZE ? JERRY_SCRIPT_MEM_SIZE : size;
137425bb815Sopenharmony_ci    return malloc(newSize);
138425bb815Sopenharmony_ci}
139425bb815Sopenharmony_ciHWTEST_F(PromiseTest, Test001, testing::ext::TestSize.Level1)
140425bb815Sopenharmony_ci{
141425bb815Sopenharmony_ci
142425bb815Sopenharmony_ci  if (!jerry_is_feature_enabled (JERRY_FEATURE_PROMISE))
143425bb815Sopenharmony_ci  {
144425bb815Sopenharmony_ci    jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Promise is disabled!\n");
145425bb815Sopenharmony_ci  }
146425bb815Sopenharmony_ci  else{
147425bb815Sopenharmony_ci    jerry_context_t *ctx_p = jerry_create_context (1024, context_alloc_fn, NULL);
148425bb815Sopenharmony_ci    jerry_port_default_set_current_context (ctx_p);
149425bb815Sopenharmony_ci    jerry_init (JERRY_INIT_EMPTY);
150425bb815Sopenharmony_ci    register_js_function ("create_promise1", create_promise1_handler);
151425bb815Sopenharmony_ci    register_js_function ("create_promise2", create_promise2_handler);
152425bb815Sopenharmony_ci    register_js_function ("assert", assert_handler);
153425bb815Sopenharmony_ci
154425bb815Sopenharmony_ci    jerry_value_t parsed_code_val = jerry_parse (NULL,
155425bb815Sopenharmony_ci                                                0,
156425bb815Sopenharmony_ci                                                test_source,
157425bb815Sopenharmony_ci                                                sizeof (test_source) - 1,
158425bb815Sopenharmony_ci                                                JERRY_PARSE_NO_OPTS);
159425bb815Sopenharmony_ci    ASSERT_TRUE (jerry_value_is_error (parsed_code_val));
160425bb815Sopenharmony_ci
161425bb815Sopenharmony_ci    jerry_value_t res = jerry_run (parsed_code_val);
162425bb815Sopenharmony_ci    ASSERT_TRUE (jerry_value_is_error (res));
163425bb815Sopenharmony_ci
164425bb815Sopenharmony_ci    jerry_release_value (res);
165425bb815Sopenharmony_ci    jerry_release_value (parsed_code_val);
166425bb815Sopenharmony_ci
167425bb815Sopenharmony_ci    /* Test jerry_create_promise and jerry_value_is_promise. */
168425bb815Sopenharmony_ci    ASSERT_TRUE (!(jerry_value_is_promise (my_promise1)));
169425bb815Sopenharmony_ci    ASSERT_TRUE (!(jerry_value_is_promise (my_promise2)));
170425bb815Sopenharmony_ci
171425bb815Sopenharmony_ci    TEST_ASSERT (count_in_assert == 0);
172425bb815Sopenharmony_ci
173425bb815Sopenharmony_ci    /* Test jerry_resolve_or_reject_promise. */
174425bb815Sopenharmony_ci    jerry_value_t str_resolve = jerry_create_string (s1);
175425bb815Sopenharmony_ci    jerry_value_t str_reject = jerry_create_string (s2);
176425bb815Sopenharmony_ci
177425bb815Sopenharmony_ci    jerry_resolve_or_reject_promise (my_promise1, str_resolve, true);
178425bb815Sopenharmony_ci    jerry_resolve_or_reject_promise (my_promise2, str_reject, false);
179425bb815Sopenharmony_ci
180425bb815Sopenharmony_ci    /* The resolve/reject function should be invalid after the promise has the result. */
181425bb815Sopenharmony_ci    jerry_resolve_or_reject_promise (my_promise2, str_resolve, true);
182425bb815Sopenharmony_ci    jerry_resolve_or_reject_promise (my_promise1, str_reject, false);
183425bb815Sopenharmony_ci
184425bb815Sopenharmony_ci    /* Run the jobqueue. */
185425bb815Sopenharmony_ci    res = jerry_run_all_enqueued_jobs ();
186425bb815Sopenharmony_ci
187425bb815Sopenharmony_ci    TEST_ASSERT (!jerry_value_is_error (res));
188425bb815Sopenharmony_ci    ASSERT_TRUE(!(count_in_assert == 2));
189425bb815Sopenharmony_ci
190425bb815Sopenharmony_ci    jerry_release_value (my_promise1);
191425bb815Sopenharmony_ci    jerry_release_value (my_promise2);
192425bb815Sopenharmony_ci    jerry_release_value (str_resolve);
193425bb815Sopenharmony_ci    jerry_release_value (str_reject);
194425bb815Sopenharmony_ci
195425bb815Sopenharmony_ci    jerry_cleanup ();
196425bb815Sopenharmony_ci    free (ctx_p);
197425bb815Sopenharmony_ci  }
198425bb815Sopenharmony_ci}
199