crash-reporter: Clean up testing code.

Link all of the crash reporter's tests into a single
crash_reporter_test executable instead of building separate
binaries that lead to a bunch of duplication.

BUG=none
TEST=tests pass
CQ-DEPEND=I7f6623dd0ae36395efac89bdcfae54926ea6c918

Change-Id: I18e2d929d6545e7d05e88232bdaec089ae620544
Reviewed-on: https://chromium-review.googlesource.com/243940
Reviewed-by: Dan Erat <derat@chromium.org>
Commit-Queue: Dan Erat <derat@chromium.org>
Trybot-Ready: Dan Erat <derat@chromium.org>
Tested-by: Dan Erat <derat@chromium.org>
diff --git a/crash_reporter/chrome_collector_test.cc b/crash_reporter/chrome_collector_test.cc
index 0f3b4fb..d44c82c 100644
--- a/crash_reporter/chrome_collector_test.cc
+++ b/crash_reporter/chrome_collector_test.cc
@@ -6,26 +6,25 @@
 
 #include <stdio.h>
 
-#include <dbus/dbus-glib-lowlevel.h>
-
 #include <base/auto_reset.h>
 #include <base/files/file_util.h>
 #include <base/files/scoped_temp_dir.h>
 #include <chromeos/syslog_logging.h>
-#include <chromeos/test_helpers.h>
 #include <gtest/gtest.h>
 
 using base::FilePath;
 
-static const char kCrashFormatGood[] = "value1:10:abcdefghijvalue2:5:12345";
-static const char kCrashFormatEmbeddedNewline[] =
-    "value1:10:abcd\r\nghijvalue2:5:12\n34";
-static const char kCrashFormatBad1[] = "value1:10:abcdefghijvalue2:6=12345";
-static const char kCrashFormatBad2[] = "value1:10:abcdefghijvalue2:512345";
-static const char kCrashFormatBad3[] = "value1:10::abcdefghijvalue2:5=12345";
-static const char kCrashFormatBad4[] = "value1:10:abcdefghijvalue2:4=12345";
+namespace {
 
-static const char kCrashFormatWithFile[] =
+const char kCrashFormatGood[] = "value1:10:abcdefghijvalue2:5:12345";
+const char kCrashFormatEmbeddedNewline[] =
+    "value1:10:abcd\r\nghijvalue2:5:12\n34";
+const char kCrashFormatBad1[] = "value1:10:abcdefghijvalue2:6=12345";
+const char kCrashFormatBad2[] = "value1:10:abcdefghijvalue2:512345";
+const char kCrashFormatBad3[] = "value1:10::abcdefghijvalue2:5=12345";
+const char kCrashFormatBad4[] = "value1:10:abcdefghijvalue2:4=12345";
+
+const char kCrashFormatWithFile[] =
     "value1:10:abcdefghijvalue2:5:12345"
     "some_file\"; filename=\"foo.txt\":15:12345\n789\n12345"
     "value3:2:ok";
@@ -33,12 +32,14 @@
 void CountCrash() {
 }
 
-static bool s_allow_crash = false;
+bool s_allow_crash = false;
 
 bool IsMetrics() {
   return s_allow_crash;
 }
 
+}  // namespace
+
 class ChromeCollectorTest : public ::testing::Test {
  protected:
   void ExpectFileEquals(const char *golden,
@@ -139,9 +140,3 @@
   }
   ExpectFileEquals(ChromeCollector::kSuccessMagic, log_file);
 }
-
-int main(int argc, char **argv) {
-  ::g_type_init();
-  SetUpTests(&argc, argv, false);
-  return RUN_ALL_TESTS();
-}
diff --git a/crash_reporter/crash-reporter.gyp b/crash_reporter/crash-reporter.gyp
index 974d8d0..b80924b 100644
--- a/crash_reporter/crash-reporter.gyp
+++ b/crash_reporter/crash-reporter.gyp
@@ -95,58 +95,21 @@
     ['USE_test == 1', {
       'targets': [
         {
-          'target_name': 'chrome_collector_test',
+          'target_name': 'crash_reporter_test',
           'type': 'executable',
           'includes': ['../common-mk/common_test.gypi'],
           'dependencies': ['libcrash'],
           'sources': [
             'chrome_collector_test.cc',
-          ]
-        },
-        {
-          'target_name': 'crash_collector_test',
-          'type': 'executable',
-          'includes': ['../common-mk/common_test.gypi'],
-          'dependencies': ['libcrash'],
-          'sources': [
             'crash_collector_test.cc',
-          ]
-        },
-        {
-          'target_name': 'kernel_collector_test',
-          'type': 'executable',
-          'includes': ['../common-mk/common_test.gypi'],
-          'dependencies': ['libcrash'],
-          'sources': [
+            'crash_collector_test.h',
             'kernel_collector_test.cc',
-          ]
-        },
-        {
-          'target_name': 'udev_collector_test',
-          'type': 'executable',
-          'includes': ['../common-mk/common_test.gypi'],
-          'dependencies': ['libcrash'],
-          'sources': [
+            'kernel_collector_test.h',
+            'testrunner.cc',
             'udev_collector_test.cc',
-          ]
-        },
-        {
-          'target_name': 'unclean_shutdown_collector_test',
-          'type': 'executable',
-          'includes': ['../common-mk/common_test.gypi'],
-          'dependencies': ['libcrash'],
-          'sources': [
             'unclean_shutdown_collector_test.cc',
-          ]
-        },
-        {
-          'target_name': 'user_collector_test',
-          'type': 'executable',
-          'includes': ['../common-mk/common_test.gypi'],
-          'dependencies': ['libcrash'],
-          'sources': [
             'user_collector_test.cc',
-          ]
+          ],
         },
       ],
     }],
