OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/test/base/ui_test_utils.h" | |
6 | |
7 #include "base/file_path.h" | |
8 #include "base/file_util.h" | |
9 #include "base/logging.h" | |
10 #include "base/message_loop.h" | |
11 #include "base/path_service.h" | |
12 #include "base/stringprintf.h" | |
13 #include "base/time.h" | |
14 #include "chrome/browser/ui/browser.h" | |
15 #include "chrome/browser/ui/browser_window.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" | |
19 #include "ui/base/win/foreground_helper.h" | |
20 #include "ui/ui_controls/ui_controls.h" | |
21 #include "ui/views/focus/focus_manager.h" | |
22 | |
23 namespace ui_test_utils { | |
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 | |
54 bool IsViewFocused(const Browser* browser, ViewID vid) { | |
55 BrowserWindow* browser_window = browser->window(); | |
56 DCHECK(browser_window); | |
57 gfx::NativeWindow window = browser_window->GetNativeWindow(); | |
58 DCHECK(window); | |
59 const views::Widget* widget = | |
60 views::Widget::GetTopLevelWidgetForNativeView(window); | |
61 DCHECK(widget); | |
62 const views::FocusManager* focus_manager = widget->GetFocusManager(); | |
63 DCHECK(focus_manager); | |
64 return focus_manager->GetFocusedView()->id() == vid; | |
65 } | |
66 | |
67 void ClickOnView(const Browser* browser, ViewID vid) { | |
68 BrowserWindow* browser_window = browser->window(); | |
69 DCHECK(browser_window); | |
70 views::View* view = | |
71 reinterpret_cast<BrowserView*>(browser_window)->GetViewByID(vid); | |
72 DCHECK(view); | |
73 MoveMouseToCenterAndPress( | |
74 view, | |
75 ui_controls::LEFT, | |
76 ui_controls::DOWN | ui_controls::UP, | |
77 MessageLoop::QuitClosure()); | |
78 content::RunMessageLoop(); | |
79 } | |
80 | |
81 void HideNativeWindow(gfx::NativeWindow window) { | |
82 // TODO(jcampan): retrieve the NativeWidgetWin and show/hide on it instead of | |
83 // using Windows API. | |
84 ::ShowWindow(window, SW_HIDE); | |
85 } | |
86 | |
87 bool ShowAndFocusNativeWindow(gfx::NativeWindow window) { | |
88 // TODO(jcampan): retrieve the NativeWidgetWin and show/hide on it instead of | |
89 // using Windows API. | |
90 ::ShowWindow(window, SW_SHOW); | |
91 | |
92 if (GetForegroundWindow() != window) { | |
93 VLOG(1) << "Forcefully refocusing front window"; | |
94 ui::ForegroundHelper::SetForeground(window); | |
95 } | |
96 | |
97 // ShowWindow does not necessarily activate the window. In particular if a | |
98 // window from another app is the foreground window then the request to | |
99 // activate the window fails. See SetForegroundWindow for details. | |
100 return GetForegroundWindow() == window; | |
101 } | |
102 | |
103 void MoveMouseToCenterAndPress(views::View* view, | |
104 ui_controls::MouseButton button, | |
105 int state, | |
106 const base::Closure& task) { | |
107 DCHECK(view); | |
108 DCHECK(view->GetWidget()); | |
109 gfx::Point view_center(view->width() / 2, view->height() / 2); | |
110 views::View::ConvertPointToScreen(view, &view_center); | |
111 ui_controls::SendMouseMove(view_center.x(), view_center.y()); | |
112 ui_controls::SendMouseEventsNotifyWhenDone(button, state, task); | |
113 } | |
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 (chrome::internal::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 | |
151 } // namespace ui_test_utils | |
OLD | NEW |