iced_aw 0.14.1

Additional widgets for the Iced GUI library
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
//! Integration tests for the Sidebar and SidebarWithContent widgets
//!
//! These tests verify the widgets' behavior and public API
//! from an external perspective, testing the widgets as a user of the
//! library would interact with them.

// Test Notes:
// Button cheat sheet
//  cancel          → \u{e800}  // Used for close/cancel buttons

// Simulator API https://raw.githubusercontent.com/iced-rs/iced/master/test/src/simulator.rs

#[macro_use]
mod common;

use iced_aw::sidebar::{Sidebar, SidebarPosition, SidebarWithContent, TabLabel};
use iced_test::Error;
use iced_widget::text::Text;

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
enum TabId {
    Home,
    Settings,
    Profile,
}

#[derive(Clone, Debug)]
#[allow(dead_code)]
enum Message {
    TabSelected(TabId),
    TabClosed(TabId),
}

// Generate test helpers for this Message type
test_helpers!(Message);

// ============================================================================
// Sidebar Tests
// ============================================================================

#[test]
fn sidebar_can_find_tab_text_labels() -> Result<(), Error> {
    let (mut app, _) = App::new(move || {
        Sidebar::new(Message::TabSelected)
            .push(TabId::Home, TabLabel::Text("Home".to_string()))
            .push(TabId::Settings, TabLabel::Text("Settings".to_string()))
            .push(TabId::Profile, TabLabel::Text("Profile".to_string()))
            .into()
    });

    let ui = simulator(&app);
    process_messages(ui, &mut app);

    // Create a new simulator to verify the rendered content
    let mut ui = simulator(&app);

    // Snapshot testing: verify visual rendering matches baseline
    assert_snapshot_matches(&mut ui, "tests/snapshots/sidebar_can_find_tab_text_labels")?;

    assert!(
        ui.find("Home").is_ok(),
        "Tab label 'Home' should be findable"
    );
    assert!(
        ui.find("Settings").is_ok(),
        "Tab label 'Settings' should be findable"
    );
    assert!(
        ui.find("Profile").is_ok(),
        "Tab label 'Profile' should be findable"
    );

    Ok(())
}

#[test]
fn sidebar_clicking_tab_produces_tab_selected_message() -> Result<(), Error> {
    let (mut app, _) = App::new(move || {
        Sidebar::new(Message::TabSelected)
            .push(TabId::Home, TabLabel::Text("Home".to_string()))
            .push(TabId::Settings, TabLabel::Text("Settings".to_string()))
            .push(TabId::Profile, TabLabel::Text("Profile".to_string()))
            .into()
    });

    let mut ui = simulator(&app);

    // Verify the tab is clickable
    assert!(
        ui.find("Settings").is_ok(),
        "Settings tab should be findable"
    );

    ui.click("Settings")?;

    // Process messages to verify we got TabSelected message
    assert_message_received(
        ui,
        &mut app,
        |m| matches!(m, Message::TabSelected(TabId::Settings)),
        "Clicking tab should produce TabSelected message",
    );

    Ok(())
}

#[test]
fn sidebar_can_find_different_tab_label_types() -> Result<(), Error> {
    let (mut app, _) = App::new(move || {
        Sidebar::new(Message::TabSelected)
            .push(TabId::Home, TabLabel::Icon('H'))
            .push(TabId::Settings, TabLabel::Text("Settings".to_string()))
            .push(
                TabId::Profile,
                TabLabel::IconText('P', "Profile".to_string()),
            )
            .into()
    });

    let ui = simulator(&app);
    process_messages(ui, &mut app);

    let mut ui = simulator(&app);

    // Icon label
    assert!(ui.find("H").is_ok(), "Icon 'H' should be findable");

    // Text label
    assert!(
        ui.find("Settings").is_ok(),
        "Text label 'Settings' should be findable"
    );

    // IconText label
    assert!(ui.find("P").is_ok(), "Icon 'P' should be findable");
    assert!(
        ui.find("Profile").is_ok(),
        "Text 'Profile' should be findable"
    );

    Ok(())
}

