Unregister from GCM when the only GCM app is removed
[chromium-blink-merge.git] / chrome / test / base / interactive_test_utils.cc
blob2241bbafe6e1f70200dc9f797ce7a2cfa81a727d
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 return SendKeyPressToWindowSync(window, key, control, shift, alt, command);
45 bool SendKeyPressToWindowSync(const gfx::NativeWindow window,
46 ui::KeyboardCode key,
47 bool control,
48 bool shift,
49 bool alt,
50 bool command) {
51 scoped_refptr<content::MessageLoopRunner> runner =
52 new content::MessageLoopRunner;
53 bool result;
54 result = ui_controls::SendKeyPressNotifyWhenDone(
55 window, key, control, shift, alt, command, runner->QuitClosure());
56 #if defined(OS_WIN)
57 if (!result && ui_test_utils::ShowAndFocusNativeWindow(window)) {
58 result = ui_controls::SendKeyPressNotifyWhenDone(
59 window, key, control, shift, alt, command, runner->QuitClosure());
61 #endif
62 if (!result) {
63 LOG(ERROR) << "ui_controls::SendKeyPressNotifyWhenDone failed";
64 return false;
67 // Run the message loop. It'll stop running when either the key was received
68 // or the test timed out (in which case testing::Test::HasFatalFailure should
69 // be set).
70 runner->Run();
71 return !testing::Test::HasFatalFailure();
74 bool SendKeyPressAndWait(const Browser* browser,
75 ui::KeyboardCode key,
76 bool control,
77 bool shift,
78 bool alt,
79 bool command,
80 int type,
81 const content::NotificationSource& source) {
82 content::WindowedNotificationObserver observer(type, source);
84 if (!SendKeyPressSync(browser, key, control, shift, alt, command))
85 return false;
87 observer.Wait();
88 return !testing::Test::HasFatalFailure();
91 bool SendMouseMoveSync(const gfx::Point& location) {
92 scoped_refptr<content::MessageLoopRunner> runner =
93 new content::MessageLoopRunner;
94 if (!ui_controls::SendMouseMoveNotifyWhenDone(
95 location.x(), location.y(), runner->QuitClosure())) {
96 return false;
98 runner->Run();
99 return !testing::Test::HasFatalFailure();
102 bool SendMouseEventsSync(ui_controls::MouseButton type, int state) {
103 scoped_refptr<content::MessageLoopRunner> runner =
104 new content::MessageLoopRunner;
105 if (!ui_controls::SendMouseEventsNotifyWhenDone(
106 type, state, runner->QuitClosure())) {
107 return false;
109 runner->Run();
110 return !testing::Test::HasFatalFailure();
113 namespace internal {
115 void ClickTask(ui_controls::MouseButton button,
116 int state,
117 const base::Closure& followup) {
118 if (!followup.is_null())
119 ui_controls::SendMouseEventsNotifyWhenDone(button, state, followup);
120 else
121 ui_controls::SendMouseEvents(button, state);
124 } // namespace internal
126 } // namespace ui_test_utils