1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/test/base/interactive_test_utils.h"
#include "base/logging.h"
#include "base/memory/scoped_refptr.h"
#include "base/task/current_thread.h"
#include "build/build_config.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "content/public/test/test_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/test/ui_controls.h"
#include "ui/display/display.h"
#include "ui/display/screen.h"
#include "ui/events/keycodes/keyboard_codes.h"
namespace ui_test_utils {
namespace {
bool GetNativeWindow(const Browser* browser, gfx::NativeWindow* native_window) {
BrowserWindow* window = browser->window();
if (!window) {
return false;
}
*native_window = window->GetNativeWindow();
return !!(*native_window);
}
} // namespace
BrowserActivationWaiter::BrowserActivationWaiter(const Browser* browser) {
// When the active browser closes, the next "last active browser" in the
// BrowserList might not be immediately activated. So we need to wait for the
// "last active browser" to actually be active.
if (chrome::FindLastActive() == browser && browser->window()->IsActive()) {
observed_ = true;
return;
}
BrowserView* browser_view = BrowserView::GetBrowserViewForBrowser(browser);
browser_view->frame()->AddObserver(this);
}
BrowserActivationWaiter::~BrowserActivationWaiter() = default;
void BrowserActivationWaiter::WaitForActivation() {
if (observed_) {
return;
}
DCHECK(!run_loop_.running()) << "WaitForActivation() can be called at most "
"once. Construct a new "
"BrowserActivationWaiter instead.";
run_loop_.Run();
}
void BrowserActivationWaiter::OnWidgetActivationChanged(views::Widget* widget,
bool active) {
if (!active) {
return;
}
observed_ = true;
widget->RemoveObserver(this);
if (run_loop_.running()) {
run_loop_.Quit();
}
}
BrowserDeactivationWaiter::BrowserDeactivationWaiter(const Browser* browser)
: browser_(browser->AsWeakPtr()) {
if (chrome::FindLastActive() != browser && !browser->window()->IsActive()) {
observed_ = true;
return;
}
BrowserList::AddObserver(this);
}
BrowserDeactivationWaiter::~BrowserDeactivationWaiter() = default;
void BrowserDeactivationWaiter::WaitForDeactivation() {
if (observed_) {
return;
}
DCHECK(!run_loop_.running()) << "WaitForDeactivation() can be called at most "
"once. Construct a new "
"BrowserDeactivationWaiter instead.";
run_loop_.Run();
}
void BrowserDeactivationWaiter::OnBrowserNoLongerActive(Browser* browser) {
if (browser != browser_.get()) {
return;
}
observed_ = true;
BrowserList::RemoveObserver(this);
if (run_loop_.running()) {
run_loop_.Quit();
}
}
bool BringBrowserWindowToFront(const Browser* browser) {
BrowserActivationWaiter waiter(browser);
gfx::NativeWindow window = gfx::NativeWindow();
if (!GetNativeWindow(browser, &window)) {
return false;
}
if (!ShowAndFocusNativeWindow(window)) {
return false;
}
waiter.WaitForActivation();
return true;
}
bool SendKeyPressSync(const Browser* browser,
ui::KeyboardCode key,
bool control,
bool shift,
bool alt,
bool command,
ui_controls::KeyEventType wait_for) {
gfx::NativeWindow window = gfx::NativeWindow();
if (!GetNativeWindow(browser, &window)) {
return false;
}
return SendKeyPressToWindowSync(window, key, control, shift, alt, command,
wait_for);
}
bool SendKeyPressToWindowSync(const gfx::NativeWindow window,
ui::KeyboardCode key,
bool control,
bool shift,
bool alt,
bool command,
ui_controls::KeyEventType wait_for) {
CHECK(wait_for == ui_controls::KeyEventType::kKeyPress ||
wait_for == ui_controls::KeyEventType::kKeyRelease);
#if BUILDFLAG(IS_WIN)
DCHECK(key != ui::VKEY_ESCAPE || !control)
<< "'ctrl + esc' opens start menu on Windows. Start menu on windows "
"2012 is a full-screen always on top window. It breaks all "
"interactive tests.";
#endif
base::RunLoop run_loop(base::RunLoop::Type::kNestableTasksAllowed);
bool result = ui_controls::SendKeyPressNotifyWhenDone(
window, key, control, shift, alt, command, run_loop.QuitClosure(),
wait_for);
#if BUILDFLAG(IS_WIN)
if (!result && ui_test_utils::ShowAndFocusNativeWindow(window)) {
result = ui_controls::SendKeyPressNotifyWhenDone(
window, key, control, shift, alt, command, run_loop.QuitClosure(),
wait_for);
}
#endif
if (!result) {
LOG(ERROR) << "ui_controls::SendKeyPressNotifyWhenDone failed";
return false;
}
// Run the message loop. It'll stop running when either the key was received
// or the test timed out (in which case testing::Test::HasFatalFailure should
// be set).
run_loop.Run();
return !testing::Test::HasFatalFailure();
}
bool SendMouseMoveSync(const gfx::Point& location,
gfx::NativeWindow window_hint) {
scoped_refptr<content::MessageLoopRunner> runner =
new content::MessageLoopRunner;
if (!ui_controls::SendMouseMoveNotifyWhenDone(
location.x(), location.y(), runner->QuitClosure(), window_hint)) {
return false;
}
runner->Run();
return !testing::Test::HasFatalFailure();
}
bool SendMouseEventsSync(ui_controls::MouseButton type,
int button_state,
gfx::NativeWindow window_hint) {
scoped_refptr<content::MessageLoopRunner> runner =
new content::MessageLoopRunner;
if (!ui_controls::SendMouseEventsNotifyWhenDone(
type, button_state, runner->QuitClosure(),
ui_controls::kNoAccelerator, window_hint)) {
return false;
}
runner->Run();
return !testing::Test::HasFatalFailure();
}
namespace internal {
void ClickTask(ui_controls::MouseButton button,
int button_state,
base::OnceClosure followup,
int accelerator_state) {
if (!followup.is_null()) {
ui_controls::SendMouseEventsNotifyWhenDone(
button, button_state, std::move(followup), accelerator_state);
} else {
ui_controls::SendMouseEvents(button, button_state, accelerator_state);
}
}
} // namespace internal
display::Display GetSecondaryDisplay(display::Screen* screen) {
for (const auto& iter : screen->GetAllDisplays()) {
if (iter.id() != screen->GetPrimaryDisplay().id()) {
return iter;
}
}
NOTREACHED();
}
std::pair<display::Display, display::Display> GetDisplays(
display::Screen* screen) {
return std::make_pair(screen->GetPrimaryDisplay(),
GetSecondaryDisplay(screen));
}
} // namespace ui_test_utils
|