0

Modify chrome_frame_net_tests to integrate the refactoring to ProcessSingleton.

BUG=None
TEST=None


Review URL: https://chromiumcodereview.appspot.com/9959106

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@154628 0039d316-1c4b-4281-b951-d872f2087c98
This commit is contained in:
erikwright@chromium.org
2012-09-01 23:04:08 +00:00
parent 41c2a972a3
commit 400daf014d
5 changed files with 46 additions and 177 deletions

@ -418,8 +418,6 @@
'test/win_event_receiver.h',
'test/net/fake_external_tab.cc',
'test/net/fake_external_tab.h',
'test/net/process_singleton_subclass.cc',
'test/net/process_singleton_subclass.h',
'test/net/test_automation_provider.cc',
'test/net/test_automation_provider.h',
'test/net/test_automation_resource_message_filter.cc',

@ -51,6 +51,7 @@
#include "chrome/test/logging/win/test_log_collector.h"
#include "chrome_frame/crash_server_init.h"
#include "chrome_frame/test/chrome_frame_test_utils.h"
#include "chrome_frame/test/ie_configurator.h"
#include "chrome_frame/test/net/test_automation_resource_message_filter.h"
#include "chrome_frame/test/simulate_input.h"
#include "chrome_frame/test/win_event_receiver.h"
@ -63,6 +64,7 @@
#include "content/public/common/content_client.h"
#include "content/public/common/content_paths.h"
#include "net/base/net_util.h"
#include "net/url_request/url_request_test_util.h"
#include "sandbox/win/src/sandbox_types.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/resource/resource_bundle.h"
@ -441,7 +443,6 @@ FakeExternalTab::FakeExternalTab() {
PathService::Get(chrome::DIR_USER_DATA, &overridden_user_dir_);
PathService::Override(chrome::DIR_USER_DATA, user_data_dir_);
process_singleton_.reset(new ProcessSingleton(user_data_dir_));
}
FakeExternalTab::~FakeExternalTab() {
@ -490,7 +491,6 @@ void FakeExternalTab::InitializePostThreadsCreated() {
void FakeExternalTab::Shutdown() {
browser_process_.reset();
g_browser_process = NULL;
process_singleton_.reset();
ResourceBundle::CleanupSharedInstance();
}
@ -592,17 +592,6 @@ void CFUrlRequestUnittestRunner::Shutdown() {
OleUninitialize();
}
void CFUrlRequestUnittestRunner::OnConnectAutomationProviderToChannel(
const std::string& channel_id) {
Profile* profile = g_browser_process->profile_manager()->
GetDefaultProfile(fake_chrome_->user_data());
AutomationProviderList* list = g_browser_process->GetAutomationProviderList();
DCHECK(list);
list->AddProvider(
TestAutomationProvider::NewAutomationProvider(profile, channel_id, this));
}
void CFUrlRequestUnittestRunner::OnInitialTabLoaded() {
test_http_server_.reset();
BrowserThread::PostTask(
@ -750,15 +739,42 @@ int CFUrlRequestUnittestRunner::PreCreateThreads() {
fake_chrome_.reset(new FakeExternalTab());
fake_chrome_->Initialize();
fake_chrome_->browser_process()->PreCreateThreads();
pss_subclass_.reset(new ProcessSingletonSubclass(this));
EXPECT_TRUE(pss_subclass_->Subclass(fake_chrome_->user_data()));
StartChromeFrameInHostBrowser();
process_singleton_.reset(new ProcessSingleton(fake_chrome_->user_data()));
process_singleton_->Lock(NULL);
return 0;
}
bool CFUrlRequestUnittestRunner::ProcessSingletonNotificationCallback(
const CommandLine& command_line, const FilePath& current_directory) {
std::string channel_id = command_line.GetSwitchValueASCII(
switches::kAutomationClientChannelID);
EXPECT_FALSE(channel_id.empty());
Profile* profile = g_browser_process->profile_manager()->GetDefaultProfile(
fake_chrome_->user_data());
AutomationProviderList* list = g_browser_process->GetAutomationProviderList();
DCHECK(list);
list->AddProvider(
TestAutomationProvider::NewAutomationProvider(profile, channel_id, this));
return true;
}
void CFUrlRequestUnittestRunner::PreMainMessageLoopRun() {
fake_chrome_->InitializePostThreadsCreated();
ProcessSingleton::NotificationCallback callback(
base::Bind(
&CFUrlRequestUnittestRunner::ProcessSingletonNotificationCallback,
base::Unretained(this)));
if (!process_singleton_->Create(callback)) {
LOG(FATAL) << "Failed to start up ProcessSingleton. Is another test "
<< "executable or Chrome Frame running?";
if (crash_service_)
base::KillProcess(crash_service_, 0, false);
::ExitProcess(1);
}
StartChromeFrameInHostBrowser();
}
bool CFUrlRequestUnittestRunner::MainMessageLoopRun(int* result_code) {
@ -767,12 +783,13 @@ bool CFUrlRequestUnittestRunner::MainMessageLoopRun(int* result_code) {
// We need to allow IO on the main thread for these tests.
base::ThreadRestrictions::SetIOAllowed(true);
process_singleton_->Unlock();
StartInitializationTimeout();
return false;
}
void CFUrlRequestUnittestRunner::PostMainMessageLoopRun() {
process_singleton_->Cleanup();
fake_chrome_->browser_process()->StartTearDown();
// Must do this separately as the mock profile_manager_ is not the
@ -787,6 +804,7 @@ void CFUrlRequestUnittestRunner::PostMainMessageLoopRun() {
}
void CFUrlRequestUnittestRunner::PostDestroyThreads() {
process_singleton_.reset();
fake_chrome_->browser_process()->PostDestroyThreads();
fake_chrome_->Shutdown();
fake_chrome_.reset();

@ -13,18 +13,17 @@
#include "base/process.h"
#include "base/win/scoped_handle.h"
#include "chrome/browser/browser_process_impl.h"
#include "chrome_frame/test/ie_configurator.h"
#include "chrome_frame/test/net/process_singleton_subclass.h"
#include "chrome_frame/test/net/test_automation_provider.h"
#include "chrome_frame/test/test_server.h"
#include "chrome_frame/test_utils.h"
#include "content/public/browser/browser_main_parts.h"
#include "content/public/browser/browser_thread.h"
#include "net/base/net_test_suite.h"
#include "net/url_request/url_request_test_util.h"
class CommandLine;
class FakeBrowserProcessImpl;
class ProcessSingleton;
class ScopedCustomUrlRequestTestHttpHost;
namespace content {
class NotificationService;
@ -34,6 +33,10 @@ namespace logging_win {
class FileLogger;
} // namespace logging_win
namespace chrome_frame_test {
class IEConfigurator;
} // namespace chrome_frame_test
class FakeExternalTab {
public:
FakeExternalTab();
@ -53,7 +56,6 @@ class FakeExternalTab {
scoped_ptr<FakeBrowserProcessImpl> browser_process_;
FilePath overridden_user_dir_;
FilePath user_data_dir_;
scoped_ptr<ProcessSingleton> process_singleton_;
scoped_ptr<content::NotificationService> notificaton_service_;
DISALLOW_COPY_AND_ASSIGN(FakeExternalTab);
@ -68,7 +70,6 @@ class FakeExternalTab {
// lifetime events.
class CFUrlRequestUnittestRunner
: public NetTestSuite,
public ProcessSingletonSubclassDelegate,
public TestAutomationProviderDelegate,
public content::BrowserMainParts {
public:
@ -83,10 +84,6 @@ class CFUrlRequestUnittestRunner
virtual void Initialize();
virtual void Shutdown();
// ProcessSingletonSubclassDelegate.
virtual void OnConnectAutomationProviderToChannel(
const std::string& channel_id);
// TestAutomationProviderDelegate.
virtual void OnInitialTabLoaded();
virtual void OnProviderDestroyed();
@ -132,7 +129,6 @@ class CFUrlRequestUnittestRunner
// The fake chrome instance.
scoped_ptr<FakeExternalTab> fake_chrome_;
scoped_ptr<ProcessSingletonSubclass> pss_subclass_;
ScopedChromeFrameRegistrar registrar_;
int test_result_;
@ -148,9 +144,13 @@ class CFUrlRequestUnittestRunner
void StartInitializationTimeout();
void OnInitializationTimeout();
bool ProcessSingletonNotificationCallback(const CommandLine& command_line,
const FilePath& current_directory);
bool launch_browser_;
bool prompt_after_setup_;
bool tests_ran_;
scoped_ptr<ProcessSingleton> process_singleton_;
base::CancelableClosure timeout_closure_;
scoped_ptr<logging_win::FileLogger> file_logger_;
FilePath log_file_;

@ -1,110 +0,0 @@
// Copyright (c) 2010 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.
#include "chrome_frame/test/net/process_singleton_subclass.h"
#include "base/command_line.h"
#include "base/path_service.h"
#include "base/string_util.h"
#include "chrome/browser/browser_process_impl.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "chrome_frame/test/net/test_automation_provider.h"
#include "chrome_frame/function_stub.h"
#include "testing/gtest/include/gtest/gtest.h"
ProcessSingletonSubclass::ProcessSingletonSubclass(
ProcessSingletonSubclassDelegate* delegate)
: stub_(NULL), delegate_(delegate), original_wndproc_(NULL) {
}
ProcessSingletonSubclass::~ProcessSingletonSubclass() {
if (stub_) {
stub_->BypassStub(reinterpret_cast<void*>(original_wndproc_));
}
}
bool ProcessSingletonSubclass::Subclass(const FilePath& user_data_dir) {
DCHECK(stub_ == NULL);
DCHECK(original_wndproc_ == NULL);
HWND hwnd = FindWindowEx(HWND_MESSAGE, NULL, chrome::kMessageWindowClass,
user_data_dir.value().c_str());
if (!::IsWindow(hwnd))
return false;
// The window must be in this process for us to be able to subclass it.
DWORD pid = 0;
::GetWindowThreadProcessId(hwnd, &pid);
EXPECT_EQ(pid, ::GetCurrentProcessId());
original_wndproc_ = reinterpret_cast<WNDPROC>(::GetWindowLongPtr(hwnd,
GWLP_WNDPROC));
stub_ = FunctionStub::Create(reinterpret_cast<uintptr_t>(this),
&SubclassWndProc);
DCHECK(stub_);
::SetWindowLongPtr(hwnd, GWLP_WNDPROC,
reinterpret_cast<LONG_PTR>(stub_->code()));
return true;
}
// static
LRESULT ProcessSingletonSubclass::SubclassWndProc(ProcessSingletonSubclass* me,
HWND hwnd, UINT msg,
WPARAM wp, LPARAM lp) {
switch (msg) {
case WM_COPYDATA:
return me->OnCopyData(hwnd, reinterpret_cast<HWND>(wp),
reinterpret_cast<COPYDATASTRUCT*>(lp));
default:
break;
}
return me->original_wndproc_(hwnd, msg, wp, lp);
}
// static
LRESULT ProcessSingletonSubclass::OnCopyData(HWND hwnd, HWND from_hwnd,
const COPYDATASTRUCT* cds) {
// We should have enough room for the shortest command (min_message_size)
// and also be a multiple of wchar_t bytes. The shortest command
// possible is L"START\0\0" (empty current directory and command line).
static const int kMinMessageSize = sizeof(L"START\0");
EXPECT_TRUE(kMinMessageSize <= cds->cbData);
if (kMinMessageSize > cds->cbData)
return TRUE;
// We split the string into 4 parts on NULLs.
const wchar_t* begin = reinterpret_cast<const wchar_t*>(cds->lpData);
const wchar_t* end = begin + (cds->cbData / sizeof(wchar_t));
const wchar_t kNull = L'\0';
const wchar_t* eos = wmemchr(begin, kNull, end - begin);
EXPECT_NE(eos, end);
if (lstrcmpW(begin, L"START") == 0) {
begin = eos + 1;
EXPECT_TRUE(begin <= end);
eos = wmemchr(begin, kNull, end - begin);
EXPECT_NE(eos, end);
// Get current directory.
const wchar_t* cur_dir = begin;
begin = eos + 1;
EXPECT_TRUE(begin <= end);
eos = wmemchr(begin, kNull, end - begin);
// eos might be equal to end at this point.
// Get command line.
std::wstring cmd_line(begin, static_cast<size_t>(end - begin));
CommandLine parsed_command_line = CommandLine::FromString(cmd_line);
std::string channel_id = parsed_command_line.GetSwitchValueASCII(
switches::kAutomationClientChannelID);
EXPECT_FALSE(channel_id.empty());
delegate_->OnConnectAutomationProviderToChannel(channel_id);
}
return TRUE;
}

@ -1,37 +0,0 @@
// Copyright (c) 2010 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 CHROME_FRAME_TEST_NET_PROCESS_SINGLETON_SUBCLASS_H_
#define CHROME_FRAME_TEST_NET_PROCESS_SINGLETON_SUBCLASS_H_
#include <windows.h>
#include <string>
class FilePath;
struct FunctionStub;
class ProcessSingletonSubclassDelegate {
public:
virtual void OnConnectAutomationProviderToChannel(
const std::string& channel_id) = 0;
};
class ProcessSingletonSubclass {
public:
explicit ProcessSingletonSubclass(ProcessSingletonSubclassDelegate* delegate);
~ProcessSingletonSubclass();
bool Subclass(const FilePath& user_data_dir);
protected:
static LRESULT CALLBACK SubclassWndProc(ProcessSingletonSubclass* me,
HWND hwnd, UINT msg, WPARAM wp, LPARAM lp);
LRESULT OnCopyData(HWND hwnd, HWND from_hwnd, const COPYDATASTRUCT* cds);
protected:
FunctionStub* stub_;
ProcessSingletonSubclassDelegate* delegate_;
WNDPROC original_wndproc_;
};
#endif // CHROME_FRAME_TEST_NET_PROCESS_SINGLETON_SUBCLASS_H_