chromium/chrome/test/chromedriver/BUILD.gn

# Copyright 2015 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import("//build/buildflag_header.gni")
import("//build/config/python.gni")
import("//build/config/ui.gni")
if (is_linux) {
  import("//build/linux/strip_binary.gni")
}
import("//build/util/lastchange.gni")
import("//testing/test.gni")
import("//third_party/ffmpeg/ffmpeg_options.gni")
import("//ui/webui/resources/tools/minify_js.gni")

declare_args() {
  # Make macOS request permissions for the chrome binary instead of its parent
  # process (usually the Terminmal.app).
  # This is required for running chrome tests on Google's CI machines, but for
  # some vendor builds like Microsoft Teams they need to turn this off.
  chromedriver_disclaim_responsibility = true
}

action("embed_js_in_cpp") {
  script = "embed_js_in_cpp.py"

  js_files = [
    "js/call_function.js",
    "js/dispatch_touch_event.js",
    "js/execute_async_script.js",
    "js/execute_script.js",
    "js/focus.js",
    "js/get_element_location.js",
    "js/get_element_region.js",
    "js/is_option_element_toggleable.js",
  ]

  inputs = [ "cpp_source.py" ] + js_files

  outputs = [
    "$target_gen_dir/chrome/js.cc",
    "$target_gen_dir/chrome/js.h",
  ]
  args = [
    "--directory",
    rebase_path("$target_gen_dir/chrome", root_build_dir),
  ]
  args += rebase_path(js_files, root_build_dir)
}

action("embed_mobile_devices_in_cpp") {
  script = "embed_mobile_devices_in_cpp.py"

  ts_files = [ "//third_party/devtools-frontend/src/front_end/models/emulation/EmulatedDevices.ts" ]
  inputs = [
    "//chrome/VERSION",
    "cpp_source.py",
  ]
  inputs += ts_files

  outputs = [
    "$target_gen_dir/chrome/mobile_device_list.cc",
    "$target_gen_dir/chrome/mobile_device_list.h",
  ]
  args = [
    "--directory",
    rebase_path("$target_gen_dir/chrome", root_build_dir),
  ]
  args += rebase_path(ts_files, root_build_dir)
}

minify_js("minify_bidimapper") {
  deps = [ "//third_party/bidimapper:bidimapper" ]
  input_dir =
      get_label_info("//third_party/bidimapper:bidimapper", "target_gen_dir")
  in_folder = "$input_dir"
  in_files = [ "mapper.js" ]
  out_folder = "$target_gen_dir/bidimapper/minified"
}

action("embed_bidimapper_in_cpp") {
  script = "embed_bidimapper_in_cpp.py"

  input_dir = get_label_info(":minify_bidimapper", "target_gen_dir")
  js_files = [ "$input_dir/bidimapper/minified/mapper.js" ]

  inputs = [ "cpp_source.py" ] + js_files

  outputs = [
    "$target_gen_dir/bidimapper/bidimapper.cc",
    "$target_gen_dir/bidimapper/bidimapper.h",
  ]
  args = [
    "--directory",
    rebase_path("$target_gen_dir/bidimapper", root_build_dir),
  ]
  args += rebase_path(js_files, root_build_dir)

  deps = [ ":minify_bidimapper" ]
}

action("embed_user_data_dir_in_cpp") {
  script = "embed_user_data_dir_in_cpp.py"

  files = [
    "chrome/preferences.txt",
    "chrome/local_state.txt",
  ]

  inputs = [ "cpp_source.py" ] + files
  outputs = [
    "$target_gen_dir/chrome/user_data_dir.cc",
    "$target_gen_dir/chrome/user_data_dir.h",
  ]

  args = [
    "--directory",
    rebase_path("$target_gen_dir/chrome", root_build_dir),
  ]
  args += rebase_path(files, root_build_dir)
}

