add new test project to chromium

前言

想在自己的测试程序中,验证一些chromium的知识点。
先加一个自己的测试工程到chromium。
我先翻了一圈,找到chromium中有一个test_child_process的测试程序(exe)。
参考test_child_process,加入了自己的测试工程test_by_me.

实验

在chromium工程中建立自己工程的文件夹

Z:\chromium\src\base\test\test_by_me

放入自己的测试工程实现

// @file Z:\chromium\src\base\test\test_by_me\main.cpp
// @brief write some test code to study chromium project

/**
# cd /d Z:\chromium\src\
# gn --ide=vs args out\my_x86_d
# gn ls out\my_x86_d > d:\my_tmp\gn_list_target.log
# 新加的测试工程位置//base/test:test_by_me
# autoninja -C out\my_x86_d test_by_me
*/

#include <stdlib.h>
#include <stdio.h>

#define PROG_NAME "test_my_me"

int main(int argc, char** argv)
{
	printf(">> %s\n", PROG_NAME);
	printf("hello chromium\n");

	system("pause");
	return EXIT_SUCCESS;
}

修改Z:\chromium\src\base\test\BUILD.gn

为啥要修改这个编译配置? 因为我用vs2017搜索test_child_process.cc时(指定目录为Z:\chromium\src, 文件类型为*.gn; *.gyp; *.gni),只有这个配置文件中有test_child_process.cc的配置。
在Z:\chromium\src\base\test\BUILD.gn尾部,加上test_by_me工程需要编译的实现文件。

executable("test_by_me") {
  sources = [
    "test_by_me/main.cpp",
  ]
}

完整的Z:\chromium\src\base\test\BUILD.gn内容

# Copyright (c) 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import("//build/compiled_action.gni")
import("//build/config/ui.gni")
import("//build/config/nacl/config.gni")

if (is_android) {
  import("//build/config/android/rules.gni")
}

static_library("test_config") {
  testonly = true
  sources = [
    "test_switches.cc",
    "test_switches.h",
    "test_timeouts.cc",
    "test_timeouts.h",
  ]
  deps = [
    "//base",
  ]
}

