1 // Copyright 2014 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/browser/renderer_context_menu/render_view_context_menu_browsertest_util.h"
8 #include "base/location.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "chrome/app/chrome_command_ids.h"
12 #include "chrome/browser/chrome_notification_types.h"
13 #include "chrome/browser/renderer_context_menu/render_view_context_menu.h"
14 #include "content/public/browser/notification_service.h"
15 #include "content/public/test/test_utils.h"
17 ContextMenuNotificationObserver::ContextMenuNotificationObserver(
18 int command_to_execute
)
19 : command_to_execute_(command_to_execute
) {
21 chrome::NOTIFICATION_RENDER_VIEW_CONTEXT_MENU_SHOWN
,
22 content::NotificationService::AllSources());
25 ContextMenuNotificationObserver::~ContextMenuNotificationObserver() {
28 void ContextMenuNotificationObserver::Observe(
30 const content::NotificationSource
& source
,
31 const content::NotificationDetails
& details
) {
33 case chrome::NOTIFICATION_RENDER_VIEW_CONTEXT_MENU_SHOWN
: {
34 RenderViewContextMenu
* context_menu
=
35 content::Source
<RenderViewContextMenu
>(source
).ptr();
36 base::ThreadTaskRunnerHandle::Get()->PostTask(
38 base::Bind(&ContextMenuNotificationObserver::ExecuteCommand
,
39 base::Unretained(this), context_menu
));
48 void ContextMenuNotificationObserver::ExecuteCommand(
49 RenderViewContextMenu
* context_menu
) {
50 context_menu
->ExecuteCommand(command_to_execute_
, 0);
51 context_menu
->Cancel();
54 ContextMenuWaiter::ContextMenuWaiter(const content::NotificationSource
& source
)
55 : menu_visible_(false) {
56 registrar_
.Add(this, chrome::NOTIFICATION_RENDER_VIEW_CONTEXT_MENU_SHOWN
,
57 content::NotificationService::AllSources());
60 ContextMenuWaiter::~ContextMenuWaiter() {
63 void ContextMenuWaiter::Observe(int type
,
64 const content::NotificationSource
& source
,
65 const content::NotificationDetails
& details
) {
67 case chrome::NOTIFICATION_RENDER_VIEW_CONTEXT_MENU_SHOWN
: {
69 RenderViewContextMenu
* context_menu
=
70 content::Source
<RenderViewContextMenu
>(source
).ptr();
71 base::ThreadTaskRunnerHandle::Get()->PostTask(
72 FROM_HERE
, base::Bind(&ContextMenuWaiter::Cancel
,
73 base::Unretained(this), context_menu
));
82 void ContextMenuWaiter::WaitForMenuOpenAndClose() {
83 content::WindowedNotificationObserver
menu_observer(
84 chrome::NOTIFICATION_RENDER_VIEW_CONTEXT_MENU_SHOWN
,
85 content::NotificationService::AllSources());
89 content::RunAllPendingInMessageLoop();
90 menu_visible_
= false;
93 content::ContextMenuParams
& ContextMenuWaiter::params() {
97 void ContextMenuWaiter::Cancel(RenderViewContextMenu
* context_menu
) {
98 params_
= context_menu
->params();
99 context_menu
->Cancel();