web browser (Servo)
git clone https://git.lucas.co/cce-browser.git
examples/wpe_input.rs (2.8K)
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 // The host's vi channel and scripts.
26 #[path = "../src/vi.rs"]
27 #[allow(dead_code)]
28 mod vi;
29
30 #[path = "../src/wpe/mod.rs"]
31 mod wpe;
32
33 #[cfg(feature = "wpe")]
34 fn main() {
35 use cce_ui::widget::{ElementState, Key, KeyEvent, MouseButton};
36
37 let url = std::env::args()
38 .nth(1)
39 .unwrap_or_else(|| "http://127.0.0.1:8750/input.html".into());
40 let mut host = wpe::WebKitHost::new(url::Url::parse(&url).unwrap(), (1200, 800));
41
42 let settle = |h: &mut wpe::WebKitHost, n: u32| {
43 for _ in 0..n {
44 h.pump(); h.frame_drawn();
45 std::thread::sleep(std::time::Duration::from_millis(50));
46 }
47 };
48
49 settle(&mut host, 40);
50 println!("loaded: title={:?}", host.title());
51 host.focus(true);
52
53 println!("-- pointer move + left click at (300,220) --");
54 host.mouse_move(300.0, 220.0);
55 settle(&mut host, 4);
56 host.mouse_button_ui(MouseButton::Left, true, 300.0, 220.0);
57 host.mouse_button_ui(MouseButton::Left, false, 300.0, 220.0);
58 settle(&mut host, 8);
59 println!(" title={:?}", host.title());
60
61 println!("-- key 'a' --");
62 let key = |c: &str, pressed: bool| KeyEvent {
63 state: if pressed { ElementState::Pressed } else { ElementState::Released },
64 logical_key: Key::Character(c.into()),
65 text: Some(c.into()),
66 repeat: false,
67 ctrl: false,
68 shift: false,
69 alt: false,
70 };
71 host.key_ui(&key("a", true));
72 host.key_ui(&key("a", false));
73 settle(&mut host, 8);
74 println!(" title={:?}", host.title());
75
76 println!("-- wheel down --");
77 host.wheel(0.0, -120.0, 300.0, 220.0);
78 settle(&mut host, 8);
79 let title = host.title().unwrap_or_default();
80 println!(" title={title:?}");
81
82 println!("\n=== RESULT ===");
83 for (label, needle) in [
84 ("pointer move", "move"),
85 ("button down", "down0"),
86 ("button up", "up0"),
87 ("click", "click@300,220"),
88 ("keydown 'a'", "key:a"),
89 ("wheel", "wheel:"),
90 ] {
91 println!(" {:<14} {}", label, if title.contains(needle) { "OK" } else { "MISSING" });
92 }
93 }