Make hitting "Enter" submit the add/change profile dialog.
[chromium-blink-merge.git] / chrome / test / base / interactive_test_utils.cc
blob8029ab6b4bd89cc74085d8fb21a182de3ee0c03f
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.
5 #include "chrome/test/base/interactive_test_utils.h"
7 #include "chrome/browser/ui/browser.h"
8 #include "chrome/browser/ui/browser_window.h"
10 namespace ui_test_utils {
12 namespace {
14 bool GetNativeWindow(const Browser* browser, gfx::NativeWindow* native_window) {
15 BrowserWindow* window = browser->window();
16 if (!window)
17 return false;
19 *native_window = window->GetNativeWindow();
20 return *native_window;
23 } // namespace
25 bool BringBrowserWindowToFront(const Browser* browser) {
26 gfx::NativeWindow window = NULL;
27 if (!GetNativeWindow(browser, &window))
28 return false;
30 return ui_test_utils::ShowAndFocusNativeWindow(window);
33 bool SendKeyPressSync(const Browser* browser,
34 ui::KeyboardCode key,
35 bool control,
36 bool shift,
37 bool alt,
38 bool command) {
39 gfx::NativeWindow window = NULL;
40 if (!GetNativeWindow(browser, &window))
41 return false;
42 scoped_refptr<content::MessageLoopRunner> runner =
43 new content::MessageLoopRunner;
44 bool result;
45 result = ui_controls::SendKeyPressNotifyWhenDone(
46 window, key, control, shift, alt, command, runner->QuitClosure());
47 #if defined(OS_WIN)
48 if (!result && BringBrowserWindowToFront(browser)) {
49 result = ui_controls::SendKeyPressNotifyWhenDone(
50 window, key, control, shift, alt, command, runner->QuitClosure());
52 #endif
53 if (!result) {
54 LOG(ERROR) << "ui_controls::SendKeyPressNotifyWhenDone failed";
55 return false;
58 // Run the message loop. It'll stop running when either the key was received
59 // or the test timed out (in which case testing::Test::HasFatalFailure should
60 // be set).
61 runner->Run();
62 return !testing::Test::HasFatalFailure();
65 bool SendKeyPressAndWait(const Browser* browser,
66 ui::KeyboardCode key,
67 bool control,
68 bool shift,
69 bool alt,
70 bool command,
71 int type,
72 const content::NotificationSource& source) {
73 content::WindowedNotificationObserver observer(type, source);
75 if (!SendKeyPressSync(browser, key, control, shift, alt, command))
76 return false;
78 observer.Wait();
79 return !testing::Test::HasFatalFailure();
82 bool SendMouseMoveSync(const gfx::Point& location) {
83 scoped_refptr<content::MessageLoopRunner> runner =
84 new content::MessageLoopRunner;
85 if (!ui_controls::SendMouseMoveNotifyWhenDone(
86 location.x(), location.y(), runner->QuitClosure())) {
87 return false;
89 runner->Run();
90 return !testing::Test::HasFatalFailure();
93 bool SendMouseEventsSync(ui_controls::MouseButton type, int state) {
94 scoped_refptr<content::MessageLoopRunner> runner =
95 new content::MessageLoopRunner;
96 if (!ui_controls::SendMouseEventsNotifyWhenDone(
97 type, state, runner->QuitClosure())) {
98 return false;
100 runner->Run();
101 return !testing::Test::HasFatalFailure();
105 } // namespace ui_test_utils