Repository navigation
Expand file tree
/
Copy pathengine_test.go
More file actions
332 lines (314 loc) · 10.7 KB
/
Copy pathengine_test.go
File metadata and controls
332 lines (314 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package main
import (
"strings"
"testing"
"time"
)
type fakeEnv struct{ gone map[string]bool }
func (f fakeEnv) PlotRect(p *Plot) (Rect, float64) { return RectWH(p.X, p.Y, p.W, p.H), 1 }
func (f fakeEnv) FileGone(path string) bool { return f.gone[path] }
func (f fakeEnv) FolderCount(p *Plot) int { return 0 }
func (f fakeEnv) IsDir(path string) bool { return strings.HasSuffix(path, "/") }
func newTestEngine() (*Engine, *Config) {
cfg := DefaultConfig()
cfg.Plots = []*Plot{
{ID: "a", Title: "A", X: 1000, Y: 0, W: 320, H: 250},
{ID: "b", Title: "B", X: 1000, Y: 400, W: 320, H: 250},
}
e := NewEngine(cfg, fakeEnv{gone: map[string]bool{}})
e.Spacing = Point{75, 100}
return e, cfg
}
func apply(items []Item, moves []Move) []Item {
for _, m := range moves {
for i := range items {
if items[i].Path == m.Path {
items[i].Pos = m.To
}
}
}
return items
}
func find(items []Item, p string) *Item {
for i := range items {
if items[i].Path == p {
return &items[i]
}
}
return nil
}
func TestDragInOutReorder(t *testing.T) {
e, cfg := newTestEngine()
items := []Item{{"x", Point{0, 0}}, {"y", Point{0, 100}}, {"z", Point{0, 200}}}
items = apply(items, e.Reconcile(items)) // first tick: nothing moves
if len(cfg.Plots[0].AllItems()) != 0 {
t.Fatal("nothing should be adopted on first tick")
}
// user drags x into plot A
find(items, "x").Pos = Point{1100, 100}
m := e.Reconcile(items)
items = apply(items, m)
if len(cfg.Plots[0].AllItems()) != 1 || cfg.Plots[0].AllItems()[0] != "x" {
t.Fatalf("x not in A: %v", cfg.Plots[0].AllItems())
}
if len(m) != 1 {
t.Fatalf("expected a snap move, got %v", m)
}
// settle tick
if m := e.Reconcile(items); len(m) != 0 {
t.Fatalf("unexpected moves %v", m)
}
// drag y into A before x (first cell)
v := e.View("a")
find(items, "y").Pos = Point{v.Grid.X, v.Grid.Y}
items = apply(items, e.Reconcile(items))
if got := cfg.Plots[0].AllItems(); len(got) != 2 || got[0] != "y" || got[1] != "x" {
t.Fatalf("order wrong: %v", got)
}
items = apply(items, e.Reconcile(items))
// drag x to plot B
find(items, "x").Pos = Point{1050, 470}
items = apply(items, e.Reconcile(items))
if len(cfg.Plots[1].AllItems()) != 1 || len(cfg.Plots[0].AllItems()) != 1 {
t.Fatalf("move to B failed: %v %v", cfg.Plots[0].AllItems(), cfg.Plots[1].AllItems())
}
items = apply(items, e.Reconcile(items))
// drag y out to the open desktop
find(items, "y").Pos = Point{300, 300}
items = apply(items, e.Reconcile(items))
if len(cfg.Plots[0].AllItems()) != 0 {
t.Fatalf("y should be unassigned: %v", cfg.Plots[0].AllItems())
}
if find(items, "y").Pos != (Point{300, 300}) {
t.Fatal("y should stay where dropped")
}
}
func TestInboxAndRename(t *testing.T) {
e, cfg := newTestEngine()
cfg.InboxPlot = "b"
items := []Item{{"x", Point{0, 0}}}
items = apply(items, e.Reconcile(items))
// a new download appears somewhere (auto-placed by Explorer)
items = append(items, Item{"dl.zip", Point{0, 100}})
items = apply(items, e.Reconcile(items))
if len(cfg.Plots[1].AllItems()) != 1 {
t.Fatalf("download not in inbox: %v", cfg.Plots[1].AllItems())
}
items = apply(items, e.Reconcile(items))
// rename keeps assignment
p := find(items, "dl.zip")
p.Path = "renamed.zip"
items = apply(items, e.Reconcile(items))
if cfg.Plots[1].AllItems()[0] != "renamed.zip" {
t.Fatalf("rename lost: %v", cfg.Plots[1].AllItems())
}
// a temporarily missing item is kept unless the file is gone
items = items[:1]
items = apply(items, e.Reconcile(items))
if len(cfg.Plots[1].AllItems()) != 1 {
t.Fatal("transiently missing item dropped")
}
e.Env.(fakeEnv).gone["renamed.zip"] = true
e.Reconcile(items)
if len(cfg.Plots[1].AllItems()) != 0 {
t.Fatal("deleted item kept")
}
}
func TestUserDropBeatsInbox(t *testing.T) {
e, cfg := newTestEngine()
cfg.InboxPlot = "b"
items := []Item{{"x", Point{0, 0}}}
items = apply(items, e.Reconcile(items))
e.Clicks = []ClickHint{{Point{1150, 120}, time.Now(), 5 * time.Second}}
items = append(items, Item{"dragged.txt", Point{1150 - 37, 120 - 50}})
e.Reconcile(items)
if len(cfg.Plots[0].AllItems()) != 1 {
t.Fatalf("user drop should land in A: A=%v B=%v", cfg.Plots[0].AllItems(), cfg.Plots[1].AllItems())
}
}
func TestGrowAndAdopt(t *testing.T) {
e, cfg := newTestEngine()
cfg.GrowToFit = true
var items []Item
for i := 0; i < 12; i++ {
items = append(items, Item{string(rune('a' + i)), Point{1000 + (i%4)*75, 40 + (i/4)*70}})
}
items = apply(items, e.Reconcile(items))
e.AdoptUnder(items)
items = apply(items, e.Reconcile(items))
if len(cfg.Plots[0].AllItems()) != 12 {
t.Fatalf("adopted %d", len(cfg.Plots[0].AllItems()))
}
v := e.View("a")
if v.Rect.H() <= 250 {
t.Fatalf("plot should grow, h=%d", v.Rect.H())
}
if m := e.Reconcile(items); len(m) != 0 {
t.Fatalf("not stable: %v", m)
}
}
func TestReappearKeepsPlot(t *testing.T) {
e, cfg := newTestEngine()
cfg.InboxPlot = "b"
items := []Item{{"x", Point{0, 0}}, {"y", Point{0, 100}}, {"z", Point{0, 200}}}
items = apply(items, e.Reconcile(items))
find(items, "x").Pos = Point{1100, 100}
items = apply(items, e.Reconcile(items))
items = apply(items, e.Reconcile(items))
// Explorer view flakes: x and y vanish, then come back somewhere else
short := []Item{items[2]}
e.Reconcile(append(short, Item{"w", Point{0, 300}}))
back := []Item{{"x", Point{0, 0}}, {"y", Point{0, 100}}, items[2], {"w", Point{0, 300}}}
e.Reconcile(back)
if len(cfg.Plots[0].AllItems()) != 1 || cfg.Plots[0].AllItems()[0] != "x" {
t.Fatalf("x should stay in A: %v / inbox %v", cfg.Plots[0].AllItems(), cfg.Plots[1].AllItems())
}
for _, it := range cfg.Plots[1].AllItems() {
if it == "y" {
t.Fatal("known item y must not go to inbox")
}
}
}
func parkedCount(e *Engine, items []Item) int {
n := 0
for _, it := range items {
if it.Pos == e.ParkAt {
n++
}
}
return n
}
func TestScrollParksOverflow(t *testing.T) {
e, cfg := newTestEngine()
var items []Item
for i := 0; i < 12; i++ {
items = append(items, Item{string(rune('a' + i)), Point{1000 + (i%4)*75, 40 + (i/4)*70}})
}
items = apply(items, e.Reconcile(items))
e.AdoptUnder(items)
items = apply(items, e.Reconcile(items))
// 320 wide -> 4 cols; 250 high -> 2 rows visible; 12 icons = 3 rows
v := e.View("a")
if v.Rect.H() != 250 || v.MaxScroll != 1 {
t.Fatalf("expected fixed height with 1 row of scroll, h=%d max=%d", v.Rect.H(), v.MaxScroll)
}
if n := parkedCount(e, items); n != 4 {
t.Fatalf("4 icons should be parked, got %d", n)
}
cfg.Plots[0].Scroll = 1
items = apply(items, e.Reconcile(items))
if n := parkedCount(e, items); n != 4 {
t.Fatalf("after scrolling, 4 icons should be parked, got %d", n)
}
if find(items, "a").Pos != e.ParkAt {
t.Fatal("first row should be scrolled away")
}
}
func TestTabsAndRollup(t *testing.T) {
e, cfg := newTestEngine()
p := cfg.Plots[0]
p.Norm()
p.Tabs = []*Tab{{Name: "One", Items: []string{"x"}}, {Name: "Two", Items: []string{"y"}}}
items := []Item{{"x", Point{0, 0}}, {"y", Point{0, 100}}}
items = apply(items, e.Reconcile(items))
if find(items, "y").Pos != e.ParkAt || find(items, "x").Pos == e.ParkAt {
t.Fatalf("tab Two should be hidden: %v", items)
}
p.ActiveTab = 1
items = apply(items, e.Reconcile(items))
if find(items, "x").Pos != e.ParkAt || find(items, "y").Pos == e.ParkAt {
t.Fatalf("tab One should be hidden now: %v", items)
}
p.Collapsed = true
items = apply(items, e.Reconcile(items))
if parkedCount(e, items) != 2 || e.View("a").Rect.H() != cfg.TitleH(1) {
t.Fatal("rolled-up plot should hide everything and shrink to its title")
}
e.Peek = "a"
items = apply(items, e.Reconcile(items))
if parkedCount(e, items) != 1 {
t.Fatal("peeking should show the active tab")
}
// parked icons stay assigned through a few ticks
for i := 0; i < 3; i++ {
items = apply(items, e.Reconcile(items))
}
if len(p.AllItems()) != 2 {
t.Fatalf("items lost: %v", p.AllItems())
}
}
func TestRules(t *testing.T) {
e, cfg := newTestEngine()
cfg.InboxPlot = "b"
cfg.Plots[0].Norm()
cfg.Plots[0].SetTabNames([]string{"Docs", "Pics"})
cfg.Rules = []*Rule{{Pattern: "*.pdf;.docx", PlotID: "a", Tab: "Docs"}, {Pattern: "*.png", PlotID: "a", Tab: "pics"}}
items := []Item{{"x", Point{0, 0}}}
items = apply(items, e.Reconcile(items))
items = append(items, Item{"C:/d/Report.PDF", Point{0, 100}}, Item{"C:/d/cat.png", Point{0, 200}}, Item{"C:/d/a.zip", Point{0, 300}}, Item{"C:/d/b.docx", Point{0, 400}})
e.Reconcile(items)
a := cfg.Plots[0]
if len(a.Tabs[0].Items) != 2 || len(a.Tabs[1].Items) != 1 {
t.Fatalf("rules misrouted: %v / %v", a.Tabs[0].Items, a.Tabs[1].Items)
}
if got := cfg.Plots[1].AllItems(); len(got) != 1 || got[0] != "C:/d/a.zip" {
t.Fatalf("unmatched should go to inbox: %v", got)
}
}
func TestSetTabNamesKeepsItems(t *testing.T) {
p := &Plot{Items: []string{"1", "2"}}
p.SetTabNames([]string{"A", "B", "C"})
p.Tabs[2].Items = []string{"3"}
p.SetTabNames([]string{"A"})
if len(p.Tabs) != 1 || len(p.AllItems()) != 3 {
t.Fatalf("merge failed: %+v", p.AllItems())
}
}
func TestFolderPlotReceivesFiles(t *testing.T) {
e, cfg := newTestEngine()
cfg.Plots[1].Folder = "C:/Docs"
cfg.InboxPlot = "b"
var moved []string
e.FolderDrop = func(path string, p *Plot) { moved = append(moved, path) }
items := []Item{{"x", Point{0, 0}}}
items = apply(items, e.Reconcile(items))
items = append(items, Item{"new.pdf", Point{0, 100}})
e.Reconcile(items)
if len(moved) != 1 || len(cfg.Plots[1].AllItems()) != 0 {
t.Fatalf("new item should be moved into the folder, got %v / %v", moved, cfg.Plots[1].AllItems())
}
}
func TestMergeAndSplitTabs(t *testing.T) {
c := DefaultConfig()
a := &Plot{ID: "a", Title: "Work", Tabs: []*Tab{{Items: []string{"w1"}}}}
b := &Plot{ID: "b", Title: "Games", Tabs: []*Tab{{Name: "PC", Items: []string{"g1"}}, {Name: "Retro", Items: []string{"g2"}}}}
f := &Plot{ID: "f", Title: "Docs", Folder: `C:\Docs`}
c.Plots = []*Plot{a, b, f}
c.InboxPlot = "b"
c.Rules = []*Rule{{Pattern: "*.exe", PlotID: "b", Tab: "Retro"}}
if c.MergePlots("a", "f") {
t.Fatal("merged into a live folder")
}
if !c.MergePlots("b", "a") {
t.Fatal("merge failed")
}
if c.Plot("b") != nil || len(a.Tabs) != 3 || a.Tabs[0].Name != "Work" || a.Tabs[2].Name != "Retro" || a.ActiveTab != 1 {
t.Fatalf("bad merge: %+v", a.TabNames())
}
if c.InboxPlot != "a" || c.Rules[0].PlotID != "a" || c.Rules[0].Tab != "Retro" {
t.Fatalf("inbox/rules not moved: %s %+v", c.InboxPlot, c.Rules[0])
}
np := c.SplitTab("a", 2)
if np == nil || np.Title != "Retro" || np.Tabs[0].Items[0] != "g2" || len(a.Tabs) != 2 {
t.Fatalf("bad split: %+v", np)
}
if c.Rules[0].PlotID != np.ID {
t.Fatal("rule didn't follow the split tab")
}
if !a.MoveTab(1, 0) || a.Tabs[0].Name != "PC" || a.ActiveTab != 0 {
t.Fatalf("bad move: %v", a.TabNames())
}
if c.SplitTab("f", 0) != nil {
t.Fatal("split a live folder")
}
}