From af0635bb34187aa62059f7e19e41ab1d4ad9bfde Mon Sep 17 00:00:00 2001 From: Marshall Greenblatt Date: Thu, 22 Jan 2015 21:39:36 +0000 Subject: [PATCH] cefclient: Use test_runner::Alert instead of platform-specific notifications (issue #1500). git-svn-id: https://chromiumembedded.googlecode.com/svn/trunk@1984 5089003a-bbd8-11dd-ad1f-f1f9622dbc98 --- tests/cefclient/cefclient.cc | 4 ++ tests/cefclient/cefclient.h | 5 +- tests/cefclient/cefclient_gtk.cc | 14 ++++-- tests/cefclient/cefclient_mac.mm | 40 ++++------------ tests/cefclient/cefclient_win.cc | 59 ++++-------------------- tests/cefclient/client_handler.cc | 66 ++++++++++----------------- tests/cefclient/client_handler.h | 22 ++------- tests/cefclient/client_handler_gtk.cc | 4 -- tests/cefclient/client_handler_mac.mm | 22 --------- tests/cefclient/client_handler_win.cc | 18 -------- tests/cefclient/process_helper_mac.cc | 3 ++ tests/cefclient/resource.h | 3 -- 12 files changed, 65 insertions(+), 195 deletions(-) diff --git a/tests/cefclient/cefclient.cc b/tests/cefclient/cefclient.cc index 1141a20b3..bae274fcd 100644 --- a/tests/cefclient/cefclient.cc +++ b/tests/cefclient/cefclient.cc @@ -37,6 +37,10 @@ ClientWindowHandle AppGetMainWindowHandle() { return g_handler->GetMainWindowHandle(); } +std::string AppGetConsoleLogPath() { + return AppGetWorkingDirectory() + "console.log"; +} + void AppInitCommandLine(int argc, const char* const* argv) { g_command_line = CefCommandLine::CreateCommandLine(); #if defined(OS_WIN) diff --git a/tests/cefclient/cefclient.h b/tests/cefclient/cefclient.h index ac97aed5e..2161a5e37 100644 --- a/tests/cefclient/cefclient.h +++ b/tests/cefclient/cefclient.h @@ -21,9 +21,12 @@ CefRefPtr AppGetBrowser(); // Returns the main application window handle. ClientWindowHandle AppGetMainWindowHandle(); -// Returns the application working directory. +// Returns the application working directory including trailing path separator. std::string AppGetWorkingDirectory(); +// Returns the full path to the console log file. +std::string AppGetConsoleLogPath(); + // Initialize the application command line. void AppInitCommandLine(int argc, const char* const* argv); diff --git a/tests/cefclient/cefclient_gtk.cc b/tests/cefclient/cefclient_gtk.cc index c56b4c009..cc2a75837 100644 --- a/tests/cefclient/cefclient_gtk.cc +++ b/tests/cefclient/cefclient_gtk.cc @@ -34,8 +34,6 @@ extern CefRefPtr g_handler; namespace { -char szWorkingDir[512]; // The current working directory - // Height of the buttons at the top of the GTK window. int g_toolbar_height = 0; @@ -334,9 +332,6 @@ int main(int argc, char* argv[]) { if (exit_code >= 0) return exit_code; - if (!getcwd(szWorkingDir, sizeof (szWorkingDir))) - return -1; - // Parse command line arguments. AppInitCommandLine(argc, argv_copy); @@ -498,6 +493,15 @@ int main(int argc, char* argv[]) { // Global functions std::string AppGetWorkingDirectory() { + char szWorkingDir[256]; + if (getcwd(szWorkingDir, sizeof(szWorkingDir) - 1) == NULL) { + szWorkingDir[0] = 0; + } else { + // Add trailing path separator. + size_t len = strlen(szWorkingDir); + szWorkingDir[len] = '/'; + szWorkingDir[len + 1] = 0; + } return szWorkingDir; } diff --git a/tests/cefclient/cefclient_mac.mm b/tests/cefclient/cefclient_mac.mm index aef96cd6c..9db5a35d0 100644 --- a/tests/cefclient/cefclient_mac.mm +++ b/tests/cefclient/cefclient_mac.mm @@ -30,8 +30,6 @@ class MainBrowserProvider : public OSRBrowserProvider { } } g_main_browser_provider; -char szWorkingDir[512]; // The current working directory - // Sizes for URL bar layout #define BUTTON_HEIGHT 22 #define BUTTON_WIDTH 72 @@ -129,9 +127,6 @@ const int kWindowHeight = 600; - (IBAction)stopLoading:(id)sender; - (IBAction)takeURLStringValueFrom:(NSTextField *)sender; - (void)alert:(NSString*)title withMessage:(NSString*)message; -- (void)notifyConsoleMessage:(id)object; -- (void)notifyDownloadComplete:(id)object; -- (void)notifyDownloadError:(id)object; @end @implementation ClientWindowDelegate @@ -207,29 +202,6 @@ const int kWindowHeight = 600; [alert runModal]; } -- (void)notifyConsoleMessage:(id)object { - std::stringstream ss; - ss << "Console messages will be written to " << g_handler->GetLogFile(); - NSString* str = [NSString stringWithUTF8String:(ss.str().c_str())]; - [self alert:@"Console Messages" withMessage:str]; -} - -- (void)notifyDownloadComplete:(id)object { - std::stringstream ss; - ss << "File \"" << g_handler->GetLastDownloadFile() << - "\" downloaded successfully."; - NSString* str = [NSString stringWithUTF8String:(ss.str().c_str())]; - [self alert:@"File Download" withMessage:str]; -} - -- (void)notifyDownloadError:(id)object { - std::stringstream ss; - ss << "File \"" << g_handler->GetLastDownloadFile() << - "\" failed to download."; - NSString* str = [NSString stringWithUTF8String:(ss.str().c_str())]; - [self alert:@"File Download" withMessage:str]; -} - // Called when we are activated (when we gain focus). - (void)windowDidBecomeKey:(NSNotification*)notification { if (g_handler.get()) { @@ -517,9 +489,6 @@ int main(int argc, char* argv[]) { if (exit_code >= 0) return exit_code; - // Retrieve the current working directory. - getcwd(szWorkingDir, sizeof(szWorkingDir)); - // Initialize the AutoRelease pool. NSAutoreleasePool* autopool = [[NSAutoreleasePool alloc] init]; @@ -575,6 +544,15 @@ int main(int argc, char* argv[]) { // Global functions std::string AppGetWorkingDirectory() { + char szWorkingDir[256]; + if (getcwd(szWorkingDir, sizeof(szWorkingDir) - 1) == NULL) { + szWorkingDir[0] = 0; + } else { + // Add trailing path separator. + size_t len = strlen(szWorkingDir); + szWorkingDir[len] = '/'; + szWorkingDir[len + 1] = 0; + } return szWorkingDir; } diff --git a/tests/cefclient/cefclient_win.cc b/tests/cefclient/cefclient_win.cc index 08f19271e..08023353c 100644 --- a/tests/cefclient/cefclient_win.cc +++ b/tests/cefclient/cefclient_win.cc @@ -51,7 +51,6 @@ HINSTANCE hInst; // current instance TCHAR szTitle[MAX_LOADSTRING]; // The title bar text TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name TCHAR szOSRWindowClass[MAX_LOADSTRING]; // the OSR window class name -char szWorkingDir[MAX_PATH]; // The current working directory UINT uFindMsg; // Message identifier for find events. HWND hFindDlg = NULL; // Handle for the find dialog. @@ -99,10 +98,6 @@ int APIENTRY wWinMain(HINSTANCE hInstance, if (exit_code >= 0) return exit_code; - // Retrieve the current working directory. - if (_getcwd(szWorkingDir, MAX_PATH) == NULL) - szWorkingDir[0] = 0; - // Parse command line arguments. The passed in values are ignored on Windows. AppInitCommandLine(0, NULL); @@ -214,46 +209,6 @@ BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { return TRUE; } -// Show a warning message on the UI thread. -static void ShowWarning(CefRefPtr browser, int id) { - if (!CefCurrentlyOn(TID_UI)) { - // Execute on the UI thread. - CefPostTask(TID_UI, base::Bind(&ShowWarning, browser, id)); - return; - } - - if (!g_handler) - return; - - std::wstring caption; - std::wstringstream message; - - switch (id) { - case ID_WARN_CONSOLEMESSAGE: - caption = L"Console Messages"; - message << L"Console messages will be written to " << - std::wstring(CefString(g_handler->GetLogFile())); - break; - case ID_WARN_DOWNLOADCOMPLETE: - case ID_WARN_DOWNLOADERROR: - caption = L"File Download"; - message << L"File \"" << - std::wstring(CefString(g_handler->GetLastDownloadFile())) << - L"\" "; - - if (id == ID_WARN_DOWNLOADCOMPLETE) - message << L"downloaded successfully."; - else - message << L"failed to download."; - break; - } - - MessageBox(g_handler->GetMainWindowHandle(), - message.str().c_str(), - caption.c_str(), - MB_OK | MB_ICONINFORMATION); -} - // Set focus to |browser| on the UI thread. static void SetFocusToBrowser(CefRefPtr browser) { if (!CefCurrentlyOn(TID_UI)) { @@ -472,11 +427,6 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, if (g_handler.get()) g_handler->CloseAllBrowsers(false); return 0; - case ID_WARN_CONSOLEMESSAGE: - case ID_WARN_DOWNLOADCOMPLETE: - case ID_WARN_DOWNLOADERROR: - ShowWarning(browser, wmId); - return 0; case ID_FIND: if (!hFindDlg) { // Create the find dialog. @@ -682,6 +632,15 @@ INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { // Global functions std::string AppGetWorkingDirectory() { + char szWorkingDir[MAX_PATH + 1]; + if (_getcwd(szWorkingDir, MAX_PATH) == NULL) { + szWorkingDir[0] = 0; + } else { + // Add trailing path separator. + size_t len = strlen(szWorkingDir); + szWorkingDir[len] = '\\'; + szWorkingDir[len + 1] = 0; + } return szWorkingDir; } diff --git a/tests/cefclient/client_handler.cc b/tests/cefclient/client_handler.cc index b36d54205..e538eddfc 100644 --- a/tests/cefclient/client_handler.cc +++ b/tests/cefclient/client_handler.cc @@ -25,6 +25,12 @@ #include "cefclient/resource_util.h" #include "cefclient/test_runner.h" +#if defined(OS_WIN) +#define NEWLINE "\r\n" +#else +#define NEWLINE "\n" +#endif + namespace { // Custom menu command Ids. @@ -52,11 +58,15 @@ ClientHandler::ClientHandler() forward_handle_(NULL), stop_handle_(NULL), reload_handle_(NULL), + console_log_file_(AppGetConsoleLogPath()), + first_console_message_(true), focus_on_editable_field_(false) { #if defined(OS_LINUX) gtk_dialog_ = NULL; #endif + DCHECK(!console_log_file_.empty()); + // Read command line settings. CefRefPtr command_line = CefCommandLine::GetGlobalCommandLine(); @@ -166,36 +176,21 @@ bool ClientHandler::OnConsoleMessage(CefRefPtr browser, int line) { CEF_REQUIRE_UI_THREAD(); - bool first_message; - std::string logFile; - - { - first_message = log_file_.empty(); - if (first_message) { - std::stringstream ss; - ss << AppGetWorkingDirectory(); -#if defined(OS_WIN) - ss << "\\"; -#else - ss << "/"; -#endif - ss << "console.log"; - log_file_ = ss.str(); - } - logFile = log_file_; - } - - FILE* file = fopen(logFile.c_str(), "a"); + FILE* file = fopen(console_log_file_.c_str(), "a"); if (file) { std::stringstream ss; - ss << "Message: " << std::string(message) << "\r\nSource: " << - std::string(source) << "\r\nLine: " << line << - "\r\n-----------------------\r\n"; + ss << "Message: " << message.ToString() << NEWLINE << + "Source: " << source.ToString() << NEWLINE << + "Line: " << line << NEWLINE << + "-----------------------" << NEWLINE; fputs(ss.str().c_str(), file); fclose(file); - if (first_message) - SendNotification(NOTIFY_CONSOLE_MESSAGE); + if (first_console_message_) { + client::test_runner::Alert( + browser, "Console messages written to \"" + console_log_file_ + "\""); + first_console_message_ = false; + } } return false; @@ -219,8 +214,10 @@ void ClientHandler::OnDownloadUpdated( CEF_REQUIRE_UI_THREAD(); if (download_item->IsComplete()) { - SetLastDownloadFile(download_item->GetFullPath()); - SendNotification(NOTIFY_DOWNLOAD_COMPLETE); + client::test_runner::Alert( + browser, + "File \"" + download_item->GetFullPath().ToString() + + "\" downloaded successfully."); } } @@ -699,21 +696,6 @@ bool ClientHandler::IsClosing() const { return is_closing_; } -std::string ClientHandler::GetLogFile() const { - CEF_REQUIRE_UI_THREAD(); - return log_file_; -} - -void ClientHandler::SetLastDownloadFile(const std::string& fileName) { - CEF_REQUIRE_UI_THREAD(); - last_download_file_ = fileName; -} - -std::string ClientHandler::GetLastDownloadFile() const { - CEF_REQUIRE_UI_THREAD(); - return last_download_file_; -} - void ClientHandler::ShowDevTools(CefRefPtr browser, const CefPoint& inspect_element_at) { CefWindowInfo windowInfo; diff --git a/tests/cefclient/client_handler.h b/tests/cefclient/client_handler.h index 306205b8f..dc08da430 100644 --- a/tests/cefclient/client_handler.h +++ b/tests/cefclient/client_handler.h @@ -271,20 +271,6 @@ class ClientHandler : public CefClient, // 'onbeforeunload' JavaScript events during window close. bool IsClosing() const; - std::string GetLogFile() const; - - void SetLastDownloadFile(const std::string& fileName); - std::string GetLastDownloadFile() const; - - // Send a notification to the application. Notifications should not block the - // caller. - enum NotificationType { - NOTIFY_CONSOLE_MESSAGE, - NOTIFY_DOWNLOAD_COMPLETE, - NOTIFY_DOWNLOAD_ERROR, - }; - void SendNotification(NotificationType type); - void ShowDevTools(CefRefPtr browser, const CefPoint& inspect_element_at); void CloseDevTools(CefRefPtr browser); @@ -358,11 +344,9 @@ class ClientHandler : public CefClient, // The handler for off-screen rendering, if any. CefRefPtr osr_handler_; - // Support for logging. - std::string log_file_; - - // Support for downloading files. - std::string last_download_file_; + // Used for console logging purposes. + const std::string console_log_file_; + bool first_console_message_; // True if an editable field currently has focus. bool focus_on_editable_field_; diff --git a/tests/cefclient/client_handler_gtk.cc b/tests/cefclient/client_handler_gtk.cc index 58aab23c7..2869961bd 100644 --- a/tests/cefclient/client_handler_gtk.cc +++ b/tests/cefclient/client_handler_gtk.cc @@ -456,10 +456,6 @@ void ClientHandler::OnDialogResponse(GtkDialog* dialog, handler->OnResetDialogState(NULL); } -void ClientHandler::SendNotification(NotificationType type) { - // TODO(port): Implement this method. -} - void ClientHandler::SetLoading(bool isLoading) { CEF_REQUIRE_UI_THREAD(); diff --git a/tests/cefclient/client_handler_mac.mm b/tests/cefclient/client_handler_mac.mm index 318ee808a..ccd2419cb 100644 --- a/tests/cefclient/client_handler_mac.mm +++ b/tests/cefclient/client_handler_mac.mm @@ -35,28 +35,6 @@ void ClientHandler::OnTitleChange(CefRefPtr browser, [window setTitle:str]; } -void ClientHandler::SendNotification(NotificationType type) { - SEL sel = nil; - switch(type) { - case NOTIFY_CONSOLE_MESSAGE: - sel = @selector(notifyConsoleMessage:); - break; - case NOTIFY_DOWNLOAD_COMPLETE: - sel = @selector(notifyDownloadComplete:); - break; - case NOTIFY_DOWNLOAD_ERROR: - sel = @selector(notifyDownloadError:); - break; - } - - if (sel == nil) - return; - - NSWindow* window = [AppGetMainWindowHandle() window]; - NSObject* delegate = [window delegate]; - [delegate performSelectorOnMainThread:sel withObject:nil waitUntilDone:NO]; -} - void ClientHandler::SetLoading(bool isLoading) { // TODO(port): Change button status. } diff --git a/tests/cefclient/client_handler_win.cc b/tests/cefclient/client_handler_win.cc index 9b0c6b342..769f6da31 100644 --- a/tests/cefclient/client_handler_win.cc +++ b/tests/cefclient/client_handler_win.cc @@ -35,24 +35,6 @@ void ClientHandler::OnTitleChange(CefRefPtr browser, SetWindowText(hwnd, std::wstring(title).c_str()); } -void ClientHandler::SendNotification(NotificationType type) { - UINT id; - switch (type) { - case NOTIFY_CONSOLE_MESSAGE: - id = ID_WARN_CONSOLEMESSAGE; - break; - case NOTIFY_DOWNLOAD_COMPLETE: - id = ID_WARN_DOWNLOADCOMPLETE; - break; - case NOTIFY_DOWNLOAD_ERROR: - id = ID_WARN_DOWNLOADERROR; - break; - default: - return; - } - PostMessage(main_handle_, WM_COMMAND, id, 0); -} - void ClientHandler::SetLoading(bool isLoading) { DCHECK(edit_handle_ != NULL && reload_handle_ != NULL && stop_handle_ != NULL); diff --git a/tests/cefclient/process_helper_mac.cc b/tests/cefclient/process_helper_mac.cc index 4ff9953c7..405c5aa2e 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 AppGetConsoleLogPath() { + return std::string(); +} std::string AppGetDownloadPath(const std::string& file_name) { return std::string(); } diff --git a/tests/cefclient/resource.h b/tests/cefclient/resource.h index 0d1b91769..a6b4e1150 100644 --- a/tests/cefclient/resource.h +++ b/tests/cefclient/resource.h @@ -22,9 +22,6 @@ #define IDC_NAV_FORWARD 201 #define IDC_NAV_RELOAD 202 #define IDC_NAV_STOP 203 -#define ID_WARN_CONSOLEMESSAGE 32000 -#define ID_WARN_DOWNLOADCOMPLETE 32001 -#define ID_WARN_DOWNLOADERROR 32002 #define ID_QUIT 32500 #define ID_FIND 32501 #define ID_TESTS_FIRST 32700