azul-core 0.0.8

Common datatypes used for the Azul document object model, shared across all azul-* crates
Documentation
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

//! Unit tests for the Phase 3.5 event system
//!
//! Tests cover:
//! - Event type creation
//! - DOM path traversal
//! - Event propagation (capture/target/bubble)
//! - Event filter matching
//! - Lifecycle event detection

use std::collections::BTreeMap;

use azul_core::{
    dom::{DomId, DomNodeId},
    events::*,
    geom::{LogicalPosition, LogicalRect, LogicalSize},
    id::{Node, NodeHierarchy, NodeId},
    styled_dom::NodeHierarchyItemId,
    task::{Instant, SystemTick},
};

// Helper: Create a test Instant
fn test_instant() -> Instant {
    Instant::Tick(SystemTick::new(0))
}

// Helper: Create a simple 3-node tree (root -> child1 -> grandchild)
fn create_test_hierarchy() -> NodeHierarchy {
    let nodes = vec![
        Node {
            parent: None,
            previous_sibling: None,
            next_sibling: None,
            last_child: Some(NodeId::new(1)),
        },
        Node {
            parent: Some(NodeId::new(0)),
            previous_sibling: None,
            next_sibling: None,
            last_child: Some(NodeId::new(2)),
        },
        Node {
            parent: Some(NodeId::new(1)),
            previous_sibling: None,
            next_sibling: None,
            last_child: None,
        },
    ];
    NodeHierarchy::new(nodes)
}

#[test]
fn test_event_source_enum() {
    // Test that EventSource variants can be created
    let _user = EventSource::User;
    let _programmatic = EventSource::Programmatic;
    let _synthetic = EventSource::Synthetic;
    let _lifecycle = EventSource::Lifecycle;
}

#[test]
fn test_event_phase_enum() {
    // Test that EventPhase variants can be created
    let _capture = EventPhase::Capture;
    let _target = EventPhase::Target;
    let _bubble = EventPhase::Bubble;

    // Test default
    assert_eq!(EventPhase::default(), EventPhase::Bubble);
}

#[test]
fn test_synthetic_event_creation() {
    let dom_id = DomId { inner: 1 };
    let node_id = NodeHierarchyItemId::from_crate_internal(Some(NodeId::new(0)));
    let target = DomNodeId {
        dom: dom_id,
        node: node_id,
    };

    let event = SyntheticEvent::new(
        EventType::Click,
        EventSource::User,
        target,
        test_instant(),
        EventData::None,
    );

    assert_eq!(event.event_type, EventType::Click);
    assert_eq!(event.source, EventSource::User);
    assert_eq!(event.phase, EventPhase::Target);
    assert_eq!(event.target, target);
    assert_eq!(event.current_target, target);
    assert!(!event.stopped);
    assert!(!event.stopped_immediate);
    assert!(!event.prevented_default);
}

#[test]
fn test_stop_propagation() {
    let dom_id = DomId { inner: 1 };
    let node_id = NodeHierarchyItemId::from_crate_internal(Some(NodeId::new(0)));
    let target = DomNodeId {
        dom: dom_id,
        node: node_id,
    };

    let mut event = SyntheticEvent::new(
        EventType::Click,
        EventSource::User,
        target,
        test_instant(),
        EventData::None,
    );

    assert!(!event.is_propagation_stopped());

    event.stop_propagation();

    assert!(event.is_propagation_stopped());
    assert!(!event.is_immediate_propagation_stopped());
}

#[test]
fn test_stop_immediate_propagation() {
    let dom_id = DomId { inner: 1 };
    let node_id = NodeHierarchyItemId::from_crate_internal(Some(NodeId::new(0)));
    let target = DomNodeId {
        dom: dom_id,
        node: node_id,
    };

    let mut event = SyntheticEvent::new(
        EventType::Click,
        EventSource::User,
        target,
        test_instant(),
        EventData::None,
    );

    event.stop_immediate_propagation();

    assert!(event.is_propagation_stopped());
    assert!(event.is_immediate_propagation_stopped());
}