#[test]
fn sidebar_with_close_callback_displays_close_icon() -> Result<(), Error> {
    let (mut app, _) = App::new(move || {
        Sidebar::new(Message::TabSelected)
            .push(TabId::Home, TabLabel::Text("Home".to_string()))
            .push(TabId::Settings, TabLabel::Text("Settings".to_string()))
            .on_close(Message::TabClosed)
            .into()
    });

    let ui = simulator(&app);
    process_messages(ui, &mut app);

    let mut ui = simulator(&app);

    // Verify tabs are findable
    assert!(ui.find("Home").is_ok(), "Home tab should be findable");
    assert!(
        ui.find("Settings").is_ok(),
        "Settings tab should be findable"
    );

    Ok(())
}

// Note: The close icon test is currently not working because the icon character
// needs to be matched exactly as it appears in the font. The operate() method
// has been implemented to expose the close icon, but the simulator's find()
// may need the exact Unicode character that the font renders.
#[test]
#[ignore]
fn sidebar_clicking_close_icon_produces_tab_closed_message() -> Result<(), Error> {
    let (mut app, _) = App::new(move || {
        Sidebar::new(Message::TabSelected)
            .push(TabId::Home, TabLabel::Text("Home".to_string()))
            .push(TabId::Settings, TabLabel::Text("Settings".to_string()))
            .on_close(Message::TabClosed)
            .into()
    });

    let mut ui = simulator(&app);

    // Verify the close icon is findable
    // The character \u{e800} is the cancel icon from the iced_aw font
    assert!(
        ui.find("\u{e800}").is_ok(),
        "Close icon should be findable in the sidebar"
    );

    // Click the close icon (cancel button)
    ui.click("\u{e800}")?;

    // Process messages to verify we got TabClosed message
    assert_message_received(
        ui,
        &mut app,
        |m| matches!(m, Message::TabClosed(_)),
        "Clicking close icon should produce TabClosed message",
    );

    Ok(())
}

#[test]
fn sidebar_with_close_exposes_elements_through_operate() -> Result<(), Error> {
    // This test verifies that the operate() method is being called and
    // the sidebar structure is exposed for testing
    let (mut app, _) = App::new(move || {
        Sidebar::new(Message::TabSelected)
            .push(TabId::Home, TabLabel::Text("Home".to_string()))
            .push(TabId::Settings, TabLabel::Text("Settings".to_string()))
            .on_close(Message::TabClosed)
            .into()
    });

    let ui = simulator(&app);
    process_messages(ui, &mut app);

    let mut ui = simulator(&app);

    // Verify tab labels are findable (proving operate() works)
    assert!(ui.find("Home").is_ok(), "Home tab should be findable");
    assert!(
        ui.find("Settings").is_ok(),
        "Settings tab should be findable"
    );

    Ok(())
}

// ============================================================================
// SidebarWithContent Tests
// ============================================================================

#[test]
fn sidebar_with_content_displays_tabs_and_content() -> Result<(), Error> {
    let (mut app, _) = App::new(move || {
        SidebarWithContent::new(Message::TabSelected)
            .push(
                TabId::Home,
                TabLabel::Text("Home".to_string()),
                Text::new("Home Content"),
            )
            .push(
                TabId::Settings,
                TabLabel::Text("Settings".to_string()),
                Text::new("Settings Content"),
            )
            .push(
                TabId::Profile,
                TabLabel::Text("Profile".to_string()),
                Text::new("Profile Content"),
            )
            .set_active_tab(&TabId::Home)
            .into()
    });

    let ui = simulator(&app);
    process_messages(ui, &mut app);

    let mut ui = simulator(&app);

    // Verify tabs are findable
    assert!(ui.find("Home").is_ok(), "Home tab should be findable");
    assert!(
        ui.find("Settings").is_ok(),
        "Settings tab should be findable"
    );
    assert!(ui.find("Profile").is_ok(), "Profile tab should be findable");

    // Verify active tab content is displayed
    assert!(
        ui.find("Home Content").is_ok(),
        "Active tab content should be displayed"
    );

    Ok(())
}

