web browser (Servo)
git clone https://git.lucas.co/cce-browser.git
examples/wpe_focus.rs (2.5K)
1 //! Proves the page believes it is focused — the condition WebKit gates the
2 //! text caret on.
3 //!
4 //! A caret is painted only in a frame that is both *focused* (the view's
5 //! focus) and *active* (the toplevel's `ACTIVE` state). Without either, a
6 //! field takes typing perfectly well and shows no caret, which is how it
7 //! shipped. `document.hasFocus()` reads the same two conditions, so the page
8 //! reports it into `document.title` and this reads it back, across the window
9 //! losing and regaining focus and across a tab switch.
10 //!
11 //! `cargo run --release -p cce-browser --example wpe_focus`
12
13 #[cfg(not(feature = "wpe"))]
14 fn main() {
15 eprintln!("build with --features wpe");
16 }
17
18 #[cfg(feature = "wpe")]
19 #[derive(Debug, Clone, Copy)]
20 pub enum EditingCommand { Copy, Cut, Paste }
21
22 #[cfg(feature = "wpe")]
23 #[path = "../src/pages.rs"]
24 mod pages;
25 #[cfg(feature = "wpe")]
26 #[path = "../src/downloads.rs"]
27 mod downloads;
28 #[cfg(feature = "wpe")]
29 // The host's vi channel and scripts.
30 #[path = "../src/vi.rs"]
31 #[allow(dead_code)]
32 mod vi;
33
34 #[path = "../src/wpe/mod.rs"]
35 mod wpe;
36
37 #[cfg(feature = "wpe")]
38 fn main() {
39 let page = "data:text/html,<input autofocus><script>\
40 setInterval(() => document.title = 'focus=' + document.hasFocus(), 50)\
41 </script>";
42 let mut host = wpe::WebKitHost::new(url::Url::parse(page).unwrap(), (400, 300));
43 let settle = |h: &mut wpe::WebKitHost, n: u32| {
44 for _ in 0..n {
45 h.pump(); h.frame_drawn();
46 std::thread::sleep(std::time::Duration::from_millis(50));
47 }
48 };
49
50 let mut ok = true;
51 let mut expect = |h: &mut wpe::WebKitHost, what: &str, want: bool| {
52 settle(h, 10);
53 let got = h.title();
54 let pass = got.as_deref() == Some(if want { "focus=true" } else { "focus=false" });
55 ok &= pass;
56 println!("{what:<34} {got:?} {}", if pass { "OK" } else { "WRONG" });
57 };
58
59 settle(&mut host, 30);
60 expect(&mut host, "loaded, window not focused", false);
61 host.focus(true);
62 expect(&mut host, "window focused", true);
63 host.focus(false);
64 expect(&mut host, "window unfocused", false);
65 host.focus(true);
66 expect(&mut host, "window refocused", true);
67
68 // A tab opened while the window is focused must arrive focused too.
69 host.open_tab(url::Url::parse(page).unwrap());
70 settle(&mut host, 30);
71 expect(&mut host, "new tab", true);
72 host.activate(0);
73 expect(&mut host, "back to the first tab", true);
74
75 println!("\nfocus: {}", if ok { "OK" } else { "BROKEN" });
76 std::process::exit(if ok { 0 } else { 1 });
77 }