crashpad/test/gtest_main.cc
Joshua Peraza af2be80bdb android: configure a native test suite
Most Android tests in Chromium's infrastructure are
launched from an APK, but that's not appropriate for
Crashpad where many things expect to be run in a
plain executable.

Bug: chromium:1050178
Change-Id: I6eeb3f5e4889193e5bbe2a3bad2cd99a18e970ba
Reviewed-on: https://chromium-review.googlesource.com/c/crashpad/crashpad/+/2112342
Reviewed-by: Mark Mentovai <mark@chromium.org>
Commit-Queue: Joshua Peraza <jperaza@chromium.org>
2020-03-23 21:54:41 +00:00

111 lines
3.5 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Copyright 2017 The Crashpad Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "build/build_config.h"
#include "gtest/gtest.h"
#include "test/main_arguments.h"
#include "test/multiprocess_exec.h"
#if defined(CRASHPAD_TEST_LAUNCHER_GMOCK)
#include "gmock/gmock.h"
#endif // CRASHPAD_TEST_LAUNCHER_GMOCK
#if defined(OS_IOS)
#include "test/ios/google_test_setup.h"
#endif
#if defined(OS_WIN)
#include "test/win/win_child_process.h"
#endif // OS_WIN
#if defined(CRASHPAD_IS_IN_CHROMIUM)
#include "base/bind.h"
#include "base/test/launcher/unit_test_launcher.h"
#include "base/test/test_suite.h"
#endif // CRASHPAD_IS_IN_CHROMIUM
namespace {
#if !defined(OS_IOS)
bool GetChildTestFunctionName(std::string* child_func_name) {
constexpr size_t arg_length =
sizeof(crashpad::test::internal::kChildTestFunction) - 1;
for (const auto& it : crashpad::test::GetMainArguments()) {
if (it.compare(
0, arg_length, crashpad::test::internal::kChildTestFunction) == 0) {
*child_func_name = it.substr(arg_length);
return true;
}
}
return false;
}
#endif // !OS_IOS
} // namespace
int main(int argc, char* argv[]) {
crashpad::test::InitializeMainArguments(argc, argv);
#if !defined(OS_IOS)
std::string child_func_name;
if (GetChildTestFunctionName(&child_func_name)) {
return crashpad::test::internal::CheckedInvokeMultiprocessChild(
child_func_name);
}
#endif // !OS_IOS
#if defined(CRASHPAD_IS_IN_CHROMIUM)
#if defined(OS_WIN)
// Chromiums test launcher interferes with WinMultiprocess-based tests. Allow
// their child processes to be launched by the standard gtest-based test
// runner.
const bool use_chromium_test_launcher =
!crashpad::test::WinChildProcess::IsChildProcess();
#elif defined(OS_ANDROID)
constexpr bool use_chromium_test_launcher = false;
#else // OS_WIN
constexpr bool use_chromium_test_launcher = true;
#endif // OS_WIN
if (use_chromium_test_launcher) {
// This supports --test-launcher-summary-output, which writes a JSON file
// containing test details needed by Swarming.
base::TestSuite test_suite(argc, argv);
return base::LaunchUnitTests(
argc,
argv,
base::BindOnce(&base::TestSuite::Run, base::Unretained(&test_suite)));
}
#endif // CRASHPAD_IS_IN_CHROMIUM
#if defined(CRASHPAD_TEST_LAUNCHER_GMOCK)
testing::InitGoogleMock(&argc, argv);
#elif defined(CRASHPAD_TEST_LAUNCHER_GTEST)
testing::InitGoogleTest(&argc, argv);
#else // CRASHPAD_TEST_LAUNCHER_GMOCK
#error #define CRASHPAD_TEST_LAUNCHER_GTEST or CRASHPAD_TEST_LAUNCHER_GMOCK
#endif // CRASHPAD_TEST_LAUNCHER_GMOCK
#if defined(OS_IOS)
// iOS needs to run tests within the context of an app, so call a helper that
// invokes UIApplicationMain(). The application delegate will call
// RUN_ALL_TESTS() and exit before returning control to this function.
crashpad::test::IOSLaunchApplicationAndRunTests(argc, argv);
#else
return RUN_ALL_TESTS();
#endif
}