diff --git a/compositor/host/face_test.mbt b/compositor/host/face_test.mbt index 0e70e99..3e5eb33 100644 --- a/compositor/host/face_test.mbt +++ b/compositor/host/face_test.mbt @@ -264,5 +264,5 @@ test "the Face unlock window: screen fill-light boosts backlight and restores on assert_true(comp.shell.face.window is None) assert_eq(@shell.read_backlight_percent(), 50) comp.close() - @shell.set_backlight_root("/sys/class/backlight") + @shell.set_backlight_root(@shell.default_backlight_root()) } diff --git a/compositor/host/input_test.mbt b/compositor/host/input_test.mbt index 0e3a87b..9a258bb 100644 --- a/compositor/host/input_test.mbt +++ b/compositor/host/input_test.mbt @@ -1876,5 +1876,5 @@ test "settings Power tab displays and adjusts backlight brightness" { assert_true(bright_row >= 1) click_at(comp, pr.x + 25, pr.y + 40 + bright_row * 28 + 13, 10U) assert_eq(@shell.read_backlight_percent(), 70) - @shell.set_backlight_root("/sys/class/backlight") + @shell.set_backlight_root(@shell.default_backlight_root()) } diff --git a/compositor/shell/backlight.mbt b/compositor/shell/backlight.mbt index 1c90f72..d5ec1ea 100644 --- a/compositor/shell/backlight.mbt +++ b/compositor/shell/backlight.mbt @@ -1,6 +1,17 @@ ///| Linux backlight provider for the `power` capability (sysfs read + logind SetBrightness fallback). -let backlight_root : Ref[String] = { val: "/sys/class/backlight" } +///| Where backlight devices are: /sys/class/backlight, or $CHANDRA_BACKLIGHT_ROOT (the test runner +/// gives every test a fake one, so no test changes the real screen's brightness). +pub fn default_backlight_root() -> String { + let env = @sys.getenv("CHANDRA_BACKLIGHT_ROOT") + if env.length() > 0 { + env + } else { + "/sys/class/backlight" + } +} + +let backlight_root : Ref[String] = { val: default_backlight_root() } let backlight_sample_sec : Ref[Int] = { val: -1 } @@ -124,7 +135,9 @@ pub fn write_backlight_percent( let raw0 = (clamped.to_int64() * dev.max + 50L) / 100L let raw = if raw0 < 1L { 1L } else if raw0 > dev.max { dev.max } else { raw0 } let data = @utf8.encode(raw.to_string() + "\n") - if @sys.write_file_bytes(dev.path + "/brightness", data) < 0 { + // Under test (CHANDRA_TEST_CONFIG=1) never ask logind: that would change the real screen. + if @sys.write_file_bytes(dev.path + "/brightness", data) < 0 && + @sys.getenv("CHANDRA_TEST_CONFIG") != "1" { let sid = @sys.getenv("XDG_SESSION_ID").trim(chars=" \n\r\t").to_string() let sess = if sid.length() > 0 && safe_backlight_name(sid) { "/org/freedesktop/login1/session/" + sid diff --git a/scripts/test-native.py b/scripts/test-native.py index c375258..4721a4c 100755 --- a/scripts/test-native.py +++ b/scripts/test-native.py @@ -18,6 +18,10 @@ BEGIN, END = "----- BEGIN MOON TEST RESULT -----", "----- END MOON TEST RESULT - def environment(): env = dict(os.environ, CHANDRA_TEST_CONFIG="1") + # Nothing reaches the real session through logind (screen brightness, ...). Tests that want a + # system bus give their own address. + env.pop("XDG_SESSION_ID", None) + env["DBUS_SYSTEM_BUS_ADDRESS"] = "unix:path=/nonexistent" env["PATH"] = f"{ROOT}/.tools/bin:{os.path.expanduser('~')}/.moon/bin:" + env["PATH"] wasmer = ROOT + "/.tools/wasmer" if os.path.isdir(wasmer + "/lib"): @@ -42,6 +46,12 @@ def run(test, env, timeout): with open(f"{tmp}/d/applications/test-{n.lower()}.desktop", "w") as f: f.write(f"[Desktop Entry]\nType=Application\nName=Test {n}\nExec=true\nCategories=Utility;\n") os.mkdir(tmp + "/r", 0o700) + # A fake backlight (25 of 100), so a test that changes the brightness can't touch the real screen. + os.makedirs(tmp + "/bl/intel_backlight") + for n, v in (("max_brightness", "100"), ("brightness", "25")): + with open(f"{tmp}/bl/intel_backlight/{n}", "w") as f: + f.write(v + "\n") + e["CHANDRA_BACKLIGHT_ROOT"] = tmp + "/bl" t = time.monotonic() # Output goes to a file: a pipe would stay open as long as anything the test left running. with open(tmp + "/out", "w+b") as out: