Adds support for running iOS unittests via XCTest.

Using XCTest allows us to drive tests from the commandline via
xcodebuild, and it also simplifies running tests on physical devices.

Tests put themselves into "XCTest-mode" if the
"XCTestConfigurationFilePath" environment variable is present.  This
variable is only set when XCTests are running.

Change-Id: If55199a7470f0479f107097eef1dfb1a705015e9
Reviewed-on: https://chromium-review.googlesource.com/c/crashpad/crashpad/+/2033427
Commit-Queue: Rohit Rao <rohitrao@chromium.org>
Reviewed-by: Mark Mentovai <mark@chromium.org>
This commit is contained in:
Rohit Rao 2020-01-31 14:51:39 -05:00 committed by Commit Bot
parent 3b007333a0
commit 39116ab723
8 changed files with 140 additions and 33 deletions

View File

@ -28,8 +28,9 @@ if (crashpad_is_in_chromium) {
outputs = [ "{{bundle_contents_dir}}/{{source_file_part}}" ]
}
ios_app_bundle(target_name) {
ios_xctest_test(target_name) {
testonly = true
xctest_module_target = "//test/ios:google_test_runner"
info_plist = "//build/ios/Unittest-Info.plist"
extra_substitutions = [ "GTEST_BUNDLE_ID_SUFFIX=$target_name" ]
forward_variables_from(invoker, "*")

View File

@ -233,19 +233,6 @@ if (!crashpad_is_ios) {
}
}
if (crashpad_is_ios) {
source_set("test_runner_ios") {
testonly = true
sources = [
"gtest_runner_ios.h",
"gtest_runner_ios.mm",
]
configs += [ "..:crashpad_config" ]
deps = [ "../third_party/gtest:gtest" ]
libs = [ "UIKit.framework" ]
}
}
static_library("gmock_main") {
testonly = true
sources = [ "gtest_main.cc" ]
@ -259,7 +246,7 @@ static_library("gmock_main") {
"../third_party/mini_chromium:base_test_support",
]
if (crashpad_is_ios) {
deps += [ ":test_runner_ios" ]
deps += [ "ios:google_test_setup" ]
}
}
@ -275,6 +262,6 @@ static_library("gtest_main") {
"../third_party/mini_chromium:base_test_support",
]
if (crashpad_is_ios) {
deps += [ ":test_runner_ios" ]
deps += [ "ios:google_test_setup" ]
}
}

View File

@ -22,7 +22,7 @@
#endif // CRASHPAD_TEST_LAUNCHER_GMOCK
#if defined(OS_IOS)
#include "test/gtest_runner_ios.h"
#include "test/ios/google_test_setup.h"
#endif
#if defined(OS_WIN)

View File

