Commit 7c09e61b authored by Rohit Rao's avatar Rohit Rao Committed by Commit Bot

[ios] Adds startup code to EG2 test bundles.

Registers an NSPrincipalClass in the test bundle's Info.plist, which
provides the name of a class that is automatically instantiated when the
test bundle is loaded into the test runner process.  We can use this
class, along with the XCTestObservation protocol, to run startup code
before any tests are run.

In this CL, we copy pak files into the test runner's bundle and
initialize the ResourceBundle at startup, which allows EG2 tests to use
string resources as needed.

BUG=922813

Change-Id: I348ed41be3939aea2a7525205969a538bbaaefa2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1618024
Commit-Queue: Rohit Rao <rohitrao@chromium.org>
Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Cr-Commit-Position: refs/heads/master@{#661032}
parent e4ba0404
......@@ -378,6 +378,8 @@ source_set("eg_test_support+eg2") {
"chrome_earl_grey.h",
"chrome_earl_grey.mm",
"chrome_earl_grey_app_interface.h",
"chrome_egtest_bundle_main.h",
"chrome_egtest_bundle_main.mm",
"chrome_error_util.h",
"chrome_matchers.h",
"chrome_matchers.mm",
......@@ -393,6 +395,7 @@ source_set("eg_test_support+eg2") {
"//ios/testing/earl_grey:eg_test_support+eg2",
"//ios/third_party/earl_grey2:test_lib",
"//ios/web/public/test:element_selector",
"//ui/base",
"//url",
]
}
// Copyright 2019 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.
#ifndef IOS_CHROME_TEST_EARL_GREY_CHROME_EGTEST_BUNDLE_MAIN_H_
#define IOS_CHROME_TEST_EARL_GREY_CHROME_EGTEST_BUNDLE_MAIN_H_
#import <Foundation/Foundation.h>
// An object that can be set as the NSPrincipalClass for an EG2 test bundle and
// performs various startup-related tasks. This is necessary because test
// bundles don't have a fixed entry point in the way that application bundles
// do.
@interface ChromeEGTestBundleMain : NSObject
@end
#endif // IOS_CHROME_TEST_EARL_GREY_CHROME_EGTEST_BUNDLE_MAIN_H_
// Copyright 2019 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 "ios/chrome/test/earl_grey/chrome_egtest_bundle_main.h"
#import <XCTest/XCTest.h>
#include <memory>
#include "base/at_exit.h"
#include "base/command_line.h"
#include "base/logging.h"
#include "ui/base/l10n/l10n_util_mac.h"
#include "ui/base/resource/resource_bundle.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
namespace {
// Contains startup code for a Chrome EG2 test module. Performs startup tasks
// when constructed and shutdown tasks when destroyed. Callers should create an
// instance of this object before running any tests and destroy the instance
// after tests have completed.
class TestMain {
public:
TestMain() {
// Initialize the CommandLine, because ResourceBundle requires it to exist.
base::CommandLine::Init(/*argc=*/0, /*argv=*/nullptr);
// Load pak files into the ResourceBundle.
l10n_util::OverrideLocaleWithCocoaLocale();
const std::string loaded_locale =
ui::ResourceBundle::InitSharedInstanceWithLocale(
/*pref_locale=*/std::string(), /*delegate=*/nullptr,
ui::ResourceBundle::LOAD_COMMON_RESOURCES);
CHECK(!loaded_locale.empty());
}
~TestMain() {}
private:
base::AtExitManager exit_manager_;
DISALLOW_COPY_AND_ASSIGN(TestMain);
};
}
@interface ChromeEGTestBundleMain () <XCTestObservation> {
std::unique_ptr<TestMain> _testMain;
}
@end
@implementation ChromeEGTestBundleMain
- (instancetype)init {
if ((self = [super init])) {
[[XCTestObservationCenter sharedTestObservationCenter]
addTestObserver:self];
}
return self;
}
#pragma mark - XCTestObservation
- (void)testBundleWillStart:(NSBundle*)testBundle {
DCHECK(!_testMain);
_testMain = std::make_unique<TestMain>();
// Ensure that //ios/web and the bulk of //ios/chrome/browser are not compiled
// into the test module. This is hard to assert at compile time, due to
// transitive dependencies, but at runtime it's easy to check if certain key
// classes are present or absent.
CHECK(NSClassFromString(@"CRWWebController") == nil);
CHECK(NSClassFromString(@"MainController") == nil);
CHECK(NSClassFromString(@"BrowserViewController") == nil);
}
- (void)testBundleDidFinish:(NSBundle*)testBundle {
[[XCTestObservationCenter sharedTestObservationCenter]
removeTestObserver:self];
_testMain.reset();
}
@end
......@@ -67,9 +67,11 @@ source_set("eg2_tests") {
deps = [
":shared_helper_headers",
"//ios/chrome/app/strings",
"//ios/chrome/test/earl_grey:eg_test_support+eg2",
"//ios/testing/earl_grey:eg_test_support+eg2",
"//ios/third_party/earl_grey2:test_lib",
"//ui/base",
]
libs = [ "UIKit.framework" ]
......
......@@ -148,10 +148,14 @@ template("chrome_ios_eg2_test") {
"deps",
])
xctest_bundle_principal_class = "ChromeEGTestBundleMain"
if (!defined(deps)) {
deps = []
}
deps += [ "//ios/chrome/test/earl_grey:eg_test_support+eg2" ]
bundle_deps = [ "//ios/chrome/app/resources" ]
}
}
......
......@@ -5,6 +5,7 @@
#import <TestLib/EarlGreyImpl/EarlGrey.h>
#import <UIKit/UIKit.h>
#include "ios/chrome/grit/ios_strings.h"
#import "ios/chrome/test/earl_grey/chrome_actions.h"
#import "ios/chrome/test/earl_grey/chrome_earl_grey.h"
#import "ios/chrome/test/earl_grey/chrome_error_util.h"
......@@ -12,6 +13,7 @@
#import "ios/chrome/test/earl_grey2/chrome_earl_grey_edo.h"
#import "ios/testing/earl_grey/base_earl_grey_test_case.h"
#import "ios/testing/earl_grey/earl_grey_test.h"
#include "ui/base/l10n/l10n_util_mac.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
......@@ -85,4 +87,19 @@
[ChromeEarlGrey clearBrowsingHistory];
}
// Tests that string resources are loaded into the ResourceBundle and available
// for use in tests.
- (void)testAppResourcesArePresent {
[[EarlGrey selectElementWithMatcher:chrome_test_util::ToolsMenuButton()]
performAction:grey_tap()];
NSString* settingsLabel = l10n_util::GetNSString(IDS_IOS_TOOLBAR_SETTINGS);
[[EarlGrey selectElementWithMatcher:grey_accessibilityLabel(settingsLabel)]
assertWithMatcher:grey_sufficientlyVisible()];
// Tap a second time to close the menu.
[[EarlGrey selectElementWithMatcher:chrome_test_util::ToolsMenuButton()]
performAction:grey_tap()];
}
@end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment