///| The battery: reading a fake /sys/class/power_supply, and the battery panel module drawing it. ///| Write a fake power supply tree: (directory, [(file, contents)]) under a fresh directory. fn fake_supply(tag : String, entries : Array[(String, Array[(String, String)])]) -> String { let root = "/tmp/chandra-power-" + tag + "-" + @sys.getpid().to_string() for e in entries { let (name, files) = e ignore(@sys.mkdir_p(root + "/" + name)) for f in files { let (file, text) = f ignore(@sys.write_file_bytes(root + "/" + name + "/" + file, @sys.str_to_utf8_bytes(text + "\n"))) } } ignore(@sys.mkdir_p(root)) root } ///| fn bat(status : String, capacity : Int, now : Int, full : Int, rate : Int) -> Array[(String, String)] { [ ("type", "Battery"), ("status", status), ("capacity", capacity.to_string()), ("energy_now", now.to_string()), ("energy_full", full.to_string()), ("power_now", rate.to_string()), ] } ///| test "no battery: percent -1, state none" { let root = fake_supply("none", [("AC", [("type", "Mains"), ("online", "1")])]) assert_eq(@shell.read_battery(root~), { percent: -1, state: 0, secs_left: 0 }) } ///| test "a discharging battery: percent from energy, time left at the current power" { let root = fake_supply("dis", [("BAT0", bat("Discharging", 59, 30000000, 50000000, 10000000))]) // 30 Wh of 50 Wh at 10 W: 60 %, three hours. assert_eq(@shell.read_battery(root~), { percent: 60, state: 1, secs_left: 3 * 3600 }) } ///| test "charging: time until full; full and not charging have no time" { let root = fake_supply("chg", [("BAT0", bat("Charging", 40, 20000000, 50000000, 15000000))]) assert_eq(@shell.read_battery(root~), { percent: 40, state: 2, secs_left: 2 * 3600 }) let root = fake_supply("full", [("BAT0", bat("Full", 100, 50000000, 50000000, 0))]) assert_eq(@shell.read_battery(root~), { percent: 100, state: 3, secs_left: 0 }) let root = fake_supply("hold", [("BAT0", bat("Not charging", 80, 40000000, 50000000, 0))]) assert_eq(@shell.read_battery(root~), { percent: 80, state: 4, secs_left: 0 }) } ///| test "two packs in charge units count together; a negative current is a discharge" { let root = fake_supply("two", [ ( "BAT0", [ ("type", "Battery"), ("status", "Discharging"), ("capacity", "50"), ("charge_now", "2000000"), ("charge_full", "4000000"), ("current_now", "-1000000"), ], ), ( "BAT1", [ ("type", "Battery"), ("status", "Not charging"), ("capacity", "100"), ("charge_now", "4000000"), ("charge_full", "4000000"), ("current_now", "0"), ], ), ]) // 6 of 8 Ah, draining at 1 A: six hours. assert_eq(@shell.read_battery(root~), { percent: 75, state: 1, secs_left: 6 * 3600 }) } ///| test "a battery without energy or charge files uses its capacity; time is unknown" { let root = fake_supply("cap", [("BAT1", [("status", "Discharging"), ("capacity", "42")])]) assert_eq(@shell.read_battery(root~), { percent: 42, state: 1, secs_left: 0 }) } ///| test "time left is a running average while the state holds" { let m = @shell.BatteryMeter::new() m.feed({ percent: 50, state: 1, secs_left: 4000 }) assert_eq(m.cur.secs_left, 4000) m.feed({ percent: 50, state: 1, secs_left: 8000 }) assert_eq(m.cur.secs_left, 5000) // Plugging in starts over. m.feed({ percent: 50, state: 2, secs_left: 1200 }) assert_eq(m.cur.secs_left, 1200) } ///| fn module_texts(comp : @host.ChandraCompositor) -> Array[String] { let out : Array[String] = [] for op in comp.shell.slot_ops(comp.shell.slot_of("battery")) { if op is Text(t) { out.push(t.text) } } out } ///| A compositor whose panel has the battery module. A fresh panel gets it from the (fake) battery; /// a panel.edn another test saved may not have it, so it is added (in place of their extra clock, /// which would not leave room). fn battery_comp(tag : String) -> @host.ChandraCompositor { let comp = new_comp(tag) if comp.shell.slot_of("battery") == "" { let extra = comp.shell.slot_of("clock-binary") if extra != "" { ignore(comp.shell.unload_slot(extra)) } ignore(comp.shell.load_module("battery")) ignore(comp.compose_frame()) } comp } ///| Wait for the wall clock's next second, so the shell samples the battery again. fn next_second(comp : @host.ChandraCompositor) -> Unit { let s = @sys.wall_time_field(5) let t0 = @sys.monotonic_ms() while @sys.wall_time_field(5) == s && @sys.monotonic_ms() - t0 < 2000UL { @sys.sleep_ms(20) } ignore(comp.compose_frame()) } ///| test "the battery module shows charge and time left, its settings, and warns when critical" { let root = fake_supply("module", [("BAT0", bat("Discharging", 60, 30000000, 50000000, 10000000))]) @shell.set_power_supply_root(root) let comp = battery_comp("battery") let slot = comp.shell.slot_of("battery") assert_true(comp.shell.slot_rect(slot).w > 0) assert_eq(module_texts(comp), ["60% 3:00"]) assert_eq(comp.shell.slot_config(slot).map(i => i.key), ["percent", "time", "no-battery"]) comp.shell.set_module_setting(slot, "time", "off") assert_eq(module_texts(comp), ["60%"]) comp.shell.set_module_setting(slot, "time", "on") // A click opens the details. click_module(comp, "battery", 1U) catch { _ => fail("battery module not placed") } assert_true(comp.shell.slot_popup_rect(slot).w > 0) click_module(comp, "battery", 5U) catch { _ => fail("battery module not placed") } assert_eq(comp.shell.slot_popup_rect(slot).w, 0) // Down to 8 %: the warning opens by itself. ignore( @sys.write_file_bytes(root + "/BAT0/energy_now", @sys.str_to_utf8_bytes("4000000\n")), ) next_second(comp) ignore(comp.compose_frame()) // (The time left eases toward the new 24 minutes, a quarter per sample.) assert_true(module_texts(comp)[0].has_prefix("8% ")) assert_true(comp.shell.slot_popup_rect(slot).w > 0) @shell.set_power_supply_root("/sys/class/power_supply") } ///| test "without a battery the module shows a plug, or nothing" { let root = fake_supply("module-none", []) @shell.set_power_supply_root(root) let comp = battery_comp("battery-none") let slot = comp.shell.slot_of("battery") let ops = comp.shell.slot_ops(slot) assert_true(ops.length() > 0) assert_eq(module_texts(comp), []) comp.shell.set_module_setting(slot, "no-battery", "Show nothing") assert_eq(comp.shell.slot_ops(slot).length(), 0) @shell.set_power_supply_root("/sys/class/power_supply") }