@ -28,12 +28,43 @@ group("all_tests") {
]
}
source_set("xcuitests") {
source_set("google_test_runner_shared_headers") {
testonly = true
sources = [ "cptest_google_test_runner_delegate.h" ]
}
source_set("google_test_runner") {
testonly = true
sources = [ "cptest_google_test_runner.mm" ]
configs += [ "../..:crashpad_config" ]
deps = [
"../../build:ios_enable_arc",
"../../build:ios_xctest",
"../../third_party/mini_chromium:base",
]
libs = [ "UIKit.framework" ]
}
source_set("google_test_setup") {
testonly = true
sources = [
"crash_type_xctest.mm",
"google_test_setup.h",
"google_test_setup.mm",
]
configs += [ "../..:crashpad_config" ]
deps = [
":google_test_runner_shared_headers",
"../../build:ios_enable_arc",
"../../third_party/gtest:gtest",
"../../third_party/mini_chromium:base",
]
libs = [ "UIKit.framework" ]
}
source_set("xcuitests") {
testonly = true
sources = [ "crash_type_xctest.mm" ]
configs += [ "../..:crashpad_config" ]
deps = [
"../../build:ios_enable_arc",
"../../build:ios_xctest",
@ -44,7 +75,5 @@ source_set("xcuitests") {
ios_xcuitest_test("ios_crash_xcuitests_module") {
xcode_test_application_name = "ios_crash_xcuitests"
deps = [
":xcuitests",
]
deps = [ ":xcuitests" ]
}

View File

@ -0,0 +1,41 @@
// Copyright 2020 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.
#import <UIKit/UIKit.h>
#import <XCTest/XCTest.h>
#include "base/logging.h"
#import "test/ios/cptest_google_test_runner_delegate.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
@interface CPTestGoogleTestRunner : XCTestCase
@end
@implementation CPTestGoogleTestRunner
- (void)testRunGoogleTests {
id appDelegate = UIApplication.sharedApplication.delegate;
DCHECK([appDelegate
conformsToProtocol:@protocol(CPTestGoogleTestRunnerDelegate)]);
id<CPTestGoogleTestRunnerDelegate> runnerDelegate =
static_cast<id<CPTestGoogleTestRunnerDelegate>>(appDelegate);
DCHECK(runnerDelegate.supportsRunningGoogleTestsWithXCTest);
XCTAssertTrue([runnerDelegate runGoogleTests] == 0);
}
@end

View File

@ -0,0 +1,30 @@
// Copyright 2020 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.
#ifndef CRASHPAD_TEST_IOS_CPTEST_GOOGLE_TEST_RUNNER_DELEGATE_
#define CRASHPAD_TEST_IOS_CPTEST_GOOGLE_TEST_RUNNER_DELEGATE_
@protocol CPTestGoogleTestRunnerDelegate
// Returns YES if this delegate supports running GoogleTests via a call to
// |runGoogleTests|.
@property(nonatomic, readonly, assign)
BOOL supportsRunningGoogleTestsWithXCTest;
// Runs GoogleTests and returns the final exit code.
- (int)runGoogleTests;
@end
#endif // CRASHPAD_TEST_IOS_CPTEST_GOOGLE_TEST_RUNNER_DELEGATE_H_

View File

@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef CRASHPAD_TEST_GTEST_RUNNER_IOS_
#define CRASHPAD_TEST_GTEST_RUNNER_IOS_
#ifndef CRASHPAD_TEST_IOS_GOOGLE_TEST_SETUP_
#define CRASHPAD_TEST_IOS_GOOGLE_TEST_SETUP_
namespace crashpad {
namespace test {
@ -30,4 +30,4 @@ void IOSLaunchApplicationAndRunTests(int argc, char* argv[]);
} // namespace test
} // namespace crashpad
#endif // CRASHPAD_TEST_GTEST_RUNNER_IOS_
#endif // CRASHPAD_TEST_IOS_GOOGLE_TEST_SETUP_

View File

@ -12,11 +12,17 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "test/gtest_runner_ios.h"
#include "test/ios/google_test_setup.h"
#import <UIKit/UIKit.h>
#include "base/logging.h"
#include "gtest/gtest.h"
#include "test/ios/cptest_google_test_runner_delegate.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
@interface UIApplication (Testing)
- (void)_terminateWithStatus:(int)status;
@ -47,7 +53,7 @@ void RegisterTestEndListener() {
} // namespace
@interface CrashpadUnitTestDelegate : NSObject
@interface CrashpadUnitTestDelegate : NSObject <CPTestGoogleTestRunnerDelegate>
@property(nonatomic, readwrite, strong) UIWindow* window;
- (void)runTests;
@end
@ -71,15 +77,29 @@ void RegisterTestEndListener() {
[controller.view addSubview:label];
// Queue up the test run.
[self performSelector:@selector(runTests) withObject:nil afterDelay:0.1];
if (![self supportsRunningGoogleTestsWithXCTest]) {
// When running in XCTest mode, XCTest will invoke |runGoogleTest| directly.
// Otherwise, schedule a call to |runTests|.
[self performSelector:@selector(runTests) withObject:nil afterDelay:0.1];
}
return YES;
}
- (void)runTests {
RegisterTestEndListener();
- (BOOL)supportsRunningGoogleTestsWithXCTest {
return getenv("XCTestConfigurationFilePath") != nullptr;
}
- (int)runGoogleTests {
RegisterTestEndListener();
int exitStatus = RUN_ALL_TESTS();
return exitStatus;
}
- (void)runTests {
DCHECK(![self supportsRunningGoogleTestsWithXCTest]);
int exitStatus = [self runGoogleTests];
// If a test app is too fast, it will exit before Instruments has has a
// a chance to initialize and no test results will be seen.
@ -98,7 +118,6 @@ void RegisterTestEndListener() {
@end
namespace crashpad {
namespace test {
@ -110,5 +129,5 @@ void IOSLaunchApplicationAndRunTests(int argc, char* argv[]) {
}
}
} // namespace crashpad
} // namespace test
} // namespace test
} // namespace crashpad