| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/test/base/ui_test_utils.h" | 5 #include "chrome/test/base/ui_test_utils.h" |
| 6 | 6 |
| 7 #include "base/file_path.h" |
| 8 #include "base/file_util.h" |
| 7 #include "base/logging.h" | 9 #include "base/logging.h" |
| 8 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| 11 #include "base/path_service.h" |
| 12 #include "base/stringprintf.h" |
| 13 #include "base/time.h" |
| 9 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
| 10 #include "chrome/browser/ui/browser_window.h" | 15 #include "chrome/browser/ui/browser_window.h" |
| 11 #include "chrome/browser/ui/views/frame/browser_view.h" | 16 #include "chrome/browser/ui/views/frame/browser_view.h" |
| 17 #include "chrome/browser/ui/window_snapshot/window_snapshot.h" |
| 18 #include "chrome/common/chrome_paths.h" |
| 12 #include "ui/base/win/foreground_helper.h" | 19 #include "ui/base/win/foreground_helper.h" |
| 13 #include "ui/ui_controls/ui_controls.h" | 20 #include "ui/ui_controls/ui_controls.h" |
| 14 #include "ui/views/focus/focus_manager.h" | 21 #include "ui/views/focus/focus_manager.h" |
| 15 | 22 |
| 16 namespace ui_test_utils { | 23 namespace ui_test_utils { |
| 17 | 24 |
| 25 namespace { |
| 26 |
| 27 const char kSnapshotBaseName[] = "ChromiumSnapshot"; |
| 28 const char kSnapshotExtension[] = ".png"; |
| 29 |
| 30 FilePath GetSnapshotFileName(const FilePath& snapshot_directory) { |
| 31 base::Time::Exploded the_time; |
| 32 |
| 33 base::Time::Now().LocalExplode(&the_time); |
| 34 std::string filename(StringPrintf("%s%04d%02d%02d%02d%02d%02d%s", |
| 35 kSnapshotBaseName, the_time.year, the_time.month, the_time.day_of_month, |
| 36 the_time.hour, the_time.minute, the_time.second, kSnapshotExtension)); |
| 37 |
| 38 FilePath snapshot_file = snapshot_directory.AppendASCII(filename); |
| 39 if (file_util::PathExists(snapshot_file)) { |
| 40 int index = 0; |
| 41 std::string suffix; |
| 42 FilePath trial_file; |
| 43 do { |
| 44 suffix = StringPrintf(" (%d)", ++index); |
| 45 trial_file = snapshot_file.InsertBeforeExtensionASCII(suffix); |
| 46 } while (file_util::PathExists(trial_file)); |
| 47 snapshot_file = trial_file; |
| 48 } |
| 49 return snapshot_file; |
| 50 } |
| 51 |
| 52 } // namespace |
| 53 |
| 18 bool IsViewFocused(const Browser* browser, ViewID vid) { | 54 bool IsViewFocused(const Browser* browser, ViewID vid) { |
| 19 BrowserWindow* browser_window = browser->window(); | 55 BrowserWindow* browser_window = browser->window(); |
| 20 DCHECK(browser_window); | 56 DCHECK(browser_window); |
| 21 gfx::NativeWindow window = browser_window->GetNativeHandle(); | 57 gfx::NativeWindow window = browser_window->GetNativeHandle(); |
| 22 DCHECK(window); | 58 DCHECK(window); |
| 23 const views::Widget* widget = | 59 const views::Widget* widget = |
| 24 views::Widget::GetTopLevelWidgetForNativeView(window); | 60 views::Widget::GetTopLevelWidgetForNativeView(window); |
| 25 DCHECK(widget); | 61 DCHECK(widget); |
| 26 const views::FocusManager* focus_manager = widget->GetFocusManager(); | 62 const views::FocusManager* focus_manager = widget->GetFocusManager(); |
| 27 DCHECK(focus_manager); | 63 DCHECK(focus_manager); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 int state, | 105 int state, |
| 70 const base::Closure& task) { | 106 const base::Closure& task) { |
| 71 DCHECK(view); | 107 DCHECK(view); |
| 72 DCHECK(view->GetWidget()); | 108 DCHECK(view->GetWidget()); |
| 73 gfx::Point view_center(view->width() / 2, view->height() / 2); | 109 gfx::Point view_center(view->width() / 2, view->height() / 2); |
| 74 views::View::ConvertPointToScreen(view, &view_center); | 110 views::View::ConvertPointToScreen(view, &view_center); |
| 75 ui_controls::SendMouseMove(view_center.x(), view_center.y()); | 111 ui_controls::SendMouseMove(view_center.x(), view_center.y()); |
| 76 ui_controls::SendMouseEventsNotifyWhenDone(button, state, task); | 112 ui_controls::SendMouseEventsNotifyWhenDone(button, state, task); |
| 77 } | 113 } |
| 78 | 114 |
| 115 bool SaveScreenSnapshotToDirectory(const FilePath& directory, |
| 116 FilePath* screenshot_path) { |
| 117 bool succeeded = false; |
| 118 FilePath out_path(GetSnapshotFileName(directory)); |
| 119 |
| 120 MONITORINFO monitor_info = {}; |
| 121 monitor_info.cbSize = sizeof(monitor_info); |
| 122 HMONITOR main_monitor = MonitorFromWindow(NULL, MONITOR_DEFAULTTOPRIMARY); |
| 123 if (GetMonitorInfo(main_monitor, &monitor_info)) { |
| 124 RECT& rect = monitor_info.rcMonitor; |
| 125 |
| 126 std::vector<unsigned char> png_data; |
| 127 if (browser::GrabWindowSnapshot(NULL, &png_data, |
| 128 gfx::Rect(rect.right - rect.left, |
| 129 rect.bottom - rect.top)) && |
| 130 png_data.size() <= INT_MAX) { |
| 131 int bytes = static_cast<int>(png_data.size()); |
| 132 int written = file_util::WriteFile( |
| 133 out_path, reinterpret_cast<char*>(&png_data[0]), bytes); |
| 134 succeeded = (written == bytes); |
| 135 } |
| 136 } |
| 137 |
| 138 if (succeeded && screenshot_path != NULL) |
| 139 *screenshot_path = out_path; |
| 140 |
| 141 return succeeded; |
| 142 } |
| 143 |
| 144 bool SaveScreenSnapshotToDesktop(FilePath* screenshot_path) { |
| 145 FilePath desktop; |
| 146 |
| 147 return PathService::Get(chrome::DIR_USER_DESKTOP, &desktop) && |
| 148 SaveScreenSnapshotToDirectory(desktop, screenshot_path); |
| 149 } |
| 150 |
| 79 } // namespace ui_test_utils | 151 } // namespace ui_test_utils |
| OLD | NEW |