2010-10-03 23:04:50 +02:00
|
|
|
// Copyright (c) 2009 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.
|
|
|
|
|
|
|
|
#ifndef _TEST_HANDLER_H
|
|
|
|
#define _TEST_HANDLER_H
|
|
|
|
|
|
|
|
#include "include/cef.h"
|
2011-01-07 22:34:20 +01:00
|
|
|
#include "base/synchronization/waitable_event.h"
|
2010-10-03 23:04:50 +02:00
|
|
|
#include "testing/gtest/include/gtest/gtest.h"
|
|
|
|
|
2011-02-21 23:44:06 +01:00
|
|
|
class TrackCallback
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TrackCallback(): gotit_(false) {}
|
|
|
|
void yes() { gotit_ = true; }
|
|
|
|
bool isSet() { return gotit_; }
|
2011-04-21 18:46:16 +02:00
|
|
|
void reset() { gotit_ = false; }
|
2011-02-21 23:44:06 +01:00
|
|
|
operator bool() const { return gotit_; }
|
|
|
|
protected:
|
|
|
|
bool gotit_;
|
|
|
|
};
|
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
// Base implementation of CefHandler for unit tests.
|
|
|
|
class TestHandler : public CefThreadSafeBase<CefHandler>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TestHandler() : browser_hwnd_(NULL), completion_event_(true, false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~TestHandler()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implement this method to run the test
|
|
|
|
virtual void RunTest() =0;
|
|
|
|
|
|
|
|
virtual RetVal HandleBeforeCreated(CefRefPtr<CefBrowser> parentBrowser,
|
|
|
|
CefWindowInfo& createInfo, bool popup,
|
2010-11-18 22:05:25 +01:00
|
|
|
const CefPopupFeatures& popupFeatures,
|
2010-10-03 23:04:50 +02:00
|
|
|
CefRefPtr<CefHandler>& handler,
|
2011-05-10 15:55:06 +02:00
|
|
|
const CefString& url,
|
2010-11-18 22:05:25 +01:00
|
|
|
CefBrowserSettings& settings)
|
2010-10-03 23:04:50 +02:00
|
|
|
{
|
|
|
|
return RV_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual RetVal HandleAfterCreated(CefRefPtr<CefBrowser> browser)
|
|
|
|
{
|
|
|
|
Lock();
|
|
|
|
if(!browser->IsPopup())
|
|
|
|
{
|
|
|
|
// Keep the main child window, but not popup windows
|
|
|
|
browser_ = browser;
|
|
|
|
browser_hwnd_ = browser->GetWindowHandle();
|
|
|
|
}
|
|
|
|
Unlock();
|
|
|
|
return RV_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual RetVal HandleAddressChange(CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
2010-11-22 18:49:46 +01:00
|
|
|
const CefString& url)
|
2010-10-03 23:04:50 +02:00
|
|
|
{
|
|
|
|
return RV_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2011-03-24 21:36:47 +01:00
|
|
|
virtual RetVal HandleNavStateChange(CefRefPtr<CefBrowser> browser,
|
|
|
|
bool canGoBack, bool canGoForward)
|
|
|
|
{
|
|
|
|
return RV_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
virtual RetVal HandleTitleChange(CefRefPtr<CefBrowser> browser,
|
2010-11-22 18:49:46 +01:00
|
|
|
const CefString& title)
|
2010-10-03 23:04:50 +02:00
|
|
|
{
|
|
|
|
return RV_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual RetVal HandleBeforeBrowse(CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
|
|
|
CefRefPtr<CefRequest> request,
|
|
|
|
NavType navType, bool isRedirect)
|
|
|
|
{
|
|
|
|
return RV_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual RetVal HandleLoadStart(CefRefPtr<CefBrowser> browser,
|
2011-03-08 20:33:18 +01:00
|
|
|
CefRefPtr<CefFrame> frame)
|
2010-10-03 23:04:50 +02:00
|
|
|
{
|
|
|
|
return RV_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual RetVal HandleLoadEnd(CefRefPtr<CefBrowser> browser,
|
2011-01-12 00:46:14 +01:00
|
|
|
CefRefPtr<CefFrame> frame,
|
2011-01-28 19:02:16 +01:00
|
|
|
int httpStatusCode)
|
2010-10-03 23:04:50 +02:00
|
|
|
{
|
|
|
|
return RV_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual RetVal HandleLoadError(CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
|
|
|
ErrorCode errorCode,
|
2010-11-22 18:49:46 +01:00
|
|
|
const CefString& failedUrl,
|
|
|
|
CefString& errorText)
|
2010-10-03 23:04:50 +02:00
|
|
|
{
|
|
|
|
return RV_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual RetVal HandleBeforeResourceLoad(CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefRequest> request,
|
2010-11-22 18:49:46 +01:00
|
|
|
CefString& redirectUrl,
|
2010-10-03 23:04:50 +02:00
|
|
|
CefRefPtr<CefStreamReader>& resourceStream,
|
2011-04-21 18:46:16 +02:00
|
|
|
CefRefPtr<CefResponse> response,
|
2010-10-03 23:04:50 +02:00
|
|
|
int loadFlags)
|
|
|
|
{
|
|
|
|
Lock();
|
|
|
|
if(resource_map_.size() > 0) {
|
2010-11-22 18:49:46 +01:00
|
|
|
CefString url = request->GetURL();
|
2010-10-03 23:04:50 +02:00
|
|
|
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());
|
2011-04-21 18:46:16 +02:00
|
|
|
response->SetMimeType(it->second.second);
|
|
|
|
response->SetStatus(200);
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Unlock();
|
|
|
|
|
|
|
|
return RV_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2011-01-12 02:33:03 +01:00
|
|
|
virtual RetVal HandleProtocolExecution(CefRefPtr<CefBrowser> browser,
|
|
|
|
const CefString& url,
|
2011-04-09 03:05:30 +02:00
|
|
|
bool& allow_os_execution)
|
2011-01-12 02:33:03 +01:00
|
|
|
{
|
|
|
|
return RV_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2010-10-16 21:10:11 +02:00
|
|
|
virtual RetVal HandleDownloadResponse(CefRefPtr<CefBrowser> browser,
|
2010-11-22 18:49:46 +01:00
|
|
|
const CefString& mimeType,
|
|
|
|
const CefString& fileName,
|
2010-10-16 21:10:11 +02:00
|
|
|
int64 contentLength,
|
|
|
|
CefRefPtr<CefDownloadHandler>& handler)
|
|
|
|
{
|
|
|
|
return RV_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2011-01-07 02:24:17 +01:00
|
|
|
virtual RetVal HandleAuthenticationRequest(CefRefPtr<CefBrowser> browser,
|
|
|
|
bool isProxy,
|
|
|
|
const CefString& host,
|
|
|
|
const CefString& realm,
|
|
|
|
const CefString& scheme,
|
|
|
|
CefString& username,
|
|
|
|
CefString& password)
|
|
|
|
{
|
|
|
|
return RV_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
virtual RetVal HandleBeforeMenu(CefRefPtr<CefBrowser> browser,
|
|
|
|
const MenuInfo& menuInfo)
|
|
|
|
{
|
|
|
|
return RV_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
virtual RetVal HandleGetMenuLabel(CefRefPtr<CefBrowser> browser,
|
2010-11-22 18:49:46 +01:00
|
|
|
MenuId menuId, CefString& label)
|
2010-10-03 23:04:50 +02:00
|
|
|
{
|
|
|
|
return RV_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual RetVal HandleMenuAction(CefRefPtr<CefBrowser> browser,
|
|
|
|
MenuId menuId)
|
|
|
|
{
|
|
|
|
return RV_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual RetVal HandlePrintOptions(CefRefPtr<CefBrowser> browser,
|
|
|
|
CefPrintOptions& printOptions)
|
|
|
|
{
|
|
|
|
return RV_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual RetVal HandlePrintHeaderFooter(CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
|
|
|
CefPrintInfo& printInfo,
|
2010-11-22 18:49:46 +01:00
|
|
|
const CefString& url,
|
|
|
|
const CefString& title,
|
2010-10-03 23:04:50 +02:00
|
|
|
int currentPage, int maxPages,
|
2010-11-22 18:49:46 +01:00
|
|
|
CefString& topLeft,
|
|
|
|
CefString& topCenter,
|
|
|
|
CefString& topRight,
|
|
|
|
CefString& bottomLeft,
|
|
|
|
CefString& bottomCenter,
|
|
|
|
CefString& bottomRight)
|
2010-10-03 23:04:50 +02:00
|
|
|
{
|
|
|
|
return RV_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual RetVal HandleJSAlert(CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
2010-11-22 18:49:46 +01:00
|
|
|
const CefString& message)
|
2010-10-03 23:04:50 +02:00
|
|
|
{
|
|
|
|
return RV_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual RetVal HandleJSConfirm(CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
2010-11-22 18:49:46 +01:00
|
|
|
const CefString& message, bool& retval)
|
2010-10-03 23:04:50 +02:00
|
|
|
{
|
|
|
|
return RV_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual RetVal HandleJSPrompt(CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
2010-11-22 18:49:46 +01:00
|
|
|
const CefString& message,
|
|
|
|
const CefString& defaultValue,
|
2010-10-03 23:04:50 +02:00
|
|
|
bool& retval,
|
2010-11-22 18:49:46 +01:00
|
|
|
CefString& result)
|
2010-10-03 23:04:50 +02:00
|
|
|
{
|
|
|
|
return RV_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual RetVal HandleJSBinding(CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
|
|
|
CefRefPtr<CefV8Value> object)
|
|
|
|
{
|
|
|
|
return RV_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual RetVal HandleBeforeWindowClose(CefRefPtr<CefBrowser> browser)
|
|
|
|
{
|
|
|
|
Lock();
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
Unlock();
|
|
|
|
return RV_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual RetVal HandleTakeFocus(CefRefPtr<CefBrowser> browser, bool reverse)
|
|
|
|
{
|
|
|
|
return RV_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual RetVal HandleSetFocus(CefRefPtr<CefBrowser> browser,
|
|
|
|
bool isWidget)
|
|
|
|
{
|
|
|
|
return RV_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual RetVal HandleKeyEvent(CefRefPtr<CefBrowser> browser,
|
|
|
|
KeyEventType type, int code,
|
|
|
|
int modifiers, bool isSystemKey)
|
|
|
|
{
|
|
|
|
return RV_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual RetVal HandleTooltip(CefRefPtr<CefBrowser> browser,
|
2010-11-22 18:49:46 +01:00
|
|
|
CefString& text)
|
2010-10-03 23:04:50 +02:00
|
|
|
{
|
|
|
|
return RV_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2011-01-26 16:12:49 +01:00
|
|
|
virtual RetVal HandleStatus(CefRefPtr<CefBrowser> browser,
|
|
|
|
const CefString& text, StatusType type)
|
|
|
|
{
|
|
|
|
return RV_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
virtual RetVal HandleConsoleMessage(CefRefPtr<CefBrowser> browser,
|
2010-11-22 18:49:46 +01:00
|
|
|
const CefString& message,
|
|
|
|
const CefString& source, int line)
|
2010-10-03 23:04:50 +02:00
|
|
|
{
|
|
|
|
return RV_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual RetVal HandleFindResult(CefRefPtr<CefBrowser> browser,
|
|
|
|
int identifier, int count,
|
|
|
|
const CefRect& selectionRect,
|
|
|
|
int activeMatchOrdinal, bool finalUpdate)
|
|
|
|
{
|
|
|
|
return RV_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2011-03-24 21:36:47 +01:00
|
|
|
virtual RetVal HandleGetRect(CefRefPtr<CefBrowser> browser, bool screen,
|
|
|
|
CefRect& rect)
|
|
|
|
{
|
|
|
|
return RV_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual RetVal HandleGetScreenPoint(CefRefPtr<CefBrowser> browser,
|
|
|
|
int viewX, int viewY, int& screenX,
|
|
|
|
int& screenY)
|
|
|
|
{
|
|
|
|
return RV_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual RetVal HandlePopupChange(CefRefPtr<CefBrowser> browser, bool show,
|
|
|
|
const CefRect& rect)
|
|
|
|
{
|
|
|
|
return RV_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual RetVal HandlePaint(CefRefPtr<CefBrowser> browser,
|
|
|
|
PaintElementType type, const CefRect& dirtyRect,
|
|
|
|
const void* buffer)
|
|
|
|
{
|
|
|
|
return RV_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual RetVal HandleCursorChange(CefRefPtr<CefBrowser> browser,
|
|
|
|
CefCursorHandle cursor)
|
|
|
|
{
|
|
|
|
return RV_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
CefRefPtr<CefBrowser> GetBrowser()
|
|
|
|
{
|
|
|
|
return browser_;
|
|
|
|
}
|
|
|
|
|
2010-11-15 16:39:56 +01:00
|
|
|
CefWindowHandle GetBrowserHwnd()
|
2010-10-03 23:04:50 +02:00
|
|
|
{
|
|
|
|
return browser_hwnd_;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
// returns.
|
|
|
|
void ExecuteTest()
|
|
|
|
{
|
|
|
|
// Run the test
|
|
|
|
RunTest();
|
|
|
|
|
|
|
|
// Wait for the test to complete
|
|
|
|
completion_event_.Wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// Destroy the browser window. Once the window is destroyed test completion
|
|
|
|
// will be signaled.
|
|
|
|
void DestroyTest()
|
|
|
|
{
|
|
|
|
Lock();
|
|
|
|
#if defined(OS_WIN)
|
|
|
|
if(browser_hwnd_ != NULL)
|
|
|
|
PostMessage(browser_hwnd_, WM_CLOSE, 0, 0);
|
|
|
|
#endif
|
|
|
|
Unlock();
|
|
|
|
}
|
|
|
|
|
2010-11-22 18:49:46 +01:00
|
|
|
void CreateBrowser(const CefString& url)
|
2010-10-03 23:04:50 +02:00
|
|
|
{
|
|
|
|
CefWindowInfo windowInfo;
|
|
|
|
#if defined(OS_WIN)
|
2010-11-22 18:49:46 +01:00
|
|
|
windowInfo.SetAsPopup(NULL, "CefUnitTest");
|
2010-10-03 23:04:50 +02:00
|
|
|
windowInfo.m_dwStyle |= WS_VISIBLE;
|
|
|
|
#endif
|
|
|
|
CefBrowser::CreateBrowser(windowInfo, false, this, url);
|
|
|
|
}
|
|
|
|
|
2010-11-22 18:49:46 +01:00
|
|
|
void AddResource(const CefString& key, const std::string& value,
|
|
|
|
const CefString& mimeType)
|
2010-10-03 23:04:50 +02:00
|
|
|
{
|
|
|
|
resource_map_.insert(std::make_pair(key, std::make_pair(value, mimeType)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClearResources()
|
|
|
|
{
|
|
|
|
resource_map_.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// The child browser window
|
|
|
|
CefRefPtr<CefBrowser> browser_;
|
|
|
|
|
|
|
|
// The browser window handle
|
|
|
|
CefWindowHandle browser_hwnd_;
|
|
|
|
|
|
|
|
// Handle used to notify when the test is complete
|
|
|
|
base::WaitableEvent completion_event_;
|
|
|
|
|
|
|
|
// Map of resources that can be automatically loaded
|
2010-11-22 18:49:46 +01:00
|
|
|
typedef std::map<CefString, std::pair<std::string, CefString> > ResourceMap;
|
2010-10-03 23:04:50 +02:00
|
|
|
ResourceMap resource_map_;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // _TEST_HANDLER_H
|