rust_widgets 0.9.6

Pure Rust cross-platform native GUI library with hardware-adaptive rendering, 60+ widgets, touch/gesture support, i18n, and SVG-pipeline-accurate output
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
//! Shared core for WebEngineViewEnhanced and WebViewEnhanced.
//!
//! Both types delegate to [`WebViewCore`] to avoid ~95% code duplication.
//! Each wrapper adds only its own unique signals and behavior on top of this core.
//!
//! # Simulated Navigation
//!
//! All navigation methods are **simulated** — no real web engine is used.
//! Progress callbacks are emitted as 0% → 50% → 100% to allow UI bindings
//! to observe load lifecycle events. For real web content, wire a real
//! WebEngine/WebView backend.

use super::history::{BrowserHistory, SessionHistory};
use super::js_engine::{JsContext, JsEngine, JsResult, JsValue, SimpleJsEngine};
use super::plugins::PluginManager;
use super::privacy::{CookieJar, PrivacySettings, TrackingProtection};
use crate::core::Rect;
use crate::signal::Signal1;
use crate::widget::{BaseWidget, WidgetKind};

/// A pluggable simulation engine that can be set externally (e.g. in tests)
/// to inject mock content or simulate different network conditions.
pub trait SimulationEngine: Send {
    /// Simulate navigation to the given URL and return simulated content.
    fn simulate_navigation(&mut self, url: &str) -> Result<String, String>;
}

/// Shared fields used by both WebEngineViewEnhanced and WebViewEnhanced.
pub struct WebViewCore {
    pub base: BaseWidget,
    pub url: String,
    pub loading: bool,
    pub title: String,
    pub load_progress: u8,
    pub history: SessionHistory,
    pub browser_history: BrowserHistory,
    pub js_engine: Box<dyn JsEngine>,
    pub js_context: JsContext,
    pub cookies: CookieJar,
    pub privacy: TrackingProtection,
    pub plugins: PluginManager,
    pub settings: super::WebSettings,
    pub security: super::SecuritySettings,
    pub loading_started: Signal1<String>,
    pub loading_finished: Signal1<String>,
    pub loading_progress: Signal1<u8>,
    pub title_changed: Signal1<String>,
    pub url_changed: Signal1<String>,
    pub _error_occurred: Signal1<String>,
    pub navigation_state_changed: Signal1<(bool, bool)>,
    pub console_message: Signal1<(String, u32, String)>,
    pub content: String,
    /// Optional simulation engine for injecting mock data in tests.
    /// When set, navigation methods will delegate to this engine
    /// instead of the default simulated 0→50→100 progress.
    pub simulation_engine: Option<Box<dyn SimulationEngine>>,
}

impl WebViewCore {
    pub fn new(
        kind: WidgetKind,
        geometry: Rect,
        widget_name: &'static str,
        initial_url: &str,
    ) -> Self {
        Self {
            base: BaseWidget::new(kind, geometry, widget_name),
            url: initial_url.to_string(),
            loading: false,
            title: String::new(),
            load_progress: 0,
            history: SessionHistory::default(),
            browser_history: BrowserHistory::new(),
            js_engine: Box::new(SimpleJsEngine::new()),
            js_context: JsContext::new(),
            cookies: CookieJar::new(),
            privacy: TrackingProtection::new(PrivacySettings::balanced()),
            plugins: PluginManager::new(),
            settings: super::WebSettings::default(),
            security: super::SecuritySettings::default(),
            loading_started: Signal1::new(),
            loading_finished: Signal1::new(),
            loading_progress: Signal1::new(),
            title_changed: Signal1::new(),
            url_changed: Signal1::new(),
            _error_occurred: Signal1::new(),
            navigation_state_changed: Signal1::new(),
            console_message: Signal1::new(),
            content: String::new(),
            simulation_engine: None,
        }
    }

    pub fn url(&self) -> &str {
        &self.url
    }
    pub fn is_loading(&self) -> bool {
        self.loading
    }
    pub fn title(&self) -> &str {
        &self.title
    }
    pub fn load_progress(&self) -> u8 {
        self.load_progress
    }
    pub fn can_go_back(&self) -> bool {
        self.history.can_go_back()
    }
    pub fn can_go_forward(&self) -> bool {
        self.history.can_go_forward()
    }
    pub fn settings(&self) -> &super::WebSettings {
        &self.settings
    }
    pub fn settings_mut(&mut self) -> &mut super::WebSettings {
        &mut self.settings
    }
    pub fn security(&self) -> &super::SecuritySettings {
        &self.security
    }
    pub fn security_mut(&mut self) -> &mut super::SecuritySettings {
        &mut self.security
    }
    pub fn cookies(&self) -> &CookieJar {
        &self.cookies
    }
    pub fn cookies_mut(&mut self) -> &mut CookieJar {
        &mut self.cookies
    }
    pub fn privacy(&self) -> &TrackingProtection {
        &self.privacy
    }
    pub fn privacy_mut(&mut self) -> &mut TrackingProtection {
        &mut self.privacy
    }
    pub fn plugins(&self) -> &PluginManager {
        &self.plugins
    }
    pub fn plugins_mut(&mut self) -> &mut PluginManager {
        &mut self.plugins
    }
    pub fn history(&self) -> &SessionHistory {
        &self.history
    }
    pub fn browser_history(&self) -> &BrowserHistory {
        &self.browser_history
    }

    pub fn load_url(&mut self, url: &str) {
        self.set_url(url.to_string());
    }

    /// SIMULATED: No real web engine — this simulates URL navigation with
    /// 0% → 50% → 100% progress callbacks.
    ///
    /// Validates that the URL starts with a supported scheme (`http://`,
    /// `https://`, or `file://`) before accepting it. If the URL is invalid
    /// or the simulation engine returns an error, the navigation is refused
    /// and the error is logged.
    pub fn set_url(&mut self, url: String) {
        // Validate URL scheme.
        if !url.starts_with("http://")
            && !url.starts_with("https://")
            && !url.starts_with("file://")
        {
            log::error!(
                "[web] Invalid URL scheme: '{}' — must start with http://, https://, or file://",
                url
            );
            return;
        }

        if self.url == url {
            // URL already set — mark as fully loaded.
            self.load_progress = 100;
            return;
        }

        // Delegate to simulation engine if one is set.
        if let Some(ref mut engine) = self.simulation_engine {
            match engine.simulate_navigation(&url) {
                Ok(content) => {
                    self.content = content;
                }
                Err(e) => {
                    log::error!("[web] simulation engine error for '{}': {}", url, e);
                    return;
                }
            }
        }

        self.url = url.clone();
        self.loading = true;
        self.load_progress = 0;
        self.url_changed.emit(url.clone());
        self.loading_started.emit(url.clone());
        self.history.navigate(url.clone());

        // Emit progress at 50%.
        self.load_progress = 50;
        self.loading_progress.emit(self.load_progress);

        // Emit progress at 100% and finish.
        self.load_progress = 100;
        self.loading = false;
        self.loading_progress.emit(self.load_progress);
        self.loading_finished.emit(self.url.clone());
        self.update_navigation_state();
        self.browser_history.add_entry(self.url.clone(), self.title.clone());
        self.base.request_redraw();
    }

