<commit_before>{ system ? builtins.currentSystem
, config ? {}
, pkgs ? import ../.. { inherit system config; }
, channelMap ? { # Maps "channels" to packages
    stable        = pkgs.chromium;
    beta          = pkgs.chromiumBeta;
    dev           = pkgs.chromiumDev;
    ungoogled     = pkgs.ungoogled-chromium;
    chrome-stable = pkgs.google-chrome;
    chrome-beta   = pkgs.google-chrome-beta;
    chrome-dev    = pkgs.google-chrome-dev;
  }
}:

with import ../lib/testing-python.nix { inherit system pkgs; };
with pkgs.lib;

mapAttrs (channel: chromiumPkg: makeTest rec {
  name = "chromium-${channel}";
  meta = {
    maintainers = with maintainers; [ aszlig primeos ];
    # https://github.com/NixOS/hydra/issues/591#issuecomment-435125621
    inherit (chromiumPkg.meta) timeout;
  };

  enableOCR = true;

  user = "alice";

  machine.imports = [ ./common/user-account.nix ./common/x11.nix ];
  machine.virtualisation.memorySize = 2047;
  machine.test-support.displayManager.auto.user = user;
  machine.environment.systemPackages = [ chromiumPkg ];

  startupHTML = pkgs.writeText "chromium-startup.html" ''
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Chromium startup notifier</title>
    </head>
    <body onload="javascript:document.title='startup done'">
      <img src="file://${pkgs.fetchurl {
        url = "https://nixos.org/logo/nixos-hex.svg";
        sha256 = "07ymq6nw8kc22m7kzxjxldhiq8gzmc7f45kq2bvhbdm0w5s112s4";
      }}" />
    </body>
    </html>
  '';

  testScript = let
    xdo = name: text: let
      xdoScript = pkgs.writeText "${name}.xdo" text;
    in "${pkgs.xdotool}/bin/xdotool '${xdoScript}'";
  in ''
    import shlex
    from contextlib import contextmanager, _GeneratorContextManager


    # Run as user alice
    def ru(cmd):
        return "su - ${user} -c " + shlex.quote(cmd)


    def get_browser_binary():
        """Returns the name of the browser binary."""
        pname = "${getName chromiumPkg.name}"
        if pname.find("chromium") != -1:
            return "chromium"  # Same name for all channels and ungoogled-chromium
        if pname == "google-chrome":
            return "google-chrome-stable"
        if pname == "google-chrome-dev":
            return "google-chrome-unstable"
        # For google-chrome-beta and as fallback:
        return pname


    def create_new_win():
        with machine.nested("Creating a new Chromium window"):
            machine.execute(
                ru(
                    "${xdo "new-window" ''
                      search --onlyvisible --name "startup done"
                      windowfocus --sync
                      windowactivate --sync
                    ''}"
                )
            )
            machine.execute(
                ru(
                    "${xdo "new-window" ''
                      key Ctrl+n
                    ''}"
                )
            )


    def close_win():
        def try_close(_):
            status, _ = machine.execute(
                ru(
                    "${xdo "close-window" ''
                      search --onlyvisible --name "new tab"
                      windowfocus --sync
                      windowactivate --sync
                    ''}"
                )
            )
            if status == 0:
                machine.execute(
                    ru(
                        "${xdo "close-window" ''
                          key Ctrl+w
                        ''}"
                    )
                )
            for _ in range(1, 20):
                status, out = 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():
        ret = False
        with machine.nested("Waiting for new Chromium window to appear"):
            for _ in range(1, 20):
                status, out = machine.execute(
                    ru(
                        "${xdo "wait-for-window" ''
                          search --onlyvisible --name "new tab"
                          windowfocus --sync
                          windowactivate --sync
                        ''}"
                    )
                )
                if status == 0:
                    ret = True
                    machine.sleep(10)
                    break<commit_msg>nixos/tests/chromium: Fix another potential race condition<commit_after>{ system ? builtins.currentSystem
, config ? {}
, pkgs ? import ../.. { inherit system config; }
, channelMap ? { # Maps "channels" to packages
    stable        = pkgs.chromium;
    beta          = pkgs.chromiumBeta;
    dev           = pkgs.chromiumDev;
    ungoogled     = pkgs.ungoogled-chromium;
    chrome-stable = pkgs.google-chrome;
    chrome-beta   = pkgs.google-chrome-beta;
    chrome-dev    = pkgs.google-chrome-dev;
  }
}:

