///| The network panel module (components/network) driven through the shell, against a fake /// network backend: the status it shows, its Wi-Fi list, and the actions its clicks and keys ask /// for. Needs the modules: run scripts/build-modules.sh. ///| fn net_status() -> @network.NetStatus { { state: Wifi, name: "Home", signal: 72, message: "", wifi_enabled: true, airplane: false, managed: true, has_wifi: true, } } ///| fn net_comp(tag : String) -> (@host.ChandraCompositor, @network.FakeNet) { let comp = new_comp(tag) let fake = @network.FakeNet::new(net_status(), [ { ssid: "Home", signal: 72, secured: true, active: true, saved: true }, { ssid: "Office", signal: 55, secured: true, active: false, saved: false }, { ssid: "Cafe", signal: 30, secured: false, active: false, saved: false }, { ssid: "Lab", signal: 20, secured: true, active: false, saved: true }, ]) comp.shell.set_network_backend(fake as &@network.NetBackend) ignore(comp.compose_frame()) (comp, fake) } ///| The middle of popup row `i` (6 px margin, 30 px rows), `dx` from its left edge. fn net_row(comp : @host.ChandraCompositor, i : Int, dx? : Int = 60) -> (Int, Int) { let pr = comp.shell.slot_popup_rect(comp.shell.slot_of("network")) (pr.x + dx, pr.y + 6 + i * 30 + 14) } ///| fn click_row(comp : @host.ChandraCompositor, i : Int, t : UInt, dx? : Int = 60) -> Unit { let (x, y) = net_row(comp, i, dx~) click_at(comp, x, y, t) } ///| fn last_action(fake : @network.FakeNet) -> @network.NetAction? { fake.actions.last() } ///| test "the network module lists Wi-Fi networks and asks to connect, disconnect and forget" { let (comp, fake) = net_comp("net-list") let slot = comp.shell.slot_of("network") assert_true(comp.shell.slot_rect(slot).w > 0) // it is in the default panel assert_true(fake.ticks > 0) click_module(comp, "network", 1U) assert_eq(last_action(fake), Some(Scan)) // opening the popup scans // Status, Wi-Fi, Airplane mode, the heading, four networks. let pr = comp.shell.slot_popup_rect(slot) assert_eq(pr.h, 12 + 8 * 30) // An open network connects at once; so does a saved one. click_row(comp, 6, 10U) assert_eq(last_action(fake), Some(Connect("Cafe", ""))) click_row(comp, 7, 15U) assert_eq(last_action(fake), Some(Connect("Lab", ""))) // "Forget" at the right of a saved network. click_row(comp, 7, 20U, dx=pr.w - 30) assert_eq(last_action(fake), Some(Forget("Lab"))) // The one in use disconnects. click_row(comp, 4, 25U) assert_eq(last_action(fake), Some(Disconnect)) assert_eq(fake.actions.length(), 5) } ///| test "a new secured network asks for its password in the popup" { let (comp, fake) = net_comp("net-password") let slot = comp.shell.slot_of("network") click_module(comp, "network", 1U) click_row(comp, 5, 5U) // Office: a password field opens under it assert_eq(comp.shell.slot_popup_rect(slot).h, 12 + 9 * 30) assert_eq(last_action(fake), Some(Scan)) for k in ["s", "3", "c", "r", "e", "t", "x", "Backspace", "Space", "1"] { comp.shell.key_down(k) } comp.shell.key_down("Enter") ignore(comp.compose_frame()) assert_eq(last_action(fake), Some(Connect("Office", "s3cret 1"))) assert_eq(comp.shell.slot_popup_rect(slot).h, 12 + 8 * 30) // the field is gone // Escape leaves the field first, then the popup. click_row(comp, 5, 10U) comp.shell.key_down("p") comp.shell.key_down("Escape") ignore(comp.compose_frame()) assert_eq(comp.shell.slot_popup_rect(slot).h, 12 + 8 * 30) comp.shell.key_down("Escape") ignore(comp.compose_frame()) assert_eq(comp.shell.slot_popup_rect(slot).w, 0) assert_eq(last_action(fake), Some(Connect("Office", "s3cret 1"))) // nothing sent for "p" } ///| test "the Wi-Fi and airplane switches, and status only without NetworkManager" { let (comp, fake) = net_comp("net-switch") let slot = comp.shell.slot_of("network") click_module(comp, "network", 1U) click_row(comp, 1, 5U) // Wi-Fi off: the list goes assert_eq(last_action(fake), Some(SetWifi(false))) assert_eq(comp.shell.slot_popup_rect(slot).h, 12 + 3 * 30) click_row(comp, 1, 10U) assert_eq(last_action(fake), Some(SetWifi(true))) click_row(comp, 2, 15U) assert_eq(last_action(fake), Some(SetAirplane(true))) // A message shows under the status. fake.status = { ..fake.status, message: "Couldn't connect to Office: wrong password?" } ignore(comp.compose_frame()) assert_eq(comp.shell.slot_popup_rect(slot).h, 12 + 4 * 30) // Read-only (no NetworkManager): the status and why, nothing to switch. fake.status = { ..net_status(), state: Wired, name: "enp3s0", managed: false, message: "NetworkManager isn't running: status only", } ignore(comp.compose_frame()) assert_eq(comp.shell.slot_popup_rect(slot).h, 12 + 2 * 30) let n = fake.actions.length() click_row(comp, 1, 20U) assert_eq(fake.actions.length(), n) }