Commit 8432eaa4 authored by zhaoyangli's avatar zhaoyangli Committed by Commit Bot

Enabling reading command line args in EG2 tests

Initialize base::CommandLine arguments from NSProcessInfo in EG2 test
bundle so that command line arguments can be accessed in EG2 test cases
through base::CommandLine::ForCurrentProcess().

Bug: 922813, 1000474
Change-Id: Ie214ca281fedd5f24b489e2f1605c28ce3abd669
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1833044Reviewed-by: default avatarRohit Rao <rohitrao@chromium.org>
Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Commit-Queue: Zhaoyang Li <zhaoyangli@chromium.org>
Cr-Commit-Position: refs/heads/master@{#702103}
parent 6b920324
......@@ -10,6 +10,7 @@
#include "base/at_exit.h"
#include "base/command_line.h"
#include "base/logging.h"
#include "base/strings/sys_string_conversions.h"
#include "ui/base/l10n/l10n_util_mac.h"
#include "ui/base/resource/resource_bundle.h"
......@@ -26,8 +27,27 @@ namespace {
class TestMain {
public:
TestMain() {
// Initialize the CommandLine, because ResourceBundle requires it to exist.
base::CommandLine::Init(/*argc=*/0, /*argv=*/nullptr);
NSArray* arguments = NSProcessInfo.processInfo.arguments;
// Convert NSArray to the required input type of |base::CommandLine::Init|.
int argc = arguments.count;
const char* argv[argc];
std::vector<std::string> argv_store;
// Avoid using std::vector::push_back (or any other method that could cause
// the vector to grow) as this will cause the std::string to be copied or
// moved (depends on the C++ implementation) which may invalidates the
// pointer returned by std::string::c_str(). Even if the strings are moved,
// this may cause garbage if std::string uses optimisation for small strings
// (by returning pointer to the object internals in that case).
argv_store.resize(argc);
for (int i = 0; i < argc; i++) {
argv_store[i] = base::SysNSStringToUTF8(arguments[i]);
argv[i] = argv_store[i].c_str();
}
// Initialize the CommandLine with arguments. ResourceBundle requires
// CommandLine to exist.
base::CommandLine::Init(argc, argv);
// Load pak files into the ResourceBundle.
l10n_util::OverrideLocaleWithCocoaLocale();
......
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