nixos/tests/chromium: Simplify the logic (#110715)

- Improve the documentation (Python docstrings for functions and more
  meaningful xdotool script names).
- Make more use of the existing methods (e.g. wait_until_succeeds(), and
  send_key())
  - Note: This increases timeouts from 60 seconds to (currently)
    15 minutes but the defaults from test-driver.py should be fine.

This should make it simpler to read the code, understand the test
output, and drop the custom xdotool scripts in the future.
This commit is contained in:
Michael Weiss 2021-01-24 20:35:29 +01:00 committed by GitHub
parent 52fc21b10f
commit 27955d37e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -51,7 +51,7 @@ mapAttrs (channel: chromiumPkg: makeTest rec {
testScript = let testScript = let
xdo = name: text: let xdo = name: text: let
xdoScript = pkgs.writeText "${name}.xdo" text; xdoScript = pkgs.writeText "${name}.xdo" text;
in "${pkgs.xdotool}/bin/xdotool '${xdoScript}'"; in "${pkgs.xdotool}/bin/xdotool ${xdoScript}";
in '' in ''
import shlex import shlex
from contextlib import contextmanager, _GeneratorContextManager from contextlib import contextmanager, _GeneratorContextManager
@ -76,96 +76,59 @@ mapAttrs (channel: chromiumPkg: makeTest rec {
def create_new_win(): def create_new_win():
"""Creates a new Chromium window."""
with machine.nested("Creating a new Chromium window"): with machine.nested("Creating a new Chromium window"):
status, _ = machine.execute( machine.wait_until_succeeds(
ru( ru(
"${xdo "new-window" '' "${xdo "create_new_win-select_main_window" ''
search --onlyvisible --name "startup done" search --onlyvisible --name "startup done"
windowfocus --sync windowfocus --sync
windowactivate --sync windowactivate --sync
''}" ''}"
) )
) )
if status == 0: machine.send_key("ctrl-n")
machine.execute( # Wait until the new window appears:
ru( machine.wait_until_succeeds(
"${xdo "new-window" ''
key Ctrl+n
''}"
)
)
def close_win():
def try_close(_):
status, _ = machine.execute(
ru( ru(
"${xdo "close-window" '' "${xdo "create_new_win-wait_for_window" ''
search --onlyvisible --name "new tab" search --onlyvisible --name "New Tab"
windowfocus --sync windowfocus --sync
windowactivate --sync windowactivate --sync
''}" ''}"
) )
) )
if status == 0:
machine.execute(
ru(
"${xdo "close-window" ''
key Ctrl+w
''}"
)
)
for _ in range(1, 20):
status, _ = machine.execute(
ru(
"${xdo "wait-for-close" ''
search --onlyvisible --name "new tab"
''}"
)
)
if status != 0:
return True
machine.sleep(1)
return False
retry(try_close)
def wait_for_new_win(): def close_new_tab_win():
ret = False """Closes the Chromium window with the title "New Tab"."""
with machine.nested("Waiting for new Chromium window to appear"): machine.wait_until_succeeds(
for _ in range(1, 20): ru(
status, _ = machine.execute( "${xdo "close_new_tab_win-select_main_window" ''
ru( search --onlyvisible --name "New Tab"
"${xdo "wait-for-window" '' windowfocus --sync
search --onlyvisible --name "new tab" windowactivate --sync
windowfocus --sync ''}"
windowactivate --sync )
''}" )
) machine.send_key("ctrl-w")
) # Wait until the closed window disappears:
if status == 0: machine.wait_until_fails(
ret = True ru(
machine.sleep(10) "${xdo "close_new_tab_win-wait_for_close" ''
break search --onlyvisible --name "New Tab"
machine.sleep(1) ''}"
return ret )
)
def create_and_wait_for_new_win():
for _ in range(1, 3):
create_new_win()
if wait_for_new_win():
return True
assert False, "new window did not appear within 60 seconds"
@contextmanager @contextmanager
def test_new_win(description): def test_new_win(description):
create_and_wait_for_new_win() create_new_win()
with machine.nested(description): with machine.nested(description):
yield yield
close_win() # Close the newly created window:
machine.send_key("ctrl-w")
machine.wait_for_x() machine.wait_for_x()
@ -192,9 +155,11 @@ mapAttrs (channel: chromiumPkg: makeTest rec {
) )
) )
create_and_wait_for_new_win() create_new_win()
# Optional: Wait for the new tab page to fully load before taking the screenshot:
machine.wait_for_text("Web Store")
machine.screenshot("empty_windows") machine.screenshot("empty_windows")
close_win() close_new_tab_win()
machine.screenshot("startup_done") machine.screenshot("startup_done")
@ -202,7 +167,7 @@ mapAttrs (channel: chromiumPkg: makeTest rec {
machine.succeed( machine.succeed(
ru( ru(
"${xdo "type-url" '' "${xdo "type-url" ''
search --sync --onlyvisible --name "new tab" search --sync --onlyvisible --name "New Tab"
windowfocus --sync windowfocus --sync
type --delay 1000 "chrome://sandbox" type --delay 1000 "chrome://sandbox"
''}" ''}"
@ -212,7 +177,7 @@ mapAttrs (channel: chromiumPkg: makeTest rec {
machine.succeed( machine.succeed(
ru( ru(
"${xdo "submit-url" '' "${xdo "submit-url" ''
search --sync --onlyvisible --name "new tab" search --sync --onlyvisible --name "New Tab"
windowfocus --sync windowfocus --sync
key --delay 1000 Return key --delay 1000 Return
''}" ''}"
@ -224,7 +189,7 @@ mapAttrs (channel: chromiumPkg: makeTest rec {
machine.succeed( machine.succeed(
ru( ru(
"${xdo "find-window" '' "${xdo "find-window" ''
search --sync --onlyvisible --name "sandbox status" search --sync --onlyvisible --name "Sandbox Status"
windowfocus --sync windowfocus --sync
''}" ''}"
) )
@ -258,7 +223,7 @@ mapAttrs (channel: chromiumPkg: makeTest rec {
machine.succeed( machine.succeed(
ru( ru(
"${xdo "find-window-after-copy" '' "${xdo "find-window-after-copy" ''
search --onlyvisible --name "sandbox status" search --onlyvisible --name "Sandbox Status"
''}" ''}"
) )
) )