git.lucas.co / cce-browser
web browser (Servo)
git clone https://git.lucas.co/cce-browser.git

examples/wpe_input.rs (2.7K)

 1 //! Proves input actually reaches the page.
 2 //!
 3 //! Loads a page that appends every event it receives to `document.title`,
 4 //! then drives `WebKitHost`'s input methods and reads the title back — which
 5 //! works because page-state sync is already wired. No JS-evaluation API needed.
 6 //!
 7 //! `cargo run --release -p cce-browser --features wpe --example wpe_input -- <url>`
 8 
 9 #[cfg(not(feature = "wpe"))]
10 fn main() {
11     eprintln!("build with --features wpe");
12 }
13 
14 #[cfg(feature = "wpe")]
15 #[derive(Debug, Clone, Copy)]
16 pub enum EditingCommand { Copy, Cut, Paste }
17 
18 #[cfg(feature = "wpe")]
19 #[path = "../src/pages.rs"]
20 mod pages;
21 #[cfg(feature = "wpe")]
22 #[path = "../src/downloads.rs"]
23 mod downloads;
24 #[cfg(feature = "wpe")]
25 #[path = "../src/wpe/mod.rs"]
26 mod wpe;
27 
28 #[cfg(feature = "wpe")]
29 fn main() {
30     use cce_ui::widget::{ElementState, Key, KeyEvent, MouseButton};
31 
32     let url = std::env::args()
33         .nth(1)
34         .unwrap_or_else(|| "http://127.0.0.1:8750/input.html".into());
35     let mut host = wpe::WebKitHost::new(url::Url::parse(&url).unwrap(), (1200, 800));
36 
37     let settle = |h: &mut wpe::WebKitHost, n: u32| {
38         for _ in 0..n {
39             h.pump();
40             std::thread::sleep(std::time::Duration::from_millis(50));
41         }
42     };
43 
44     settle(&mut host, 40);
45     println!("loaded: title={:?}", host.title());
46     host.focus(true);
47 
48     println!("-- pointer move + left click at (300,220) --");
49     host.mouse_move(300.0, 220.0);
50     settle(&mut host, 4);
51     host.mouse_button_ui(MouseButton::Left, true, 300.0, 220.0);
52     host.mouse_button_ui(MouseButton::Left, false, 300.0, 220.0);
53     settle(&mut host, 8);
54     println!("   title={:?}", host.title());
55 
56     println!("-- key 'a' --");
57     let key = |c: &str, pressed: bool| KeyEvent {
58         state: if pressed { ElementState::Pressed } else { ElementState::Released },
59         logical_key: Key::Character(c.into()),
60         text: Some(c.into()),
61         repeat: false,
62         ctrl: false,
63         shift: false,
64         alt: false,
65     };
66     host.key_ui(&key("a", true));
67     host.key_ui(&key("a", false));
68     settle(&mut host, 8);
69     println!("   title={:?}", host.title());
70 
71     println!("-- wheel down --");
72     host.wheel(0.0, -120.0, 300.0, 220.0);
73     settle(&mut host, 8);
74     let title = host.title().unwrap_or_default();
75     println!("   title={title:?}");
76 
77     println!("\n=== RESULT ===");
78     for (label, needle) in [
79         ("pointer move", "move"),
80         ("button down", "down0"),
81         ("button up", "up0"),
82         ("click", "click@300,220"),
83         ("keydown 'a'", "key:a"),
84         ("wheel", "wheel:"),
85     ] {
86         println!("  {:<14} {}", label, if title.contains(needle) { "OK" } else { "MISSING" });
87     }
88 }