source_set("automation_client_lib") {
  testonly = true
  sources = [
    "chrome/adb.h",
    "chrome/adb_impl.cc",
    "chrome/adb_impl.h",
    "chrome/bidi_tracker.cc",
    "chrome/bidi_tracker.h",
    "chrome/browser_info.cc",
    "chrome/browser_info.h",
    "chrome/cast_tracker.cc",
    "chrome/cast_tracker.h",
    "chrome/chrome.h",
    "chrome/chrome_android_impl.cc",
    "chrome/chrome_android_impl.h",
    "chrome/chrome_desktop_impl.cc",
    "chrome/chrome_desktop_impl.h",
    "chrome/chrome_finder.cc",
    "chrome/chrome_finder.h",
    "chrome/chrome_impl.cc",
    "chrome/chrome_impl.h",
    "chrome/chrome_remote_impl.cc",
    "chrome/chrome_remote_impl.h",
    "chrome/client_hints.cc",
    "chrome/client_hints.h",
    "chrome/console_logger.cc",
    "chrome/console_logger.h",
    "chrome/device_manager.cc",
    "chrome/device_manager.h",
    "chrome/device_metrics.cc",
    "chrome/device_metrics.h",
    "chrome/devtools_client.cc",
    "chrome/devtools_client.h",
    "chrome/devtools_client_impl.cc",
    "chrome/devtools_client_impl.h",
    "chrome/devtools_endpoint.cc",
    "chrome/devtools_endpoint.h",
    "chrome/devtools_event_listener.cc",
    "chrome/devtools_event_listener.h",
    "chrome/devtools_http_client.cc",
    "chrome/devtools_http_client.h",
    "chrome/download_directory_override_manager.cc",
    "chrome/download_directory_override_manager.h",
    "chrome/fedcm_tracker.cc",
    "chrome/fedcm_tracker.h",
    "chrome/frame_tracker.cc",
    "chrome/frame_tracker.h",
    "chrome/geolocation_override_manager.cc",
    "chrome/geolocation_override_manager.h",
    "chrome/geoposition.h",
    "chrome/heap_snapshot_taker.cc",
    "chrome/heap_snapshot_taker.h",
    "chrome/log.cc",
    "chrome/log.h",
    "chrome/mobile_device.cc",
    "chrome/mobile_device.h",
    "chrome/mobile_emulation_override_manager.cc",
    "chrome/mobile_emulation_override_manager.h",
    "chrome/navigation_tracker.cc",
    "chrome/navigation_tracker.h",
    "chrome/network_conditions.cc",
    "chrome/network_conditions.h",
    "chrome/network_conditions_override_manager.cc",
    "chrome/network_conditions_override_manager.h",
    "chrome/network_list.cc",
    "chrome/network_list.h",
    "chrome/non_blocking_navigation_tracker.cc",
    "chrome/non_blocking_navigation_tracker.h",
    "chrome/page_load_strategy.cc",
    "chrome/page_load_strategy.h",
    "chrome/page_tracker.cc",
    "chrome/page_tracker.h",
    "chrome/scoped_temp_dir_with_retry.cc",
    "chrome/scoped_temp_dir_with_retry.h",
    "chrome/status.cc",
    "chrome/status.h",
    "chrome/target_utils.cc",
    "chrome/target_utils.h",
    "chrome/ui_events.cc",
    "chrome/ui_events.h",
    "chrome/util.cc",
    "chrome/util.h",
    "chrome/web_view.h",
    "chrome/web_view_impl.cc",
    "chrome/web_view_impl.h",
    "chrome/web_view_info.cc",
    "chrome/web_view_info.h",
    "constants/version.h",
    "log_replay/chrome_replay_impl.cc",
    "log_replay/chrome_replay_impl.h",
    "log_replay/devtools_log_reader.cc",
    "log_replay/devtools_log_reader.h",
    "log_replay/log_replay_socket.cc",
    "log_replay/log_replay_socket.h",
    "log_replay/replay_http_client.cc",
    "log_replay/replay_http_client.h",
    "net/adb_client_socket.cc",
    "net/adb_client_socket.h",
    "net/command_id.cc",
    "net/command_id.h",
    "net/net_util.cc",
    "net/net_util.h",
    "net/pipe_builder.cc",
    "net/pipe_builder.h",
    "net/pipe_connection.h",
    "net/sync_websocket.h",
    "net/sync_websocket_factory.cc",
    "net/sync_websocket_factory.h",
    "net/sync_websocket_impl.cc",
    "net/sync_websocket_impl.h",
    "net/timeout.cc",
    "net/timeout.h",
    "net/url_request_context_getter.cc",
    "net/url_request_context_getter.h",
    "net/websocket.cc",
    "net/websocket.h",
  ]

  if (is_posix) {
    sources += [
      "net/pipe_connection_posix.cc",
      "net/pipe_connection_posix.h",
      "net/pipe_reader_posix.cc",
      "net/pipe_reader_posix.h",
      "net/pipe_writer_posix.cc",
      "net/pipe_writer_posix.h",
    ]
  }

  if (is_mac) {
    sources += [ "chrome/chrome_finder_mac.mm" ]
  }

  if (is_win) {
    sources += [
      "net/pipe_connection_win.cc",
      "net/pipe_connection_win.h",
    ]
  }

  # Also compile the generated files.
  sources += get_target_outputs(":embed_js_in_cpp")
  sources += get_target_outputs(":embed_mobile_devices_in_cpp")
  sources += get_target_outputs(":embed_bidimapper_in_cpp")
  sources += get_target_outputs(":embed_user_data_dir_in_cpp")

  deps = [
    ":embed_bidimapper_in_cpp",
    ":embed_js_in_cpp",
    ":embed_mobile_devices_in_cpp",
    ":embed_user_data_dir_in_cpp",
    "//base",
    "//build:branding_buildflags",
    "//chrome/common:non_code_constants",
    "//chrome/common:version_header",
    "//net",
    "//net/traffic_annotation:test_support",
    "//services/network/public/cpp",
    "//services/network/public/mojom",
    "//third_party/blink/public:buildflags",
    "//third_party/zlib:minizip",
    "//third_party/zlib/google:zip",
    "//ui/accessibility:ax_enums_mojo",
    "//ui/base",
    "//ui/events:dom_keycode_converter",
    "//ui/gfx",
    "//ui/gfx/geometry",
    "//url",
  ]
}

