| 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/interactive_test_utils.h" |
| 6 | 6 |
| 7 #include "base/file_path.h" | 7 #include "base/file_path.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| 11 #include "base/stringprintf.h" | 11 #include "base/stringprintf.h" |
| 12 #include "base/time.h" | 12 #include "base/time.h" |
| 13 #include "chrome/browser/ui/window_snapshot/window_snapshot.h" | 13 #include "chrome/browser/ui/window_snapshot/window_snapshot.h" |
| 14 #include "ui/base/win/foreground_helper.h" | 14 #include "ui/base/win/foreground_helper.h" |
| 15 #include "ui/ui_controls/ui_controls.h" | 15 #include "ui/ui_controls/ui_controls.h" |
| 16 #include "ui/views/focus/focus_manager.h" | 16 #include "ui/views/focus/focus_manager.h" |
| 17 | 17 |
| 18 #if defined(USE_AURA) | 18 #if defined(USE_AURA) |
| 19 #include "chrome/browser/ui/host_desktop.h" | 19 #include "chrome/browser/ui/host_desktop.h" |
| 20 #include "chrome/browser/ui/views/test/ui_test_utils_aura.h" | 20 #include "chrome/test/base/interactive_test_utils_aura.h" |
| 21 #include "ui/aura/root_window.h" | 21 #include "ui/aura/root_window.h" |
| 22 #endif | 22 #endif |
| 23 | 23 |
| 24 namespace ui_test_utils { | 24 namespace ui_test_utils { |
| 25 | 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) { | 26 void HideNativeWindow(gfx::NativeWindow window) { |
| 56 #if defined(USE_AURA) | 27 #if defined(USE_AURA) |
| 57 if (chrome::GetHostDesktopTypeForNativeWindow(window) == | 28 if (chrome::GetHostDesktopTypeForNativeWindow(window) == |
| 58 chrome::HOST_DESKTOP_TYPE_ASH) { | 29 chrome::HOST_DESKTOP_TYPE_ASH) { |
| 59 HideNativeWindowAura(window); | 30 HideNativeWindowAura(window); |
| 60 return; | 31 return; |
| 61 } | 32 } |
| 62 HWND hwnd = window->GetRootWindow()->GetAcceleratedWidget(); | 33 HWND hwnd = window->GetRootWindow()->GetAcceleratedWidget(); |
| 63 #else | 34 #else |
| 64 HWND hwnd = window; | 35 HWND hwnd = window; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 83 VLOG(1) << "Forcefully refocusing front window"; | 54 VLOG(1) << "Forcefully refocusing front window"; |
| 84 ui::ForegroundHelper::SetForeground(hwnd); | 55 ui::ForegroundHelper::SetForeground(hwnd); |
| 85 } | 56 } |
| 86 | 57 |
| 87 // ShowWindow does not necessarily activate the window. In particular if a | 58 // ShowWindow does not necessarily activate the window. In particular if a |
| 88 // window from another app is the foreground window then the request to | 59 // window from another app is the foreground window then the request to |
| 89 // activate the window fails. See SetForegroundWindow for details. | 60 // activate the window fails. See SetForegroundWindow for details. |
| 90 return GetForegroundWindow() == hwnd; | 61 return GetForegroundWindow() == hwnd; |
| 91 } | 62 } |
| 92 | 63 |
| 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 | 64 } // namespace ui_test_utils |
| OLD | NEW |