#[test]
fn sidebar_with_content_clicking_tab_shows_new_content() -> Result<(), Error> {
    let (mut app, _) = App::new(move || {
        SidebarWithContent::new(Message::TabSelected)
            .push(
                TabId::Home,
                TabLabel::Text("Home".to_string()),
                Text::new("Home Content"),
            )
            .push(
                TabId::Settings,
                TabLabel::Text("Settings".to_string()),
                Text::new("Settings Content"),
            )
            .set_active_tab(&TabId::Home)
            .into()
    });

    let mut ui = simulator(&app);

    // Verify initial content
    assert!(
        ui.find("Home Content").is_ok(),
        "Home content should be displayed initially"
    );

    // Click on Settings tab
    ui.click("Settings")?;

    // Verify we got the TabSelected message
    assert_message_received(
        ui,
        &mut app,
        |m| matches!(m, Message::TabSelected(TabId::Settings)),
        "Clicking Settings tab should produce TabSelected message",
    );

    Ok(())
}

#[test]
fn sidebar_with_content_start_position() -> Result<(), Error> {
    let (mut app, _) = App::new(move || {
        SidebarWithContent::new(Message::TabSelected)
            .push(
                TabId::Home,
                TabLabel::Text("Home".to_string()),
                Text::new("Home Content"),
            )
            .push(
                TabId::Settings,
                TabLabel::Text("Settings".to_string()),
                Text::new("Settings Content"),
            )
            .sidebar_position(SidebarPosition::Start)
            .set_active_tab(&TabId::Home)
            .into()
    });

    let ui = simulator(&app);
    process_messages(ui, &mut app);

    let mut ui = simulator(&app);

    // Verify both sidebar and content are findable
    assert!(ui.find("Home").is_ok(), "Sidebar should be findable");
    assert!(
        ui.find("Home Content").is_ok(),
        "Content should be findable"
    );

    Ok(())
}

#[test]
fn sidebar_with_content_end_position() -> Result<(), Error> {
    let (mut app, _) = App::new(move || {
        SidebarWithContent::new(Message::TabSelected)
            .push(
                TabId::Home,
                TabLabel::Text("Home".to_string()),
                Text::new("Home Content"),
            )
            .push(
                TabId::Settings,
                TabLabel::Text("Settings".to_string()),
                Text::new("Settings Content"),
            )
            .sidebar_position(SidebarPosition::End)
            .set_active_tab(&TabId::Home)
            .into()
    });

    let ui = simulator(&app);
    process_messages(ui, &mut app);

    let mut ui = simulator(&app);

    // Verify both sidebar and content are findable
    assert!(ui.find("Home").is_ok(), "Sidebar should be findable");
    assert!(
        ui.find("Home Content").is_ok(),
        "Content should be findable"
    );

    Ok(())
}

#[test]
fn sidebar_with_content_multiple_tab_clicks() -> Result<(), Error> {
    let (mut app, _) = App::new(move || {
        SidebarWithContent::new(Message::TabSelected)
            .push(
                TabId::Home,
                TabLabel::Text("Home".to_string()),
                Text::new("Home Content"),
            )
            .push(
                TabId::Settings,
                TabLabel::Text("Settings".to_string()),
                Text::new("Settings Content"),
            )
            .push(
                TabId::Profile,
                TabLabel::Text("Profile".to_string()),
                Text::new("Profile Content"),
            )
            .set_active_tab(&TabId::Home)
            .into()
    });

    let mut ui = simulator(&app);

    // Click Settings
    ui.click("Settings")?;

    assert_message_received(
        ui,
        &mut app,
        |m| matches!(m, Message::TabSelected(TabId::Settings)),
        "Should receive Settings tab selection",
    );

    // Create new UI and click Profile
    let mut ui = simulator(&app);
    ui.click("Profile")?;

    assert_message_received(
        ui,
        &mut app,
        |m| matches!(m, Message::TabSelected(TabId::Profile)),
        "Should receive Profile tab selection",
    );

    Ok(())
}