Build with a different gtest harness when in Chromium

Running test in a Chromium build requires using their gtest and gmock
targets as well as a harness that is in //base. Since we want to run
Dawn tests on the Chromium GPU bots, we need to support two
configurations both building tests standalone and in Chromium.

BUG=chromium:870747

Change-Id: I862e62a607e193a27562ece0f1f6d46d8728e446
Reviewed-on: https://dawn-review.googlesource.com/c/2080
Reviewed-by: Kai Ninomiya <kainino@chromium.org>
Reviewed-by: Stephen White <senorblanco@chromium.org>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Commit-Queue: Corentin Wallez <cwallez@chromium.org>
diff --git a/BUILD.gn b/BUILD.gn
index 3fc7711..e235a3b 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -14,6 +14,7 @@
 
 import("scripts/dawn_overrides_with_defaults.gni")
 import("scripts/dawn_features.gni")
+import("//build_overrides/build.gni")
 
 import("//testing/test.gni")
 
@@ -750,13 +751,11 @@
     ":libdawn_native_sources",
     ":libdawn_wire",
     ":mock_dawn_gen",
-    "third_party:gmock",
-    "third_party:gtest",
+    "third_party:gmock_and_gtest",
   ]
 
   sources = get_target_outputs(":mock_dawn_gen")
   sources += [
-    "src/tests/UnittestsMain.cpp",
     "src/tests/unittests/BitSetIteratorTests.cpp",
     "src/tests/unittests/CommandAllocatorTests.cpp",
     "src/tests/unittests/EnumClassBitmasksTests.cpp",
@@ -791,6 +790,14 @@
   if (dawn_enable_d3d12) {
     sources += [ "src/tests/unittests/d3d12/CopySplitTests.cpp" ]
   }
+
+  # When building inside Chromium, use their gtest main function because it is
+  # needed to run in swarming correctly.
+  if (build_with_chromium) {
+    sources += [ "//gpu/dawn_unittests_main.cc" ]
+  } else {
+    sources += [ "src/tests/UnittestsMain.cpp" ]
+  }
 }
 
 test("dawn_end2end_tests") {
@@ -803,13 +810,12 @@
     ":libdawn_native",
     ":libdawn_wire",
     "third_party:glfw",
-    "third_party:gtest",
+    "third_party:gmock_and_gtest",
   ]
 
   sources = [
     "src/tests/DawnTest.cpp",
     "src/tests/DawnTest.h",
-    "src/tests/End2EndTestsMain.cpp",
     "src/tests/end2end/BasicTests.cpp",
     "src/tests/end2end/BindGroupTests.cpp",
     "src/tests/end2end/BlendStateTests.cpp",
@@ -828,6 +834,14 @@
     "src/tests/end2end/TextureViewTests.cpp",
     "src/tests/end2end/ViewportOrientationTests.cpp",
   ]
+
+  # When building inside Chromium, use their gtest main function because it is
+  # needed to run in swarming correctly.
+  if (build_with_chromium) {
+    sources += [ "//gpu/dawn_end2end_tests_main.cc" ]
+  } else {
+    sources += [ "src/tests/End2EndTestsMain.cpp" ]
+  }
 }
 
 ###############################################################################
diff --git a/third_party/BUILD.gn b/third_party/BUILD.gn
index 3e1a023..83b2c69 100644
--- a/third_party/BUILD.gn
+++ b/third_party/BUILD.gn
@@ -14,6 +14,7 @@
 
 import("../scripts/dawn_overrides_with_defaults.gni")
 import("../scripts/dawn_features.gni")
+import("//build_overrides/build.gni")
 
 is_msvc = is_win && !is_clang
 
@@ -107,41 +108,69 @@
 }
 
 ###############################################################################
-# Gtest Gmock - build targets when outside of Chrome
-# TODO(cwallez@chromium.org): Adapt them to build with Chromium's harness
+# Gtest Gmock - Handle building inside and outside of Chromium.
 ###############################################################################
 
-googletest_dir = dawn_googletest_dir
+# When building outside of Chromium we need to define our own targets for GTest
+# and GMock. However when compiling inside of Chromium we need to reuse the
+# existing targets, both because Chromium has a special harness for swarming
+# and because otherwise the "gn check" fails.
 
-config("gtest_config") {
-  include_dirs = [
-    "${googletest_dir}/googletest",
-    "${googletest_dir}/googletest/include",
-  ]
-}
+if (!build_with_chromium) {
+  # When we aren't in Chromium we define out own targets based on the location
+  # of the googletest repo.
+  googletest_dir = dawn_googletest_dir
 
-static_library("gtest") {
-  testonly = true
-  sources = [
-    "${googletest_dir}/googletest/src/gtest-all.cc",
-  ]
-  public_configs = [ ":gtest_config" ]
-}
+  config("gtest_config") {
+    include_dirs = [
+      "${googletest_dir}/googletest",
+      "${googletest_dir}/googletest/include",
+    ]
+  }
 
-config("gmock_config") {
-  include_dirs = [
-    "${googletest_dir}/googlemock",
-    "${googletest_dir}/googlemock/include",
-    "${googletest_dir}/googletest/include",
-  ]
-}
+  static_library("gtest") {
+    testonly = true
+    sources = [
+      "${googletest_dir}/googletest/src/gtest-all.cc",
+    ]
+    public_configs = [ ":gtest_config" ]
+  }
 
-static_library("gmock") {
-  testonly = true
-  sources = [
-    "${googletest_dir}/googlemock/src/gmock-all.cc",
-  ]
-  public_configs = [ ":gmock_config" ]
+  config("gmock_config") {
+    include_dirs = [
+      "${googletest_dir}/googlemock",
+      "${googletest_dir}/googlemock/include",
+      "${googletest_dir}/googletest/include",
+    ]
+  }
+
+  static_library("gmock") {
+    testonly = true
+    sources = [
+      "${googletest_dir}/googlemock/src/gmock-all.cc",
+    ]
+    public_configs = [ ":gmock_config" ]
+  }
+
+  group("gmock_and_gtest") {
+    testonly = true
+    public_deps = [
+      ":gmock",
+      ":gtest",
+    ]
+  }
+} else {
+  # When we are in Chromium we reuse its targets, and also add some deps that
+  # are needed to launch the test in swarming mode.
+  group("gmock_and_gtest") {
+    testonly = true
+    public_deps = [
+      "//base",
+      "//base/test:test_support",
+      "//testing/gmock",
+      "//testing/gtest",
+    ]
+  }
 }
 
 ###############################################################################