static_library("test_support") {
  testonly = true
  sources = [
    "../trace_event/trace_config_memory_test_util.h",
    "android/java_handler_thread_helpers.cc",
    "android/java_handler_thread_helpers.h",
    "android/url_utils.cc",
    "android/url_utils.h",
    "bind_test_util.h",
    "copy_only_int.h",
    "fuzzed_data_provider.cc",
    "fuzzed_data_provider.h",
    "gtest_util.cc",
    "gtest_util.h",
    "gtest_xml_unittest_result_printer.cc",
    "gtest_xml_unittest_result_printer.h",
    "gtest_xml_util.cc",
    "gtest_xml_util.h",
    "histogram_tester.cc",
    "histogram_tester.h",
    "icu_test_util.cc",
    "icu_test_util.h",
    "ios/wait_util.h",
    "ios/wait_util.mm",
    "launcher/test_result.cc",
    "launcher/test_result.h",
    "launcher/test_results_tracker.h",
    "launcher/unit_test_launcher.h",
    "mock_callback.h",
    "mock_chrome_application_mac.h",
    "mock_chrome_application_mac.mm",
    "mock_devices_changed_observer.cc",
    "mock_devices_changed_observer.h",
    "mock_entropy_provider.cc",
    "mock_entropy_provider.h",
    "mock_log.cc",
    "mock_log.h",
    "move_only_int.h",
    "multiprocess_test.h",
    "multiprocess_test_android.cc",
    "null_task_runner.cc",
    "null_task_runner.h",
    "perf_log.cc",
    "perf_log.h",
    "perf_test_suite.cc",
    "perf_test_suite.h",
    "perf_time_logger.cc",
    "perf_time_logger.h",
    "power_monitor_test_base.cc",
    "power_monitor_test_base.h",
    "scoped_command_line.cc",
    "scoped_command_line.h",
    "scoped_environment_variable_override.cc",
    "scoped_environment_variable_override.h",
    "scoped_feature_list.cc",
    "scoped_feature_list.h",
    "scoped_mock_time_message_loop_task_runner.cc",
    "scoped_mock_time_message_loop_task_runner.h",
    "scoped_path_override.cc",
    "scoped_path_override.h",
    "scoped_task_environment.cc",
    "scoped_task_environment.h",
    "sequenced_task_runner_test_template.cc",
    "sequenced_task_runner_test_template.h",
    "simple_test_clock.cc",
    "simple_test_clock.h",
    "simple_test_tick_clock.cc",
    "simple_test_tick_clock.h",
    "task_runner_test_template.cc",
    "task_runner_test_template.h",
    "test_discardable_memory_allocator.cc",
    "test_discardable_memory_allocator.h",
    "test_file_util.cc",
    "test_file_util.h",
    "test_file_util_android.cc",
    "test_file_util_linux.cc",
    "test_file_util_mac.cc",
    "test_file_util_win.cc",
    "test_io_thread.cc",
    "test_io_thread.h",
    "test_listener_ios.h",
    "test_listener_ios.mm",
    "test_message_loop.cc",
    "test_message_loop.h",
    "test_mock_time_task_runner.cc",
    "test_mock_time_task_runner.h",
    "test_pending_task.cc",
    "test_pending_task.h",
    "test_reg_util_win.cc",
    "test_reg_util_win.h",
    "test_shared_memory_util.cc",
    "test_shared_memory_util.h",
    "test_shortcut_win.cc",
    "test_shortcut_win.h",
    "test_simple_task_runner.cc",
    "test_simple_task_runner.h",
    "test_suite.cc",
    "test_suite.h",
    "test_support_android.cc",
    "test_support_android.h",
    "test_support_ios.h",
    "test_support_ios.mm",
    "test_ui_thread_android.cc",
    "test_ui_thread_android.h",
    "thread_test_helper.cc",
    "thread_test_helper.h",
    "trace_event_analyzer.cc",
    "trace_event_analyzer.h",
    "trace_to_file.cc",
    "trace_to_file.h",
    "user_action_tester.cc",
    "user_action_tester.h",
    "values_test_util.cc",
    "values_test_util.h",
  ]

  if (is_ios) {
    sources += [ "launcher/unit_test_launcher_ios.cc" ]
  } else if (!is_nacl_nonsfi) {
    sources += [
      "launcher/test_launcher.cc",
      "launcher/test_launcher.h",
      "launcher/test_launcher_tracer.cc",
      "launcher/test_launcher_tracer.h",
      "launcher/test_results_tracker.cc",
      "launcher/unit_test_launcher.cc",
      "multiprocess_test.cc",
    ]
  }

  configs += [ "//build/config:precompiled_headers" ]

  data = [
    # The isolate needs this script for setting up the test. It's not actually
    # needed to run this target locally.
    "//testing/test_env.py",
  ]

  public_deps = [
    ":test_config",
    "//base",
    "//base:base_static",
    "//base:i18n",
  ]
  deps = [
    "//base/third_party/dynamic_annotations",
    "//testing/gmock",
    "//testing/gtest",
    "//third_party/icu:icuuc",
    "//third_party/libxml",
  ]

  if (is_posix || is_fuchsia) {
    sources += [
      "scoped_locale.cc",
      "scoped_locale.h",
      "test_file_util_posix.cc",
    ]
  }

  if (is_linux) {
    public_deps += [ ":fontconfig_util_linux" ]
    data_deps = [
      "//third_party/test_fonts",
    ]
    if (current_toolchain == host_toolchain) {
      data_deps += [ ":do_generate_fontconfig_caches" ]
      data += [ "$root_out_dir/fontconfig_caches/" ]
    }
  }

  if (is_ios) {
    set_sources_assignment_filter([])
    sources += [ "test_file_util_mac.cc" ]
    set_sources_assignment_filter(sources_assignment_filter)
  }

  if (is_mac) {
    libs = [ "AppKit.framework" ]
  }

  if (is_android) {
    set_sources_assignment_filter([])
    sources += [ "test_file_util_linux.cc" ]
    set_sources_assignment_filter(sources_assignment_filter)
    deps += [
      ":base_unittests_jni_headers",
      ":test_support_jni_headers",
    ]
    public_deps += [ ":test_support_java" ]
  }

  if (is_nacl_nonsfi) {
    sources += [
      "launcher/test_launcher.h",
      "launcher/test_result.h",
      "launcher/unit_test_launcher.h",
      "launcher/unit_test_launcher_nacl_nonsfi.cc",
    ]
    sources -= [
      "gtest_xml_util.cc",
      "gtest_xml_util.h",
      "icu_test_util.cc",
      "icu_test_util.h",
      "perf_test_suite.cc",
      "perf_test_suite.h",
      "scoped_path_override.cc",
      "scoped_path_override.h",
      "test_discardable_memory_allocator.cc",
      "test_discardable_memory_allocator.h",
      "test_file_util.cc",
      "test_file_util.h",
      "test_file_util_posix.cc",
      "test_suite.cc",
      "test_suite.h",
      "trace_to_file.cc",
      "trace_to_file.h",
    ]
    public_deps -= [ "//base:i18n" ]
    deps -= [
      "//third_party/icu:icuuc",
      "//third_party/libxml",
    ]
  }
}

