Skip to content

Commit

Permalink
Run ContentMain in a browser_test's browser process. This removes dup…
Browse files Browse the repository at this point in the history
…lication of code in the browser test harness for setting up the browser process, and also ensures that initialization code in ContentMainRunner runs.

Most of the changes are to unit tests which run in browser test executables. These were getting all the setup that these binaries did for browser tests even though they were unit tests. Now they have to explicitly setup objects that they need. This would be done automatically if they were in a unit test binary and therefore using the unit test harness. The goal should be to move these tests to unit test binaries, and make them support launching some tests in separate processes building on the work that Pawel did.

BUG=350550
R=sky@chromium.org

Review URL: https://codereview.chromium.org/190663012

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@257597 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
jam@chromium.org committed Mar 18, 2014
1 parent d75d143 commit a88f636
Show file tree
Hide file tree
Showing 77 changed files with 764 additions and 700 deletions.
25 changes: 24 additions & 1 deletion ash/drag_drop/drag_drop_interactive_uitest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@
#include "ash/test/ash_test_base.h"
#include "base/bind.h"
#include "base/message_loop/message_loop.h"
#include "base/path_service.h"
#include "base/strings/utf_string_conversions.h"
#include "ui/aura/window_event_dispatcher.h"
#include "ui/base/dragdrop/drag_drop_types.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/base/test/ui_controls.h"
#include "ui/base/ui_base_paths.h"
#include "ui/gl/gl_surface.h"
#include "ui/views/view.h"
#include "ui/views/widget/widget.h"

Expand Down Expand Up @@ -118,7 +122,26 @@ void DragDropAcrossMultiDisplay_Step1() {

} // namespace

typedef test::AshTestBase DragDropTest;
class DragDropTest : public test::AshTestBase {
public:
DragDropTest() {}
virtual ~DragDropTest() {}

virtual void SetUp() OVERRIDE {
gfx::GLSurface::InitializeOneOffForTests();

ui::RegisterPathProvider();
ResourceBundle::InitSharedInstanceWithLocale("en-US", NULL);
base::FilePath resources_pack_path;
PathService::Get(base::DIR_MODULE, &resources_pack_path);
resources_pack_path =
resources_pack_path.Append(FILE_PATH_LITERAL("resources.pak"));
ResourceBundle::GetSharedInstance().AddDataPackFromPath(
resources_pack_path, ui::SCALE_FACTOR_NONE);

test::AshTestBase::SetUp();
}
};

#if defined(OS_WIN)
#define MAYBE_DragDropAcrossMultiDisplay DISABLED_DragDropAcrossMultiDisplay
Expand Down
12 changes: 12 additions & 0 deletions ash/test/ash_test_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
#include "win8/test/test_registrar_constants.h"
#endif

#if defined(USE_X11)
#include <X11/Xlib.h>
#endif

