diff --git a/cef_paths2.gypi b/cef_paths2.gypi index da76542bc..825084839 100644 --- a/cef_paths2.gypi +++ b/cef_paths2.gypi @@ -178,8 +178,8 @@ 'tests/cefclient/resource_util.h', 'tests/cefclient/scheme_test.cc', 'tests/cefclient/scheme_test.h', - 'tests/cefclient/string_util.cc', - 'tests/cefclient/string_util.h', + 'tests/cefclient/test_runner.cc', + 'tests/cefclient/test_runner.h', 'tests/cefclient/window_test.cc', 'tests/cefclient/window_test.h', '<@(cefclient_bundle_resources_common)', @@ -227,6 +227,8 @@ 'tests/cefclient/client_switches.h', 'tests/cefclient/dialog_test.cc', 'tests/cefclient/dialog_test.h', + 'tests/cefclient/main_message_loop.cc', + 'tests/cefclient/main_message_loop.h', 'tests/cefclient/performance_test.cc', 'tests/cefclient/performance_test.h', 'tests/cefclient/performance_test_setup.h', @@ -237,8 +239,8 @@ 'tests/cefclient/resource_util_posix.cc', 'tests/cefclient/scheme_test.cc', 'tests/cefclient/scheme_test.h', - 'tests/cefclient/string_util.cc', - 'tests/cefclient/string_util.h', + 'tests/cefclient/test_runner.cc', + 'tests/cefclient/test_runner.h', 'tests/cefclient/window_test.cc', 'tests/cefclient/window_test.h', 'tests/cefclient/window_test_mac.mm', diff --git a/tests/cefclient/binding_test.cc b/tests/cefclient/binding_test.cc index 8fe4a5a14..5dcebaabf 100644 --- a/tests/cefclient/binding_test.cc +++ b/tests/cefclient/binding_test.cc @@ -9,6 +9,7 @@ #include "include/wrapper/cef_stream_resource_handler.h" +namespace client { namespace binding_test { namespace { @@ -48,8 +49,9 @@ class Handler : public CefMessageRouterBrowserSide::Handler { } // namespace -void CreateMessageHandlers(ClientHandler::MessageHandlerSet& handlers) { +void CreateMessageHandlers(test_runner::MessageHandlerSet& handlers) { handlers.insert(new Handler()); } } // namespace binding_test +} // namespace client diff --git a/tests/cefclient/binding_test.h b/tests/cefclient/binding_test.h index e8f1e02a4..d3a67c2b1 100644 --- a/tests/cefclient/binding_test.h +++ b/tests/cefclient/binding_test.h @@ -6,13 +6,15 @@ #define CEF_TESTS_CEFCLIENT_BINDING_TEST_H_ #pragma once -#include "cefclient/client_handler.h" +#include "cefclient/test_runner.h" +namespace client { namespace binding_test { -// Handler creation. Called from ClientHandler. -void CreateMessageHandlers(ClientHandler::MessageHandlerSet& handlers); +// Handler creation. +void CreateMessageHandlers(test_runner::MessageHandlerSet& handlers); } // namespace binding_test +} // namespace client #endif // CEF_TESTS_CEFCLIENT_BINDING_TEST_H_ diff --git a/tests/cefclient/cefclient.cc b/tests/cefclient/cefclient.cc index 749e05fbc..1141a20b3 100644 --- a/tests/cefclient/cefclient.cc +++ b/tests/cefclient/cefclient.cc @@ -15,7 +15,6 @@ #include "include/wrapper/cef_helpers.h" #include "cefclient/client_handler.h" #include "cefclient/client_switches.h" -#include "cefclient/string_util.h" CefRefPtr g_handler; @@ -94,112 +93,3 @@ bool AppIsOffScreenRenderingEnabled() { return (g_offscreen_state == 1); } - -void RunGetSourceTest(CefRefPtr browser) { - class Visitor : public CefStringVisitor { - public: - explicit Visitor(CefRefPtr browser) : browser_(browser) {} - virtual void Visit(const CefString& string) OVERRIDE { - std::string source = StringReplace(string, "<", "<"); - source = StringReplace(source, ">", ">"); - std::stringstream ss; - ss << "Source:
" << source <<
-            "
"; - browser_->GetMainFrame()->LoadString(ss.str(), "http://tests/getsource"); - } - private: - CefRefPtr browser_; - IMPLEMENT_REFCOUNTING(Visitor); - }; - - browser->GetMainFrame()->GetSource(new Visitor(browser)); -} - -void RunGetTextTest(CefRefPtr browser) { - class Visitor : public CefStringVisitor { - public: - explicit Visitor(CefRefPtr browser) : browser_(browser) {} - virtual void Visit(const CefString& string) OVERRIDE { - std::string text = StringReplace(string, "<", "<"); - text = StringReplace(text, ">", ">"); - std::stringstream ss; - ss << "Text:
" << text <<
-            "
"; - browser_->GetMainFrame()->LoadString(ss.str(), "http://tests/gettext"); - } - private: - CefRefPtr browser_; - IMPLEMENT_REFCOUNTING(Visitor); - }; - - browser->GetMainFrame()->GetText(new Visitor(browser)); -} - -void RunRequestTest(CefRefPtr browser) { - // Create a new request - CefRefPtr request(CefRequest::Create()); - - // Set the request URL - request->SetURL("http://tests/request"); - - // Add post data to the request. The correct method and content- - // type headers will be set by CEF. - CefRefPtr postDataElement(CefPostDataElement::Create()); - std::string data = "arg1=val1&arg2=val2"; - postDataElement->SetToBytes(data.length(), data.c_str()); - CefRefPtr postData(CefPostData::Create()); - postData->AddElement(postDataElement); - request->SetPostData(postData); - - // Add a custom header - CefRequest::HeaderMap headerMap; - headerMap.insert( - std::make_pair("X-My-Header", "My Header Value")); - request->SetHeaderMap(headerMap); - - // Load the request - browser->GetMainFrame()->LoadRequest(request); -} - -void RunPopupTest(CefRefPtr browser) { - browser->GetMainFrame()->ExecuteJavaScript( - "window.open('http://www.google.com');", "about:blank", 0); -} - -void RunPluginInfoTest(CefRefPtr browser) { - class Visitor : public CefWebPluginInfoVisitor { - public: - explicit Visitor(CefRefPtr browser) - : browser_(browser) { - html_ = "Plugin Info Test" - "" - "\nInstalled plugins:"; - } - ~Visitor() { - html_ += "\n"; - - // Load the html in the browser. - browser_->GetMainFrame()->LoadString(html_, "http://tests/plugin_info"); - } - - virtual bool Visit(CefRefPtr info, int count, int total) - OVERRIDE { - html_ += "\n