    /// SIMULATED: No real web engine — loads HTML content with simulated
    /// 0% → 100% progress callbacks.
    pub fn load_html(&mut self, html: &str, base_url: Option<&str>) {
        self.url = base_url.unwrap_or("data:text/html").to_string();
        self.title = "HTML Content".to_string();
        self.loading = true;
        self.load_progress = 0;
        self.loading_started.emit(self.url.clone());
        self.content = html.to_string();

        // Emit progress at 50%.
        self.load_progress = 50;
        self.loading_progress.emit(self.load_progress);

        // Emit progress at 100% and finish.
        self.load_progress = 100;
        self.loading = false;
        self.loading_progress.emit(self.load_progress);
        self.loading_finished.emit(self.url.clone());
        self.title_changed.emit(self.title.clone());
        self.url_changed.emit(self.url.clone());
        self.update_navigation_state();
        self.base.request_redraw();
    }

    /// SIMULATED: No real web engine — loads binary data as a string with simulated
    /// 0% → 50% → 100% progress callbacks.
    pub fn load_data(&mut self, data: &[u8], mime_type: &str, base_url: &str) {
        self.url = base_url.to_string();
        self.title = format!("Data: {}", mime_type);
        self.loading = true;
        self.load_progress = 0;
        self.loading_started.emit(self.url.clone());
        self.content = String::from_utf8_lossy(data).to_string();

        // Emit progress at 50%.
        self.load_progress = 50;
        self.loading_progress.emit(self.load_progress);

        // Emit progress at 100% and finish.
        self.load_progress = 100;
        self.loading = false;
        self.loading_progress.emit(self.load_progress);
        self.loading_finished.emit(self.url.clone());
        self.title_changed.emit(self.title.clone());
        self.update_navigation_state();
        self.base.request_redraw();
    }

    /// SIMULATED: No real web engine — navigates back in history with simulated
    /// loading callbacks.
    pub fn go_back(&mut self) {
        if let Some(url) = self.history.go_back() {
            self.url = url;
            self.loading = true;
            self.load_progress = 0;
            self.loading_started.emit(self.url.clone());

            // Emit progress at 50%.
            self.load_progress = 50;
            self.loading_progress.emit(self.load_progress);

            // Emit progress at 100% and finish.
            self.load_progress = 100;
            self.loading = false;
            self.loading_progress.emit(self.load_progress);
            self.loading_finished.emit(self.url.clone());
            self.update_navigation_state();
            self.base.request_redraw();
        }
    }

    /// SIMULATED: No real web engine — navigates forward in history with simulated
    /// loading callbacks.
    pub fn go_forward(&mut self) {
        if let Some(url) = self.history.go_forward() {
            self.url = url;
            self.loading = true;
            self.load_progress = 0;
            self.loading_started.emit(self.url.clone());

            // Emit progress at 50%.
            self.load_progress = 50;
            self.loading_progress.emit(self.load_progress);

            // Emit progress at 100% and finish.
            self.load_progress = 100;
            self.loading = false;
            self.loading_progress.emit(self.load_progress);
            self.loading_finished.emit(self.url.clone());
            self.update_navigation_state();
            self.base.request_redraw();
        }
    }

    /// SIMULATED: No real web engine — simulates a page reload with
    /// 0% → 50% → 100% progress callbacks.
    pub fn reload(&mut self) {
        if !self.url.is_empty() {
            self.loading = true;
            self.load_progress = 0;
            self.loading_started.emit(self.url.clone());

            // Emit progress at 50%.
            self.load_progress = 50;
            self.loading_progress.emit(self.load_progress);

            // Emit progress at 100% and finish.
            self.load_progress = 100;
            self.loading = false;
            self.loading_progress.emit(self.load_progress);
            self.loading_finished.emit(self.url.clone());
            self.base.request_redraw();
        }
    }

    pub fn stop(&mut self) {
        if self.loading {
            self.loading = false;
            self.load_progress = 0;
            self.loading_finished.emit(self.url.clone());
            self.base.request_redraw();
        }
    }

    pub fn set_title(&mut self, title: String) {
        if self.title != title {
            self.title = title.clone();
            self.title_changed.emit(title);
        }
    }