#[test]
fn test_prevent_default() {
    let dom_id = DomId { inner: 1 };
    let node_id = NodeHierarchyItemId::from_crate_internal(Some(NodeId::new(0)));
    let target = DomNodeId {
        dom: dom_id,
        node: node_id,
    };

    let mut event = SyntheticEvent::new(
        EventType::Click,
        EventSource::User,
        target,
        test_instant(),
        EventData::None,
    );

    assert!(!event.is_default_prevented());

    event.prevent_default();

    assert!(event.is_default_prevented());
}

#[test]
fn test_get_dom_path_single_node() {
    let hierarchy = NodeHierarchy::new(vec![Node {
        parent: None,
        previous_sibling: None,
        next_sibling: None,
        last_child: None,
    }]);

    let target = NodeHierarchyItemId::from_crate_internal(Some(NodeId::new(0)));
    let path = get_dom_path(&hierarchy, target);

    assert_eq!(path.len(), 1);
    assert_eq!(path[0], NodeId::new(0));
}

#[test]
fn test_get_dom_path_three_nodes() {
    let hierarchy = create_test_hierarchy();

    // Test path to grandchild (node 2)
    let target = NodeHierarchyItemId::from_crate_internal(Some(NodeId::new(2)));
    let path = get_dom_path(&hierarchy, target);

    assert_eq!(path.len(), 3);
    assert_eq!(path[0], NodeId::new(0)); // root
    assert_eq!(path[1], NodeId::new(1)); // child
    assert_eq!(path[2], NodeId::new(2)); // grandchild
}

#[test]
fn test_get_dom_path_middle_node() {
    let hierarchy = create_test_hierarchy();

    // Test path to middle node (node 1)
    let target = NodeHierarchyItemId::from_crate_internal(Some(NodeId::new(1)));
    let path = get_dom_path(&hierarchy, target);

    assert_eq!(path.len(), 2);
    assert_eq!(path[0], NodeId::new(0)); // root
    assert_eq!(path[1], NodeId::new(1)); // child
}

#[test]
fn test_propagate_event_empty_callbacks() {
    let hierarchy = create_test_hierarchy();
    let dom_id = DomId { inner: 1 };
    let target_node = NodeHierarchyItemId::from_crate_internal(Some(NodeId::new(2)));
    let target = DomNodeId {
        dom: dom_id,
        node: target_node,
    };

    let mut event = SyntheticEvent::new(
        EventType::Click,
        EventSource::User,
        target,
        test_instant(),
        EventData::None,
    );

    let callbacks: BTreeMap<NodeId, Vec<EventFilter>> = BTreeMap::new();
    let result = propagate_event(&mut event, &hierarchy, &callbacks);

    // No callbacks, so nothing should be invoked
    assert_eq!(result.callbacks_to_invoke.len(), 0);
    assert!(!result.default_prevented);
}

#[test]
fn test_mouse_event_data_creation() {
    let mouse_data = MouseEventData {
        position: LogicalPosition { x: 100.0, y: 200.0 },
        button: MouseButton::Left,
        buttons: 1,
        modifiers: KeyModifiers::new(),
    };

    assert_eq!(mouse_data.position.x, 100.0);
    assert_eq!(mouse_data.position.y, 200.0);
    assert_eq!(mouse_data.button, MouseButton::Left);
}

#[test]
fn test_key_modifiers() {
    let modifiers = KeyModifiers::new().with_shift().with_ctrl();

    assert!(modifiers.shift);
    assert!(modifiers.ctrl);
    assert!(!modifiers.alt);
    assert!(!modifiers.meta);
    assert!(!modifiers.is_empty());

    let empty = KeyModifiers::new();
    assert!(empty.is_empty());
}

