-
-
Notifications
You must be signed in to change notification settings - Fork 382
Expand file tree
/
Copy pathContextMenuTest.cs
More file actions
364 lines (321 loc) · 12.3 KB
/
ContextMenuTest.cs
File metadata and controls
364 lines (321 loc) · 12.3 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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License
// See the LICENSE file in the project root for more information.
// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone
using AngleSharp.Dom;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Options;
using System.Diagnostics;
namespace UnitTest.Components;
public class ContextMenuTest : BootstrapBlazorTestBase
{
[Fact]
public async Task ContextMenu_Ok()
{
var localizer = Context.Services.GetRequiredService<IStringLocalizer<Foo>>();
var foo = Foo.Generate(localizer);
var clicked = false;
var cut = Context.Render<ContextMenuZone>(pb =>
{
pb.AddChildContent<ContextMenuTrigger>(pb =>
{
pb.Add(a => a.WrapperTag, "div");
pb.Add(a => a.ContextItem, foo);
pb.Add(a => a.IsInvisibleWhenTouchMove, true);
pb.AddChildContent(pb =>
{
pb.OpenElement(0, "div");
pb.AddAttribute(1, "class", "context-trigger");
pb.AddContent(1, foo.Name);
pb.CloseElement();
});
});
pb.AddChildContent<ContextMenu>(pb =>
{
pb.Add(a => a.ShowShadow, true);
pb.AddChildContent<ContextMenuItem>(pb =>
{
pb.Add(a => a.Icon, "fa fa-test");
pb.Add(a => a.Text, "Test");
pb.Add(a => a.Disabled, true);
pb.Add(a => a.OnClick, (item, value) =>
{
clicked = true;
return Task.CompletedTask;
});
});
});
});
var row = cut.Find(".context-trigger");
row.ContextMenu(new MouseEventArgs() { Detail = 0, ScreenX = 10, ScreenY = 10, ClientX = 10, ClientY = 10, Button = 2, Buttons = 2 });
var menu = cut.FindComponent<ContextMenu>();
menu.Contains("shadow");
var pi = typeof(ContextMenu).GetField("_contextItem", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
Assert.NotNull(pi);
var v = pi.GetValue(menu.Instance);
Assert.NotNull(v);
var item = menu.Find(".dropdown-item");
Assert.DoesNotContain("blazor:onclick", item.InnerHtml);
var contextItem = cut.FindComponent<ContextMenuItem>();
contextItem.Render(pb =>
{
pb.Add(a => a.Disabled, false);
pb.Add(a => a.OnDisabledCallback, (item, v) =>
{
return true;
});
});
item = menu.Find(".dropdown-item");
Assert.DoesNotContain("blazor:onclick", item.InnerHtml);
// trigger OnBeforeShowCallback
bool menuCallback = false;
contextItem.Render(pb =>
{
pb.Add(a => a.Disabled, false);
pb.Add(a => a.OnDisabledCallback, (item, v) =>
{
menuCallback = true;
return false;
});
});
menu.Render(pb =>
{
pb.Add(a => a.OnBeforeShowCallback, v =>
{
return Task.CompletedTask;
});
});
item = menu.Find(".dropdown-item");
item.Click();
Assert.True(menuCallback);
// 测试 Touch 事件
TriggerTouchStart(row);
await Task.Delay(500);
row.TouchEnd();
Assert.True(clicked);
// 触发 TouchMove 事件
row.TouchMove();
}
[Theory]
[InlineData(true, true)]
[InlineData(false, false)]
public void IsShow_Ok(bool show, bool expected)
{
var cut = Context.Render<ContextMenuZone>(pb =>
{
pb.AddChildContent<ContextMenu>(pb =>
{
pb.AddChildContent<ContextMenuItem>(pb =>
{
pb.Add(a => a.Icon, "fa fa-test");
pb.Add(a => a.Text, "Test1");
pb.Add(a => a.IsShow, show);
});
pb.AddChildContent<ContextMenuDivider>(pb =>
{
pb.Add(a => a.IsShow, show);
});
pb.AddChildContent<ContextMenuItem>(pb =>
{
pb.Add(a => a.Icon, "fa fa-test");
pb.Add(a => a.Text, "Test2");
});
});
});
var actual = cut.Markup.Contains("Test1");
Assert.Equal(expected, actual);
}
[Fact]
public async Task ContextMenu_TouchWithTimeout_Ok()
{
const int Delay = 300; // ms
var localizer = Context.Services.GetRequiredService<IStringLocalizer<Foo>>();
var foo = Foo.Generate(localizer);
var cut = Context.Render<ContextMenuZone>(x =>
{
x.AddChildContent<ContextMenuTrigger>(y =>
{
y.Add(z => z.ContextItem, foo);
y.Add(z => z.OnTouchDelay, Delay);
y.AddChildContent(z =>
{
z.OpenElement(0, "div");
z.AddAttribute(1, "class", "context-trigger");
z.AddContent(2, foo.Name);
z.CloseElement();
});
});
x.AddChildContent<ContextMenu>(y =>
{
y.AddChildContent<ContextMenuItem>(z =>
{
z.Add(a => a.Icon, "fa fa-test");
z.Add(a => a.Text, "Test");
z.Add(a => a.OnClick, (_, _) => Task.CompletedTask);
});
});
});
var element = cut.Find(".context-trigger");
var sw = Stopwatch.StartNew();
await element.TouchStartAsync(new TouchEventArgs
{
Detail = 0,
Touches = [new TouchPoint { ClientX = 10, ClientY = 10, ScreenX = 10, ScreenY = 10 }]
});
var trigger = cut.FindComponent<ContextMenuTrigger>().Instance;
Assert.NotNull(trigger);
Assert.True(trigger.IsTouchStarted);
await cut.InvokeAsync(() => element.TouchEndAsync());
sw.Stop();
Assert.True(sw.ElapsedMilliseconds >= Delay * 2);
Assert.False(trigger.IsTouchStarted);
}
[Theory]
[InlineData(TableRenderMode.Table)]
[InlineData(TableRenderMode.CardView)]
public async Task ContextMenu_Table(TableRenderMode renderMode)
{
var localizer = Context.Services.GetRequiredService<IStringLocalizer<Foo>>();
var items = Foo.GenerateFoo(localizer, 2);
var clicked = false;
var cut = Context.Render<ContextMenuZone>(pb =>
{
pb.AddChildContent<Table<Foo>>(pb =>
{
pb.Add(a => a.RenderMode, renderMode);
pb.Add(a => a.Items, items);
pb.Add(a => a.TableColumns, foo => builder =>
{
builder.OpenComponent<TableColumn<Foo, string>>(0);
builder.AddAttribute(1, "Field", "Name");
builder.AddAttribute(2, "FieldExpression", Utility.GenerateValueExpression(foo, "Name", typeof(string)));
builder.CloseComponent();
});
});
pb.AddChildContent<ContextMenu>(pb =>
{
pb.AddChildContent<ContextMenuItem>(pb =>
{
pb.Add(a => a.Icon, "fa fa-test");
pb.Add(a => a.Text, "Test");
pb.Add(a => a.Disabled, false);
pb.Add(a => a.OnClick, (item, value) =>
{
clicked = true;
return Task.CompletedTask;
});
});
});
});
var row = renderMode == TableRenderMode.CardView ? cut.Find(".table-row") : cut.Find("tbody tr");
row.ContextMenu(new MouseEventArgs() { Detail = 0, ScreenX = 10, ScreenY = 10, ClientX = 10, ClientY = 10, Button = 2, Buttons = 2 });
var menu = cut.FindComponent<ContextMenu>();
var pi = typeof(ContextMenu).GetField("_contextItem", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
Assert.NotNull(pi);
var v = pi.GetValue(menu.Instance);
Assert.NotNull(v);
var item = menu.Find(".dropdown-item");
item.Click();
Assert.True(clicked);
TriggerTouchStart(row);
TriggerTouchStart(row);
var options = Context.Services.GetRequiredService<IOptions<BootstrapBlazorOptions>>();
await Task.Delay(100 + options.Value.ContextMenuOptions.OnTouchDelay);
row.TouchEnd();
}
[Fact]
public async Task ContextMenu_TreeView()
{
var items = new List<TreeFoo>
{
new() { Text = "Node1", Id = "1010" },
new() { Text = "Node2", Id = "1020" },
new() { Text = "Node11", Id = "1011", ParentId = "1010" },
new() { Text = "Node12", Id = "1011", ParentId = "1010" },
new() { Text = "Node21", Id = "1021", ParentId = "1020" },
new() { Text = "Node22", Id = "1021", ParentId = "1020" }
};
// 根节点
var nodes = TreeFoo.CascadingTree(items).ToList();
var clicked = false;
var cut = Context.Render<ContextMenuZone>(pb =>
{
pb.AddChildContent<TreeView<TreeFoo>>(pb =>
{
pb.Add(a => a.Items, nodes);
});
pb.AddChildContent<ContextMenu>(pb =>
{
pb.AddChildContent<ContextMenuItem>(pb =>
{
pb.Add(a => a.Icon, "fa fa-test");
pb.Add(a => a.Text, "Test");
pb.Add(a => a.Disabled, false);
pb.Add(a => a.OnClick, (item, value) =>
{
clicked = true;
return Task.CompletedTask;
});
});
});
});
var row = cut.Find(".tree-content");
row.ContextMenu(new MouseEventArgs() { Detail = 0, ScreenX = 10, ScreenY = 10, ClientX = 10, ClientY = 10, Button = 2, Buttons = 2 });
var menu = cut.FindComponent<ContextMenu>();
var pi = typeof(ContextMenu).GetField("_contextItem", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
Assert.NotNull(pi);
var v = pi.GetValue(menu.Instance);
Assert.NotNull(v);
var item = menu.Find(".dropdown-item");
item.Click();
Assert.True(clicked);
var options = Context.Services.GetRequiredService<IOptionsMonitor<BootstrapBlazorOptions>>();
options.CurrentValue.ContextMenuOptions.OnTouchDelay = 100;
TriggerTouchStart(row);
TriggerTouchStart(row);
await Task.Delay(100 + 2 * options.CurrentValue.ContextMenuOptions.OnTouchDelay);
row.TouchEnd();
}
private static void TriggerTouchStart(IElement row)
{
row.TouchStart(new TouchEventArgs()
{
Touches =
[
new()
{
ClientX = 10,
ClientY = 10,
ScreenX = 10,
ScreenY = 10
}
]
});
}
[Fact]
public void ContextMenuDivider_Ok()
{
var cut = Context.Render<ContextMenuZone>(pb =>
{
pb.AddChildContent<ContextMenu>(pb =>
{
pb.AddChildContent<ContextMenuItem>(builder =>
{
builder.Add(a => a.Text, "Item1");
});
pb.AddChildContent<ContextMenuDivider>();
pb.AddChildContent<ContextMenuItem>(builder =>
{
builder.Add(a => a.Text, "Item2");
});
});
});
var menu = cut.Find(".dropdown-menu");
var children = menu.Children;
Assert.Equal(3, children.Length);
Assert.Equal("dropdown-item", children[0].ClassName);
Assert.Equal("divider", children[1].ClassName);
Assert.Equal("dropdown-item", children[2].ClassName);
}
}