namespace ash {
namespace test {
namespace {
Expand Down Expand Up @@ -90,6 +94,14 @@ AshTestBase::AshTestBase()
: setup_called_(false),
teardown_called_(false),
start_session_(true) {
#if defined(USE_X11)
// This is needed for tests which use this base class but are run in browser
// test binaries so don't get the default initialization in the unit test
// suite.
XInitThreads();
#endif

thread_bundle_.reset(new content::TestBrowserThreadBundle);
// Must initialize |ash_test_helper_| here because some tests rely on
// AshTestBase methods before they call AshTestBase::SetUp().
ash_test_helper_.reset(new AshTestHelper(base::MessageLoopForUI::current()));
Expand Down
2 changes: 1 addition & 1 deletion ash/test/ash_test_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class AshTestBase : public testing::Test {
bool teardown_called_;
// |SetUp()| doesn't activate session if this is set to false.
bool start_session_;
content::TestBrowserThreadBundle thread_bundle_;
scoped_ptr<content::TestBrowserThreadBundle> thread_bundle_;
scoped_ptr<AshTestHelper> ash_test_helper_;
scoped_ptr<aura::test::EventGenerator> event_generator_;
#if defined(OS_WIN)
Expand Down
26 changes: 25 additions & 1 deletion ash/wm/ash_native_cursor_manager_interactive_uitest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@
#include "ash/shell.h"
#include "ash/test/ash_test_base.h"
#include "ash/test/cursor_manager_test_api.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "ui/aura/window.h"
#include "ui/aura/window_event_dispatcher.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/base/test/ui_controls.h"
#include "ui/base/ui_base_paths.h"
#include "ui/gl/gl_surface.h"

#if defined(USE_X11)
#include <X11/Xlib.h>
Expand All @@ -22,7 +26,27 @@
namespace ash {

using ::wm::CursorManager;
typedef test::AshTestBase AshNativeCursorManagerTest;

class AshNativeCursorManagerTest : public test::AshTestBase {
public:
AshNativeCursorManagerTest() {}
virtual ~AshNativeCursorManagerTest() {}

virtual void SetUp() OVERRIDE {
gfx::GLSurface::InitializeOneOffForTests();

ui::RegisterPathProvider();
ResourceBundle::InitSharedInstanceWithLocale("en-US", NULL);
base::FilePath resources_pack_path;
PathService::Get(base::DIR_MODULE, &resources_pack_path);
resources_pack_path =
resources_pack_path.Append(FILE_PATH_LITERAL("resources.pak"));
ResourceBundle::GetSharedInstance().AddDataPackFromPath(
resources_pack_path, ui::SCALE_FACTOR_NONE);

test::AshTestBase::SetUp();
}
};

namespace {

Expand Down
27 changes: 20 additions & 7 deletions base/i18n/icu_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,21 @@
namespace base {
namespace i18n {

namespace {

#if !defined(NDEBUG)
// Assert that we are not called more than once. Even though calling this
// function isn't harmful (ICU can handle it), being called twice probably
// indicates a programming error.
bool g_called_once = false;
bool g_check_called_once = true;
#endif
}

bool InitializeICU() {
#ifndef NDEBUG
// Assert that we are not called more than once. Even though calling this
// function isn't harmful (ICU can handle it), being called twice probably
// indicates a programming error.
static bool called_once = false;
DCHECK(!called_once);
called_once = true;
#if !defined(NDEBUG)
DCHECK(!g_check_called_once || !g_called_once);
g_called_once = true;
#endif

#if (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_SHARED)
Expand Down Expand Up @@ -124,5 +131,11 @@ bool InitializeICU() {
#endif
}

void AllowMultipleInitializeCallsForTesting() {
#if !defined(NDEBUG)
g_check_called_once = false;
#endif
}

} // namespace i18n
} // namespace base
3 changes: 3 additions & 0 deletions base/i18n/icu_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ namespace i18n {
// function should be called before ICU is used.
BASE_I18N_EXPORT bool InitializeICU();

// In a test binary, the call above might occur twice.
BASE_I18N_EXPORT void AllowMultipleInitializeCallsForTesting();

} // namespace i18n
} // namespace base

Expand Down
1 change: 1 addition & 0 deletions chrome/browser/apps/web_view_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ class WebViewTest : public extensions::PlatformAppBrowserTest {
}

virtual void SetUpOnMainThread() OVERRIDE {
extensions::PlatformAppBrowserTest::SetUpOnMainThread();
const testing::TestInfo* const test_info =
testing::UnitTest::GetInstance()->current_test_info();
// Mock out geolocation for geolocation specific tests.
Expand Down
75 changes: 35 additions & 40 deletions chrome/browser/chrome_browser_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -868,49 +868,44 @@ int ChromeBrowserMainParts::PreCreateThreadsImpl() {
// just changed it to include experiments.
crash_keys::SetSwitchesFromCommandLine(CommandLine::ForCurrentProcess());

// If we're running tests (ui_task is non-null), then the ResourceBundle
// has already been initialized.
if (parameters().ui_task &&
!local_state_->IsManagedPreference(prefs::kApplicationLocale)) {
browser_process_->SetApplicationLocale("en-US");
} else {
// Mac starts it earlier in |PreMainMessageLoopStart()| (because it is
// needed when loading the MainMenu.nib and the language doesn't depend on
// anything since it comes from Cocoa.
// Mac starts it earlier in |PreMainMessageLoopStart()| (because it is
// needed when loading the MainMenu.nib and the language doesn't depend on
// anything since it comes from Cocoa.
#if defined(OS_MACOSX)
browser_process_->SetApplicationLocale(l10n_util::GetLocaleOverride());
std::string locale =
parameters().ui_task ? "en-US" : l10n_util::GetLocaleOverride();
browser_process_->SetApplicationLocale(locale);
#else
const std::string locale =
local_state_->GetString(prefs::kApplicationLocale);

// On a POSIX OS other than ChromeOS, the parameter that is passed to the
// method InitSharedInstance is ignored.

TRACE_EVENT_BEGIN0("startup",
"ChromeBrowserMainParts::PreCreateThreadsImpl:InitResourceBundle");
const std::string loaded_locale =
ResourceBundle::InitSharedInstanceWithLocale(locale, NULL);
TRACE_EVENT_END0("startup",
"ChromeBrowserMainParts::PreCreateThreadsImpl:InitResourceBundle");

if (loaded_locale.empty() &&
!parsed_command_line().HasSwitch(switches::kNoErrorDialogs)) {
ShowMissingLocaleMessageBox();
return chrome::RESULT_CODE_MISSING_DATA;
}
CHECK(!loaded_locale.empty()) << "Locale could not be found for " << locale;
browser_process_->SetApplicationLocale(loaded_locale);

base::FilePath resources_pack_path;
PathService::Get(chrome::FILE_RESOURCES_PACK, &resources_pack_path);
{
TRACE_EVENT0("startup",
"ChromeBrowserMainParts::PreCreateThreadsImpl:AddDataPack");
ResourceBundle::GetSharedInstance().AddDataPackFromPath(
resources_pack_path, ui::SCALE_FACTOR_NONE);
}
#endif // defined(OS_MACOSX)
const std::string locale =
local_state_->GetString(prefs::kApplicationLocale);

// On a POSIX OS other than ChromeOS, the parameter that is passed to the
// method InitSharedInstance is ignored.

TRACE_EVENT_BEGIN0("startup",
"ChromeBrowserMainParts::PreCreateThreadsImpl:InitResourceBundle");
const std::string loaded_locale =
ResourceBundle::InitSharedInstanceWithLocale(locale, NULL);
TRACE_EVENT_END0("startup",
"ChromeBrowserMainParts::PreCreateThreadsImpl:InitResourceBundle");

if (loaded_locale.empty() &&
!parsed_command_line().HasSwitch(switches::kNoErrorDialogs)) {
ShowMissingLocaleMessageBox();
return chrome::RESULT_CODE_MISSING_DATA;
}
CHECK(!loaded_locale.empty()) << "Locale could not be found for " << locale;
browser_process_->SetApplicationLocale(loaded_locale);

base::FilePath resources_pack_path;
PathService::Get(chrome::FILE_RESOURCES_PACK, &resources_pack_path);
{
TRACE_EVENT0("startup",
"ChromeBrowserMainParts::PreCreateThreadsImpl:AddDataPack");
ResourceBundle::GetSharedInstance().AddDataPackFromPath(
resources_pack_path, ui::SCALE_FACTOR_NONE);
}
#endif // defined(OS_MACOSX)

#if defined(TOOLKIT_GTK)
g_set_application_name(l10n_util::GetStringUTF8(IDS_PRODUCT_NAME).c_str());
Expand Down
29 changes: 14 additions & 15 deletions chrome/browser/chrome_browser_main_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -174,27 +174,26 @@ void RecordCatSixtyFour() {
// Initialize NSApplication using the custom subclass.
chrome_browser_application_mac::RegisterBrowserCrApp();

// If ui_task is not NULL, the app is actually a browser_test, so startup is
// handled outside of BrowserMain (which is what called this).
// If ui_task is not NULL, the app is actually a browser_test.
if (!parameters().ui_task) {
// The browser process only wants to support the language Cocoa will use,
// so force the app locale to be overriden with that value.
l10n_util::OverrideLocaleWithCocoaLocale();

// Before we load the nib, we need to start up the resource bundle so we
// have the strings avaiable for localization.
// TODO(markusheintz): Read preference pref::kApplicationLocale in order
// to enforce the application locale.
const std::string loaded_locale =
ResourceBundle::InitSharedInstanceWithLocale(std::string(), NULL);
CHECK(!loaded_locale.empty()) << "Default locale could not be found";

base::FilePath resources_pack_path;
PathService::Get(chrome::FILE_RESOURCES_PACK, &resources_pack_path);
ResourceBundle::GetSharedInstance().AddDataPackFromPath(
resources_pack_path, ui::SCALE_FACTOR_NONE);
}

// Before we load the nib, we need to start up the resource bundle so we
// have the strings avaiable for localization.
// TODO(markusheintz): Read preference pref::kApplicationLocale in order
// to enforce the application locale.
const std::string loaded_locale =
ResourceBundle::InitSharedInstanceWithLocale(std::string(), NULL);
CHECK(!loaded_locale.empty()) << "Default locale could not be found";

base::FilePath resources_pack_path;
PathService::Get(chrome::FILE_RESOURCES_PACK, &resources_pack_path);
ResourceBundle::GetSharedInstance().AddDataPackFromPath(
resources_pack_path, ui::SCALE_FACTOR_NONE);

// This is a no-op if the KeystoneRegistration framework is not present.
// The framework is only distributed with branded Google Chrome builds.
[[KeystoneGlue defaultKeystoneGlue] registerWithKeystone];
Expand Down
Loading

0 comments on commit a88f636

Please sign in to comment.