config("perf_test_config") {
  defines = [ "PERF_TEST" ]
}

# This is a source set instead of a static library because it seems like some
# linkers get confused when "main" is in a static library, and if you link to
# this, you always want the object file anyway.
source_set("test_support_perf") {
  testonly = true
  sources = [
    "run_all_perftests.cc",
  ]
  deps = [
    ":test_support",
    "//base",
    "//testing/gtest",
  ]

  public_configs = [ ":perf_test_config" ]
}

static_library("test_launcher_nacl_nonsfi") {
  testonly = true
  sources = [
    "launcher/test_launcher_nacl_nonsfi.cc",
    "launcher/test_launcher_nacl_nonsfi.h",
  ]
  deps = [
    ":test_support",
  ]
}

static_library("run_all_unittests") {
  testonly = true
  sources = [
    "run_all_unittests.cc",
  ]
  deps = [
    ":test_support",
  ]
}

# These sources are linked into both the base_unittests binary and the test
# shared library target below.
source_set("native_library_test_utils") {
  testonly = true
  sources = [
    "native_library_test_utils.cc",
    "native_library_test_utils.h",
  ]
}

# This shared library is dynamically loaded by NativeLibrary unittests.
shared_library("test_shared_library") {
  testonly = true
  sources = [
    "test_shared_library.cc",
  ]

  deps = [
    ":native_library_test_utils",
  ]
}

static_library("run_all_base_unittests") {
  # Only targets in base should depend on this, targets outside base
  # should depend on run_all_unittests above.
  visibility = [ "//base/*" ]
  testonly = true
  sources = [
    "run_all_base_unittests.cc",
  ]
  deps = [
    ":test_support",
  ]
}

if (is_linux) {
  source_set("fontconfig_util_linux") {
    sources = [
      "fontconfig_util_linux.cc",
      "fontconfig_util_linux.h",
    ]
    deps = [
      "//base",
      "//third_party/fontconfig",
    ]
  }

  if (current_toolchain == host_toolchain) {
    executable("generate_fontconfig_caches") {
      testonly = true
      sources = [
        "generate_fontconfig_caches.cc",
      ]
      deps = [
        ":fontconfig_util_linux",
        "//base",
        "//build/config:exe_and_shlib_deps",
      ]
    }

    compiled_action("do_generate_fontconfig_caches") {
      testonly = true
      tool = ":generate_fontconfig_caches"
      data_deps = [
        "//third_party/test_fonts",
      ]
      args = []
      outputs = [
        "$root_out_dir/fontconfig_caches/STAMP",
      ]
    }
  }

  shared_library("malloc_wrapper") {
    testonly = true
    sources = [
      "malloc_wrapper.cc",
    ]
    deps = [
      "//base",
      "//build/config:exe_and_shlib_deps",
    ]
  }
}

