-
Notifications
You must be signed in to change notification settings - Fork 570
/
Copy pathmod.rs
489 lines (415 loc) · 18.2 KB
/
mod.rs
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
// Copyright 2020 The Druid Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Additional unit tests that cross file or module boundaries.
pub(crate) mod harness;
mod helpers;
mod layout_tests;
use std::cell::Cell;
use std::env;
use std::fs;
use std::rc::Rc;
use crate::widget::*;
use crate::*;
use harness::*;
use helpers::*;
use kurbo::Vec2;
/// This function creates a temporary directory and returns a PathBuf to it.
///
/// This directory will be created relative to the executable and will therefor
/// be created in the target directory for tests when running with cargo. The
/// directory will be cleaned up at the end of the PathBufs lifetime. This
/// uses the `tempfile` crate.
#[allow(dead_code)]
pub fn temp_dir_for_test() -> std::path::PathBuf {
let current_exe_path = env::current_exe().unwrap();
let mut exe_dir = current_exe_path.parent().unwrap();
if exe_dir.ends_with("deps") {
exe_dir = exe_dir.parent().unwrap();
}
let test_dir = exe_dir.parent().unwrap().join("tests");
fs::create_dir_all(&test_dir).unwrap();
tempfile::Builder::new()
.prefix("TempDir")
.tempdir_in(test_dir)
.unwrap()
.into_path()
}
/// test that the first widget to request focus during an event gets it.
#[test]
fn propogate_hot() {
let (button, pad, root, empty) = widget_id4();
let root_rec = Recording::default();
let padding_rec = Recording::default();
let button_rec = Recording::default();
let widget = Split::columns(
SizedBox::empty().with_id(empty),
Button::new("hot")
.record(&button_rec)
.with_id(button)
.padding(50.)
.record(&padding_rec)
.with_id(pad),
)
.record(&root_rec)
.with_id(root);
fn move_mouse(x: f64, y: f64) -> MouseEvent {
let pos = Point::new(x, y);
MouseEvent {
pos,
window_pos: pos,
buttons: MouseButtons::default(),
mods: Modifiers::default(),
count: 0,
focus: false,
button: MouseButton::None,
wheel_delta: Vec2::ZERO,
}
}
#[allow(clippy::cognitive_complexity)]
Harness::create_simple((), widget, |harness| {
harness.send_initial_events();
harness.just_layout();
// we don't care about setup events, so discard them now.
root_rec.clear();
padding_rec.clear();
button_rec.clear();
harness.inspect_state(|state| assert!(!state.is_hot));
// What we are doing here is moving the mouse to different widgets,
// and verifying both the widget's `is_hot` status and also that
// each widget received the expected HotChanged messages.
harness.event(Event::MouseMove(move_mouse(10., 10.)));
assert!(harness.get_state(root).is_hot);
assert!(harness.get_state(empty).is_hot);
assert!(!harness.get_state(pad).is_hot);
assert_matches!(root_rec.next(), Record::L(LifeCycle::HotChanged(true)));
assert_matches!(root_rec.next(), Record::E(Event::MouseMove(_)));
assert!(root_rec.is_empty() && padding_rec.is_empty() && button_rec.is_empty());
harness.event(Event::MouseMove(move_mouse(210., 10.)));
assert!(harness.get_state(root).is_hot);
assert!(!harness.get_state(empty).is_hot);
assert!(!harness.get_state(button).is_hot);
assert!(harness.get_state(pad).is_hot);
assert_matches!(root_rec.next(), Record::E(Event::MouseMove(_)));
assert_matches!(padding_rec.next(), Record::L(LifeCycle::HotChanged(true)));
assert_matches!(padding_rec.next(), Record::E(Event::MouseMove(_)));
assert!(root_rec.is_empty() && padding_rec.is_empty() && button_rec.is_empty());
harness.event(Event::MouseMove(move_mouse(260., 60.)));
assert!(harness.get_state(root).is_hot);
assert!(!harness.get_state(empty).is_hot);
assert!(harness.get_state(button).is_hot);
assert!(harness.get_state(pad).is_hot);
assert_matches!(root_rec.next(), Record::E(Event::MouseMove(_)));
assert_matches!(padding_rec.next(), Record::E(Event::MouseMove(_)));
assert_matches!(button_rec.next(), Record::L(LifeCycle::HotChanged(true)));
assert_matches!(button_rec.next(), Record::E(Event::MouseMove(_)));
assert!(root_rec.is_empty() && padding_rec.is_empty() && button_rec.is_empty());
harness.event(Event::MouseMove(move_mouse(10., 10.)));
assert!(harness.get_state(root).is_hot);
assert!(harness.get_state(empty).is_hot);
assert!(!harness.get_state(button).is_hot);
assert!(!harness.get_state(pad).is_hot);
assert_matches!(root_rec.next(), Record::E(Event::MouseMove(_)));
assert_matches!(padding_rec.next(), Record::L(LifeCycle::HotChanged(false)));
assert_matches!(padding_rec.next(), Record::E(Event::MouseMove(_)));
assert_matches!(button_rec.next(), Record::L(LifeCycle::HotChanged(false)));
assert_matches!(button_rec.next(), Record::E(Event::MouseMove(_)));
assert!(root_rec.is_empty() && padding_rec.is_empty() && button_rec.is_empty());
});
}
#[test]
fn take_focus() {
const TAKE_FOCUS: Selector = Selector::new("druid-tests.take-focus");
/// A widget that takes focus when sent a particular command.
/// The widget records focus change events into the inner cell.
fn make_focus_taker(inner: Rc<Cell<Option<bool>>>) -> impl Widget<bool> {
ModularWidget::new(inner)
.event_fn(|_, ctx, event, _data, _env| {
if let Event::Command(cmd) = event {
if cmd.is(TAKE_FOCUS) {
ctx.request_focus();
}
}
})
.lifecycle_fn(|is_focused, _, event, _data, _env| {
if let LifeCycle::FocusChanged(focus) = event {
is_focused.set(Some(*focus));
}
})
}
let (id_1, id_2, _id_3) = widget_id3();
// we use these so that we can check the widget's internal state
let left_focus: Rc<Cell<Option<bool>>> = Default::default();
let right_focus: Rc<Cell<Option<bool>>> = Default::default();
assert!(left_focus.get().is_none());
let left = make_focus_taker(left_focus.clone()).with_id(id_1);
let right = make_focus_taker(right_focus.clone()).with_id(id_2);
let app = Split::columns(left, right).padding(5.0);
let data = true;
Harness::create_simple(data, app, |harness| {
harness.send_initial_events();
// nobody should have focus
assert!(left_focus.get().is_none());
assert!(right_focus.get().is_none());
// this is sent to all widgets; the last widget to request focus should get it
harness.submit_command(TAKE_FOCUS, None);
assert_eq!(harness.window().focus, Some(id_2));
assert_eq!(left_focus.get(), None);
assert_eq!(right_focus.get(), Some(true));
// this is sent to all widgets; the last widget to request focus should still get it
// NOTE: This tests siblings in particular, so careful when moving away from Split.
harness.submit_command(TAKE_FOCUS, None);
assert_eq!(harness.window().focus, Some(id_2));
assert_eq!(left_focus.get(), None);
assert_eq!(right_focus.get(), Some(true));
// this is sent to a specific widget; it should get focus
harness.submit_command(TAKE_FOCUS, id_1);
assert_eq!(harness.window().focus, Some(id_1));
assert_eq!(left_focus.get(), Some(true));
assert_eq!(right_focus.get(), Some(false));
// this is sent to a specific widget; it should get focus
harness.submit_command(TAKE_FOCUS, id_2);
assert_eq!(harness.window().focus, Some(id_2));
assert_eq!(left_focus.get(), Some(false));
assert_eq!(right_focus.get(), Some(true));
})
}
#[test]
fn focus_changed() {
const TAKE_FOCUS: Selector = Selector::new("druid-tests.take-focus");
const ALL_TAKE_FOCUS_BEFORE: Selector = Selector::new("druid-tests.take-focus-before");
const ALL_TAKE_FOCUS_AFTER: Selector = Selector::new("druid-tests.take-focus-after");
fn make_focus_container(children: Vec<WidgetPod<(), Box<dyn Widget<()>>>>) -> impl Widget<()> {
ModularWidget::new(children)
.event_fn(|children, ctx, event, data, env| {
if let Event::Command(cmd) = event {
if cmd.is(TAKE_FOCUS) {
ctx.request_focus();
// Stop propagating this command so children
// aren't requesting focus too.
ctx.set_handled();
} else if cmd.is(ALL_TAKE_FOCUS_BEFORE) {
ctx.request_focus();
}
}
children
.iter_mut()
.for_each(|a| a.event(ctx, event, data, env));
if let Event::Command(cmd) = event {
if cmd.is(ALL_TAKE_FOCUS_AFTER) {
ctx.request_focus();
}
}
})
.lifecycle_fn(|children, ctx, event, data, env| {
children
.iter_mut()
.for_each(|a| a.lifecycle(ctx, event, data, env));
})
}
let a_rec = Recording::default();
let b_rec = Recording::default();
let c_rec = Recording::default();
let (id_a, id_b, id_c) = widget_id3();
// a contains b which contains c
let c = make_focus_container(vec![]).record(&c_rec).with_id(id_c);
let b = make_focus_container(vec![WidgetPod::new(c).boxed()])
.record(&b_rec)
.with_id(id_b);
let a = make_focus_container(vec![WidgetPod::new(b).boxed()])
.record(&a_rec)
.with_id(id_a);
let f = |a| match a {
Record::L(LifeCycle::FocusChanged(c)) => Some(c),
_ => None,
};
let no_change = |a: &Recording| a.drain().filter_map(f).count() == 0;
let changed = |a: &Recording, b| a.drain().filter_map(f).eq(std::iter::once(b));
Harness::create_simple((), a, |harness| {
harness.send_initial_events();
// focus none -> a
harness.submit_command(TAKE_FOCUS, id_a);
assert_eq!(harness.window().focus, Some(id_a));
assert!(changed(&a_rec, true));
assert!(no_change(&b_rec));
assert!(no_change(&c_rec));
// focus a -> b
harness.submit_command(TAKE_FOCUS, id_b);
assert_eq!(harness.window().focus, Some(id_b));
assert!(changed(&a_rec, false));
assert!(changed(&b_rec, true));
assert!(no_change(&c_rec));
// focus b -> c
harness.submit_command(TAKE_FOCUS, id_c);
assert_eq!(harness.window().focus, Some(id_c));
assert!(no_change(&a_rec));
assert!(changed(&b_rec, false));
assert!(changed(&c_rec, true));
// focus c -> a
harness.submit_command(TAKE_FOCUS, id_a);
assert_eq!(harness.window().focus, Some(id_a));
assert!(changed(&a_rec, true));
assert!(no_change(&b_rec));
assert!(changed(&c_rec, false));
// all focus before passing down the event
harness.submit_command(ALL_TAKE_FOCUS_BEFORE, None);
assert_eq!(harness.window().focus, Some(id_c));
assert!(changed(&a_rec, false));
assert!(no_change(&b_rec));
assert!(changed(&c_rec, true));
// all focus after passing down the event
harness.submit_command(ALL_TAKE_FOCUS_AFTER, None);
assert_eq!(harness.window().focus, Some(id_a));
assert!(changed(&a_rec, true));
assert!(no_change(&b_rec));
assert!(changed(&c_rec, false));
})
}
#[test]
fn simple_lifecyle() {
let record = Recording::default();
let widget = SizedBox::empty().record(&record);
Harness::create_simple(true, widget, |harness| {
harness.send_initial_events();
assert_matches!(record.next(), Record::L(LifeCycle::WidgetAdded));
assert_matches!(record.next(), Record::E(Event::WindowConnected));
assert_matches!(record.next(), Record::E(Event::WindowSize(_)));
assert!(record.is_empty());
})
}
#[test]
/// Test that lifecycle events are sent correctly to a child added during event
/// handling
fn adding_child_lifecycle() {
let record = Recording::default();
let record_new_child = Recording::default();
let record_new_child2 = record_new_child.clone();
let replacer = ReplaceChild::new(TextBox::new(), move || {
Split::columns(TextBox::new(), TextBox::new().record(&record_new_child2))
});
let widget = Split::columns(Label::new("hi").record(&record), replacer);
Harness::create_simple(String::new(), widget, |harness| {
harness.send_initial_events();
assert_matches!(record.next(), Record::L(LifeCycle::WidgetAdded));
assert_matches!(record.next(), Record::E(Event::WindowConnected));
assert!(record.is_empty());
assert!(record_new_child.is_empty());
harness.submit_command(REPLACE_CHILD, None);
assert_matches!(record.next(), Record::E(Event::Command(_)));
assert_matches!(record_new_child.next(), Record::L(LifeCycle::WidgetAdded));
assert!(record_new_child.is_empty());
})
}
#[test]
fn participate_in_autofocus() {
let (id_1, id_2, id_3, id_4, id_5, id_6) = widget_id6();
// this widget starts with a single child, and will replace them with a split
// when we send it a command.
let replacer = ReplaceChild::new(TextBox::new().with_id(id_4), move || {
Split::columns(TextBox::new().with_id(id_5), TextBox::new().with_id(id_6))
});
let widget = Split::columns(
Flex::row()
.with_flex_child(TextBox::new().with_id(id_1), 1.0)
.with_flex_child(TextBox::new().with_id(id_2), 1.0)
.with_flex_child(TextBox::new().with_id(id_3), 1.0),
replacer,
);
Harness::create_simple("my test text".to_string(), widget, |harness| {
// verify that all widgets are marked as having children_changed
// (this should always be true for a new widget)
harness.inspect_state(|state| assert!(state.children_changed));
harness.send_initial_events();
// verify that we start out with four widgets registered for focus
assert_eq!(harness.window().focus_chain(), &[id_1, id_2, id_3, id_4]);
// tell the replacer widget to swap its children
harness.submit_command(REPLACE_CHILD, None);
// verify that the two new children are registered for focus.
assert_eq!(
harness.window().focus_chain(),
&[id_1, id_2, id_3, id_5, id_6]
);
// verify that no widgets still report that their children changed:
harness.inspect_state(|state| assert!(!state.children_changed))
})
}
#[test]
fn child_tracking() {
let (id_1, id_2, id_3, id_4) = widget_id4();
let widget = Split::columns(
SizedBox::empty().with_id(id_1),
SizedBox::empty().with_id(id_2),
)
.with_id(id_3)
.padding(5.0)
.with_id(id_4);
Harness::create_simple(true, widget, |harness| {
harness.send_initial_events();
let root = harness.get_state(id_4);
assert_eq!(root.children.entry_count(), 3);
assert!(root.children.may_contain(&id_1));
assert!(root.children.may_contain(&id_2));
assert!(root.children.may_contain(&id_3));
let split = harness.get_state(id_3);
assert!(split.children.may_contain(&id_1));
assert!(split.children.may_contain(&id_2));
assert_eq!(split.children.entry_count(), 2);
});
}
#[test]
/// Test that all children are registered correctly after a child is replaced.
fn register_after_adding_child() {
let (id_1, id_2, id_3, id_4, id_5, id_6) = widget_id6();
let id_7 = WidgetId::next();
let replacer = ReplaceChild::new(TextBox::new().with_id(id_1), move || {
Split::columns(TextBox::new().with_id(id_2), TextBox::new().with_id(id_3)).with_id(id_7)
})
.with_id(id_6);
let widget = Split::columns(Label::new("hi").with_id(id_4), replacer).with_id(id_5);
Harness::create_simple(String::new(), widget, |harness| {
harness.send_initial_events();
assert!(harness.get_state(id_5).children.may_contain(&id_6));
assert!(harness.get_state(id_5).children.may_contain(&id_1));
assert!(harness.get_state(id_5).children.may_contain(&id_4));
assert_eq!(harness.get_state(id_5).children.entry_count(), 3);
harness.submit_command(REPLACE_CHILD, None);
assert!(harness.get_state(id_5).children.may_contain(&id_6));
assert!(harness.get_state(id_5).children.may_contain(&id_4));
assert!(harness.get_state(id_5).children.may_contain(&id_7));
assert!(harness.get_state(id_5).children.may_contain(&id_2));
assert!(harness.get_state(id_5).children.may_contain(&id_3));
assert_eq!(harness.get_state(id_5).children.entry_count(), 5);
})
}
#[test]
/// Test that request_update actually causes the request.
fn request_update() {
const REQUEST_UPDATE: Selector = Selector::new("druid-tests.request_update");
let updated: Rc<Cell<bool>> = Default::default();
let updated_clone = updated.clone();
let widget = ModularWidget::new(())
.event_fn(|_, ctx, event, _data, _env| {
if matches!(event, Event::Command(cmd) if cmd.is(REQUEST_UPDATE)) {
ctx.request_update();
}
})
.update_fn(move |_, _ctx, _old_data, _data, _env| {
updated_clone.set(true);
});
Harness::create_simple((), widget, |harness| {
harness.send_initial_events();
assert!(!updated.get());
harness.submit_command(REQUEST_UPDATE, None);
assert!(updated.get());
})
}