with import ../lib/testing-python.nix { inherit system pkgs; };
with pkgs.lib;

mapAttrs (channel: chromiumPkg: makeTest rec {
  name = "chromium-${channel}";
  meta = {
    maintainers = with maintainers; [ aszlig primeos ];
    # https://github.com/NixOS/hydra/issues/591#issuecomment-435125621
    inherit (chromiumPkg.meta) timeout;
  };

  enableOCR = true;

  user = "alice";

  machine.imports = [ ./common/user-account.nix ./common/x11.nix ];
  machine.virtualisation.memorySize = 2047;
  machine.test-support.displayManager.auto.user = user;
  machine.environment.systemPackages = [ chromiumPkg ];

  startupHTML = pkgs.writeText "chromium-startup.html" ''
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Chromium startup notifier</title>
    </head>
    <body onload="javascript:document.title='startup done'">
      <img src="file://${pkgs.fetchurl {
        url = "https://nixos.org/logo/nixos-hex.svg";
        sha256 = "07ymq6nw8kc22m7kzxjxldhiq8gzmc7f45kq2bvhbdm0w5s112s4";
      }}" />
    </body>
    </html>
  '';

  testScript = let
    xdo = name: text: let
      xdoScript = pkgs.writeText "${name}.xdo" text;
    in "${pkgs.xdotool}/bin/xdotool '${xdoScript}'";
  in ''
    import shlex
    from contextlib import contextmanager, _GeneratorContextManager


    # Run as user alice
    def ru(cmd):
        return "su - ${user} -c " + shlex.quote(cmd)


    def get_browser_binary():
        """Returns the name of the browser binary."""
        pname = "${getName chromiumPkg.name}"
        if pname.find("chromium") != -1:
            return "chromium"  # Same name for all channels and ungoogled-chromium
        if pname == "google-chrome":
            return "google-chrome-stable"
        if pname == "google-chrome-dev":
            return "google-chrome-unstable"
        # For google-chrome-beta and as fallback:
        return pname


    def create_new_win():
        with machine.nested("Creating a new Chromium window"):
            status, _ = machine.execute(
                ru(
                    "${xdo "new-window" ''
                      search --onlyvisible --name "startup done"
                      windowfocus --sync
                      windowactivate --sync
                    ''}"
                )
            )
            if status == 0:
                machine.execute(
                    ru(
                        "${xdo "new-window" ''
                          key Ctrl+n
                        ''}"
                    )
                )


    def close_win():
        def try_close(_):
            status, _ = machine.execute(
                ru(
                    "${xdo "close-window" ''
                      search --onlyvisible --name "new tab"
                      windowfocus --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():
        ret = False
        with machine.nested("Waiting for new Chromium window to appear"):
            for _ in range(1, 20):
                status, _ = machine.execute(
                    ru(
                        "${xdo "wait-for-window" ''
                          search --onlyvisible --name "new tab"
                          windowfocus --sync
                          windowactivate --sync
                        ''}"
                    )
                )
                if status == 0:
                    ret = True
                    machine.sleep(10)
                    break
{'fraction_non_alphanumeric': 0.11286342667095806, 'fraction_numerical': 0.011479454994099346, 'mean_word_length': 1.289855072463768, 'pattern_counts': {'":': 4, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '779588', 'n_tokens_mistral': 2787, 'n_tokens_neox': 2528, 'n_words': 564}