summaryrefslogtreecommitdiffstats
path: root/chrome/browser/automation/testing_automation_provider_views.cc
blob: 6048cd8191ba053c07d59d539faa626ffa80588b (plain)
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
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/automation/testing_automation_provider.h"

#include "chrome/browser/automation/automation_browser_tracker.h"
#include "chrome/browser/automation/automation_window_tracker.h"
#include "chrome/browser/browser_window.h"
#include "chrome/browser/views/frame/browser_view.h"
#include "chrome/browser/views/toolbar_view.h"
#include "chrome/test/automation/automation_messages.h"
#include "gfx/point.h"
#include "views/controls/menu/menu_wrapper.h"
#include "views/view.h"
#include "views/widget/root_view.h"
#include "views/widget/widget.h"

namespace {

// Helper class that waits until the focus has changed to a view other
// than the one with the provided view id.
class ViewFocusChangeWaiter : public views::FocusChangeListener {
 public:
  ViewFocusChangeWaiter(views::FocusManager* focus_manager,
                        int previous_view_id,
                        AutomationProvider* automation,
                        IPC::Message* reply_message)
      : focus_manager_(focus_manager),
        previous_view_id_(previous_view_id),
        automation_(automation),
        reply_message_(reply_message),
        ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) {
    focus_manager_->AddFocusChangeListener(this);
    // Call the focus change notification once in case the focus has
    // already changed.
    FocusWillChange(NULL, focus_manager_->GetFocusedView());
  }

  ~ViewFocusChangeWaiter() {
    focus_manager_->RemoveFocusChangeListener(this);
  }

  // Inherited from FocusChangeListener
  virtual void FocusWillChange(views::View* focused_before,
                               views::View* focused_now) {
    // This listener is called before focus actually changes. Post a task
    // that will get run after focus changes.
    MessageLoop::current()->PostTask(
        FROM_HERE,
        method_factory_.NewRunnableMethod(
            &ViewFocusChangeWaiter::FocusChanged,
            focused_before,
            focused_now));
  }

 private:
  void FocusChanged(views::View* focused_before,
                    views::View* focused_now) {
    if (focused_now && focused_now->GetID() != previous_view_id_) {
      AutomationMsg_WaitForFocusedViewIDToChange::WriteReplyParams(
          reply_message_, true, focused_now->GetID());

      automation_->Send(reply_message_);
      delete this;
    }
  }

  views::FocusManager* focus_manager_;
  int previous_view_id_;
  AutomationProvider* automation_;
  IPC::Message* reply_message_;
  ScopedRunnableMethodFactory<ViewFocusChangeWaiter> method_factory_;

  DISALLOW_COPY_AND_ASSIGN(ViewFocusChangeWaiter);
};

}  // namespace

class TestingAutomationProvider::PopupMenuWaiter : public views::MenuListener {
 public:
  PopupMenuWaiter(ToolbarView* toolbar_view,
                  TestingAutomationProvider* automation)
      : toolbar_view_(toolbar_view),
        automation_(automation),
        reply_message_(NULL) {
    toolbar_view_->AddMenuListener(this);
  }

  // Implementation of views::MenuListener
  virtual void OnMenuOpened() {
    toolbar_view_->RemoveMenuListener(this);
    automation_->popup_menu_opened_ = true;
    automation_->popup_menu_waiter_ = NULL;
    if (reply_message_) {
      AutomationMsg_WaitForPopupMenuToOpen::WriteReplyParams(
          reply_message_, true);
      automation_->Send(reply_message_);
    }
    delete this;
  }

  void set_reply_message(IPC::Message* reply_message) {
    reply_message_ = reply_message;
  }

 private:
  ToolbarView* toolbar_view_;
  TestingAutomationProvider* automation_;
  IPC::Message* reply_message_;

  DISALLOW_COPY_AND_ASSIGN(PopupMenuWaiter);
};

void TestingAutomationProvider::WindowGetViewBounds(int handle,
                                                    int view_id,
                                                    bool screen_coordinates,
                                                    bool* success,
                                                    gfx::Rect* bounds) {
  *success = false;

  if (window_tracker_->ContainsHandle(handle)) {
    gfx::NativeWindow window = window_tracker_->GetResource(handle);
    views::RootView* root_view = views::Widget::FindRootView(window);
    if (root_view) {
      views::View* view = root_view->GetViewByID(view_id);
      if (view) {
        *success = true;
        gfx::Point point;
        if (screen_coordinates)
          views::View::ConvertPointToScreen(view, &point);
        else
          views::View::ConvertPointToView(view, root_view, &point);
        *bounds = view->GetLocalBounds(false);
        bounds->set_origin(point);
      }
    }
  }
}

void TestingAutomationProvider::GetFocusedViewID(int handle, int* view_id) {
  *view_id = -1;
  if (window_tracker_->ContainsHandle(handle)) {
    gfx::NativeWindow window = window_tracker_->GetResource(handle);
    views::FocusManager* focus_manager =
        views::FocusManager::GetFocusManagerForNativeWindow(window);
    DCHECK(focus_manager);
    views::View* focused_view = focus_manager->GetFocusedView();
    if (focused_view)
      *view_id = focused_view->GetID();
  }
}

void TestingAutomationProvider::WaitForFocusedViewIDToChange(
    int handle, int previous_view_id, IPC::Message* reply_message) {
  if (!window_tracker_->ContainsHandle(handle))
    return;
  gfx::NativeWindow window = window_tracker_->GetResource(handle);
  views::FocusManager* focus_manager =
      views::FocusManager::GetFocusManagerForNativeWindow(window);

  // The waiter will respond to the IPC and delete itself when done.
  new ViewFocusChangeWaiter(focus_manager,
                            previous_view_id,
                            this,
                            reply_message);
}

void TestingAutomationProvider::StartTrackingPopupMenus(
    int browser_handle, bool* success) {
  if (browser_tracker_->ContainsHandle(browser_handle)) {
    Browser* browser = browser_tracker_->GetResource(browser_handle);
    BrowserView* browser_view = reinterpret_cast<BrowserView*>(
        browser->window());
    ToolbarView* toolbar_view = browser_view->GetToolbarView();
    popup_menu_opened_ = false;
    popup_menu_waiter_ = new PopupMenuWaiter(toolbar_view, this);
    *success = true;
  }
}

void TestingAutomationProvider::WaitForPopupMenuToOpen(
    IPC::Message* reply_message) {
  // See if the menu already opened and return true if so.
  if (popup_menu_opened_) {
    AutomationMsg_WaitForPopupMenuToOpen::WriteReplyParams(
          reply_message, true);
    Send(reply_message);
    return;
  }

  // Otherwise, register this reply message with the waiter,
  // which will handle responding to this IPC when the popup
  // menu opens.
  popup_menu_waiter_->set_reply_message(reply_message);
}