Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(478)

Side by Side Diff: chrome/browser/ui/views/test/ui_test_utils_win.cc

Issue 11414223: Move the test functions that deal with focus to interactive_ui_tets_utils.h and into the interactiv… (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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/path_service.h"
11 #include "base/stringprintf.h"
12 #include "base/time.h"
13 #include "chrome/browser/ui/window_snapshot/window_snapshot.h"
14 #include "ui/base/win/foreground_helper.h"
15 #include "ui/ui_controls/ui_controls.h"
16 #include "ui/views/focus/focus_manager.h"
17
18 #if defined(USE_AURA)
19 #include "chrome/browser/ui/host_desktop.h"
20 #include "chrome/browser/ui/views/test/ui_test_utils_aura.h"
21 #include "ui/aura/root_window.h"
22 #endif
23
24 namespace ui_test_utils {
25
26 namespace {
27
28 const char kSnapshotBaseName[] = "ChromiumSnapshot";
29 const char kSnapshotExtension[] = ".png";
30
31 FilePath GetSnapshotFileName(const FilePath& snapshot_directory) {
32 base::Time::Exploded the_time;
33
34 base::Time::Now().LocalExplode(&the_time);
35 std::string filename(StringPrintf("%s%04d%02d%02d%02d%02d%02d%s",
36 kSnapshotBaseName, the_time.year, the_time.month, the_time.day_of_month,
37 the_time.hour, the_time.minute, the_time.second, kSnapshotExtension));
38
39 FilePath snapshot_file = snapshot_directory.AppendASCII(filename);
40 if (file_util::PathExists(snapshot_file)) {
41 int index = 0;
42 std::string suffix;
43 FilePath trial_file;
44 do {
45 suffix = StringPrintf(" (%d)", ++index);
46 trial_file = snapshot_file.InsertBeforeExtensionASCII(suffix);
47 } while (file_util::PathExists(trial_file));
48 snapshot_file = trial_file;
49 }
50 return snapshot_file;
51 }
52
53 } // namespace
54
55 void HideNativeWindow(gfx::NativeWindow window) {
56 #if defined(USE_AURA)
57 if (chrome::GetHostDesktopTypeForNativeWindow(window) ==
58 chrome::HOST_DESKTOP_TYPE_ASH) {
59 HideNativeWindowAura(window);
60 return;
61 }
62 HWND hwnd = window->GetRootWindow()->GetAcceleratedWidget();
63 #else
64 HWND hwnd = window;
65 #endif
66 ::ShowWindow(hwnd, SW_HIDE);
67 }
68
69 bool ShowAndFocusNativeWindow(gfx::NativeWindow window) {
70 #if defined(USE_AURA)
71 if (chrome::GetHostDesktopTypeForNativeWindow(window) ==
72 chrome::HOST_DESKTOP_TYPE_ASH)
73 ShowAndFocusNativeWindowAura(window);
74 // Always make sure the window hosting ash is visible and focused.
75 HWND hwnd = window->GetRootWindow()->GetAcceleratedWidget();
76 #else
77 HWND hwnd = window;
78 #endif
79
80 ::ShowWindow(hwnd, SW_SHOW);
81
82 if (GetForegroundWindow() != hwnd) {
83 VLOG(1) << "Forcefully refocusing front window";
84 ui::ForegroundHelper::SetForeground(hwnd);
85 }
86
87 // ShowWindow does not necessarily activate the window. In particular if a
88 // window from another app is the foreground window then the request to
89 // activate the window fails. See SetForegroundWindow for details.
90 return GetForegroundWindow() == hwnd;
91 }
92
93 bool SaveScreenSnapshotToDirectory(const FilePath& directory,
94 FilePath* screenshot_path) {
95 bool succeeded = false;
96 FilePath out_path(GetSnapshotFileName(directory));
97
98 MONITORINFO monitor_info = {};
99 monitor_info.cbSize = sizeof(monitor_info);
100 HMONITOR main_monitor = MonitorFromWindow(NULL, MONITOR_DEFAULTTOPRIMARY);
101 if (GetMonitorInfo(main_monitor, &monitor_info)) {
102 RECT& rect = monitor_info.rcMonitor;
103
104 std::vector<unsigned char> png_data;
105 gfx::Rect bounds(
106 gfx::Size(rect.right - rect.left, rect.bottom - rect.top));
107 if (chrome::internal::GrabWindowSnapshot(NULL, &png_data, bounds) &&
108 png_data.size() <= INT_MAX) {
109 int bytes = static_cast<int>(png_data.size());
110 int written = file_util::WriteFile(
111 out_path, reinterpret_cast<char*>(&png_data[0]), bytes);
112 succeeded = (written == bytes);
113 }
114 }
115
116 if (succeeded && screenshot_path != NULL)
117 *screenshot_path = out_path;
118
119 return succeeded;
120 }
121
122 bool SaveScreenSnapshotToDesktop(FilePath* screenshot_path) {
123 FilePath desktop;
124
125 return PathService::Get(base::DIR_USER_DESKTOP, &desktop) &&
126 SaveScreenSnapshotToDirectory(desktop, screenshot_path);
127 }
128
129 } // namespace ui_test_utils
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698