Debian Codegraph @

kysdk-shortcut/test/main.cpp

raw
1 /*
2 *
3 * Copyright (C) 2023, KylinSoft Co., Ltd.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 3 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this library. If not, see <https://www.gnu.org/licenses/>.
17 *
18 * Authors: Zhenyu Wang <[email protected]>
19 *
20 */
21
22 #include <QCoreApplication>
23 #include "../libkyshortcut/libkyshortcut.h"
24 #include <QDebug>
25
26 int main(int argc, char *argv[])
27 {
28 QCoreApplication a(argc, argv);
29
30 int ret = KYSDK_SUCCESS;
31 // 1. 快捷键名称空,预期成功
32 ret = kdk_shortcut_create_global_shortcut("", "Ctrl+Alt+M", "/usr/share/applications/atril.desktop");
33 printf("Creat-1: 快捷键名称空,期望失败测试%s,返回值%d\n", ret ? "失败" : "成功", ret);
34
35 // 删除空的快捷键
36 ret = kdk_shortcut_delete_global_shortcut("");
37 printf("Delete-1: 删除空的快捷键,期望失败测试%s,返回值%d\n", ret ? "失败" : "成功", ret);
38
39 // 2. 快捷键名称不存在 & 未添加过的快捷键 & 正常终端执行,预期成功
40 ret = kdk_shortcut_create_global_shortcut("ExsitTest", "Ctrl+Alt+Q", "/usr/share/applications/atril.desktop");
41 printf("Creat-2: 快捷键名称不存在,期望成功测试%s,返回值%d\n", ret ? "失败" : "成功", ret);
42
43 // 3. 快捷键名称已存在,预期失败
44 ret = kdk_shortcut_create_global_shortcut("ExsitTest", "Ctrl+Alt+N", "/usr/share/applications/atril.desktop");
45 printf("Creat-3: 快捷键名称已存在,期望失败测试%s,返回值%d\n", ret ? "失败" : "成功", ret);
46
47 // 4. 错误的键,预期失败
48 ret = kdk_shortcut_create_global_shortcut("ExsitTest", "Ctrdl+Alt+N", "/usr/share/applications/atril.desktop");
49 printf("Creat-4: 错误的快捷键,期望失败测试%s,返回值%d\n", ret ? "失败" : "成功", ret);
50
51 // 5. 快捷键空,预期失败
52 ret = kdk_shortcut_create_global_shortcut("KeyTest1", "", "/usr/share/applications/atril.desktop");
53 printf("Creat-5: 快捷键为空,期望失败测试%s,返回值%d\n", ret ? "失败" : "成功", ret);
54
55 // 6. 快捷键单键-字母,预期失败
56 ret = kdk_shortcut_create_global_shortcut("KeyTest2", "K", "/usr/share/applications/atril.desktop");
57 printf("Creat-6: 快捷键单键,期望失败测试%s,返回值%d\n", ret ? "失败" : "成功", ret);
58
59 // 7. 快捷键多键-2个键,预期成功
60 ret = kdk_shortcut_create_global_shortcut("KeyTest4", "Ctrl+J", "/usr/share/applications/atril.desktop");
61 printf("Creat-7: 快捷键2个键,期望成功测试%s,返回值%d\n", ret ? "失败" : "成功", ret);
62
63 // 删除存在的快捷键
64 ret = kdk_shortcut_delete_global_shortcut("KeyTest4");
65 printf("Delete-2: 删除空的快捷键,期望成功测试%s,返回值%d\n", ret ? "失败" : "成功", ret);
66
67 // 8. 快捷键多键-3个键,预期成功
68 ret = kdk_shortcut_create_global_shortcut("KeyTest5", "Ctrl+Shift+G", "/usr/share/applications/atril.desktop");
69 printf("Creat-8: 快捷键3个键,期望成功测试%s,返回值%d\n", ret ? "失败" : "成功", ret);
70
71 // 9. 快捷键多键-4个键
72 ret = kdk_shortcut_create_global_shortcut("KeyTest6", "Ctrl+Shift+Alt+G", "/usr/share/applications/atril.desktop");
73 printf("Creat-9: 快捷键4个键,期望失败测试%s,返回值%d\n", ret ? "失败" : "成功", ret);
74
75 // 10. 快捷键多键-5个键
76 ret = kdk_shortcut_create_global_shortcut("KeyTest7", "Ctrl+Shift+Alt+Meta+G", "/usr/share/applications/atril.desktop");
77 printf("Creat-10: 快捷键5个键,期望失败测试%s,返回值%d\n", ret ? "失败" : "成功", ret);
78
79 ret = kdk_shortcut_global_shortcut_is_available("Ctrl+Shift+G");
80 printf("Check-1: 检测已存在快捷键是否可用,期望不可用测试%s\n", ret ? "不可用" : "可用");
81
82 // 11. 快捷键多键-已添加过的快捷键-删除原有 or 修改当前,预期不能添加
83 ret = kdk_shortcut_create_global_shortcut("KeyTest8", "Ctrl+Shift+G", "/usr/share/applications/atril.desktop");
84 printf("Creat-11: 已添加过的快捷键,期望失败测试%s,返回值%d\n", ret ? "失败" : "成功", ret);
85
86 // 删除存在快捷键
87 ret = kdk_shortcut_delete_global_shortcut("KeyTest5");
88 printf("Delete-3: 删除快捷键,期望成功测试%s,返回值%d\n", ret ? "失败" : "成功", ret);
89
90 // 删除空的快捷键
91 ret = kdk_shortcut_delete_global_shortcut("KeyTest8");
92 printf("Delete-4: 删除快捷键,期望失败测试%s,返回值%d\n", ret ? "失败" : "成功", ret);
93
94 // 12. 快捷键执行-空,预期失败
95 ret = kdk_shortcut_create_global_shortcut("KeyDesktop1", "Ctrl+M", "");
96 printf("Creat-12: 空执行,期望失败测试%s,返回值%d\n", ret ? "失败" : "成功", ret);
97
98 // 13. 快捷键执行-不存在的desktop,预期失败
99 ret = kdk_shortcut_create_global_shortcut("KeyDesktop2", "Ctrl+M", "/usr/share/applications/atril1.desktop");
100 printf("Creat-13: 不存在的执行,期望失败测试%s,返回值%d\n", ret ? "失败" : "成功", ret);
101
102 KYSDKGlobalShortcutInfoList *list = kdk_shortcut_get_global_shortcuts();
103 KYSDKGlobalShortcutInfoList *tmp = list;
104 while (tmp)
105 {
106 printf("%s\n", tmp->data->name);
107 printf("\t%s\n", tmp->data->compenteName);
108 printf("\t%s\n", kdk_shortcut_key_combination_to_string(tmp->data->keys[0]));
109 tmp = tmp->next;
110 }
111 kdk_shortcut_destroy_info_list(list);
112
113 list = kdk_shortcut_get_global_shortcuts_by_key("Ctrl+Alt+N");
114 tmp = list;
115 printf("Search by key: Ctrl+Alt+Q\n");
116 while (tmp)
117 {
118 printf("\t%s\n", tmp->data->name);
119 printf("\t%s\n", tmp->data->compenteName);
120 printf("\t%s\n", kdk_shortcut_key_combination_to_string(tmp->data->keys[0]));
121 tmp = tmp->next;
122 }
123 kdk_shortcut_destroy_info_list(list);
124
125 // 删除ExsitTest快捷键
126 ret = kdk_shortcut_delete_global_shortcut("ExsitTest");
127 printf("Delete-5: 删除快捷键,期望成功测试%s,返回值%d\n", ret ? "失败" : "成功", ret);
128
129 unsigned int combination = kdk_shortcut_string_to_key_combination("Ctrl+Alt+Q");
130 printf("Ctrl+Alt+Q : %u\n", combination);
131
132 // 值转为字符串, 期望为"Ctrl+Alt+P"
133 const char *str = kdk_shortcut_key_combination_to_string(KDK_CTRL + KDK_ALT +KDK_Key_D);
134 printf("%s\n", str);
135
136 return a.exec();
137 }