Name: " + info->GetName().ToString() + - "\n
Description: " + info->GetDescription().ToString() + - "\n
Version: " + info->GetVersion().ToString() + - "\n
Path: " + info->GetPath().ToString(); - return true; - } - - private: - std::string html_; - CefRefPtr browser_; - IMPLEMENT_REFCOUNTING(Visitor); - }; - - CefVisitWebPluginInfo(new Visitor(browser)); -} - -void RunOtherTests(CefRefPtr browser) { - browser->GetMainFrame()->LoadURL("http://tests/other_tests"); -} diff --git a/tests/cefclient/cefclient.h b/tests/cefclient/cefclient.h index da337a29a..ac97aed5e 100644 --- a/tests/cefclient/cefclient.h +++ b/tests/cefclient/cefclient.h @@ -38,19 +38,11 @@ void AppGetBrowserSettings(CefBrowserSettings& settings); // argument. bool AppIsOffScreenRenderingEnabled(); +// Returns the full download path for the specified file, or an empty path to +// use the default temp directory. +std::string AppGetDownloadPath(const std::string& file_name); + // Quit the application message loop. void AppQuitMessageLoop(); -// Implementations for various tests. -void RunGetSourceTest(CefRefPtr browser); -void RunGetTextTest(CefRefPtr browser); -void RunRequestTest(CefRefPtr browser); -void RunPopupTest(CefRefPtr browser); -void RunPluginInfoTest(CefRefPtr browser); -void RunOtherTests(CefRefPtr browser); - -#if defined(OS_WIN) -void RunTransparentPopupTest(CefRefPtr browser); -#endif - #endif // CEF_TESTS_CEFCLIENT_CEFCLIENT_H_ diff --git a/tests/cefclient/cefclient_gtk.cc b/tests/cefclient/cefclient_gtk.cc index e9d190a5f..c56b4c009 100644 --- a/tests/cefclient/cefclient_gtk.cc +++ b/tests/cefclient/cefclient_gtk.cc @@ -26,8 +26,8 @@ #include "cefclient/client_handler.h" #include "cefclient/client_switches.h" #include "cefclient/main_message_loop_std.h" -#include "cefclient/scheme_test.h" -#include "cefclient/string_util.h" +#include "cefclient/resource.h" +#include "cefclient/test_runner.h" // The global ClientHandler reference. extern CefRefPtr g_handler; @@ -224,108 +224,16 @@ gboolean WindowConfigure(GtkWindow* window, return FALSE; // Don't stop this message. } -// Callback for Tests > Get Source... menu item. -gboolean GetSourceActivated(GtkWidget* widget) { - if (g_handler.get() && g_handler->GetBrowserId()) - RunGetSourceTest(g_handler->GetBrowser()); - - return FALSE; // Don't stop this message. -} - -// Callback for Tests > Get Text... menu item. -gboolean GetTextActivated(GtkWidget* widget) { - if (g_handler.get() && g_handler->GetBrowserId()) - RunGetTextTest(g_handler->GetBrowser()); - - return FALSE; // Don't stop this message. -} - -// Callback for Tests > Popup Window... menu item. -gboolean PopupWindowActivated(GtkWidget* widget) { - if (g_handler.get() && g_handler->GetBrowserId()) - RunPopupTest(g_handler->GetBrowser()); - - return FALSE; // Don't stop this message. -} - -// Callback for Tests > Request... menu item. -gboolean RequestActivated(GtkWidget* widget) { - if (g_handler.get() && g_handler->GetBrowserId()) - RunRequestTest(g_handler->GetBrowser()); - - return FALSE; // Don't stop this message. -} - -// Callback for Tests > Plugin Info... menu item. -gboolean PluginInfoActivated(GtkWidget* widget) { - if (g_handler.get() && g_handler->GetBrowserId()) - RunPluginInfoTest(g_handler->GetBrowser()); - - return FALSE; // Don't stop this message. -} - -// Callback for Tests > Zoom In... menu item. -gboolean ZoomInActivated(GtkWidget* widget) { +// Callback for Tests menu items. +gboolean MenuItemActivated(GtkWidget* widget, gpointer data) { if (g_handler.get() && g_handler->GetBrowserId()) { - CefRefPtr browser = g_handler->GetBrowser(); - browser->GetHost()->SetZoomLevel(browser->GetHost()->GetZoomLevel() + 0.5); + client::test_runner::RunTest(g_handler->GetBrowser(), + GPOINTER_TO_INT(data)); } return FALSE; // Don't stop this message. } -// Callback for Tests > Zoom Out... menu item. -gboolean ZoomOutActivated(GtkWidget* widget) { - if (g_handler.get() && g_handler->GetBrowserId()) { - CefRefPtr browser = g_handler->GetBrowser(); - browser->GetHost()->SetZoomLevel(browser->GetHost()->GetZoomLevel() - 0.5); - } - - return FALSE; // Don't stop this message. -} - -// Callback for Tests > Zoom Reset... menu item. -gboolean ZoomResetActivated(GtkWidget* widget) { - if (g_handler.get() && g_handler->GetBrowserId()) { - CefRefPtr browser = g_handler->GetBrowser(); - browser->GetHost()->SetZoomLevel(0.0); - } - - return FALSE; // Don't stop this message. -} - -// Callback for Tests > Begin Tracing menu item. -gboolean BeginTracingActivated(GtkWidget* widget) { - if (g_handler.get()) - g_handler->BeginTracing(); - - return FALSE; // Don't stop this message. -} - -// Callback for Tests > End Tracing menu item. -gboolean EndTracingActivated(GtkWidget* widget) { - if (g_handler.get()) - g_handler->EndTracing(); - - return FALSE; // Don't stop this message. -} - -// Callback for Tests > Print menu item. -gboolean PrintActivated(GtkWidget* widget) { - if (g_handler.get()) - g_handler->GetBrowser()->GetHost()->Print(); - - return FALSE; // Don't stop this message. -} - -// Callback for Tests > Other Tests... menu item. -gboolean OtherTestsActivated(GtkWidget* widget) { - if (g_handler.get() && g_handler->GetBrowserId()) - RunOtherTests(g_handler->GetBrowser()); - - return FALSE; // Don't stop this message. -} - // Callback for when you click the back button. void BackButtonClicked(GtkButton* button) { if (g_handler.get() && g_handler->GetBrowserId()) @@ -375,10 +283,10 @@ gboolean URLEntryButtonPress(GtkWidget* widget, // GTK utility functions ---------------------------------------------- -GtkWidget* AddMenuEntry(GtkWidget* menu_widget, const char* text, - GCallback callback) { +GtkWidget* AddMenuEntry(GtkWidget* menu_widget, const char* text, int id) { GtkWidget* entry = gtk_menu_item_new_with_label(text); - g_signal_connect(entry, "activate", callback, NULL); + g_signal_connect(entry, "activate", G_CALLBACK(MenuItemActivated), + GINT_TO_POINTER(id)); gtk_menu_shell_append(GTK_MENU_SHELL(menu_widget), entry); return entry; } @@ -395,30 +303,18 @@ GtkWidget* CreateMenuBar() { GtkWidget* menu_bar = gtk_menu_bar_new(); GtkWidget* debug_menu = CreateMenu(menu_bar, "Tests"); - AddMenuEntry(debug_menu, "Get Source", - G_CALLBACK(GetSourceActivated)); - AddMenuEntry(debug_menu, "Get Text", - G_CALLBACK(GetTextActivated)); - AddMenuEntry(debug_menu, "Popup Window", - G_CALLBACK(PopupWindowActivated)); - AddMenuEntry(debug_menu, "Request", - G_CALLBACK(RequestActivated)); - AddMenuEntry(debug_menu, "Plugin Info", - G_CALLBACK(PluginInfoActivated)); - AddMenuEntry(debug_menu, "Zoom In", - G_CALLBACK(ZoomInActivated)); - AddMenuEntry(debug_menu, "Zoom Out", - G_CALLBACK(ZoomOutActivated)); - AddMenuEntry(debug_menu, "Zoom Reset", - G_CALLBACK(ZoomResetActivated)); - AddMenuEntry(debug_menu, "Begin Tracing", - G_CALLBACK(BeginTracingActivated)); - AddMenuEntry(debug_menu, "End Tracing", - G_CALLBACK(EndTracingActivated)); - AddMenuEntry(debug_menu, "Print", - G_CALLBACK(PrintActivated)); - AddMenuEntry(debug_menu, "Other Tests", - G_CALLBACK(OtherTestsActivated)); + AddMenuEntry(debug_menu, "Get Source", ID_TESTS_GETSOURCE); + AddMenuEntry(debug_menu, "Get Text", ID_TESTS_GETTEXT); + AddMenuEntry(debug_menu, "Popup Window", ID_TESTS_POPUP); + AddMenuEntry(debug_menu, "Request", ID_TESTS_REQUEST); + AddMenuEntry(debug_menu, "Plugin Info", ID_TESTS_PLUGIN_INFO); + AddMenuEntry(debug_menu, "Zoom In", ID_TESTS_ZOOM_IN); + AddMenuEntry(debug_menu, "Zoom Out", ID_TESTS_ZOOM_OUT); + AddMenuEntry(debug_menu, "Zoom Reset", ID_TESTS_ZOOM_RESET); + AddMenuEntry(debug_menu, "Begin Tracing", ID_TESTS_TRACING_BEGIN); + AddMenuEntry(debug_menu, "End Tracing", ID_TESTS_TRACING_END); + AddMenuEntry(debug_menu, "Print", ID_TESTS_PRINT); + AddMenuEntry(debug_menu, "Other Tests", ID_TESTS_OTHER_TESTS); return menu_bar; } @@ -468,8 +364,8 @@ int main(int argc, char* argv[]) { // Perform gtkglext initialization required by the OSR example. gtk_gl_init(&argc, &argv_copy); - // Register the scheme handler. - scheme_test::InitTest(); + // Register scheme handlers. + client::test_runner::RegisterSchemeHandlers(); GtkWidget* window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_default_size(GTK_WINDOW(window), 800, 600); @@ -605,6 +501,10 @@ std::string AppGetWorkingDirectory() { return szWorkingDir; } +std::string AppGetDownloadPath(const std::string& file_name) { + return std::string(); +} + void AppQuitMessageLoop() { client::MainMessageLoop::Get()->Quit(); } diff --git a/tests/cefclient/cefclient_mac.mm b/tests/cefclient/cefclient_mac.mm index 92de9d0df..aef96cd6c 100644 --- a/tests/cefclient/cefclient_mac.mm +++ b/tests/cefclient/cefclient_mac.mm @@ -14,9 +14,9 @@ #include "cefclient/client_handler.h" #include "cefclient/client_switches.h" #include "cefclient/main_message_loop_std.h" +#include "cefclient/resource.h" #include "cefclient/resource_util.h" -#include "cefclient/scheme_test.h" -#include "cefclient/string_util.h" +#include "cefclient/test_runner.h" // The global ClientHandler reference. extern CefRefPtr g_handler; @@ -46,19 +46,7 @@ const int kWindowHeight = 600; @interface ClientAppDelegate : NSObject - (void)createApplication:(id)object; - (void)tryToTerminateApplication:(NSApplication*)app; - -- (IBAction)testGetSource:(id)sender; -- (IBAction)testGetText:(id)sender; -- (IBAction)testPopupWindow:(id)sender; -- (IBAction)testRequest:(id)sender; -- (IBAction)testPluginInfo:(id)sender; -- (IBAction)testZoomIn:(id)sender; -- (IBAction)testZoomOut:(id)sender; -- (IBAction)testZoomReset:(id)sender; -- (IBAction)testBeginTracing:(id)sender; -- (IBAction)testEndTracing:(id)sender; -- (IBAction)testPrint:(id)sender; -- (IBAction)testOtherTests:(id)sender; +- (IBAction)menuItemSelected:(id)sender; @end // Provide the CefAppProtocol implementation required by CEF. @@ -358,6 +346,13 @@ NSButton* MakeButton(NSRect* rect, NSString* title, NSView* parent) { return button; } +void AddMenuItem(NSMenu *menu, NSString* label, int idval) { + NSMenuItem* item = [menu addItemWithTitle:label + action:@selector(menuItemSelected:) + keyEquivalent:@""]; + [item setTag:idval]; +} + @implementation ClientAppDelegate // Create the application on the UI thread. @@ -374,42 +369,18 @@ NSButton* MakeButton(NSRect* rect, NSString* title, NSView* parent) { action:nil keyEquivalent:@""] autorelease]; NSMenu *testMenu = [[[NSMenu alloc] initWithTitle:@"Tests"] autorelease]; - [testMenu addItemWithTitle:@"Get Source" - action:@selector(testGetSource:) - keyEquivalent:@""]; - [testMenu addItemWithTitle:@"Get Text" - action:@selector(testGetText:) - keyEquivalent:@""]; - [testMenu addItemWithTitle:@"Popup Window" - action:@selector(testPopupWindow:) - keyEquivalent:@""]; - [testMenu addItemWithTitle:@"Request" - action:@selector(testRequest:) - keyEquivalent:@""]; - [testMenu addItemWithTitle:@"Plugin Info" - action:@selector(testPluginInfo:) - keyEquivalent:@""]; - [testMenu addItemWithTitle:@"Zoom In" - action:@selector(testZoomIn:) - keyEquivalent:@""]; - [testMenu addItemWithTitle:@"Zoom Out" - action:@selector(testZoomOut:) - keyEquivalent:@""]; - [testMenu addItemWithTitle:@"Zoom Reset" - action:@selector(testZoomReset:) - keyEquivalent:@""]; - [testMenu addItemWithTitle:@"Begin Tracing" - action:@selector(testBeginTracing:) - keyEquivalent:@""]; - [testMenu addItemWithTitle:@"End Tracing" - action:@selector(testEndTracing:) - keyEquivalent:@""]; - [testMenu addItemWithTitle:@"Print" - action:@selector(testPrint:) - keyEquivalent:@""]; - [testMenu addItemWithTitle:@"Other Tests" - action:@selector(testOtherTests:) - keyEquivalent:@""]; + AddMenuItem(testMenu, @"Get Text", ID_TESTS_GETSOURCE); + AddMenuItem(testMenu, @"Get Source", ID_TESTS_GETTEXT); + AddMenuItem(testMenu, @"Popup Window", ID_TESTS_POPUP); + AddMenuItem(testMenu, @"Request", ID_TESTS_REQUEST); + AddMenuItem(testMenu, @"Plugin Info", ID_TESTS_PLUGIN_INFO); + AddMenuItem(testMenu, @"Zoom In", ID_TESTS_ZOOM_IN); + AddMenuItem(testMenu, @"Zoom Out", ID_TESTS_ZOOM_OUT); + AddMenuItem(testMenu, @"Zoom Reset", ID_TESTS_ZOOM_RESET); + AddMenuItem(testMenu, @"Begin Tracing", ID_TESTS_TRACING_BEGIN); + AddMenuItem(testMenu, @"End Tracing", ID_TESTS_TRACING_END); + AddMenuItem(testMenu, @"Print", ID_TESTS_PRINT); + AddMenuItem(testMenu, @"Other Tests", ID_TESTS_OTHER_TESTS); [testItem setSubmenu:testMenu]; [menubar addItem:testItem]; @@ -523,70 +494,10 @@ NSButton* MakeButton(NSRect* rect, NSString* title, NSView* parent) { g_handler->CloseAllBrowsers(false); } -- (IBAction)testGetSource:(id)sender { +- (IBAction)menuItemSelected:(id)sender { + NSMenuItem *item = (NSMenuItem*)sender; if (g_handler.get() && g_handler->GetBrowserId()) - RunGetSourceTest(g_handler->GetBrowser()); -} - -- (IBAction)testGetText:(id)sender { - if (g_handler.get() && g_handler->GetBrowserId()) - RunGetTextTest(g_handler->GetBrowser()); -} - -- (IBAction)testPopupWindow:(id)sender { - if (g_handler.get() && g_handler->GetBrowserId()) - RunPopupTest(g_handler->GetBrowser()); -} - -- (IBAction)testRequest:(id)sender { - if (g_handler.get() && g_handler->GetBrowserId()) - RunRequestTest(g_handler->GetBrowser()); -} - -- (IBAction)testPluginInfo:(id)sender { - if (g_handler.get() && g_handler->GetBrowserId()) - RunPluginInfoTest(g_handler->GetBrowser()); -} - -- (IBAction)testZoomIn:(id)sender { - if (g_handler.get() && g_handler->GetBrowserId()) { - CefRefPtr browser = g_handler->GetBrowser(); - browser->GetHost()->SetZoomLevel(browser->GetHost()->GetZoomLevel() + 0.5); - } -} - -- (IBAction)testZoomOut:(id)sender { - if (g_handler.get() && g_handler->GetBrowserId()) { - CefRefPtr browser = g_handler->GetBrowser(); - browser->GetHost()->SetZoomLevel(browser->GetHost()->GetZoomLevel() - 0.5); - } -} - -- (IBAction)testZoomReset:(id)sender { - if (g_handler.get() && g_handler->GetBrowserId()) { - CefRefPtr browser = g_handler->GetBrowser(); - browser->GetHost()->SetZoomLevel(0.0); - } -} - -- (IBAction)testBeginTracing:(id)sender { - if (g_handler.get()) - g_handler->BeginTracing(); -} - -- (IBAction)testEndTracing:(id)sender { - if (g_handler.get()) - g_handler->EndTracing(); -} - -- (IBAction)testPrint:(id)sender { - if (g_handler.get() && g_handler->GetBrowserId()) - g_handler->GetBrowser()->GetHost()->Print(); -} - -- (IBAction)testOtherTests:(id)sender { - if (g_handler.get() && g_handler->GetBrowserId()) - RunOtherTests(g_handler->GetBrowser()); + client::test_runner::RunTest(g_handler->GetBrowser(), [item tag]); } - (NSApplicationTerminateReply)applicationShouldTerminate: @@ -630,8 +541,8 @@ int main(int argc, char* argv[]) { // Initialize CEF. CefInitialize(main_args, settings, app.get(), NULL); - // Register the scheme handler. - scheme_test::InitTest(); + // Register scheme handlers. + client::test_runner::RegisterSchemeHandlers(); // Create the application delegate and window. NSObject* delegate = [[ClientAppDelegate alloc] init]; @@ -667,6 +578,10 @@ std::string AppGetWorkingDirectory() { return szWorkingDir; } +std::string AppGetDownloadPath(const std::string& file_name) { + return std::string(); +} + void AppQuitMessageLoop() { client::MainMessageLoop::Get()->Quit(); } diff --git a/tests/cefclient/cefclient_win.cc b/tests/cefclient/cefclient_win.cc index be758a7ea..08f19271e 100644 --- a/tests/cefclient/cefclient_win.cc +++ b/tests/cefclient/cefclient_win.cc @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -23,8 +24,7 @@ #include "cefclient/main_message_loop_multithreaded_win.h" #include "cefclient/main_message_loop_std.h" #include "cefclient/resource.h" -#include "cefclient/scheme_test.h" -#include "cefclient/string_util.h" +#include "cefclient/test_runner.h" #include "cefclient/util_win.h" @@ -125,8 +125,8 @@ int APIENTRY wWinMain(HINSTANCE hInstance, // Initialize CEF. CefInitialize(main_args, settings, app.get(), sandbox_info); - // Register the scheme handler. - scheme_test::InitTest(); + // Register scheme handlers. + client::test_runner::RegisterSchemeHandlers(); // Initialize global strings LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); @@ -214,18 +214,6 @@ BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { return TRUE; } -// Change the zoom factor on the UI thread. -static void ModifyZoom(CefRefPtr browser, double delta) { - if (!CefCurrentlyOn(TID_UI)) { - // Execute on the UI thread. - CefPostTask(TID_UI, base::Bind(&ModifyZoom, browser, delta)); - return; - } - - browser->GetHost()->SetZoomLevel( - browser->GetHost()->GetZoomLevel() + delta); -} - // Show a warning message on the UI thread. static void ShowWarning(CefRefPtr browser, int id) { if (!CefCurrentlyOn(TID_UI)) { @@ -469,6 +457,12 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, wmId = LOWORD(wParam); wmEvent = HIWORD(wParam); + + if (wmId >= ID_TESTS_FIRST && wmId <= ID_TESTS_LAST) { + client::test_runner::RunTest(browser, wmId); + return 0; + } + // Parse the menu selections: switch (wmId) { case IDM_ABOUT: @@ -521,52 +515,6 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, if (browser.get()) browser->StopLoad(); return 0; - case ID_TESTS_GETSOURCE: // Test the GetSource function - if (browser.get()) - RunGetSourceTest(browser); - return 0; - case ID_TESTS_GETTEXT: // Test the GetText function - if (browser.get()) - RunGetTextTest(browser); - return 0; - case ID_TESTS_POPUP: // Test a popup window - if (browser.get()) - RunPopupTest(browser); - return 0; - case ID_TESTS_REQUEST: // Test a request - if (browser.get()) - RunRequestTest(browser); - return 0; - case ID_TESTS_PLUGIN_INFO: // Test plugin info - if (browser.get()) - RunPluginInfoTest(browser); - return 0; - case ID_TESTS_ZOOM_IN: - if (browser.get()) - ModifyZoom(browser, 0.5); - return 0; - case ID_TESTS_ZOOM_OUT: - if (browser.get()) - ModifyZoom(browser, -0.5); - return 0; - case ID_TESTS_ZOOM_RESET: - if (browser.get()) - browser->GetHost()->SetZoomLevel(0.0); - return 0; - case ID_TESTS_TRACING_BEGIN: - g_handler->BeginTracing(); - return 0; - case ID_TESTS_TRACING_END: - g_handler->EndTracing(); - return 0; - case ID_TESTS_PRINT: - if(browser.get()) - browser->GetHost()->Print(); - return 0; - case ID_TESTS_OTHER_TESTS: - if (browser.get()) - RunOtherTests(browser); - return 0; } break; } @@ -737,6 +685,20 @@ std::string AppGetWorkingDirectory() { return szWorkingDir; } +std::string AppGetDownloadPath(const std::string& file_name) { + TCHAR szFolderPath[MAX_PATH]; + std::string path; + + // Save the file in the user's "My Documents" folder. + if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_PERSONAL | CSIDL_FLAG_CREATE, + NULL, 0, szFolderPath))) { + path = CefString(szFolderPath); + path += "\\" + file_name; + } + + return path; +} + void AppQuitMessageLoop() { client::MainMessageLoop::Get()->Quit(); } diff --git a/tests/cefclient/client_app_delegates.cc b/tests/cefclient/client_app_delegates.cc index 68bfb0c8b..10abeb067 100644 --- a/tests/cefclient/client_app_delegates.cc +++ b/tests/cefclient/client_app_delegates.cc @@ -18,14 +18,14 @@ void ClientApp::CreateBrowserDelegates(BrowserDelegateSet& delegates) { // static void ClientApp::CreateRenderDelegates(RenderDelegateSet& delegates) { client_renderer::CreateRenderDelegates(delegates); - performance_test::CreateRenderDelegates(delegates); + client::performance_test::CreateRenderDelegates(delegates); } // static void ClientApp::RegisterCustomSchemes( CefRefPtr registrar, std::vector& cookiable_schemes) { - scheme_test::RegisterCustomSchemes(registrar, cookiable_schemes); + client::scheme_test::RegisterCustomSchemes(registrar, cookiable_schemes); } // static diff --git a/tests/cefclient/client_handler.cc b/tests/cefclient/client_handler.cc index deeb81e0c..b36d54205 100644 --- a/tests/cefclient/client_handler.cc +++ b/tests/cefclient/client_handler.cc @@ -19,14 +19,11 @@ #include "include/cef_url.h" #include "include/wrapper/cef_closure_task.h" #include "include/wrapper/cef_stream_resource_handler.h" -#include "cefclient/binding_test.h" #include "cefclient/cefclient.h" #include "cefclient/client_renderer.h" #include "cefclient/client_switches.h" -#include "cefclient/dialog_test.h" #include "cefclient/resource_util.h" -#include "cefclient/string_util.h" -#include "cefclient/window_test.h" +#include "cefclient/test_runner.h" namespace { @@ -42,49 +39,6 @@ enum client_menu_ids { CLIENT_ID_TESTMENU_RADIOITEM3, }; -const char kTestOrigin[] = "http://tests/"; - -// Retrieve the file name and mime type based on the specified url. -bool ParseTestUrl(const std::string& url, - std::string* file_name, - std::string* mime_type) { - // Retrieve the path component. - CefURLParts parts; - CefParseURL(url, parts); - std::string file = CefString(&parts.path); - if (file.size() < 2) - return false; - - // Remove the leading slash. - file = file.substr(1); - - // Verify that the file name is valid. - for(size_t i = 0; i < file.size(); ++i) { - const char c = file[i]; - if (!isalpha(c) && !isdigit(c) && c != '_' && c != '.') - return false; - } - - // Determine the mime type based on the file extension, if any. - size_t pos = file.rfind("."); - if (pos != std::string::npos) { - std::string ext = file.substr(pos + 1); - if (ext == "html") - *mime_type = "text/html"; - else if (ext == "png") - *mime_type = "image/png"; - else - return false; - } else { - // Default to an html extension if none is specified. - *mime_type = "text/html"; - file += ".html"; - } - - *file_name = file; - return true; -} - } // namespace int ClientHandler::browser_count_ = 0; @@ -255,7 +209,7 @@ void ClientHandler::OnBeforeDownload( CEF_REQUIRE_UI_THREAD(); // Continue the download and show the "Save As" dialog. - callback->Continue(GetDownloadPath(suggested_name), true); + callback->Continue(AppGetDownloadPath(suggested_name), true); } void ClientHandler::OnDownloadUpdated( @@ -372,7 +326,7 @@ void ClientHandler::OnAfterCreated(CefRefPtr browser) { message_router_ = CefMessageRouterBrowserSide::Create(config); // Register handlers with the router. - CreateMessageHandlers(message_handler_set_); + client::test_runner::CreateMessageHandlers(message_handler_set_); MessageHandlerSet::const_iterator it = message_handler_set_.begin(); for (; it != message_handler_set_.end(); ++it) message_router_->AddHandler(*(it), false); @@ -513,35 +467,7 @@ CefRefPtr ClientHandler::GetResourceHandler( CefRefPtr frame, CefRefPtr request) { CEF_REQUIRE_IO_THREAD(); - - std::string url = request->GetURL(); - if (url.find(kTestOrigin) == 0) { - // Handle URLs in the test origin. - std::string file_name, mime_type; - if (ParseTestUrl(url, &file_name, &mime_type)) { - if (file_name == "request.html") { - // Show the request contents. - std::string dump; - DumpRequestContents(request, dump); - std::string str = "
" + dump +
-                          "
"; - CefRefPtr stream = - CefStreamReader::CreateForData( - static_cast(const_cast(str.c_str())), - str.size()); - DCHECK(stream.get()); - return new CefStreamResourceHandler("text/html", stream); - } else { - // Load the resource from file. - CefRefPtr stream = - GetBinaryResourceReader(file_name.c_str()); - if (stream.get()) - return new CefStreamResourceHandler(mime_type, stream); - } - } - } - - return NULL; + return client::test_runner::GetResourceHandler(browser, frame, request); } bool ClientHandler::OnQuotaRequest(CefRefPtr browser, @@ -809,76 +735,6 @@ std::string ClientHandler::GetStartupURL() const { return startup_url_; } -void ClientHandler::BeginTracing() { - if (!CefCurrentlyOn(TID_UI)) { - // Execute on the UI thread. - CefPostTask(TID_UI, base::Bind(&ClientHandler::BeginTracing, this)); - return; - } - - CefBeginTracing(CefString(), NULL); -} - -void ClientHandler::EndTracing() { - if (!CefCurrentlyOn(TID_UI)) { - // Execute on the UI thread. - CefPostTask(TID_UI, base::Bind(&ClientHandler::EndTracing, this)); - return; - } - - class Client : public CefEndTracingCallback, - public CefRunFileDialogCallback { - public: - explicit Client(CefRefPtr handler) - : handler_(handler) { - RunDialog(); - } - - void RunDialog() { - static const char kDefaultFileName[] = "trace.txt"; - std::string path = handler_->GetDownloadPath(kDefaultFileName); - if (path.empty()) - path = kDefaultFileName; - - // Results in a call to OnFileDialogDismissed. - handler_->GetBrowser()->GetHost()->RunFileDialog( - FILE_DIALOG_SAVE, - CefString(), // title - path, - std::vector(), // accept_filters - 0, // selected_accept_filter - this); - } - - virtual void OnFileDialogDismissed( - int selected_accept_filter, - const std::vector& file_paths) OVERRIDE { - CEF_REQUIRE_UI_THREAD(); - if (!file_paths.empty()) { - // File selected. Results in a call to OnEndTracingComplete. - CefEndTracing(file_paths.front(), this); - } else { - // No file selected. Discard the trace data. - CefEndTracing(CefString(), NULL); - } - } - - virtual void OnEndTracingComplete( - const CefString& tracing_file) OVERRIDE { - CEF_REQUIRE_UI_THREAD(); - handler_->SetLastDownloadFile(tracing_file.ToString()); - handler_->SendNotification(NOTIFY_DOWNLOAD_COMPLETE); - } - - private: - CefRefPtr handler_; - - IMPLEMENT_REFCOUNTING(Client); - }; - - new Client(this); -} - bool ClientHandler::Save(const std::string& path, const std::string& data) { FILE* f = fopen(path.c_str(), "w"); if (!f) @@ -894,18 +750,6 @@ bool ClientHandler::Save(const std::string& path, const std::string& data) { return true; } -// static -void ClientHandler::CreateMessageHandlers(MessageHandlerSet& handlers) { - // Create the dialog test handlers. - dialog_test::CreateMessageHandlers(handlers); - - // Create the binding test handlers. - binding_test::CreateMessageHandlers(handlers); - - // Create the window test handlers. - window_test::CreateMessageHandlers(handlers); -} - void ClientHandler::BuildTestMenu(CefRefPtr model) { if (model->GetCount() > 0) model->AddSeparator(); @@ -942,4 +786,3 @@ bool ClientHandler::ExecuteTestMenu(int command_id) { // Allow default handling to proceed. return false; } - diff --git a/tests/cefclient/client_handler.h b/tests/cefclient/client_handler.h index bea84ae69..306205b8f 100644 --- a/tests/cefclient/client_handler.h +++ b/tests/cefclient/client_handler.h @@ -292,19 +292,12 @@ class ClientHandler : public CefClient, // Returns the startup URL. std::string GetStartupURL() const; - void BeginTracing(); - void EndTracing(); - bool Save(const std::string& path, const std::string& data); private: void SetLoading(bool isLoading); void SetNavState(bool canGoBack, bool canGoForward); - // Create all CefMessageRouterBrowserSide::Handler objects. They will be - // deleted when the ClientHandler is destroyed. - static void CreateMessageHandlers(MessageHandlerSet& handlers); - // Test context menu creation. void BuildTestMenu(CefRefPtr model); bool ExecuteTestMenu(int command_id); @@ -314,10 +307,6 @@ class ClientHandler : public CefClient, int radio_item; } test_menu_state_; - // Returns the full download path for the specified file, or an empty path to - // use the default temp directory. - std::string GetDownloadPath(const std::string& file_name); - // START THREAD SAFE MEMBERS // The following members are thread-safe because they're initialized during // object construction and not changed thereafter. diff --git a/tests/cefclient/client_handler_gtk.cc b/tests/cefclient/client_handler_gtk.cc index 5f907ed71..58aab23c7 100644 --- a/tests/cefclient/client_handler_gtk.cc +++ b/tests/cefclient/client_handler_gtk.cc @@ -482,7 +482,3 @@ void ClientHandler::SetNavState(bool canGoBack, bool canGoForward) { else gtk_widget_set_sensitive(GTK_WIDGET(forward_handle_), false); } - -std::string ClientHandler::GetDownloadPath(const std::string& file_name) { - return std::string(); -} diff --git a/tests/cefclient/client_handler_mac.mm b/tests/cefclient/client_handler_mac.mm index 1a6de49c1..318ee808a 100644 --- a/tests/cefclient/client_handler_mac.mm +++ b/tests/cefclient/client_handler_mac.mm @@ -64,7 +64,3 @@ void ClientHandler::SetLoading(bool isLoading) { void ClientHandler::SetNavState(bool canGoBack, bool canGoForward) { // TODO(port): Change button status. } - -std::string ClientHandler::GetDownloadPath(const std::string& file_name) { - return std::string(); -} diff --git a/tests/cefclient/client_handler_win.cc b/tests/cefclient/client_handler_win.cc index 7b21ce526..9b0c6b342 100644 --- a/tests/cefclient/client_handler_win.cc +++ b/tests/cefclient/client_handler_win.cc @@ -6,7 +6,6 @@ #include #include -#include #include "include/cef_browser.h" #include "include/cef_frame.h" @@ -67,17 +66,3 @@ void ClientHandler::SetNavState(bool canGoBack, bool canGoForward) { EnableWindow(back_handle_, canGoBack); EnableWindow(forward_handle_, canGoForward); } - -std::string ClientHandler::GetDownloadPath(const std::string& file_name) { - TCHAR szFolderPath[MAX_PATH]; - std::string path; - - // Save the file in the user's "My Documents" folder. - if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_PERSONAL | CSIDL_FLAG_CREATE, - NULL, 0, szFolderPath))) { - path = CefString(szFolderPath); - path += "\\" + file_name; - } - - return path; -} diff --git a/tests/cefclient/dialog_test.cc b/tests/cefclient/dialog_test.cc index 96cea9067..7c3f6c9ec 100644 --- a/tests/cefclient/dialog_test.cc +++ b/tests/cefclient/dialog_test.cc @@ -3,10 +3,13 @@ // can be found in the LICENSE file. #include "cefclient/dialog_test.h" -#include "include/cef_browser.h" #include +#include "include/cef_browser.h" +#include "include/wrapper/cef_helpers.h" + +namespace client { namespace dialog_test { namespace { @@ -176,8 +179,9 @@ class Handler : public CefMessageRouterBrowserSide::Handler { } // namespace -void CreateMessageHandlers(ClientHandler::MessageHandlerSet& handlers) { +void CreateMessageHandlers(test_runner::MessageHandlerSet& handlers) { handlers.insert(new Handler()); } } // namespace dialog_test +} // namespace client diff --git a/tests/cefclient/dialog_test.h b/tests/cefclient/dialog_test.h index 2f2f24520..09d18b3ff 100644 --- a/tests/cefclient/dialog_test.h +++ b/tests/cefclient/dialog_test.h @@ -6,13 +6,15 @@ #define CEF_TESTS_CEFCLIENT_DIALOG_TEST_H_ #pragma once -#include "cefclient/client_handler.h" +#include "cefclient/test_runner.h" +namespace client { namespace dialog_test { -/// Handler creation. Called from ClientHandler. -void CreateMessageHandlers(ClientHandler::MessageHandlerSet& handlers); +/// Handler creation. +void CreateMessageHandlers(test_runner::MessageHandlerSet& handlers); } // namespace dialog_test +} // namespace client #endif // CEF_TESTS_CEFCLIENT_DIALOG_TEST_H_ diff --git a/tests/cefclient/performance_test.cc b/tests/cefclient/performance_test.cc index 3bcfb2e70..f9e0feba7 100644 --- a/tests/cefclient/performance_test.cc +++ b/tests/cefclient/performance_test.cc @@ -10,6 +10,7 @@ #include "include/wrapper/cef_stream_resource_handler.h" #include "cefclient/performance_test_setup.h" +namespace client { namespace performance_test { // Use more interations for a Release build. @@ -161,3 +162,4 @@ void CreateRenderDelegates(ClientApp::RenderDelegateSet& delegates) { } } // namespace performance_test +} // namespace client diff --git a/tests/cefclient/performance_test.h b/tests/cefclient/performance_test.h index 1f50f120b..055e17186 100644 --- a/tests/cefclient/performance_test.h +++ b/tests/cefclient/performance_test.h @@ -8,11 +8,13 @@ #include "cefclient/client_app.h" +namespace client { namespace performance_test { // Render delegate creation. Called from client_app_delegates.cc. void CreateRenderDelegates(ClientApp::RenderDelegateSet& delegates); } // namespace performance_test +} // namespace client #endif // CEF_TESTS_CEFCLIENT_PERFORMANCE_TEST_H_ diff --git a/tests/cefclient/performance_test_setup.h b/tests/cefclient/performance_test_setup.h index cbe3e813e..726ba9e33 100644 --- a/tests/cefclient/performance_test_setup.h +++ b/tests/cefclient/performance_test_setup.h @@ -9,6 +9,7 @@ #include "include/base/cef_macros.h" #include "include/base/cef_logging.h" +namespace client { namespace performance_test { // Default number of iterations. @@ -98,5 +99,6 @@ extern const PerfTestEntry kPerfTests[]; extern const int kPerfTestsCount; } // namespace performance_test +} // namespace client #endif // CEF_TESTS_CEFCLIENT_PERFORMANCE_TEST_H_ diff --git a/tests/cefclient/performance_test_tests.cc b/tests/cefclient/performance_test_tests.cc index bde896914..d906ebf94 100644 --- a/tests/cefclient/performance_test_tests.cc +++ b/tests/cefclient/performance_test_tests.cc @@ -6,6 +6,7 @@ #include "cefclient/performance_test_setup.h" #include "include/cef_v8.h" +namespace client { namespace performance_test { namespace { @@ -326,3 +327,4 @@ const PerfTestEntry kPerfTests[] = { const int kPerfTestsCount = (sizeof(kPerfTests) / sizeof(kPerfTests[0])); } // namespace performance_test +} // namespace client diff --git a/tests/cefclient/process_helper_mac.cc b/tests/cefclient/process_helper_mac.cc index 54616e9d0..4ff9953c7 100644 --- a/tests/cefclient/process_helper_mac.cc +++ b/tests/cefclient/process_helper_mac.cc @@ -12,6 +12,9 @@ std::string AppGetWorkingDirectory() { return std::string(); } +std::string AppGetDownloadPath(const std::string& file_name) { + return std::string(); +} CefWindowHandle AppGetMainWindowHandle() { return NULL; } diff --git a/tests/cefclient/resource.h b/tests/cefclient/resource.h index 5bcfd2b7f..0d1b91769 100644 --- a/tests/cefclient/resource.h +++ b/tests/cefclient/resource.h @@ -27,6 +27,7 @@ #define ID_WARN_DOWNLOADERROR 32002 #define ID_QUIT 32500 #define ID_FIND 32501 +#define ID_TESTS_FIRST 32700 #define ID_TESTS_GETSOURCE 32700 #define ID_TESTS_GETTEXT 32701 #define ID_TESTS_OTHER_TESTS 32702 @@ -39,6 +40,7 @@ #define ID_TESTS_ZOOM_IN 32709 #define ID_TESTS_ZOOM_OUT 32710 #define ID_TESTS_ZOOM_RESET 32711 +#define ID_TESTS_LAST 32711 #define IDC_STATIC -1 #define IDS_BINDING 1000 #define IDS_DIALOGS 1001 @@ -53,9 +55,6 @@ #define IDS_XMLHTTPREQUEST 1010 #define IDS_PERFORMANCE2 1011 -// Avoid files associated with MacOS -#define _X86_ - // Next default values for new objects // #ifdef APSTUDIO_INVOKED diff --git a/tests/cefclient/scheme_test.cc b/tests/cefclient/scheme_test.cc index 773247875..3460c4b5b 100644 --- a/tests/cefclient/scheme_test.cc +++ b/tests/cefclient/scheme_test.cc @@ -16,12 +16,9 @@ #include "include/cef_scheme.h" #include "include/wrapper/cef_helpers.h" #include "cefclient/resource_util.h" -#include "cefclient/string_util.h" - -#if defined(OS_WIN) -#include "cefclient/resource.h" -#endif +#include "cefclient/test_runner.h" +namespace client { namespace scheme_test { namespace { @@ -50,7 +47,7 @@ class ClientSchemeHandler : public CefResourceHandler { // Output a string representation of the request std::string dump; - DumpRequestContents(request, dump); + test_runner::DumpRequestContents(request, dump); data_.append(dump); data_.append("
Try the test form:" @@ -155,9 +152,10 @@ void RegisterCustomSchemes(CefRefPtr registrar, registrar->AddCustomScheme("client", true, false, false); } -void InitTest() { +void RegisterSchemeHandlers() { CefRegisterSchemeHandlerFactory("client", "tests", new ClientSchemeHandlerFactory()); } } // namespace scheme_test +} // namespace client diff --git a/tests/cefclient/scheme_test.h b/tests/cefclient/scheme_test.h index 36f670835..157e27bfc 100644 --- a/tests/cefclient/scheme_test.h +++ b/tests/cefclient/scheme_test.h @@ -12,6 +12,7 @@ class CefBrowser; class CefSchemeRegistrar; +namespace client { namespace scheme_test { // Register the scheme. @@ -19,8 +20,9 @@ void RegisterCustomSchemes(CefRefPtr registrar, std::vector& cookiable_schemes); // Create the scheme handler. -void InitTest(); +void RegisterSchemeHandlers(); } // namespace scheme_test +} // namespace client #endif // CEF_TESTS_CEFCLIENT_SCHEME_TEST_H_ diff --git a/tests/cefclient/string_util.cc b/tests/cefclient/string_util.cc deleted file mode 100644 index ebeca5c03..000000000 --- a/tests/cefclient/string_util.cc +++ /dev/null @@ -1,74 +0,0 @@ -// Copyright (c) 2010 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 "cefclient/string_util.h" -#include -#include -#include "include/cef_request.h" - -void DumpRequestContents(CefRefPtr request, std::string& str) { - std::stringstream ss; - - ss << "URL: " << std::string(request->GetURL()); - ss << "\nMethod: " << std::string(request->GetMethod()); - - CefRequest::HeaderMap headerMap; - request->GetHeaderMap(headerMap); - if (headerMap.size() > 0) { - ss << "\nHeaders:"; - CefRequest::HeaderMap::const_iterator it = headerMap.begin(); - for (; it != headerMap.end(); ++it) { - ss << "\n\t" << std::string((*it).first) << ": " << - std::string((*it).second); - } - } - - CefRefPtr postData = request->GetPostData(); - if (postData.get()) { - CefPostData::ElementVector elements; - postData->GetElements(elements); - if (elements.size() > 0) { - ss << "\nPost Data:"; - CefRefPtr element; - CefPostData::ElementVector::const_iterator it = elements.begin(); - for (; it != elements.end(); ++it) { - element = (*it); - if (element->GetType() == PDE_TYPE_BYTES) { - // the element is composed of bytes - ss << "\n\tBytes: "; - if (element->GetBytesCount() == 0) { - ss << "(empty)"; - } else { - // retrieve the data. - size_t size = element->GetBytesCount(); - char* bytes = new char[size]; - element->GetBytes(size, bytes); - ss << std::string(bytes, size); - delete [] bytes; - } - } else if (element->GetType() == PDE_TYPE_FILE) { - ss << "\n\tFile: " << std::string(element->GetFile()); - } - } - } - } - - str = ss.str(); -} - -std::string StringReplace(const std::string& str, const std::string& from, - const std::string& to) { - std::string result = str; - std::string::size_type pos = 0; - std::string::size_type from_len = from.length(); - std::string::size_type to_len = to.length(); - do { - pos = result.find(from, pos); - if (pos != std::string::npos) { - result.replace(pos, from_len, to); - pos += to_len; - } - } while (pos != std::string::npos); - return result; -} diff --git a/tests/cefclient/string_util.h b/tests/cefclient/string_util.h deleted file mode 100644 index c43e6f210..000000000 --- a/tests/cefclient/string_util.h +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright (c) 2010 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 CEF_TESTS_CEFCLIENT_STRING_UTIL_H_ -#define CEF_TESTS_CEFCLIENT_STRING_UTIL_H_ -#pragma once - -#include -#include "include/cef_base.h" - -class CefRequest; - -// Dump the contents of the request into a string. -void DumpRequestContents(CefRefPtr request, std::string& str); - -// Replace all instances of |from| with |to| in |str|. -std::string StringReplace(const std::string& str, const std::string& from, - const std::string& to); - -#endif // CEF_TESTS_CEFCLIENT_STRING_UTIL_H_ diff --git a/tests/cefclient/test_runner.cc b/tests/cefclient/test_runner.cc new file mode 100644 index 000000000..3862f9f84 --- /dev/null +++ b/tests/cefclient/test_runner.cc @@ -0,0 +1,432 @@ +// Copyright (c) 2015 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 "cefclient/test_runner.h" + +#include "include/base/cef_bind.h" +#include "include/cef_task.h" +#include "include/cef_trace.h" +#include "include/cef_url.h" +#include "include/cef_web_plugin.h" +#include "include/wrapper/cef_closure_task.h" +#include "include/wrapper/cef_stream_resource_handler.h" +#include "cefclient/binding_test.h" +#include "cefclient/cefclient.h" +#include "cefclient/dialog_test.h" +#include "cefclient/resource.h" +#include "cefclient/resource_util.h" +#include "cefclient/scheme_test.h" +#include "cefclient/window_test.h" + +namespace client { +namespace test_runner { + +namespace { + +const char kTestOrigin[] = "http://tests/"; + +// Replace all instances of |from| with |to| in |str|. +std::string StringReplace(const std::string& str, const std::string& from, + const std::string& to) { + std::string result = str; + std::string::size_type pos = 0; + std::string::size_type from_len = from.length(); + std::string::size_type to_len = to.length(); + do { + pos = result.find(from, pos); + if (pos != std::string::npos) { + result.replace(pos, from_len, to); + pos += to_len; + } + } while (pos != std::string::npos); + return result; +} + +void RunGetSourceTest(CefRefPtr browser) { + class Visitor : public CefStringVisitor { + public: + explicit Visitor(CefRefPtr browser) : browser_(browser) {} + virtual void Visit(const CefString& string) OVERRIDE { + std::string source = StringReplace(string, "<", "<"); + source = StringReplace(source, ">", ">"); + std::stringstream ss; + ss << "Source:
" << source <<
+            "
"; + browser_->GetMainFrame()->LoadString(ss.str(), "http://tests/getsource"); + } + private: + CefRefPtr browser_; + IMPLEMENT_REFCOUNTING(Visitor); + }; + + browser->GetMainFrame()->GetSource(new Visitor(browser)); +} + +void RunGetTextTest(CefRefPtr browser) { + class Visitor : public CefStringVisitor { + public: + explicit Visitor(CefRefPtr browser) : browser_(browser) {} + virtual void Visit(const CefString& string) OVERRIDE { + std::string text = StringReplace(string, "<", "<"); + text = StringReplace(text, ">", ">"); + std::stringstream ss; + ss << "Text:
" << text <<
+            "
"; + browser_->GetMainFrame()->LoadString(ss.str(), "http://tests/gettext"); + } + private: + CefRefPtr browser_; + IMPLEMENT_REFCOUNTING(Visitor); + }; + + browser->GetMainFrame()->GetText(new Visitor(browser)); +} + +void RunRequestTest(CefRefPtr browser) { + // Create a new request + CefRefPtr request(CefRequest::Create()); + + // Set the request URL + request->SetURL("http://tests/request"); + + // Add post data to the request. The correct method and content- + // type headers will be set by CEF. + CefRefPtr postDataElement(CefPostDataElement::Create()); + std::string data = "arg1=val1&arg2=val2"; + postDataElement->SetToBytes(data.length(), data.c_str()); + CefRefPtr postData(CefPostData::Create()); + postData->AddElement(postDataElement); + request->SetPostData(postData); + + // Add a custom header + CefRequest::HeaderMap headerMap; + headerMap.insert( + std::make_pair("X-My-Header", "My Header Value")); + request->SetHeaderMap(headerMap); + + // Load the request + browser->GetMainFrame()->LoadRequest(request); +} + +void RunPopupTest(CefRefPtr browser) { + browser->GetMainFrame()->ExecuteJavaScript( + "window.open('http://www.google.com');", "about:blank", 0); +} + +void RunPluginInfoTest(CefRefPtr browser) { + class Visitor : public CefWebPluginInfoVisitor { + public: + explicit Visitor(CefRefPtr browser) + : browser_(browser) { + html_ = "Plugin Info Test" + "" + "\nInstalled plugins:"; + } + ~Visitor() { + html_ += "\n"; + + // Load the html in the browser. + browser_->GetMainFrame()->LoadString(html_, "http://tests/plugin_info"); + } + + virtual bool Visit(CefRefPtr info, int count, int total) + OVERRIDE { + html_ += "\n

