// 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" #include "base/waitable_event.h" #include "testing/gtest/include/gtest/gtest.h" // Base implementation of CefHandler for unit tests. class TestHandler : public CefThreadSafeBase { 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 parentBrowser, CefWindowInfo& createInfo, bool popup, const CefPopupFeatures& popupFeatures, CefRefPtr& handler, CefString& url, CefBrowserSettings& settings) { return RV_CONTINUE; } virtual RetVal HandleAfterCreated(CefRefPtr 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 browser, CefRefPtr frame, const CefString& url) { return RV_CONTINUE; } virtual RetVal HandleTitleChange(CefRefPtr browser, const CefString& title) { return RV_CONTINUE; } virtual RetVal HandleBeforeBrowse(CefRefPtr browser, CefRefPtr frame, CefRefPtr request, NavType navType, bool isRedirect) { return RV_CONTINUE; } virtual RetVal HandleLoadStart(CefRefPtr browser, CefRefPtr frame) { return RV_CONTINUE; } virtual RetVal HandleLoadEnd(CefRefPtr browser, CefRefPtr frame) { return RV_CONTINUE; } virtual RetVal HandleLoadError(CefRefPtr browser, CefRefPtr frame, ErrorCode errorCode, const CefString& failedUrl, CefString& errorText) { return RV_CONTINUE; } virtual RetVal HandleBeforeResourceLoad(CefRefPtr browser, CefRefPtr request, CefString& redirectUrl, CefRefPtr& resourceStream, CefString& mimeType, int loadFlags) { Lock(); 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()); mimeType = it->second.second; } } Unlock(); return RV_CONTINUE; } virtual RetVal HandleDownloadResponse(CefRefPtr browser, const CefString& mimeType, const CefString& fileName, int64 contentLength, CefRefPtr& handler) { return RV_CONTINUE; } virtual RetVal HandleBeforeMenu(CefRefPtr browser, const MenuInfo& menuInfo) { return RV_CONTINUE; } virtual RetVal HandleGetMenuLabel(CefRefPtr browser, MenuId menuId, CefString& label) { return RV_CONTINUE; } virtual RetVal HandleMenuAction(CefRefPtr browser, MenuId menuId) { return RV_CONTINUE; } virtual RetVal HandlePrintOptions(CefRefPtr browser, CefPrintOptions& printOptions) { return RV_CONTINUE; } virtual RetVal HandlePrintHeaderFooter(CefRefPtr browser, CefRefPtr frame, CefPrintInfo& printInfo, const CefString& url, const CefString& title, int currentPage, int maxPages, CefString& topLeft, CefString& topCenter, CefString& topRight, CefString& bottomLeft, CefString& bottomCenter, CefString& bottomRight) { return RV_CONTINUE; } virtual RetVal HandleJSAlert(CefRefPtr browser, CefRefPtr frame, const CefString& message) { return RV_CONTINUE; } virtual RetVal HandleJSConfirm(CefRefPtr browser, CefRefPtr frame, const CefString& message, bool& retval) { return RV_CONTINUE; } virtual RetVal HandleJSPrompt(CefRefPtr browser, CefRefPtr frame, const CefString& message, const CefString& defaultValue, bool& retval, CefString& result) { return RV_CONTINUE; } virtual RetVal HandleJSBinding(CefRefPtr browser, CefRefPtr frame, CefRefPtr object) { return RV_CONTINUE; } virtual RetVal HandleBeforeWindowClose(CefRefPtr 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 browser, bool reverse) { return RV_CONTINUE; } virtual RetVal HandleSetFocus(CefRefPtr browser, bool isWidget) { return RV_CONTINUE; } virtual RetVal HandleKeyEvent(CefRefPtr browser, KeyEventType type, int code, int modifiers, bool isSystemKey) { return RV_CONTINUE; } virtual RetVal HandleTooltip(CefRefPtr browser, CefString& text) { return RV_CONTINUE; } virtual RetVal HandleConsoleMessage(CefRefPtr browser, const CefString& message, const CefString& source, int line) { return RV_CONTINUE; } virtual RetVal HandleFindResult(CefRefPtr browser, int identifier, int count, const CefRect& selectionRect, int activeMatchOrdinal, bool finalUpdate) { return RV_CONTINUE; } CefRefPtr GetBrowser() { return browser_; } CefWindowHandle GetBrowserHwnd() { 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(); } void CreateBrowser(const CefString& url) { CefWindowInfo windowInfo; #if defined(OS_WIN) windowInfo.SetAsPopup(NULL, "CefUnitTest"); windowInfo.m_dwStyle |= WS_VISIBLE; #endif CefBrowser::CreateBrowser(windowInfo, false, this, url); } void AddResource(const CefString& key, const std::string& value, const CefString& mimeType) { resource_map_.insert(std::make_pair(key, std::make_pair(value, mimeType))); } void ClearResources() { resource_map_.clear(); } private: // The child browser window CefRefPtr 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 typedef std::map > ResourceMap; ResourceMap resource_map_; }; #endif // _TEST_HANDLER_H