    pub fn evaluate_javascript(&mut self, script: &str) -> JsResult<JsValue> {
        if !self.settings.javascript_enabled {
            return Err(super::js_engine::JsError::new("JavaScript is disabled".to_string()));
        }
        let result = self.js_engine.evaluate(script, &mut self.js_context)?;
        for msg in self.js_context.console_messages() {
            let level = format!("{:?}", msg.level);
            self.console_message.emit((level, msg.line, msg.message.clone()));
        }
        Ok(result)
    }

    pub fn set_javascript_enabled(&mut self, enabled: bool) {
        self.settings.javascript_enabled = enabled;
    }

    pub fn content(&self) -> &str {
        &self.content
    }

    pub fn html(&self) -> &str {
        &self.content
    }

    pub fn clear_browsing_data(&mut self, data: super::privacy::BrowsingData) {
        if data.cookies {
            self.cookies.clear();
        }
        if data.history {
            self.browser_history.clear();
            self.history.clear();
        }
    }

    fn update_navigation_state(&self) {
        self.navigation_state_changed.emit((self.can_go_back(), self.can_go_forward()));
    }

    /// Handle common key events for navigation.
    pub fn handle_key_event(&mut self, key: u32, modifiers: u32) {
        match key {
            37 if modifiers == 0 => {
                self.go_back();
            }
            39 if modifiers == 0 => {
                self.go_forward();
            }
            116 => {
                self.reload();
            }
            82 if modifiers == 1 => {
                self.reload();
            }
            _ => { /* Other keys are not relevant */ }
        }
    }
}

// ---------------------------------------------------------------------------
// Widget trait delegation for WebViewCore (used by both wrappers)
// ---------------------------------------------------------------------------
macro_rules! delegate_widget {
    ($wrapper:ty) => {
        impl Widget for $wrapper {
            fn id(&self) -> ObjectId {
                self.core.base.id()
            }
            fn kind(&self) -> WidgetKind {
                self.core.base.kind()
            }
            fn geometry(&self) -> Rect {
                self.core.base.geometry()
            }
            fn set_geometry(&mut self, geometry: Rect) {
                self.core.base.set_geometry(geometry);
            }
            fn min_size(&self) -> Option<Size> {
                self.core.base.min_size()
            }
            fn max_size(&self) -> Option<Size> {
                self.core.base.max_size()
            }
            fn set_min_size(&mut self, min_size: Option<Size>) {
                self.core.base.set_min_size(min_size);
            }
            fn set_max_size(&mut self, max_size: Option<Size>) {
                self.core.base.set_max_size(max_size);
            }
            fn parent(&self) -> Option<ObjectId> {
                self.core.base.parent()
            }
            fn set_parent(&mut self, parent: Option<ObjectId>) {
                self.core.base.set_parent(parent);
            }
            fn children(&self) -> &[ObjectId] {
                self.core.base.children()
            }
            fn add_child(&mut self, child: ObjectId) {
                self.core.base.add_child(child);
            }
            fn remove_child(&mut self, child: ObjectId) {
                self.core.base.remove_child(child);
            }
            fn show(&mut self) {
                self.core.base.show();
            }
            fn hide(&mut self) {
                self.core.base.hide();
            }
            fn is_visible(&self) -> bool {
                self.core.base.is_visible()
            }
            fn set_enabled(&mut self, enabled: bool) {
                self.core.base.set_enabled(enabled);
            }
            fn is_enabled(&self) -> bool {
                self.core.base.is_enabled()
            }
            fn set_tooltip(&mut self, tooltip: String) {
                self.core.base.set_tooltip(tooltip);
            }
            fn tooltip(&self) -> &str {
                self.core.base.tooltip()
            }
            fn style(&self) -> &WidgetStyle {
                self.core.base.style()
            }
            fn set_style(&mut self, style: WidgetStyle) {
                self.core.base.set_style(style);
            }
            fn connection_scope(&self) -> &ConnectionScope {
                self.core.base.connection_scope()
            }
            fn hover_signal(&self) -> &Signal1<Point> {
                self.core.base.hover_signal()
            }
            fn mouse_down_signal(&self) -> &Signal1<(Point, u32)> {
                self.core.base.mouse_down_signal()
            }
            fn mouse_up_signal(&self) -> &Signal1<(Point, u32)> {
                self.core.base.mouse_up_signal()
            }
            fn key_down_signal(&self) -> &Signal1<(u32, u32)> {
                self.core.base.key_down_signal()
            }
            fn key_up_signal(&self) -> &Signal1<(u32, u32)> {
                self.core.base.key_up_signal()
            }
            fn focus_gained_signal(&self) -> &GenericSignal {
                self.core.base.focus_gained_signal()
            }
            fn focus_lost_signal(&self) -> &GenericSignal {
                self.core.base.focus_lost_signal()
            }
            fn redraw_requested_signal(&self) -> &GenericSignal {
                self.core.base.redraw_requested_signal()
            }
            fn layout_requested_signal(&self) -> &GenericSignal {
                self.core.base.layout_requested_signal()
            }
        }
    };
}