source_set("lib") {
  testonly = true
  sources = [
    "alert_commands.cc",
    "alert_commands.h",
    "basic_types.cc",
    "basic_types.h",
    "capabilities.cc",
    "capabilities.h",
    "chrome_launcher.cc",
    "chrome_launcher.h",
    "command.h",
    "command_listener.h",
    "command_listener_proxy.cc",
    "command_listener_proxy.h",
    "commands.cc",
    "commands.h",
    "connection_session_map.h",
    "devtools_events_logger.cc",
    "devtools_events_logger.h",
    "element_commands.cc",
    "element_commands.h",
    "element_util.cc",
    "element_util.h",
    "fedcm_commands.cc",
    "fedcm_commands.h",
    "key_converter.cc",
    "key_converter.h",
    "keycode_text_conversion.h",
    "logging.cc",
    "logging.h",
    "performance_logger.cc",
    "performance_logger.h",
    "prompt_behavior.cc",
    "prompt_behavior.h",
    "server/http_handler.cc",
    "server/http_handler.h",
    "server/http_server.cc",
    "server/http_server.h",
    "session.cc",
    "session.h",
    "session_commands.cc",
    "session_commands.h",
    "session_connection_map.h",
    "session_thread_map.h",
    "util.cc",
    "util.h",
    "webauthn_commands.cc",
    "webauthn_commands.h",
    "window_commands.cc",
    "window_commands.h",
  ]

  if (is_mac) {
    sources += [ "keycode_text_conversion_mac.mm" ]
  }

  if (is_win) {
    sources += [ "keycode_text_conversion_win.cc" ]
  }

  deps = [
    ":automation_client_lib",
    "//base",
    "//build:branding_buildflags",
    "//build:chromeos_buildflags",
    "//chrome/common:constants",
    "//chrome/common:version_header",
    "//chrome/test/chromedriver/constants:version_header",
    "//components/crx_file",
    "//components/embedder_support",
    "//components/version_info:generate_version_info",
    "//crypto",
    "//net",
    "//net/traffic_annotation:test_support",
    "//services/device/public/cpp/generic_sensor",
    "//services/network:network_service",
    "//services/network/public/cpp",
    "//services/network/public/mojom",
    "//third_party/selenium-atoms:atoms",
    "//third_party/zlib",
    "//third_party/zlib/google:zip",
    "//ui/base",
    "//ui/base:ozone_buildflags",
    "//ui/events:dom_keycode_converter",
    "//ui/events:events_base",
    "//ui/gfx",
    "//ui/gfx/geometry",
  ]

  if (use_ozone) {
    sources += [ "keycode_text_conversion_ozone.cc" ]
    deps += [ "//ui/events/ozone/layout" ]

    # TODO(crbug.com/40193891): clean up usages of ozone_platform_x11.
    # Ozone/X11 implementation provides a stub KeyboardLayoutEngine. Thus,
    # use plain X11 keyboard conversion code until Ozone/X11 uses a non-stub
    # engine.
    if (ozone_platform_x11) {
      sources += [ "keycode_text_conversion_x.cc" ]
      deps += [
        "//ui/base/x",
        "//ui/gfx/x",
      ]
    }
  }

  if (is_mac) {
    deps += [ ":buildflags" ]
    frameworks = [ "Carbon.framework" ]
  }
}

if (is_linux) {
  # Linux
  executable("chromedriver_server.unstripped") {
    testonly = true
    sources = [ "server/chromedriver_server.cc" ]
    output_name = "chromedriver.unstripped"

    deps = [
      ":automation_client_lib",
      ":lib",
      "//mojo/core/embedder",
      "//net",
      "//net/traffic_annotation:test_support",
      "//services/network/public/mojom",
    ]
  }

  strip_binary("chromedriver_server") {
    testonly = true
    binary_input = "$root_out_dir/chromedriver.unstripped"
    symbol_output = "$root_out_dir/chromedriver.debug"
    stripped_binary_output = "$root_out_dir/chromedriver"
    deps = [ ":chromedriver_server.unstripped" ]
  }
} else {
  # Non-Linux
  executable("chromedriver_server") {
    testonly = true
    sources = [ "server/chromedriver_server.cc" ]
    output_name = "chromedriver"

    deps = [
      ":automation_client_lib",
      ":lib",
      "//build:chromeos_buildflags",
      "//build/win:default_exe_manifest",
      "//mojo/core/embedder",
      "//net",
      "//net/traffic_annotation:test_support",
      "//services/network/public/mojom",
    ]

    if (is_mac && is_component_ffmpeg) {
      ldflags = [ "-Wl,-rpath,@executable_path/." ]
    }
  }
}

if (is_mac) {
  buildflag_header("buildflags") {
    header = "buildflags.h"
    flags = [ "CHROMEDRIVER_DISCLAIM_RESPONSIBILITY=$chromedriver_disclaim_responsibility" ]
  }
}

python_library("chromedriver_py_tests") {
  testonly = true
  deps = [
    ":chromedriver_server",
    "//chrome:chrome",
  ]

  pydeps_file = "test/run_py_tests.pydeps"
  data = [
    "//chrome/test/data/chromedriver/",
    "//chrome/test/chromedriver/js/",
    "//testing/scripts/run_chromedriver_tests.py",
  ]

  data_deps = [
    "//chrome/test/chromedriver:chromedriver_server",
    "//testing:test_scripts_shared",
  ]
  if (is_component_build && is_mac) {
    data_deps += [ "//chrome:chrome_framework" ]
  }

  if (is_win) {
    # On Windows, the following target produces the final chrome.exe
    data_deps += [ "//chrome:reorder_imports" ]
  }
}

python_library("chromedriver_py_tests_headless_shell") {
  testonly = true
  deps = [
    ":chromedriver_server",
    "//chrome:chrome",
  ]

  pydeps_file = "test/run_py_tests.pydeps"
  data = [
    "//chrome/test/data/chromedriver/",
    "//chrome/test/chromedriver/js/",
    "//testing/scripts/run_chromedriver_tests.py",
  ]

  data_deps = [
    "//chrome/test/chromedriver:chromedriver_server",
    "//headless:headless_shell",
    "//testing:test_scripts_shared",
  ]
}

python_library("chromedriver_replay_unittests") {
  testonly = true
  deps = [ ":chromedriver_server" ]

  data_deps = [ "//testing:test_scripts_shared" ]

  pydeps_file = "log_replay/client_replay_unittest.pydeps"
}