if (is_android) {
  generate_jni("base_unittests_jni_headers") {
    sources = [
      "android/java/src/org/chromium/base/ContentUriTestUtils.java",
      "android/java/src/org/chromium/base/JavaHandlerThreadHelpers.java",
      "android/java/src/org/chromium/base/TestUiThread.java",
    ]
    jni_package = "base"
  }

  generate_jni("test_support_jni_headers") {
    sources = [
      "android/java/src/org/chromium/base/MainReturnCodeResult.java",
      "android/java/src/org/chromium/base/MultiprocessTestClientLauncher.java",
      "android/javatests/src/org/chromium/base/test/util/UrlUtils.java",
    ]
    jni_package = "base"
  }

  android_library("test_support_java") {
    testonly = true
    deps = [
      "//base:base_java",
      "//testing/android/native_test:native_main_runner_java",
      "//third_party/android_tools:android_support_annotations_java",
      "//third_party/jsr-305:jsr_305_javalib",
    ]
    srcjar_deps = [ ":test_support_java_aidl" ]
    java_files = [
      "android/java/src/org/chromium/base/MainReturnCodeResult.java",
      "android/java/src/org/chromium/base/MultiprocessTestClientLauncher.java",
      "android/java/src/org/chromium/base/MultiprocessTestClientService.java",
      "android/java/src/org/chromium/base/MultiprocessTestClientService0.java",
      "android/java/src/org/chromium/base/MultiprocessTestClientService1.java",
      "android/java/src/org/chromium/base/MultiprocessTestClientService2.java",
      "android/java/src/org/chromium/base/MultiprocessTestClientService3.java",
      "android/java/src/org/chromium/base/MultiprocessTestClientService4.java",
      "android/java/src/org/chromium/base/MultiprocessTestClientServiceDelegate.java",
    ]
  }

  android_aidl("test_support_java_aidl") {
    testonly = true
    import_include = [
      "android/java/src",
      "//base/android/java/src",
    ]
    sources = [
      "android/java/src/org/chromium/base/ITestCallback.aidl",
      "android/java/src/org/chromium/base/ITestController.aidl",
    ]
  }
}

# Trivial executable which outputs space-delimited argv to stdout,
# used for testing.
executable("test_child_process") {
  testonly = true
  sources = [
    "test_child_process.cc",
  ]
  deps = [
    "//build/config:exe_and_shlib_deps",
  ]
}

executable("test_by_me") {
  sources = [
    "test_by_me/main.cpp",
  ]
}

产生工程

因为加入的测试工程是含在chromium解决方案里面的,需要完整的生成一次全部工程,在vs2017的chromium解决方案中的过滤器(src/base/test)中,才会包含test_by_me子工程.

# cd /d Z:\chromium\src\
# gn --ide=vs args out\my_x86_d
# gn ls out\my_x86_d > d:\my_tmp\gn_list_target.log

在d:\my_tmp\gn_list_target.log中找test_by_me子工程在chromium解决方案的过滤器中的位置

//base/test:test_by_me

编译子工程test_by_me

autoninja -C out\my_x86_d test_by_me

测试

这时,可以在vs2017中单步了,和用vs2017直接建立的工程一样.
在这里插入图片描述

备注

这个测试程序没有加入chromium代码库的任何特性,等后续实验再研究。

猜你喜欢

转载自blog.csdn.net/LostSpeed/article/details/85007888