Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / renderer_context_menu / render_view_context_menu_test_util.cc
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.
4
5 #include "chrome/browser/renderer_context_menu/render_view_context_menu_test_util.h"
6 #include "content/public/browser/web_contents.h"
7 #include "ui/base/models/menu_model.h"
8
9 using ui::MenuModel;
10
11 TestRenderViewContextMenu::TestRenderViewContextMenu(
12     content::RenderFrameHost* render_frame_host,
13     content::ContextMenuParams params)
14     : RenderViewContextMenu(render_frame_host, params) {}
15
16 TestRenderViewContextMenu::~TestRenderViewContextMenu() {}
17
18 // static
19 TestRenderViewContextMenu* TestRenderViewContextMenu::Create(
20     content::WebContents* web_contents,
21     const GURL& page_url,
22     const GURL& link_url,
23     const GURL& frame_url) {
24   content::ContextMenuParams params;
25   params.page_url = page_url;
26   params.link_url = link_url;
27   params.frame_url = frame_url;
28   TestRenderViewContextMenu* menu =
29       new TestRenderViewContextMenu(web_contents->GetMainFrame(), params);
30   menu->Init();
31   return menu;
32 }
33
34 bool TestRenderViewContextMenu::GetAcceleratorForCommandId(
35     int command_id,
36     ui::Accelerator* accelerator) {
37   // None of our commands have accelerators, so always return false.
38   return false;
39 }
40
41 bool TestRenderViewContextMenu::IsItemPresent(int command_id) {
42   return menu_model_.GetIndexOfCommandId(command_id) != -1;
43 }
44
45 bool TestRenderViewContextMenu::GetMenuModelAndItemIndex(
46     int command_id,
47     MenuModel** found_model,
48     int* found_index) {
49   std::vector<MenuModel*> models_to_search;
50   models_to_search.push_back(&menu_model_);
51
52   while (!models_to_search.empty()) {
53     MenuModel* model = models_to_search.back();
54     models_to_search.pop_back();
55     for (int i = 0; i < model->GetItemCount(); i++) {
56       if (model->GetCommandIdAt(i) == command_id) {
57         *found_model = model;
58         *found_index = i;
59         return true;
60       } else if (model->GetTypeAt(i) == MenuModel::TYPE_SUBMENU) {
61         models_to_search.push_back(model->GetSubmenuModelAt(i));
62       }
63     }
64   }
65
66   return false;
67 }