#[test]
fn test_lifecycle_event_mount() {
    let dom_id = DomId { inner: 1 };
    let old_hierarchy = None;
    let new_hierarchy = create_test_hierarchy();
    let old_layout = None;
    let new_layout = {
        let mut map = BTreeMap::new();
        map.insert(
            NodeId::new(0),
            LogicalRect {
                origin: LogicalPosition { x: 0.0, y: 0.0 },
                size: LogicalSize {
                    width: 100.0,
                    height: 100.0,
                },
            },
        );
        map.insert(
            NodeId::new(1),
            LogicalRect {
                origin: LogicalPosition { x: 10.0, y: 10.0 },
                size: LogicalSize {
                    width: 80.0,
                    height: 80.0,
                },
            },
        );
        map.insert(
            NodeId::new(2),
            LogicalRect {
                origin: LogicalPosition { x: 20.0, y: 20.0 },
                size: LogicalSize {
                    width: 60.0,
                    height: 60.0,
                },
            },
        );
        Some(map)
    };

    let events = detect_lifecycle_events(
        dom_id,
        dom_id,
        old_hierarchy,
        Some(&new_hierarchy),
        old_layout.as_ref(),
        new_layout.as_ref(),
        test_instant(),
    );

    // All 3 nodes should have Mount events
    assert_eq!(events.len(), 3);

    for event in &events {
        assert_eq!(event.event_type, EventType::Mount);
        assert_eq!(event.source, EventSource::Lifecycle);

        if let EventData::Lifecycle(data) = &event.data {
            assert_eq!(data.reason, LifecycleReason::InitialMount);
            assert!(data.previous_bounds.is_none());
        } else {
            panic!("Expected Lifecycle event data");
        }
    }
}

#[test]
fn test_lifecycle_event_unmount() {
    let dom_id = DomId { inner: 1 };
    let old_hierarchy = create_test_hierarchy();
    let new_hierarchy = None;
    let old_layout = {
        let mut map = BTreeMap::new();
        map.insert(
            NodeId::new(0),
            LogicalRect {
                origin: LogicalPosition { x: 0.0, y: 0.0 },
                size: LogicalSize {
                    width: 100.0,
                    height: 100.0,
                },
            },
        );
        Some(map)
    };
    let new_layout = None;

    let events = detect_lifecycle_events(
        dom_id,
        dom_id,
        Some(&old_hierarchy),
        new_hierarchy,
        old_layout.as_ref(),
        new_layout,
        test_instant(),
    );

    // All 3 nodes should have Unmount events
    assert_eq!(events.len(), 3);

    for event in &events {
        assert_eq!(event.event_type, EventType::Unmount);
        assert_eq!(event.source, EventSource::Lifecycle);
    }
}

#[test]
fn test_lifecycle_event_resize() {
    let dom_id = DomId { inner: 1 };
    let hierarchy = create_test_hierarchy();

    let old_layout = {
        let mut map = BTreeMap::new();
        map.insert(
            NodeId::new(0),
            LogicalRect {
                origin: LogicalPosition { x: 0.0, y: 0.0 },
                size: LogicalSize {
                    width: 100.0,
                    height: 100.0,
                },
            },
        );
        Some(map)
    };

    let new_layout = {
        let mut map = BTreeMap::new();
        map.insert(
            NodeId::new(0),
            LogicalRect {
                origin: LogicalPosition { x: 0.0, y: 0.0 },
                size: LogicalSize {
                    width: 200.0,
                    height: 100.0,
                }, // Width changed
            },
        );
        Some(map)
    };

    let events = detect_lifecycle_events(
        dom_id,
        dom_id,
        Some(&hierarchy),
        Some(&hierarchy),
        old_layout.as_ref(),
        new_layout.as_ref(),
        test_instant(),
    );

    // Should have 1 Resize event
    assert_eq!(events.len(), 1);
    assert_eq!(events[0].event_type, EventType::Resize);
    assert_eq!(events[0].source, EventSource::Lifecycle);

    if let EventData::Lifecycle(data) = &events[0].data {
        assert_eq!(data.reason, LifecycleReason::Resize);
        assert!(data.previous_bounds.is_some());
        assert_eq!(data.current_bounds.size.width, 200.0);
    } else {
        panic!("Expected Lifecycle event data");
    }
}

#[test]
fn test_event_filter_hover_match() {
    let dom_id = DomId { inner: 1 };
    let node_id = NodeHierarchyItemId::from_crate_internal(Some(NodeId::new(0)));
    let target = DomNodeId {
        dom: dom_id,
        node: node_id,
    };

    let _event = SyntheticEvent::new(
        EventType::MouseDown,
        EventSource::User,
        target,
        test_instant(),
        EventData::Mouse(MouseEventData {
            position: LogicalPosition { x: 0.0, y: 0.0 },
            button: MouseButton::Left,
            buttons: 1,
            modifiers: KeyModifiers::new(),
        }),
    );

    // This is tested internally via matches_hover_filter
    // We can't test it directly without making the function public
    // but it's tested indirectly through propagate_event
}