pub(crate) use delegate_widget;

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::Rect;
    use crate::web::privacy::{BrowsingData, Cookie};

    #[test]
    fn test_web_view_core_new() {
        let core = WebViewCore::new(
            WidgetKind::WebView,
            Rect::new(0, 0, 800, 600),
            "test_webview",
            "about:blank",
        );
        assert_eq!(core.url(), "about:blank");
        assert!(!core.is_loading());
        assert_eq!(core.title(), "");
        assert_eq!(core.load_progress(), 0);
        assert!(core.settings().javascript_enabled);
        assert!(core.security().block_popups);
    }

    #[test]
    fn test_web_view_core_set_url() {
        let mut core = WebViewCore::new(
            WidgetKind::WebView,
            Rect::new(0, 0, 800, 600),
            "test_webview",
            "about:blank",
        );
        core.set_url("https://example.com".to_string());
        assert_eq!(core.url(), "https://example.com");
        // set_url simulates loading and completing
        assert!(!core.is_loading());
        assert_eq!(core.load_progress(), 100);
    }

    #[test]
    fn test_web_view_core_duplicate_url() {
        let mut core = WebViewCore::new(
            WidgetKind::WebView,
            Rect::new(0, 0, 800, 600),
            "test_webview",
            "https://example.com",
        );
        // First call sets the URL.
        assert_eq!(core.url(), "https://example.com");
        // Second call with same URL — load should be short-circuited.
        core.set_url("https://example.com".to_string());
        assert!(!core.is_loading());
        assert_eq!(core.load_progress(), 100);
    }

    #[test]
    fn test_web_view_core_load_url() {
        let mut core = WebViewCore::new(
            WidgetKind::WebView,
            Rect::new(0, 0, 800, 600),
            "test_webview",
            "about:blank",
        );
        core.load_url("https://rust-lang.org");
        assert_eq!(core.url(), "https://rust-lang.org");
    }

    #[test]
    fn test_web_view_core_load_html() {
        let mut core = WebViewCore::new(
            WidgetKind::WebView,
            Rect::new(0, 0, 800, 600),
            "test_webview",
            "about:blank",
        );
        core.load_html("<h1>Hello</h1>", Some("https://base.url"));
        assert_eq!(core.url(), "https://base.url");
        assert_eq!(core.title(), "HTML Content");
        assert_eq!(core.content(), "<h1>Hello</h1>");
        assert!(!core.is_loading());
    }

    #[test]
    fn test_web_view_core_load_data() {
        let mut core = WebViewCore::new(
            WidgetKind::WebView,
            Rect::new(0, 0, 800, 600),
            "test_webview",
            "about:blank",
        );
        core.load_data(b"raw data", "text/plain", "https://data.url");
        assert_eq!(core.url(), "https://data.url");
        assert_eq!(core.title(), "Data: text/plain");
        assert_eq!(core.content(), "raw data");
    }

    #[test]
    fn test_web_view_core_go_back_forward() {
        let mut core = WebViewCore::new(
            WidgetKind::WebView,
            Rect::new(0, 0, 800, 600),
            "test_webview",
            "about:blank",
        );
        assert!(!core.can_go_back());
        assert!(!core.can_go_forward());

        core.set_url("https://page1.com".to_string());
        core.set_url("https://page2.com".to_string());
        assert!(core.can_go_back());
        assert!(!core.can_go_forward());

        core.go_back();
        // After going back, we can go forward
        assert!(core.can_go_forward());
        assert_eq!(core.url(), "https://page1.com");

        core.go_forward();
        assert_eq!(core.url(), "https://page2.com");
    }

    #[test]
    fn test_web_view_core_reload() {
        let mut core = WebViewCore::new(
            WidgetKind::WebView,
            Rect::new(0, 0, 800, 600),
            "test_webview",
            "about:blank",
        );
        // reload on empty URL does nothing
        core.reload();
        assert!(!core.is_loading());

        core.load_url("https://example.com");
        core.reload();
        assert!(!core.is_loading());
        assert_eq!(core.load_progress(), 100);
    }

    #[test]
    fn test_web_view_core_stop() {
        let mut core = WebViewCore::new(
            WidgetKind::WebView,
            Rect::new(0, 0, 800, 600),
            "test_webview",
            "about:blank",
        );
        // stop when not loading is a no-op
        core.stop();
        assert!(!core.is_loading());

        // Force loading state then stop
        core.loading = true;
        core.stop();
        assert!(!core.is_loading());
        assert_eq!(core.load_progress(), 0);
    }

    #[test]
    fn test_web_view_core_set_title() {
        let mut core = WebViewCore::new(
            WidgetKind::WebView,
            Rect::new(0, 0, 800, 600),
            "test_webview",
            "about:blank",
        );
        assert_eq!(core.title(), "");
        core.set_title("New Title".to_string());
        assert_eq!(core.title(), "New Title");
    }

    #[test]
    fn test_web_view_core_evaluate_javascript_disabled() {
        let mut core = WebViewCore::new(
            WidgetKind::WebView,
            Rect::new(0, 0, 800, 600),
            "test_webview",
            "about:blank",
        );
        core.set_javascript_enabled(false);
        let result = core.evaluate_javascript("var x = 1;");
        assert!(result.is_err());
        assert!(result.unwrap_err().message.contains("JavaScript is disabled"));
    }

    #[test]
    fn test_web_view_core_evaluate_javascript_enabled() {
        let mut core = WebViewCore::new(
            WidgetKind::WebView,
            Rect::new(0, 0, 800, 600),
            "test_webview",
            "about:blank",
        );
        core.set_javascript_enabled(true);
        let result = core.evaluate_javascript("42");
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), JsValue::Number(42.0));
    }

    #[test]
    fn test_web_view_core_clear_browsing_data_cookies_only() {
        let mut core = WebViewCore::new(
            WidgetKind::WebView,
            Rect::new(0, 0, 800, 600),
            "test_webview",
            "about:blank",
        );
        core.cookies.add(Cookie::new(
            "test".to_string(),
            "value".to_string(),
            "example.com".to_string(),
        ));
        assert!(!core.cookies.is_empty());

        core.clear_browsing_data(BrowsingData {
            cookies: true,
            history: false,
            ..Default::default()
        });
        assert!(core.cookies.is_empty());
    }

    #[test]
    fn test_web_view_core_clear_browsing_data_history_only() {
        let mut core = WebViewCore::new(
            WidgetKind::WebView,
            Rect::new(0, 0, 800, 600),
            "test_webview",
            "about:blank",
        );
        // Navigate to two URLs so the session history has a back stack
        core.set_url("https://page1.com".to_string());
        core.set_url("https://example.com".to_string());
        assert!(!core.browser_history.is_empty());
        assert!(core.history.can_go_back());

        core.clear_browsing_data(BrowsingData {
            cookies: false,
            history: true,
            ..Default::default()
        });
        assert!(core.browser_history.is_empty());
        assert!(!core.history.can_go_back());
    }

    #[test]
    fn test_web_view_core_handle_key_event_back() {
        let mut core = WebViewCore::new(
            WidgetKind::WebView,
            Rect::new(0, 0, 800, 600),
            "test_webview",
            "about:blank",
        );
        core.set_url("https://page1.com".to_string());
        core.set_url("https://page2.com".to_string());
        assert!(core.can_go_back());

        // Left arrow key (37) should trigger go_back
        core.handle_key_event(37, 0);
        assert_eq!(core.url(), "https://page1.com");
    }

    #[test]
    fn test_web_view_core_handle_key_event_forward() {
        let mut core = WebViewCore::new(
            WidgetKind::WebView,
            Rect::new(0, 0, 800, 600),
            "test_webview",
            "about:blank",
        );
        core.set_url("https://page1.com".to_string());
        core.set_url("https://page2.com".to_string());
        core.go_back();
        assert!(core.can_go_forward());

        // Right arrow key (39) should trigger go_forward
        core.handle_key_event(39, 0);
        assert_eq!(core.url(), "https://page2.com");
    }

    #[test]
    fn test_web_view_core_handle_key_event_reload() {
        let mut core = WebViewCore::new(
            WidgetKind::WebView,
            Rect::new(0, 0, 800, 600),
            "test_webview",
            "about:blank",
        );
        core.load_url("https://example.com");
        // F5 key (116) should trigger reload
        core.handle_key_event(116, 0);
        assert!(!core.is_loading());
        assert_eq!(core.load_progress(), 100);
    }

    #[test]
    fn test_web_view_core_handle_key_event_ctrl_r() {
        let mut core = WebViewCore::new(
            WidgetKind::WebView,
            Rect::new(0, 0, 800, 600),
            "test_webview",
            "about:blank",
        );
        core.load_url("https://example.com");
        // 'R' key (82) with Ctrl modifier (1) should trigger reload
        core.handle_key_event(82, 1);
        assert!(!core.is_loading());
        assert_eq!(core.load_progress(), 100);
    }

    #[test]
    fn test_web_view_core_initial_navigation_state() {
        let core = WebViewCore::new(
            WidgetKind::WebEngineView,
            Rect::new(0, 0, 1024, 768),
            "engine_view",
            "",
        );
        assert!(!core.can_go_back());
        assert!(!core.can_go_forward());
        assert_eq!(core.url(), "");
    }

    #[test]
    fn test_web_view_core_navigation_state_changes() {
        let mut core = WebViewCore::new(
            WidgetKind::WebView,
            Rect::new(10, 10, 640, 480),
            "nav_test",
            "about:blank",
        );
        core.set_url("https://page1.com".to_string());
        core.set_url("https://site-a.com".to_string());
        // After two navigations, we can go back
        assert!(core.can_go_back());
        assert!(!core.can_go_forward());

        core.go_back();
        // After going back, we can go forward again
        assert!(core.can_go_forward());
        assert_eq!(core.url(), "https://page1.com");
    }

    #[test]
    fn test_web_view_core_signals_are_initialized() {
        let core =
            WebViewCore::new(WidgetKind::WebView, Rect::new(0, 0, 800, 600), "signals_test", "");
        // Signals should exist and be ready to connect
        let _ = &core.loading_started;
        let _ = &core.loading_finished;
        let _ = &core.loading_progress;
        let _ = &core.title_changed;
        let _ = &core.url_changed;
        let _ = &core.navigation_state_changed;
        let _ = &core.console_message;
    }
}