mirror of
https://bitbucket.org/chromiumembedded/cef
synced 2025-02-09 00:28:59 +01:00
Create separate implementation file for TestHandler methods and globals.
git-svn-id: https://chromiumembedded.googlecode.com/svn/trunk@446 5089003a-bbd8-11dd-ad1f-f1f9622dbc98
This commit is contained in:
parent
8ece0ba240
commit
893176fe5a
1
cef.gyp
1
cef.gyp
@ -318,6 +318,7 @@
|
|||||||
'tests/unittests/stream_unittest.cc',
|
'tests/unittests/stream_unittest.cc',
|
||||||
'tests/unittests/string_unittest.cc',
|
'tests/unittests/string_unittest.cc',
|
||||||
'tests/unittests/storage_unittest.cc',
|
'tests/unittests/storage_unittest.cc',
|
||||||
|
'tests/unittests/test_handler.cc',
|
||||||
'tests/unittests/test_handler.h',
|
'tests/unittests/test_handler.h',
|
||||||
'tests/unittests/test_suite.cc',
|
'tests/unittests/test_suite.cc',
|
||||||
'tests/unittests/test_suite.h',
|
'tests/unittests/test_suite.h',
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
// can be found in the LICENSE file.
|
// can be found in the LICENSE file.
|
||||||
|
|
||||||
#include "include/cef_origin_whitelist.h"
|
#include "include/cef_origin_whitelist.h"
|
||||||
|
#include "include/cef_runnable.h"
|
||||||
#include "include/cef_scheme.h"
|
#include "include/cef_scheme.h"
|
||||||
#include "test_handler.h"
|
#include "test_handler.h"
|
||||||
|
|
||||||
|
129
tests/unittests/test_handler.cc
Normal file
129
tests/unittests/test_handler.cc
Normal file
@ -0,0 +1,129 @@
|
|||||||
|
// Copyright (c) 2011 The Chromium Embedded Framework 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 "test_handler.h"
|
||||||
|
#include "include/cef_runnable.h"
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
void NotifyEvent(base::WaitableEvent* event)
|
||||||
|
{
|
||||||
|
event->Signal();
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
|
||||||
|
// TestHandler
|
||||||
|
|
||||||
|
TestHandler::TestHandler()
|
||||||
|
: browser_hwnd_(NULL), completion_event_(true, false)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
TestHandler::~TestHandler()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void TestHandler::OnAfterCreated(CefRefPtr<CefBrowser> browser)
|
||||||
|
{
|
||||||
|
AutoLock lock_scope(this);
|
||||||
|
if(!browser->IsPopup())
|
||||||
|
{
|
||||||
|
// Keep the main child window, but not popup windows
|
||||||
|
browser_ = browser;
|
||||||
|
browser_hwnd_ = browser->GetWindowHandle();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void TestHandler::OnBeforeClose(CefRefPtr<CefBrowser> browser)
|
||||||
|
{
|
||||||
|
AutoLock lock_scope(this);
|
||||||
|
if(browser_hwnd_ == browser->GetWindowHandle())
|
||||||
|
{
|
||||||
|
// Free the browser pointer so that the browser can be destroyed
|
||||||
|
browser_ = NULL;
|
||||||
|
browser_hwnd_ = NULL;
|
||||||
|
|
||||||
|
// Signal that the test is now complete.
|
||||||
|
completion_event_.Signal();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool TestHandler::OnBeforeResourceLoad(CefRefPtr<CefBrowser> browser,
|
||||||
|
CefRefPtr<CefRequest> request,
|
||||||
|
CefString& redirectUrl,
|
||||||
|
CefRefPtr<CefStreamReader>& resourceStream,
|
||||||
|
CefRefPtr<CefResponse> response,
|
||||||
|
int loadFlags)
|
||||||
|
{
|
||||||
|
AutoLock lock_scope(this);
|
||||||
|
if(resource_map_.size() > 0) {
|
||||||
|
CefString url = request->GetURL();
|
||||||
|
ResourceMap::const_iterator it = resource_map_.find(url);
|
||||||
|
if(it != resource_map_.end()) {
|
||||||
|
// Return the previously mapped resource
|
||||||
|
resourceStream = CefStreamReader::CreateForData(
|
||||||
|
(void*)it->second.first.c_str(), it->second.first.length());
|
||||||
|
response->SetMimeType(it->second.second);
|
||||||
|
response->SetStatus(200);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void TestHandler::ExecuteTest()
|
||||||
|
{
|
||||||
|
// Run the test
|
||||||
|
RunTest();
|
||||||
|
|
||||||
|
// Wait for the test to complete
|
||||||
|
completion_event_.Wait();
|
||||||
|
|
||||||
|
// Reset the event so the same test can be executed again.
|
||||||
|
completion_event_.Reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
void TestHandler::DestroyTest()
|
||||||
|
{
|
||||||
|
Lock();
|
||||||
|
#if defined(OS_WIN)
|
||||||
|
if(browser_hwnd_ != NULL)
|
||||||
|
PostMessage(browser_hwnd_, WM_CLOSE, 0, 0);
|
||||||
|
#endif
|
||||||
|
Unlock();
|
||||||
|
}
|
||||||
|
|
||||||
|
void TestHandler::CreateBrowser(const CefString& url)
|
||||||
|
{
|
||||||
|
CefWindowInfo windowInfo;
|
||||||
|
CefBrowserSettings settings;
|
||||||
|
#if defined(OS_WIN)
|
||||||
|
windowInfo.SetAsPopup(NULL, "CefUnitTest");
|
||||||
|
windowInfo.m_dwStyle |= WS_VISIBLE;
|
||||||
|
#endif
|
||||||
|
CefBrowser::CreateBrowser(windowInfo, this, url, settings);
|
||||||
|
}
|
||||||
|
|
||||||
|
void TestHandler::AddResource(const CefString& key, const std::string& value,
|
||||||
|
const CefString& mimeType)
|
||||||
|
{
|
||||||
|
resource_map_.insert(std::make_pair(key, std::make_pair(value, mimeType)));
|
||||||
|
}
|
||||||
|
|
||||||
|
void TestHandler::ClearResources()
|
||||||
|
{
|
||||||
|
resource_map_.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// global functions
|
||||||
|
|
||||||
|
void WaitForThread(CefThreadId thread_id)
|
||||||
|
{
|
||||||
|
base::WaitableEvent event(true, false);
|
||||||
|
CefPostTask(thread_id, NewCefRunnableFunction(&NotifyEvent, &event));
|
||||||
|
event.Wait();
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
// Copyright (c) 2009 The Chromium Embedded Framework Authors. All rights
|
// Copyright (c) 2011 The Chromium Embedded Framework Authors. All rights
|
||||||
// reserved. Use of this source code is governed by a BSD-style license that
|
// reserved. Use of this source code is governed by a BSD-style license that
|
||||||
// can be found in the LICENSE file.
|
// can be found in the LICENSE file.
|
||||||
|
|
||||||
@ -6,9 +6,9 @@
|
|||||||
#define _TEST_HANDLER_H
|
#define _TEST_HANDLER_H
|
||||||
|
|
||||||
#include "include/cef_browser.h"
|
#include "include/cef_browser.h"
|
||||||
#include "include/cef_frame.h"
|
|
||||||
#include "include/cef_client.h"
|
#include "include/cef_client.h"
|
||||||
#include "include/cef_runnable.h"
|
#include "include/cef_frame.h"
|
||||||
|
#include "include/cef_task.h"
|
||||||
#include "base/synchronization/waitable_event.h"
|
#include "base/synchronization/waitable_event.h"
|
||||||
#include "testing/gtest/include/gtest/gtest.h"
|
#include "testing/gtest/include/gtest/gtest.h"
|
||||||
|
|
||||||
@ -34,13 +34,8 @@ class TestHandler : public CefClient,
|
|||||||
public CefV8ContextHandler
|
public CefV8ContextHandler
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
TestHandler() : browser_hwnd_(NULL), completion_event_(true, false)
|
TestHandler();
|
||||||
{
|
virtual ~TestHandler();
|
||||||
}
|
|
||||||
|
|
||||||
virtual ~TestHandler()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
// Implement this method to run the test
|
// Implement this method to run the test
|
||||||
virtual void RunTest() =0;
|
virtual void RunTest() =0;
|
||||||
@ -58,116 +53,35 @@ public:
|
|||||||
{ return this; }
|
{ return this; }
|
||||||
|
|
||||||
// CefLifeSpanHandler methods
|
// CefLifeSpanHandler methods
|
||||||
|
virtual void OnAfterCreated(CefRefPtr<CefBrowser> browser) OVERRIDE;
|
||||||
virtual void OnAfterCreated(CefRefPtr<CefBrowser> browser) OVERRIDE
|
virtual void OnBeforeClose(CefRefPtr<CefBrowser> browser) OVERRIDE;
|
||||||
{
|
|
||||||
AutoLock lock_scope(this);
|
|
||||||
if(!browser->IsPopup())
|
|
||||||
{
|
|
||||||
// Keep the main child window, but not popup windows
|
|
||||||
browser_ = browser;
|
|
||||||
browser_hwnd_ = browser->GetWindowHandle();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual void OnBeforeClose(CefRefPtr<CefBrowser> browser) OVERRIDE
|
|
||||||
{
|
|
||||||
AutoLock lock_scope(this);
|
|
||||||
if(browser_hwnd_ == browser->GetWindowHandle())
|
|
||||||
{
|
|
||||||
// Free the browser pointer so that the browser can be destroyed
|
|
||||||
browser_ = NULL;
|
|
||||||
browser_hwnd_ = NULL;
|
|
||||||
|
|
||||||
// Signal that the test is now complete.
|
|
||||||
completion_event_.Signal();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// CefRequestHandler methods
|
// CefRequestHandler methods
|
||||||
|
|
||||||
virtual bool OnBeforeResourceLoad(CefRefPtr<CefBrowser> browser,
|
virtual bool OnBeforeResourceLoad(CefRefPtr<CefBrowser> browser,
|
||||||
CefRefPtr<CefRequest> request,
|
CefRefPtr<CefRequest> request,
|
||||||
CefString& redirectUrl,
|
CefString& redirectUrl,
|
||||||
CefRefPtr<CefStreamReader>& resourceStream,
|
CefRefPtr<CefStreamReader>& resourceStream,
|
||||||
CefRefPtr<CefResponse> response,
|
CefRefPtr<CefResponse> response,
|
||||||
int loadFlags) OVERRIDE
|
int loadFlags) OVERRIDE;
|
||||||
{
|
|
||||||
AutoLock lock_scope(this);
|
|
||||||
if(resource_map_.size() > 0) {
|
|
||||||
CefString url = request->GetURL();
|
|
||||||
ResourceMap::const_iterator it = resource_map_.find(url);
|
|
||||||
if(it != resource_map_.end()) {
|
|
||||||
// Return the previously mapped resource
|
|
||||||
resourceStream = CefStreamReader::CreateForData(
|
|
||||||
(void*)it->second.first.c_str(), it->second.first.length());
|
|
||||||
response->SetMimeType(it->second.second);
|
|
||||||
response->SetStatus(200);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
CefRefPtr<CefBrowser> GetBrowser() { return browser_; }
|
||||||
}
|
CefWindowHandle GetBrowserHwnd() { return browser_hwnd_; }
|
||||||
|
|
||||||
CefRefPtr<CefBrowser> GetBrowser()
|
|
||||||
{
|
|
||||||
return browser_;
|
|
||||||
}
|
|
||||||
|
|
||||||
CefWindowHandle GetBrowserHwnd()
|
|
||||||
{
|
|
||||||
return browser_hwnd_;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Called by the test function to execute the test. This method blocks until
|
// Called by the test function to execute the test. This method blocks until
|
||||||
// the test is complete. Do not reference the object after this method
|
// the test is complete. Do not reference the object after this method
|
||||||
// returns.
|
// returns.
|
||||||
void ExecuteTest()
|
void ExecuteTest();
|
||||||
{
|
|
||||||
// Run the test
|
|
||||||
RunTest();
|
|
||||||
|
|
||||||
// Wait for the test to complete
|
|
||||||
completion_event_.Wait();
|
|
||||||
|
|
||||||
// Reset the event so the same test can be executed again.
|
|
||||||
completion_event_.Reset();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
// Destroy the browser window. Once the window is destroyed test completion
|
// Destroy the browser window. Once the window is destroyed test completion
|
||||||
// will be signaled.
|
// will be signaled.
|
||||||
void DestroyTest()
|
void DestroyTest();
|
||||||
{
|
|
||||||
Lock();
|
|
||||||
#if defined(OS_WIN)
|
|
||||||
if(browser_hwnd_ != NULL)
|
|
||||||
PostMessage(browser_hwnd_, WM_CLOSE, 0, 0);
|
|
||||||
#endif
|
|
||||||
Unlock();
|
|
||||||
}
|
|
||||||
|
|
||||||
void CreateBrowser(const CefString& url)
|
void CreateBrowser(const CefString& url);
|
||||||
{
|
|
||||||
CefWindowInfo windowInfo;
|
|
||||||
CefBrowserSettings settings;
|
|
||||||
#if defined(OS_WIN)
|
|
||||||
windowInfo.SetAsPopup(NULL, "CefUnitTest");
|
|
||||||
windowInfo.m_dwStyle |= WS_VISIBLE;
|
|
||||||
#endif
|
|
||||||
CefBrowser::CreateBrowser(windowInfo, this, url, settings);
|
|
||||||
}
|
|
||||||
|
|
||||||
void AddResource(const CefString& key, const std::string& value,
|
void AddResource(const CefString& key, const std::string& value,
|
||||||
const CefString& mimeType)
|
const CefString& mimeType);
|
||||||
{
|
void ClearResources();
|
||||||
resource_map_.insert(std::make_pair(key, std::make_pair(value, mimeType)));
|
|
||||||
}
|
|
||||||
|
|
||||||
void ClearResources()
|
|
||||||
{
|
|
||||||
resource_map_.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// The child browser window
|
// The child browser window
|
||||||
@ -190,19 +104,9 @@ private:
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
static void NotifyEvent(base::WaitableEvent* event)
|
|
||||||
{
|
|
||||||
event->Signal();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Post a task to the specified thread and wait for the task to execute as
|
// Post a task to the specified thread and wait for the task to execute as
|
||||||
// indication that all previously pending tasks on that thread have completed.
|
// indication that all previously pending tasks on that thread have completed.
|
||||||
static void WaitForThread(CefThreadId thread_id)
|
void WaitForThread(CefThreadId thread_id);
|
||||||
{
|
|
||||||
base::WaitableEvent event(true, false);
|
|
||||||
CefPostTask(thread_id, NewCefRunnableFunction(&NotifyEvent, &event));
|
|
||||||
event.Wait();
|
|
||||||
}
|
|
||||||
|
|
||||||
#define WaitForIOThread() WaitForThread(TID_IO)
|
#define WaitForIOThread() WaitForThread(TID_IO)
|
||||||
#define WaitForUIThread() WaitForThread(TID_UI)
|
#define WaitForUIThread() WaitForThread(TID_UI)
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
// reserved. Use of this source code is governed by a BSD-style license that
|
// reserved. Use of this source code is governed by a BSD-style license that
|
||||||
// can be found in the LICENSE file.
|
// can be found in the LICENSE file.
|
||||||
|
|
||||||
|
#include "include/cef_runnable.h"
|
||||||
#include "include/cef_v8.h"
|
#include "include/cef_v8.h"
|
||||||
#include "testing/gtest/include/gtest/gtest.h"
|
#include "testing/gtest/include/gtest/gtest.h"
|
||||||
#include "test_handler.h"
|
#include "test_handler.h"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user