OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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/mock_render_view_context_menu.h" |
| 6 |
| 7 #include "base/prefs/pref_service.h" |
| 8 #include "chrome/test/base/testing_profile.h" |
| 9 #include "components/renderer_context_menu/render_view_context_menu_observer.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 |
| 12 MockRenderViewContextMenu::MockMenuItem::MockMenuItem() |
| 13 : command_id(0), enabled(false), checked(false), hidden(true) {} |
| 14 |
| 15 MockRenderViewContextMenu::MockMenuItem::~MockMenuItem() {} |
| 16 |
| 17 MockRenderViewContextMenu::MockRenderViewContextMenu(bool incognito) |
| 18 : observer_(nullptr), |
| 19 original_profile_(TestingProfile::Builder().Build()), |
| 20 profile_(incognito ? original_profile_->GetOffTheRecordProfile() |
| 21 : original_profile_.get()) {} |
| 22 |
| 23 MockRenderViewContextMenu::~MockRenderViewContextMenu() {} |
| 24 |
| 25 bool MockRenderViewContextMenu::IsCommandIdChecked(int command_id) const { |
| 26 return observer_->IsCommandIdChecked(command_id); |
| 27 } |
| 28 |
| 29 bool MockRenderViewContextMenu::IsCommandIdEnabled(int command_id) const { |
| 30 return observer_->IsCommandIdEnabled(command_id); |
| 31 } |
| 32 |
| 33 void MockRenderViewContextMenu::ExecuteCommand(int command_id, |
| 34 int event_flags) { |
| 35 observer_->ExecuteCommand(command_id); |
| 36 } |
| 37 |
| 38 void MockRenderViewContextMenu::MenuWillShow(ui::SimpleMenuModel* source) {} |
| 39 |
| 40 void MockRenderViewContextMenu::MenuClosed(ui::SimpleMenuModel* source) {} |
| 41 |
| 42 bool MockRenderViewContextMenu::GetAcceleratorForCommandId( |
| 43 int command_id, |
| 44 ui::Accelerator* accelerator) { |
| 45 return false; |
| 46 } |
| 47 |
| 48 void MockRenderViewContextMenu::AddMenuItem(int command_id, |
| 49 const base::string16& title) { |
| 50 MockMenuItem item; |
| 51 item.command_id = command_id; |
| 52 item.enabled = observer_->IsCommandIdEnabled(command_id); |
| 53 item.checked = false; |
| 54 item.hidden = false; |
| 55 item.title = title; |
| 56 items_.push_back(item); |
| 57 } |
| 58 |
| 59 void MockRenderViewContextMenu::AddCheckItem(int command_id, |
| 60 const base::string16& title) { |
| 61 MockMenuItem item; |
| 62 item.command_id = command_id; |
| 63 item.enabled = observer_->IsCommandIdEnabled(command_id); |
| 64 item.checked = observer_->IsCommandIdChecked(command_id); |
| 65 item.hidden = false; |
| 66 item.title = title; |
| 67 items_.push_back(item); |
| 68 } |
| 69 |
| 70 void MockRenderViewContextMenu::AddSeparator() { |
| 71 MockMenuItem item; |
| 72 item.command_id = -1; |
| 73 item.enabled = false; |
| 74 item.checked = false; |
| 75 item.hidden = false; |
| 76 items_.push_back(item); |
| 77 } |
| 78 |
| 79 void MockRenderViewContextMenu::AddSubMenu(int command_id, |
| 80 const base::string16& label, |
| 81 ui::MenuModel* model) { |
| 82 MockMenuItem item; |
| 83 item.command_id = -1; |
| 84 item.enabled = false; |
| 85 item.checked = false; |
| 86 item.hidden = false; |
| 87 items_.push_back(item); |
| 88 } |
| 89 |
| 90 void MockRenderViewContextMenu::UpdateMenuItem(int command_id, |
| 91 bool enabled, |
| 92 bool hidden, |
| 93 const base::string16& title) { |
| 94 for (auto& item : items_) { |
| 95 if (item.command_id == command_id) { |
| 96 item.enabled = enabled; |
| 97 item.hidden = hidden; |
| 98 item.title = title; |
| 99 return; |
| 100 } |
| 101 } |
| 102 |
| 103 FAIL() << "Menu observer is trying to change a menu item it doesn't own."; |
| 104 } |
| 105 |
| 106 content::RenderViewHost* MockRenderViewContextMenu::GetRenderViewHost() const { |
| 107 return nullptr; |
| 108 } |
| 109 |
| 110 content::BrowserContext* MockRenderViewContextMenu::GetBrowserContext() const { |
| 111 return profile_; |
| 112 } |
| 113 |
| 114 content::WebContents* MockRenderViewContextMenu::GetWebContents() const { |
| 115 return nullptr; |
| 116 } |
| 117 |
| 118 void MockRenderViewContextMenu::SetObserver( |
| 119 RenderViewContextMenuObserver* observer) { |
| 120 observer_ = observer; |
| 121 } |
| 122 |
| 123 size_t MockRenderViewContextMenu::GetMenuSize() const { |
| 124 return items_.size(); |
| 125 } |
| 126 |
| 127 bool MockRenderViewContextMenu::GetMenuItem(size_t index, |
| 128 MockMenuItem* item) const { |
| 129 if (index >= items_.size()) |
| 130 return false; |
| 131 item->command_id = items_[index].command_id; |
| 132 item->enabled = items_[index].enabled; |
| 133 item->checked = items_[index].checked; |
| 134 item->hidden = items_[index].hidden; |
| 135 item->title = items_[index].title; |
| 136 return true; |
| 137 } |
| 138 |
| 139 PrefService* MockRenderViewContextMenu::GetPrefs() { |
| 140 return profile_->GetPrefs(); |
| 141 } |
OLD | NEW |