Name: " + info->GetName().ToString() + + "\n
Description: " + info->GetDescription().ToString() + + "\n
Version: " + info->GetVersion().ToString() + + "\n
Path: " + info->GetPath().ToString(); + return true; + } + + private: + std::string html_; + CefRefPtr browser_; + IMPLEMENT_REFCOUNTING(Visitor); + }; + + CefVisitWebPluginInfo(new Visitor(browser)); +} + +void ModifyZoom(CefRefPtr browser, double delta) { + if (!CefCurrentlyOn(TID_UI)) { + // Execute on the UI thread. + CefPostTask(TID_UI, base::Bind(&ModifyZoom, browser, delta)); + return; + } + + browser->GetHost()->SetZoomLevel( + browser->GetHost()->GetZoomLevel() + delta); +} + +void BeginTracing() { + if (!CefCurrentlyOn(TID_UI)) { + // Execute on the UI thread. + CefPostTask(TID_UI, base::Bind(&BeginTracing)); + return; + } + + CefBeginTracing(CefString(), NULL); +} + +void EndTracing(CefRefPtr browser) { + if (!CefCurrentlyOn(TID_UI)) { + // Execute on the UI thread. + CefPostTask(TID_UI, base::Bind(&EndTracing, browser)); + return; + } + + class Client : public CefEndTracingCallback, + public CefRunFileDialogCallback { + public: + explicit Client(CefRefPtr browser) + : browser_(browser) { + RunDialog(); + } + + void RunDialog() { + static const char kDefaultFileName[] = "trace.txt"; + std::string path = AppGetDownloadPath(kDefaultFileName); + if (path.empty()) + path = kDefaultFileName; + + // Results in a call to OnFileDialogDismissed. + browser_->GetHost()->RunFileDialog( + FILE_DIALOG_SAVE, + CefString(), // title + path, + std::vector(), // accept_filters + 0, // selected_accept_filter + this); + } + + virtual void OnFileDialogDismissed( + int selected_accept_filter, + const std::vector& file_paths) OVERRIDE { + if (!file_paths.empty()) { + // File selected. Results in a call to OnEndTracingComplete. + CefEndTracing(file_paths.front(), this); + } else { + // No file selected. Discard the trace data. + CefEndTracing(CefString(), NULL); + } + } + + virtual void OnEndTracingComplete( + const CefString& tracing_file) OVERRIDE { + Alert(browser_, + "File \"" + tracing_file.ToString() + "\" saved successfully."); + } + + private: + CefRefPtr browser_; + + IMPLEMENT_REFCOUNTING(Client); + }; + + new Client(browser); +} + +void RunOtherTests(CefRefPtr browser) { + browser->GetMainFrame()->LoadURL("http://tests/other_tests"); +} + +// Retrieve the file name and mime type based on the specified url. +bool ParseTestUrl(const std::string& url, + std::string* file_name, + std::string* mime_type) { + // Retrieve the path component. + CefURLParts parts; + CefParseURL(url, parts); + std::string file = CefString(&parts.path); + if (file.size() < 2) + return false; + + // Remove the leading slash. + file = file.substr(1); + + // Verify that the file name is valid. + for(size_t i = 0; i < file.size(); ++i) { + const char c = file[i]; + if (!isalpha(c) && !isdigit(c) && c != '_' && c != '.') + return false; + } + + // Determine the mime type based on the file extension, if any. + size_t pos = file.rfind("."); + if (pos != std::string::npos) { + std::string ext = file.substr(pos + 1); + if (ext == "html") + *mime_type = "text/html"; + else if (ext == "png") + *mime_type = "image/png"; + else + return false; + } else { + // Default to an html extension if none is specified. + *mime_type = "text/html"; + file += ".html"; + } + + *file_name = file; + return true; +} + +} // namespace + +void RunTest(CefRefPtr browser, int id) { + if (!browser) + return; + + switch (id) { + case ID_TESTS_GETSOURCE: + RunGetSourceTest(browser); + break; + case ID_TESTS_GETTEXT: + RunGetTextTest(browser); + break; + case ID_TESTS_POPUP: + RunPopupTest(browser); + break; + case ID_TESTS_REQUEST: + RunRequestTest(browser); + break; + case ID_TESTS_PLUGIN_INFO: + RunPluginInfoTest(browser); + break; + case ID_TESTS_ZOOM_IN: + ModifyZoom(browser, 0.5); + break; + case ID_TESTS_ZOOM_OUT: + ModifyZoom(browser, -0.5); + break; + case ID_TESTS_ZOOM_RESET: + browser->GetHost()->SetZoomLevel(0.0); + break; + case ID_TESTS_TRACING_BEGIN: + BeginTracing(); + break; + case ID_TESTS_TRACING_END: + EndTracing(browser); + break; + case ID_TESTS_PRINT: + browser->GetHost()->Print(); + break; + case ID_TESTS_OTHER_TESTS: + RunOtherTests(browser); + break; + } +} + +void DumpRequestContents(CefRefPtr request, std::string& str) { + std::stringstream ss; + + ss << "URL: " << std::string(request->GetURL()); + ss << "\nMethod: " << std::string(request->GetMethod()); + + CefRequest::HeaderMap headerMap; + request->GetHeaderMap(headerMap); + if (headerMap.size() > 0) { + ss << "\nHeaders:"; + CefRequest::HeaderMap::const_iterator it = headerMap.begin(); + for (; it != headerMap.end(); ++it) { + ss << "\n\t" << std::string((*it).first) << ": " << + std::string((*it).second); + } + } + + CefRefPtr postData = request->GetPostData(); + if (postData.get()) { + CefPostData::ElementVector elements; + postData->GetElements(elements); + if (elements.size() > 0) { + ss << "\nPost Data:"; + CefRefPtr element; + CefPostData::ElementVector::const_iterator it = elements.begin(); + for (; it != elements.end(); ++it) { + element = (*it); + if (element->GetType() == PDE_TYPE_BYTES) { + // the element is composed of bytes + ss << "\n\tBytes: "; + if (element->GetBytesCount() == 0) { + ss << "(empty)"; + } else { + // retrieve the data. + size_t size = element->GetBytesCount(); + char* bytes = new char[size]; + element->GetBytes(size, bytes); + ss << std::string(bytes, size); + delete [] bytes; + } + } else if (element->GetType() == PDE_TYPE_FILE) { + ss << "\n\tFile: " << std::string(element->GetFile()); + } + } + } + } + + str = ss.str(); +} + +CefRefPtr GetResourceHandler( + CefRefPtr browser, + CefRefPtr frame, + CefRefPtr request) { + std::string url = request->GetURL(); + if (url.find(kTestOrigin) == 0) { + // Handle URLs in the test origin. + std::string file_name, mime_type; + if (ParseTestUrl(url, &file_name, &mime_type)) { + if (file_name == "request.html") { + // Show the request contents. + std::string dump; + DumpRequestContents(request, dump); + std::string str = "
" + dump +
+                          "
"; + CefRefPtr stream = + CefStreamReader::CreateForData( + static_cast(const_cast(str.c_str())), + str.size()); + DCHECK(stream.get()); + return new CefStreamResourceHandler("text/html", stream); + } else { + // Load the resource from file. + CefRefPtr stream = + GetBinaryResourceReader(file_name.c_str()); + if (stream.get()) + return new CefStreamResourceHandler(mime_type, stream); + } + } + } + + return NULL; +} + +void Alert(CefRefPtr browser, const std::string& message) { + // Escape special characters in the message. + std::string msg = StringReplace(message, "\\", "\\\\"); + msg = StringReplace(msg, "'", "\\'"); + + // Execute a JavaScript alert(). + CefRefPtr frame = browser->GetMainFrame(); + frame->ExecuteJavaScript("alert('" + msg + "');", frame->GetURL(), 0); +} + +void CreateMessageHandlers(MessageHandlerSet& handlers) { + // Create the dialog test handlers. + dialog_test::CreateMessageHandlers(handlers); + + // Create the binding test handlers. + binding_test::CreateMessageHandlers(handlers); + + // Create the window test handlers. + window_test::CreateMessageHandlers(handlers); +} + +void RegisterSchemeHandlers() { + // Register the scheme handler. + scheme_test::RegisterSchemeHandlers(); +} + +} // namespace test_runner +} // namespace client diff --git a/tests/cefclient/test_runner.h b/tests/cefclient/test_runner.h new file mode 100644 index 000000000..2dc428398 --- /dev/null +++ b/tests/cefclient/test_runner.h @@ -0,0 +1,45 @@ +// Copyright (c) 2015 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 CEF_TESTS_CEFCLIENT_TEST_RUNNER_H_ +#define CEF_TESTS_CEFCLIENT_TEST_RUNNER_H_ + +#include +#include + +#include "include/cef_browser.h" +#include "include/cef_resource_handler.h" +#include "include/cef_request.h" +#include "include/wrapper/cef_message_router.h" + +namespace client { +namespace test_runner { + +// Run a test. +void RunTest(CefRefPtr browser, int id); + +// Dump the contents of the request into a string. +void DumpRequestContents(CefRefPtr request, std::string& str); + +// Get test resources. +CefRefPtr GetResourceHandler( + CefRefPtr browser, + CefRefPtr frame, + CefRefPtr request); + +// Show a JS alert message. +void Alert(CefRefPtr browser, const std::string& message); + +// Create all CefMessageRouterBrowserSide::Handler objects. They will be +// deleted when the ClientHandler is destroyed. +typedef std::set MessageHandlerSet; +void CreateMessageHandlers(MessageHandlerSet& handlers); + +// Register scheme handlers for tests. +void RegisterSchemeHandlers(); + +} // namespace test_runner +} // namespace client + +#endif // CEF_TESTS_CEFCLIENT_TEST_RUNNER_H_ diff --git a/tests/cefclient/window_test.cc b/tests/cefclient/window_test.cc index 2a86842bf..18c2ffa9f 100644 --- a/tests/cefclient/window_test.cc +++ b/tests/cefclient/window_test.cc @@ -9,8 +9,11 @@ #include #include +#include "include/base/cef_bind.h" #include "include/wrapper/cef_stream_resource_handler.h" +#include "cefclient/main_message_loop.h" +namespace client { namespace window_test { namespace { @@ -53,15 +56,19 @@ class Handler : public CefMessageRouterBrowserSide::Handler { } if (vec.size() == 4) { - SetPos(browser->GetHost()->GetWindowHandle(), - vec[0], vec[1], vec[2], vec[3]); + // Execute SetPos() on the main thread. + MAIN_POST_CLOSURE(base::Bind(&SetPos, browser, + vec[0], vec[1], vec[2], vec[3])); } } else if (message_name == kMessageMinimizeName) { - Minimize(browser->GetHost()->GetWindowHandle()); + // Execute Minimize() on the main thread. + MAIN_POST_CLOSURE(base::Bind(&Minimize, browser)); } else if (message_name == kMessageMaximizeName) { - Maximize(browser->GetHost()->GetWindowHandle()); + // Execute Maximize() on the main thread. + MAIN_POST_CLOSURE(base::Bind(&Maximize, browser)); } else if (message_name == kMessageRestoreName) { - Restore(browser->GetHost()->GetWindowHandle()); + // Execute Restore() on the main thread. + MAIN_POST_CLOSURE(base::Bind(&Restore, browser)); } else { NOTREACHED(); } @@ -73,10 +80,6 @@ class Handler : public CefMessageRouterBrowserSide::Handler { } // namespace -void CreateMessageHandlers(ClientHandler::MessageHandlerSet& handlers) { - handlers.insert(new Handler()); -} - void ModifyBounds(const CefRect& display, CefRect& window) { window.x += display.x; window.y += display.y; @@ -99,4 +102,9 @@ void ModifyBounds(const CefRect& display, CefRect& window) { window.y = display.y + display.height - window.height; } +void CreateMessageHandlers(test_runner::MessageHandlerSet& handlers) { + handlers.insert(new Handler()); +} + } // namespace window_test +} // namespace client diff --git a/tests/cefclient/window_test.h b/tests/cefclient/window_test.h index 3a99a21f1..a81f0fe9f 100644 --- a/tests/cefclient/window_test.h +++ b/tests/cefclient/window_test.h @@ -6,23 +6,26 @@ #define CEF_TESTS_CEFCLIENT_WINDOW_TEST_H_ #pragma once -#include "cefclient/client_handler.h" +#include "cefclient/test_runner.h" +namespace client { namespace window_test { -/// Handler creation. Called from ClientHandler. -void CreateMessageHandlers(ClientHandler::MessageHandlerSet& handlers); +/// Handler creation. +void CreateMessageHandlers(test_runner::MessageHandlerSet& handlers); // Fit |window| inside |display|. Coordinates are relative to the upper-left // corner of the display. void ModifyBounds(const CefRect& display, CefRect& window); // Platform implementations. -void SetPos(CefWindowHandle handle, int x, int y, int width, int height); -void Minimize(CefWindowHandle handle); -void Maximize(CefWindowHandle handle); -void Restore(CefWindowHandle handle); +void SetPos(CefRefPtr browser, + int x, int y, int width, int height); +void Minimize(CefRefPtr browser); +void Maximize(CefRefPtr browser); +void Restore(CefRefPtr browser); } // namespace window_test +} // namespace client #endif // CEF_TESTS_CEFCLIENT_WINDOW_TEST_H_ diff --git a/tests/cefclient/window_test_gtk.cc b/tests/cefclient/window_test_gtk.cc index 49dd5760a..13036a591 100644 --- a/tests/cefclient/window_test_gtk.cc +++ b/tests/cefclient/window_test_gtk.cc @@ -3,15 +3,22 @@ // can be found in the LICENSE file. #include "cefclient/window_test.h" +#include "cefclient/client_handler.h" #include +namespace client { namespace window_test { namespace { -GtkWindow* GetWindow(CefWindowHandle handle) { - return GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(handle))); +GtkWindow* GetWindow(CefRefPtr browser) { + // We can't get the GtkWidget* from the X11 Window that would be returned via + // CefBrowserHost::GetWindowHandle so retrieve it via the ClientHandler + // instance instead. + CefRefPtr handler = + static_cast(browser->GetHost()->GetClient().get()); + return GTK_WINDOW(gtk_widget_get_toplevel(handler->GetMainWindowHandle())); } bool IsMaximized(GtkWindow* window) { @@ -22,8 +29,9 @@ bool IsMaximized(GtkWindow* window) { } // namespace -void SetPos(CefWindowHandle handle, int x, int y, int width, int height) { - GtkWindow* window = GetWindow(handle); +void SetPos(CefRefPtr browser, int x, int y, int width, + int height) { + GtkWindow* window = GetWindow(browser); GdkWindow* gdk_window = gtk_widget_get_window(GTK_WIDGET(window)); // Make sure the window isn't minimized or maximized. @@ -47,8 +55,8 @@ void SetPos(CefWindowHandle handle, int x, int y, int width, int height) { window_rect.width, window_rect.height); } -void Minimize(CefWindowHandle handle) { - GtkWindow* window = GetWindow(handle); +void Minimize(CefRefPtr browser) { + GtkWindow* window = GetWindow(browser); // Unmaximize the window before minimizing so restore behaves correctly. if (IsMaximized(window)) @@ -57,12 +65,12 @@ void Minimize(CefWindowHandle handle) { gtk_window_iconify(window); } -void Maximize(CefWindowHandle handle) { - gtk_window_maximize(GetWindow(handle)); +void Maximize(CefRefPtr browser) { + gtk_window_maximize(GetWindow(browser)); } -void Restore(CefWindowHandle handle) { - GtkWindow* window = GetWindow(handle); +void Restore(CefRefPtr browser) { + GtkWindow* window = GetWindow(browser); if (IsMaximized(window)) gtk_window_unmaximize(window); else @@ -70,3 +78,4 @@ void Restore(CefWindowHandle handle) { } } // namespace window_test +} // namespace client diff --git a/tests/cefclient/window_test_mac.mm b/tests/cefclient/window_test_mac.mm index c61d1b5b1..5580b2fae 100644 --- a/tests/cefclient/window_test_mac.mm +++ b/tests/cefclient/window_test_mac.mm @@ -6,19 +6,21 @@ #import +namespace client { namespace window_test { namespace { -NSWindow* GetWindow(CefWindowHandle handle) { - NSView* view = (NSView*)handle; +NSWindow* GetWindow(CefRefPtr browser) { + NSView* view = (NSView*)browser->GetHost()->GetWindowHandle(); return [view window]; } } // namespace -void SetPos(CefWindowHandle handle, int x, int y, int width, int height) { - NSWindow* window = GetWindow(handle); +void SetPos(CefRefPtr browser, int x, int y, int width, + int height) { + NSWindow* window = GetWindow(browser); // Make sure the window isn't minimized or maximized. if ([window isMiniaturized]) @@ -50,16 +52,16 @@ void SetPos(CefWindowHandle handle, int x, int y, int width, int height) { [window setFrame:newRect display:YES]; } -void Minimize(CefWindowHandle handle) { - [GetWindow(handle) performMiniaturize:nil]; +void Minimize(CefRefPtr browser) { + [GetWindow(browser) performMiniaturize:nil]; } -void Maximize(CefWindowHandle handle) { - [GetWindow(handle) performZoom:nil]; +void Maximize(CefRefPtr browser) { + [GetWindow(browser) performZoom:nil]; } -void Restore(CefWindowHandle handle) { - NSWindow* window = GetWindow(handle); +void Restore(CefRefPtr browser) { + NSWindow* window = GetWindow(browser); if ([window isMiniaturized]) [window deminiaturize:nil]; else if ([window isZoomed]) @@ -67,3 +69,4 @@ void Restore(CefWindowHandle handle) { } } // namespace window_test +} // namespace client diff --git a/tests/cefclient/window_test_win.cc b/tests/cefclient/window_test_win.cc index f905e4312..65fc3a165 100644 --- a/tests/cefclient/window_test_win.cc +++ b/tests/cefclient/window_test_win.cc @@ -4,32 +4,36 @@ #include "cefclient/window_test.h" +namespace client { namespace window_test { namespace { -// Toggles the current display state. -void Toggle(CefWindowHandle handle, UINT nCmdShow) { - HWND root_wnd = ::GetAncestor(handle, GA_ROOT); +HWND GetRootHwnd(CefRefPtr browser) { + return ::GetAncestor(browser->GetHost()->GetWindowHandle(), GA_ROOT); +} +// Toggles the current display state. +void Toggle(HWND root_hwnd, UINT nCmdShow) { // Retrieve current window placement information. WINDOWPLACEMENT placement; - ::GetWindowPlacement(root_wnd, &placement); + ::GetWindowPlacement(root_hwnd, &placement); if (placement.showCmd == nCmdShow) - ::ShowWindow(root_wnd, SW_RESTORE); + ::ShowWindow(root_hwnd, SW_RESTORE); else - ::ShowWindow(root_wnd, nCmdShow); + ::ShowWindow(root_hwnd, nCmdShow); } } // namespace -void SetPos(CefWindowHandle handle, int x, int y, int width, int height) { - HWND root_wnd = ::GetAncestor(handle, GA_ROOT); +void SetPos(CefRefPtr browser, + int x, int y, int width, int height) { + HWND root_hwnd = GetRootHwnd(browser); // Retrieve current window placement information. WINDOWPLACEMENT placement; - ::GetWindowPlacement(root_wnd, &placement); + ::GetWindowPlacement(root_hwnd, &placement); // Retrieve information about the display that contains the window. HMONITOR monitor = MonitorFromRect(&placement.rcNormalPosition, @@ -54,25 +58,26 @@ void SetPos(CefWindowHandle handle, int x, int y, int width, int height) { placement.rcNormalPosition.right = window_rect.x + window_rect.width; placement.rcNormalPosition.top = window_rect.y; placement.rcNormalPosition.bottom = window_rect.y + window_rect.height; - ::SetWindowPlacement(root_wnd, &placement); - ::ShowWindow(root_wnd, SW_RESTORE); + ::SetWindowPlacement(root_hwnd, &placement); + ::ShowWindow(root_hwnd, SW_RESTORE); } else { // Set the window position. - ::SetWindowPos(root_wnd, NULL, window_rect.x, window_rect.y, + ::SetWindowPos(root_hwnd, NULL, window_rect.x, window_rect.y, window_rect.width, window_rect.height, SWP_NOZORDER); } } -void Minimize(CefWindowHandle handle) { - Toggle(handle, SW_MINIMIZE); +void Minimize(CefRefPtr browser) { + Toggle(GetRootHwnd(browser), SW_MINIMIZE); } -void Maximize(CefWindowHandle handle) { - Toggle(handle, SW_MAXIMIZE); +void Maximize(CefRefPtr browser) { + Toggle(GetRootHwnd(browser), SW_MAXIMIZE); } -void Restore(CefWindowHandle handle) { - ::ShowWindow(::GetAncestor(handle, GA_ROOT), SW_RESTORE); +void Restore(CefRefPtr browser) { + ::ShowWindow(GetRootHwnd(browser), SW_RESTORE); } } // namespace window_test +} // namespace client