diff --git a/crash_reporter/crash_collector_test.cc b/crash_reporter/crash_collector_test.cc
index 7e4c7c1..1548d70 100644
--- a/crash_reporter/crash_collector_test.cc
+++ b/crash_reporter/crash_collector_test.cc
@@ -6,14 +6,12 @@
 
 #include <unistd.h>
 
-#include <dbus/dbus-glib-lowlevel.h>
 #include <glib.h>
 
 #include <base/files/file_util.h>
 #include <base/strings/string_util.h>
 #include <base/strings/stringprintf.h>
 #include <chromeos/syslog_logging.h>
-#include <chromeos/test_helpers.h>
 #include <gtest/gtest.h>
 
 #include "crash-reporter/crash_collector.h"
@@ -23,6 +21,8 @@
 using chromeos::FindLog;
 using ::testing::Return;
 
+namespace {
+
 void CountCrash() {
   ADD_FAILURE();
 }
@@ -32,6 +32,8 @@
   return false;
 }
 
+}  // namespace
+
 class CrashCollectorTest : public ::testing::Test {
  public:
   void SetUp() {
@@ -343,9 +345,3 @@
   EXPECT_TRUE(base::ReadFileToString(output_file, &contents));
   EXPECT_EQ("hello world\n", contents);
 }
-
-int main(int argc, char **argv) {
-  ::g_type_init();
-  SetUpTests(&argc, argv, false);
-  return RUN_ALL_TESTS();
-}
diff --git a/crash_reporter/kernel_collector_test.cc b/crash_reporter/kernel_collector_test.cc
index 08bb8b0..b067afd 100644
--- a/crash_reporter/kernel_collector_test.cc
+++ b/crash_reporter/kernel_collector_test.cc
@@ -11,17 +11,18 @@
 #include <base/strings/string_util.h>
 #include <base/strings/stringprintf.h>
 #include <chromeos/syslog_logging.h>
-#include <chromeos/test_helpers.h>
 #include <gtest/gtest.h>
 
-static int s_crashes = 0;
-static bool s_metrics = false;
-
 using base::FilePath;
 using base::StringPrintf;
 using chromeos::FindLog;
 using chromeos::GetLog;
 
+namespace {
+
+int s_crashes = 0;
+bool s_metrics = false;
+
 void CountCrash() {
   ++s_crashes;
 }
@@ -30,6 +31,8 @@
   return s_metrics;
 }
 
+}  // namespace
+
 class KernelCollectorTest : public ::testing::Test {
  protected:
   void WriteStringToFile(const FilePath &file_path,
@@ -666,8 +669,3 @@
 
   ComputeKernelStackSignatureCommon();
 }
-
-int main(int argc, char **argv) {
-  SetUpTests(&argc, argv, false);
-  return RUN_ALL_TESTS();
-}
diff --git a/crash_reporter/testrunner.cc b/crash_reporter/testrunner.cc
new file mode 100644
index 0000000..24e2329
--- /dev/null
+++ b/crash_reporter/testrunner.cc
@@ -0,0 +1,13 @@
+// Copyright 2015 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <chromeos/test_helpers.h>
+#include <glib-object.h>
+#include <gtest/gtest.h>
+
+int main(int argc, char** argv) {
+  ::g_type_init();
+  SetUpTests(&argc, argv, true);
+  return RUN_ALL_TESTS();
+}
diff --git a/crash_reporter/udev_collector_test.cc b/crash_reporter/udev_collector_test.cc
index 148cf88..66daaf1 100644
--- a/crash_reporter/udev_collector_test.cc
+++ b/crash_reporter/udev_collector_test.cc
@@ -5,7 +5,7 @@
 #include <base/files/file_enumerator.h>
 #include <base/files/file_util.h>
 #include <base/files/scoped_temp_dir.h>
-#include <chromeos/test_helpers.h>
+#include <chromeos/syslog_logging.h>
 #include <gtest/gtest.h>
 
 #include "crash-reporter/udev_collector.h"
@@ -102,8 +102,3 @@
 
 // TODO(sque, crosbug.com/32238) - test wildcard cases, multiple identical udev
 // events.
-
-int main(int argc, char **argv) {
-  SetUpTests(&argc, argv, false);
-  return RUN_ALL_TESTS();
-}
diff --git a/crash_reporter/unclean_shutdown_collector_test.cc b/crash_reporter/unclean_shutdown_collector_test.cc
index f23174c..859b881 100644
--- a/crash_reporter/unclean_shutdown_collector_test.cc
+++ b/crash_reporter/unclean_shutdown_collector_test.cc
@@ -9,19 +9,20 @@
 #include <base/files/file_util.h>
 #include <base/strings/string_util.h>
 #include <chromeos/syslog_logging.h>
-#include <chromeos/test_helpers.h>
 #include <gtest/gtest.h>
 
-static int s_crashes = 0;
-static bool s_metrics = true;
-
-static const char kTestDirectory[] = "test";
-static const char kTestSuspended[] = "test/suspended";
-static const char kTestUnclean[] = "test/unclean";
-
 using base::FilePath;
 using ::chromeos::FindLog;
 
+namespace {
+
+int s_crashes = 0;
+bool s_metrics = true;
+
+const char kTestDirectory[] = "test";
+const char kTestSuspended[] = "test/suspended";
+const char kTestUnclean[] = "test/unclean";
+
 void CountCrash() {
   ++s_crashes;
 }
@@ -30,6 +31,8 @@
   return s_metrics;
 }
 
+}  // namespace
+
 class UncleanShutdownCollectorTest : public ::testing::Test {
   void SetUp() {
     s_crashes = 0;
@@ -120,8 +123,3 @@
   ASSERT_FALSE(collector_.Disable());
   rmdir(kTestUnclean);
 }
-
-int main(int argc, char **argv) {
-  SetUpTests(&argc, argv, false);
-  return RUN_ALL_TESTS();
-}
diff --git a/crash_reporter/user_collector_test.cc b/crash_reporter/user_collector_test.cc
index 4350f18..5a5bb0c 100644
--- a/crash_reporter/user_collector_test.cc
+++ b/crash_reporter/user_collector_test.cc
@@ -12,22 +12,23 @@
 #include <base/files/scoped_temp_dir.h>
 #include <base/strings/string_split.h>
 #include <chromeos/syslog_logging.h>
-#include <chromeos/test_helpers.h>
 #include <gtest/gtest.h>
 
-static int s_crashes = 0;
-static bool s_metrics = false;
-
-static const char kFilePath[] = "/my/path";
-
-// Keep in sync with UserCollector::ShouldDump.
-static const char kChromeIgnoreMsg[] =
-  "ignoring call by kernel - chrome crash; "
-  "waiting for chrome to call us directly";
-
 using base::FilePath;
 using chromeos::FindLog;
 
+namespace {
+
+int s_crashes = 0;
+bool s_metrics = false;
+
+const char kFilePath[] = "/my/path";
+
+// Keep in sync with UserCollector::ShouldDump.
+const char kChromeIgnoreMsg[] =
+    "ignoring call by kernel - chrome crash; "
+    "waiting for chrome to call us directly";
+
 void CountCrash() {
   ++s_crashes;
 }
@@ -36,6 +37,8 @@
   return s_metrics;
 }
 
+}  // namespace
+
 class UserCollectorTest : public ::testing::Test {
   void SetUp() {
     s_crashes = 0;
@@ -305,7 +308,7 @@
   pid_t my_pid = getpid();
   EXPECT_TRUE(collector_.GetExecutableBaseNameFromPid(my_pid, &base_name));
   EXPECT_FALSE(FindLog("Readlink failed"));
-  EXPECT_EQ("user_collector_test", base_name);
+  EXPECT_EQ("crash_reporter_test", base_name);
 }
 
 TEST_F(UserCollectorTest, GetFirstLineWithPrefix) {
@@ -539,8 +542,3 @@
   EXPECT_EQ(UserCollector::kErrorInvalidCoreFile,
             collector_.ValidateCoreFile(core_file));
 }
-
-int main(int argc, char **argv) {
-  SetUpTests(&argc, argv, false);
-  return RUN_ALL_TESTS();
-}