• 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 #include "list_close_all_action_test.h"
16 
17 #include <arkui/native_interface.h>
18 #include <thread>
19 
20 #include "../manager/plugin_manager.h"
21 #include "component/button_component.h"
22 #include "component/column_component.h"
23 #include "component/list_component.h"
24 #include "component/row_component.h"
25 
26 namespace ArkUICapiTest {
27 using namespace std;
28 std::vector<int32_t> ListCloseAllTest::retData = {};
29 
SetArrayNapiData(const std::vector<int32_t> & data,napi_env env)30 static napi_value SetArrayNapiData(const std::vector<int32_t>& data, napi_env env)
31 {
32     napi_value array;
33     napi_create_array(env, &array);
34     for (size_t i = PARAM_0; i < data.size(); i++) {
35         napi_value num;
36         napi_create_int32(env, data[i], &num);
37         napi_set_element(env, array, i, num);
38     }
39     return array;
40 }
41 
SetArrayNapiData(const std::vector<double> & data,napi_env env)42 static napi_value SetArrayNapiData(const std::vector<double>& data, napi_env env)
43 {
44     napi_value array;
45     napi_create_array(env, &array);
46     for (size_t i = PARAM_0; i < data.size(); i++) {
47         napi_value num;
48         napi_create_double(env, data[i], &num);
49         napi_set_element(env, array, i, num);
50     }
51     return array;
52 }
53 
PushBackIntData(std::vector<int32_t> & data,int32_t value)54 static void PushBackIntData(std::vector<int32_t>& data, int32_t value)
55 {
56     if (data.empty()) {
57         data.push_back(value);
58         return;
59     }
60     if (data.back() != value) {
61         data.push_back(value);
62     }
63 }
64 
PushBackFloatData(std::vector<double> & data,double value)65 static void PushBackFloatData(std::vector<double>& data, double value)
66 {
67     if (data.empty()) {
68         if (value != PARAM_0) {
69             data.push_back(value);
70         }
71         return;
72     }
73     if ((data.back() > PARAM_0 && value < PARAM_0) || (data.back() < PARAM_0 && value > PARAM_0)) {
74         data.push_back(value);
75     }
76 }
77 
SetAction(std::shared_ptr<ListItemComponent> listItem,ArkUI_ListItemSwipeEdgeEffect setEdgeEffect=ArkUI_ListItemSwipeEdgeEffect::ARKUI_LIST_ITEM_SWIPE_EDGE_EFFECT_SPRING)78 static void SetAction(std::shared_ptr<ListItemComponent> listItem,
79     ArkUI_ListItemSwipeEdgeEffect setEdgeEffect =
80         ArkUI_ListItemSwipeEdgeEffect::ARKUI_LIST_ITEM_SWIPE_EDGE_EFFECT_SPRING)
81 {
82     auto option = new ListItemSwipeActionOption();
83     auto col1 = std::make_shared<ColumnComponent>();
84     col1->SetWidth(SIZE_30);
85     col1->SetHeight(SIZE_30);
86     col1->SetBackgroundColor(COLOR_GREEN);
87     auto col2 = std::make_shared<ColumnComponent>();
88     col2->SetWidth(SIZE_30);
89     col2->SetHeight(SIZE_30);
90     col2->SetBackgroundColor(COLOR_YELLOW);
91 
92     auto item1 = OH_ArkUI_ListItemSwipeActionItem_Create();
93     auto item2 = OH_ArkUI_ListItemSwipeActionItem_Create();
94 
95     OH_ArkUI_ListItemSwipeActionItem_SetContent(item1, col1->GetComponent());
96     OH_ArkUI_ListItemSwipeActionItem_SetContent(item2, col2->GetComponent());
97     option->SetStart(item1);
98     option->SetEnd(item2);
99     option->SetEdgeEffect(setEdgeEffect);
100     listItem->SetListItemSwiperAction(option);
101 }
102 
CreateButton(std::vector<std::shared_ptr<ListComponent>> list)103 static std::shared_ptr<RowComponent> CreateButton(std::vector<std::shared_ptr<ListComponent>> list)
104 {
105     auto row = make_shared<RowComponent>();
106     for (int i = PARAM_0; i < list.size(); i++) {
107         auto eachList = list[i];
108         auto button1 = make_shared<ButtonComponent>();
109         button1->SetWidth(SIZE_30);
110         button1->SetHeight(SIZE_30);
111         button1->SetId("callbackButton" + std::to_string(i));
112         button1->SetBackgroundColor(COLOR_GREEN);
113         button1->RegisterOnClick([eachList]() {
114             void* userData = reinterpret_cast<void*>(eachList.get());
115             auto ret = OH_ArkUI_List_CloseAllSwipeActions(eachList->GetComponent(), userData, [](void* userData) {
116                 auto list = reinterpret_cast<ListComponent*>(userData);
117                 list->SetBackgroundColor(COLOR_RED);
118             });
119             ListCloseAllTest::retData.push_back(ret);
120         });
121         auto button2 = make_shared<ButtonComponent>();
122         button2->SetWidth(SIZE_30);
123         button2->SetHeight(SIZE_30);
124         button2->SetId("nullCallbackButton" + std::to_string(i));
125         button2->SetBackgroundColor(COLOR_GREEN);
126         button2->RegisterOnClick([eachList]() {
127             auto ret = OH_ArkUI_List_CloseAllSwipeActions(eachList->GetComponent(), nullptr, nullptr);
128             ListCloseAllTest::retData.push_back(ret);
129         });
130         row->AddChild(button1);
131         row->AddChild(button2);
132     }
133     return row;
134 }
135 
GetRetData(napi_env env,napi_callback_info info)136 napi_value ListCloseAllTest::GetRetData(napi_env env, napi_callback_info info)
137 {
138     OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "ListCloseAllTest", "GetOnScrollIndexData");
139     napi_value result;
140     napi_create_array(env, &result);
141     napi_set_element(env, result, PARAM_0, SetArrayNapiData(ListCloseAllTest::retData, env));
142     ListCloseAllTest::retData.clear();
143     return result;
144 }
145 
CreateListWithAction(int32_t num,int32_t vertical,string firstItemId)146 static std::shared_ptr<ListComponent> CreateListWithAction(int32_t num, int32_t vertical, string firstItemId)
147 {
148     auto list = make_shared<ListComponent>();
149     list->SetMargin(PARAM_5);
150     list->SetWidth(SIZE_80);
151     list->SetHeight(SIZE_80);
152     list->SetListDirection(vertical);
153     list->SetListDivider(COLOR_BLACK, PARAM_2, PARAM_0, PARAM_0);
154     for (int i = PARAM_0; i < num; i++) {
155         auto listItem = make_shared<ListItemComponent>();
156         if (i == PARAM_0) {
157             listItem->SetId(firstItemId);
158         }
159         listItem->SetWidth(SIZE_40);
160         listItem->SetHeight(SIZE_40);
161         listItem->SetBackgroundColor(COLOR_BLUE);
162         SetAction(listItem);
163         list->AddChild(listItem);
164     }
165     return list;
166 }
167 
CreateNativeNodeCloseAll(napi_env env,napi_callback_info info)168 napi_value ListCloseAllTest::CreateNativeNodeCloseAll(napi_env env, napi_callback_info info)
169 {
170     OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "CreateNativeNodeCloseAll", "CreateNativeNode");
171 
172     size_t argc = PARAM_1;
173     napi_value args[PARAM_1] = { nullptr };
174     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
175     size_t length = PARAM_64;
176     size_t strLength = PARAM_0;
177     char xComponentID[PARAM_64] = { PARAM_0 };
178     napi_get_value_string_utf8(env, args[PARAM_0], xComponentID, length, &strLength);
179 
180     if ((env == nullptr) || (info == nullptr)) {
181         OH_LOG_Print(
182             LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "CreateNativeNodeCloseAll", "GetContext env or info is null");
183         return nullptr;
184     }
185 
186     ArkUI_NativeNodeAPI_1* nodeAPI = nullptr;
187     OH_ArkUI_GetModuleInterface(ARKUI_NATIVE_NODE, ArkUI_NativeNodeAPI_1, nodeAPI);
188 
189     auto column = new ColumnComponent();
190     auto list1 = CreateListWithAction(SIZE_100, ArkUI_Axis::ARKUI_AXIS_VERTICAL, "list1");
191     auto list2 = CreateListWithAction(SIZE_100, ArkUI_Axis::ARKUI_AXIS_HORIZONTAL, "list2");
192     auto row = CreateButton({ list1, list2 });
193     column->AddChild(list1);
194     column->AddChild(list2);
195     column->AddChild(row);
196 
197     std::string id(xComponentID);
198     if (OH_NativeXComponent_AttachNativeRootNode(
199         PluginManager::GetInstance()->GetNativeXComponent(id), column->GetComponent()) == INVALID_PARAM) {
200         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "CreateNativeNodeCloseAll",
201             "OH_NativeXComponent_AttachNativeRootNode failed");
202     }
203 
204     napi_value exports;
205     if (napi_create_object(env, &exports) != napi_ok) {
206         napi_throw_type_error(env, NULL, "napi_create_object failed");
207         return nullptr;
208     }
209 
210     return exports;
211 }
212 } // namespace ArkUICapiTest
213