Debian Codegraph @

src/tests/tests_drag_n_drop.cpp

raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 #include "test_utils.h"
4 #include "tests.h"
5 #include <QFile>
6 #include <QRegularExpression>
7 #include <QSettings>
8
9 #include "common/config.h"
10
11 #define DRAG(DRAG_PARENT, SOURCE, TARGET) do { \
12 KEYS("mouse|PRESS|" SOURCE); \
13 KEYS("mouse|DRAG|" SOURCE); \
14 KEYS("isDraggingFrom|" DRAG_PARENT); \
15 KEYS("mouse|RELEASE|" TARGET); \
16 } while(false)
17
18 #define ITEM(TEXT) "item|text=" TEXT
19
20 #define TAB(TEXT) "tab_tree_item|text=" TEXT
21
22 void CoreTests::dragNDropItemOrder()
23 {
24 SKIP_ON_ENV("COPYQ_TESTS_SKIP_DRAG_AND_DROP");
25
26 RUN("config" << "show_simple_items" << "true", "true\n");
27
28 RUN("add" << "ITEM3" << "ITEM2" << "ITEM1", "");
29 RUN("read(0,1,2,3)", "ITEM1\nITEM2\nITEM3\n");
30
31 DRAG("ClipboardBrowser", ITEM("ITEM1"), ITEM("ITEM1"));
32 RUN("read(0,1,2,3)", "ITEM1\nITEM2\nITEM3\n");
33
34 DRAG("ClipboardBrowser", ITEM("ITEM1"), ITEM("ITEM2"));
35 RUN("read(0,1,2,3)", "ITEM1\nITEM2\nITEM3\n");
36
37 DRAG("ClipboardBrowser", ITEM("ITEM1"), ITEM("ITEM3"));
38 RUN("read(0,1,2,3)", "ITEM2\nITEM1\nITEM3\n");
39 }
40
41 void CoreTests::dragNDropItemToTabTree()
42 {
43 SKIP_ON_ENV("COPYQ_TESTS_SKIP_DRAG_AND_DROP");
44
45 RUN("config" << "tab_tree" << "true", "true\n");
46 RUN("config" << "show_simple_items" << "true", "true\n");
47 RUN("config('tabs', ['TAB1','TAB2'])", "TAB1\nTAB2\n");
48
49 RUN("tab" << "TAB1" << "add" << "ITEM0", "");
50 RUN("tab" << "TAB2" << "add" << "ITEM3" << "ITEM2" << "ITEM1", "");
51 RUN("tab" << "TAB2" << "selectItems" << "1" << "2", "true\n");
52 RUN("setCurrentTab('TAB2')", "");
53
54 DRAG("ClipboardBrowser", ITEM("ITEM2"), TAB("TAB1"));
55 RUN("tab" << "TAB1" << "read(0,1,2,3)", "ITEM2\nITEM3\nITEM0\n");
56 RUN("tab" << "TAB2" << "read(0,1,2,3)", "ITEM1\n\n\n");
57 }
58
59 void CoreTests::dragNDropTreeTab()
60 {
61 SKIP_ON_ENV("COPYQ_TESTS_SKIP_DRAG_AND_DROP");
62
63 RUN("config" << "tab_tree" << "true", "true\n");
64 RUN("config('tabs', ['TAB1','TAB2'])", "TAB1\nTAB2\n");
65 WAIT_ON_OUTPUT("tab", "TAB1\nTAB2\nCLIPBOARD\n");
66
67 DRAG("tab_tree", TAB("TAB2"), TAB("TAB1"));
68 RUN("tab", "TAB1\nTAB1/TAB2\nCLIPBOARD\n");
69 }
70
71 void CoreTests::dragNDropTreeTabNested()
72 {
73 SKIP_ON_ENV("COPYQ_TESTS_SKIP_DRAG_AND_DROP");
74
75 RUN("config" << "tab_tree" << "true", "true\n");
76 RUN("config('tabs', ['a/b/c/d','a/b/c'])", "a/b/c/d\na/b/c\n");
77 WAIT_ON_OUTPUT("tab", "a/b/c/d\na/b/c\nCLIPBOARD\n");
78
79 DRAG("tab_tree", TAB("c"), TAB("a"));
80 RUN("tab", "a/c\na/c/d\nCLIPBOARD\n");
81 }
82
83 void CoreTests::dragNDropTreeTabChildCollision()
84 {
85 SKIP_ON_ENV("COPYQ_TESTS_SKIP_DRAG_AND_DROP");
86
87 RUN("config" << "tab_tree" << "true", "true\n");
88
89 // Create tabs where dragging group 'a' under 'b' would make
90 // child 'a/x' collide with existing 'b/a/x'.
91 RUN("config('tabs', ['a/x','b/a/x'])", "a/x\nb/a/x\n");
92 WAIT_ON_OUTPUT("tab", "a/x\nb/a/x\nCLIPBOARD\n");
93
94 // Add distinct data to each tab so we can verify nothing is lost.
95 RUN("tab" << "a/x" << "add" << "DATA_AX", "");
96 RUN("tab" << "b/a/x" << "add" << "DATA_BAX", "");
97
98 // Drag group 'a' onto 'b' — child 'a/x' would become 'b/a/x'
99 // which already exists, so the moved tab must be renamed to
100 // avoid collision (e.g. 'b/a (1)/x').
101 DRAG("tab_tree", TAB("a"), TAB("b"));
102
103 // Both tabs' data must survive.
104 // The original 'b/a/x' must keep its data.
105 RUN("tab" << "b/a/x" << "read(0)", "DATA_BAX");
106
107 // The moved tab was renamed to avoid collision;
108 // verify it exists under a unique name and still has its data.
109 QByteArray tabsOutput;
110 TEST( m_test->getClientOutput(Args() << "tab", &tabsOutput) );
111 const auto tabNames = QString::fromUtf8(tabsOutput).split('\n');
112
113 // Find the renamed tab (not 'b/a/x', not 'CLIPBOARD', not empty).
114 bool foundMovedTab = false;
115 for (const auto &tabName : tabNames) {
116 if (tabName.isEmpty() || tabName == "b/a/x" || tabName == "CLIPBOARD")
117 continue;
118 // The renamed tab should be under 'b/' and contain the original data.
119 QVERIFY2(tabName.startsWith("b/"),
120 qPrintable("Moved tab should be under 'b/' but is: " + tabName));
121 RUN("tab" << tabName << "read(0)", "DATA_AX");
122 foundMovedTab = true;
123 }
124 QVERIFY2(foundMovedTab, "Moved tab 'a/x' should exist under a unique name after collision");
125 }
126
127 void CoreTests::dragNDropTreeTabPartialRenameFailure()
128 {
129 SKIP_ON_ENV("COPYQ_TESTS_SKIP_DRAG_AND_DROP");
130
131 RUN("config" << "tab_tree" << "true", "true\n");
132
133 // Two child tabs under 'a' plus an unrelated tab under 'b'
134 // so group 'b' exists in the tree.
135 RUN("config('tabs', ['a/x','a/y','b/z'])", "a/x\na/y\nb/z\n");
136 WAIT_ON_OUTPUT("tab", "a/x\na/y\nb/z\nCLIPBOARD\n");
137
138 // Add data to each tab.
139 RUN("tab" << "a/x" << "add" << "DATA_AX", "");
140 RUN("tab" << "a/y" << "add" << "DATA_AY", "");
141 RUN("tab" << "b/z" << "add" << "DATA_BZ", "");
142
143 // Plant a blocker file at the destination path for 'b/a/y'.
144 // QFile::copy() will refuse to overwrite the existing file, making
145 // moveItems() fail for that one tab while 'a/x' -> 'b/a/x' succeeds.
146 const QString blockerPath = getConfigurationFilePath("_tab_")
147 + QString::fromUtf8(QByteArray("b/a/y").toBase64()).replace('/', '-')
148 + QStringLiteral(".dat");
149 QVERIFY(QFile(blockerPath).open(QIODevice::WriteOnly));
150
151 // The ERROR log from moveItems() and the rollback warning are expected.
152 m_test->ignoreErrors(QRegularExpression(
153 "Failed to move.*Destination file exists|Rolling back tab renames"));
154
155 // Drag group 'a' onto 'b'.
156 // No tree-level group collision (b has no child 'a'), so renameToUnique
157 // does NOT fire. moveItems will fail for 'a/y' \u2192 'b/a/y' because the
158 // destination .dat file already exists.
159 DRAG("tab_tree", TAB("a"), TAB("b"));
160 RUN("tab", "a/x\na/y\nb/z\nCLIPBOARD\n");
161
162 // With all-or-nothing rollback, the entire group move is reverted.
163 // Both 'a/x' and 'a/y' remain at their original paths with data intact.
164
165 // Remove the blocker file before restart.
166 QVERIFY(QFile::remove(blockerPath));
167
168 TEST( m_test->stopServer() );
169 TEST( m_test->startServer() );
170 RUN("show", "");
171
172 // All tabs must be at their original names with data intact.
173 RUN("tab" << "a/x" << "read(0)", "DATA_AX");
174 RUN("tab" << "a/y" << "read(0)", "DATA_AY");
175 RUN("tab" << "b/z" << "read(0)", "DATA_BZ");
176
177 RUN("tab", "a/x\na/y\nb/z\nCLIPBOARD\n");
178 RUN("config('tabs')", "a/x\na/y\nb/z\nCLIPBOARD\n");
179 }
180
181 void CoreTests::dragNDropTreeTabKeepsCollapsedState()
182 {
183 SKIP_ON_ENV("COPYQ_TESTS_SKIP_DRAG_AND_DROP");
184
185 RUN("config" << "tab_tree" << "true", "true\n");
186 RUN("config('tabs', ['a/b','a/c','x'])", "a/b\na/c\nx\n");
187 WAIT_ON_OUTPUT("tab", "a/b\na/c\nx\nCLIPBOARD\n");
188
189 // Stop the server, inject collapsed state for group 'a', and restart.
190 const QString tabsIniPath = getConfigurationFilePath("_tabs.ini");
191 TEST( m_test->stopServer() );
192 {
193 QSettings settings(tabsIniPath, QSettings::IniFormat);
194 settings.setValue("TabWidget/collapsed_tabs", QStringList{"a"});
195 settings.sync();
196 }
197 TEST( m_test->startServer() );
198 RUN("show", "");
199
200 // Drag tab 'x' onto group 'a' \u2014 it becomes 'a/x'.
201 // Group 'a' was collapsed; it must stay collapsed after the move.
202 DRAG("tab_tree", TAB("x"), TAB("a"));
203 RUN("tab", "a/b\na/c\na/x\nCLIPBOARD\n");
204
205 // Verify collapsed state is preserved in the config file.
206 {
207 QSettings settings(tabsIniPath, QSettings::IniFormat);
208 const QStringList collapsed = settings.value("TabWidget/collapsed_tabs").toStringList();
209 QVERIFY2(collapsed.contains("a"),
210 qPrintable("Group 'a' should remain collapsed but collapsed_tabs = ["
211 + collapsed.join(", ") + "]"));
212 }
213 }