test("chromedriver_unittests") {
  sources = [
    "capabilities_unittest.cc",
    "chrome/bidi_tracker_unittest.cc",
    "chrome/browser_info_unittest.cc",
    "chrome/cast_tracker_unittest.cc",
    "chrome/chrome_finder_unittest.cc",
    "chrome/console_logger_unittest.cc",
    "chrome/device_manager_unittest.cc",
    "chrome/devtools_client_impl_unittest.cc",
    "chrome/devtools_endpoint_unittest.cc",
    "chrome/devtools_http_client_unittest.cc",
    "chrome/download_directory_override_manager_unittest.cc",
    "chrome/fedcm_tracker_unittest.cc",
    "chrome/frame_tracker_unittest.cc",
    "chrome/geolocation_override_manager_unittest.cc",
    "chrome/heap_snapshot_taker_unittest.cc",
    "chrome/mobile_device_unittest.cc",
    "chrome/mobile_emulation_override_manager_unittest.cc",
    "chrome/navigation_tracker_unittest.cc",
    "chrome/network_conditions_override_manager_unittest.cc",
    "chrome/recorder_devtools_client.cc",
    "chrome/recorder_devtools_client.h",
    "chrome/status_unittest.cc",
    "chrome/stub_chrome.cc",
    "chrome/stub_chrome.h",
    "chrome/stub_devtools_client.cc",
    "chrome/stub_devtools_client.h",
    "chrome/stub_web_view.cc",
    "chrome/stub_web_view.h",
    "chrome/web_view_impl_unittest.cc",
    "chrome/web_view_info_unittest.cc",
    "chrome_launcher_unittest.cc",
    "command_listener_proxy_unittest.cc",
    "commands_unittest.cc",
    "element_commands_unittest.cc",
    "fedcm_commands_unittest.cc",
    "key_converter_unittest.cc",
    "keycode_text_conversion_unittest.cc",
    "log_replay/devtools_log_reader_unittest.cc",
    "logging_unittest.cc",
    "net/adb_client_socket_unittest.cc",
    "net/net_util_unittest.cc",
    "net/pipe_builder_unittest.cc",
    "net/stub_sync_websocket.cc",
    "net/stub_sync_websocket.h",
    "net/sync_websocket_impl_unittest.cc",
    "net/test_http_server.cc",
    "net/test_http_server.h",
    "net/test_http_server_unittest.cc",
    "net/timeout_unittest.cc",
    "net/websocket_unittest.cc",
    "performance_logger_unittest.cc",
    "prompt_behavior_unittest.cc",
    "server/http_handler_unittest.cc",
    "session_commands_unittest.cc",
    "session_unittest.cc",
    "util_unittest.cc",
    "window_commands_unittest.cc",
  ]

  if (is_posix) {
    sources += [
      "net/pipe_connection_unittest.cc",
      "net/pipe_reader_posix_unittest.cc",
      "net/pipe_writer_posix_unittest.cc",
    ]
  }

  if (is_win) {
    sources += [ "net/pipe_connection_unittest.cc" ]
  }

  data = [
    "//chrome/test/data/chromedriver/",
    "//chrome/test/chromedriver/log_replay/test_data",
  ]

  deps = [
    ":automation_client_lib",
    ":lib",
    "//base",
    "//base/test:run_all_unittests",
    "//base/test:test_support",
    "//build:chromeos_buildflags",
    "//chrome/common",
    "//mojo/core/embedder",
    "//net",
    "//net:test_support",
    "//services/network/public/cpp",
    "//services/network/public/mojom",
    "//testing/gmock",
    "//testing/gtest",
    "//third_party/selenium-atoms:atoms",
    "//ui/base",
    "//ui/events:test_support",
    "//ui/gfx",
    "//ui/gfx/geometry",
    "//url",
  ]
}

if (is_win || is_posix) {
  test("chromedriver_integrationtests") {
    sources = [
      "devtools_client_impl_integrationtest.cc",
      "navigation_tracker_integrationtest.cc",
      "net/test_http_server.cc",
      "net/test_http_server.h",
    ]

    data_deps = [ "//chrome:chrome" ]

    deps = [
      ":automation_client_lib",
      ":lib",
      "//base",
      "//base/test:run_all_unittests",
      "//base/test:test_support",
      "//chrome/common",
      "//net",
      "//net/traffic_annotation:test_support",
      "//testing/gmock",
      "//testing/gtest",
      "//url",
    ]
  }
}

copy("copy_license") {
  sources = [ "//LICENSE" ]
  outputs = [ "$root_out_dir/LICENSE.chromedriver" ]
}

# Generate THIRD_PARTY_NOTICES file by recursively joining all dependent
# licenses.
action("generate_chromedriver_notices") {
  testonly = true
  _license_path = "$root_out_dir/THIRD_PARTY_NOTICES.chromedriver"
  script = "//tools/licenses/licenses.py"
  outputs = [ _license_path ]
  args = [
    "license_file",
    rebase_path(_license_path, root_build_dir),
    "--gn-target",
    "//chrome/test/chromedriver:chromedriver_server",
    "--gn-out-dir",
    ".",
    "--format",
    "notice",
  ]
  deps = [ ":chromedriver_server" ]
}

group("chromedriver") {
  testonly = true
  deps = [
    ":chromedriver_server",
    ":copy_license",
    ":generate_chromedriver_notices",
  ]
}