| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/renderer_context_menu/spelling_menu_observer.h" | 5 #include "chrome/browser/renderer_context_menu/spelling_menu_observer.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <vector> | |
| 10 | |
| 11 #include "base/macros.h" | 9 #include "base/macros.h" |
| 12 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 14 #include "chrome/app/chrome_command_ids.h" | 12 #include "chrome/app/chrome_command_ids.h" |
| 15 #include "chrome/browser/renderer_context_menu/render_view_context_menu.h" | 13 #include "chrome/browser/renderer_context_menu/mock_render_view_context_menu.h" |
| 16 #include "chrome/browser/spellchecker/spelling_service_client.h" | 14 #include "chrome/browser/spellchecker/spelling_service_client.h" |
| 17 #include "chrome/common/pref_names.h" | 15 #include "chrome/common/pref_names.h" |
| 18 #include "chrome/test/base/in_process_browser_test.h" | 16 #include "chrome/test/base/in_process_browser_test.h" |
| 19 #include "chrome/test/base/testing_profile.h" | 17 #include "content/public/common/context_menu_params.h" |
| 20 #include "components/renderer_context_menu/render_view_context_menu_observer.h" | |
| 21 | |
| 22 using content::RenderViewHost; | |
| 23 using content::WebContents; | |
| 24 | 18 |
| 25 namespace { | 19 namespace { |
| 26 | 20 |
| 27 // A mock context menu used in this test. This class overrides virtual methods | |
| 28 // derived from the RenderViewContextMenuProxy class to monitor calls from the | |
| 29 // SpellingMenuObserver class. | |
| 30 class MockRenderViewContextMenu : public RenderViewContextMenuProxy { | |
| 31 public: | |
| 32 // A menu item used in this test. This test uses a vector of this struct to | |
| 33 // hold menu items added by this test. | |
| 34 struct MockMenuItem { | |
| 35 MockMenuItem() | |
| 36 : command_id(0), | |
| 37 enabled(false), | |
| 38 checked(false), | |
| 39 hidden(true) { | |
| 40 } | |
| 41 int command_id; | |
| 42 bool enabled; | |
| 43 bool checked; | |
| 44 bool hidden; | |
| 45 base::string16 title; | |
| 46 }; | |
| 47 | |
| 48 explicit MockRenderViewContextMenu(bool incognito); | |
| 49 virtual ~MockRenderViewContextMenu(); | |
| 50 | |
| 51 // RenderViewContextMenuProxy implementation. | |
| 52 void AddMenuItem(int command_id, const base::string16& title) override; | |
| 53 void AddCheckItem(int command_id, const base::string16& title) override; | |
| 54 void AddSeparator() override; | |
| 55 void AddSubMenu(int command_id, | |
| 56 const base::string16& label, | |
| 57 ui::MenuModel* model) override; | |
| 58 void UpdateMenuItem(int command_id, | |
| 59 bool enabled, | |
| 60 bool hidden, | |
| 61 const base::string16& title) override; | |
| 62 RenderViewHost* GetRenderViewHost() const override; | |
| 63 WebContents* GetWebContents() const override; | |
| 64 content::BrowserContext* GetBrowserContext() const override; | |
| 65 | |
| 66 // Attaches a RenderViewContextMenuObserver to be tested. | |
| 67 void SetObserver(RenderViewContextMenuObserver* observer); | |
| 68 | |
| 69 // Returns the number of items added by the test. | |
| 70 size_t GetMenuSize() const; | |
| 71 | |
| 72 // Returns the i-th item. | |
| 73 bool GetMenuItem(size_t i, MockMenuItem* item) const; | |
| 74 | |
| 75 // Returns the writable profile used in this test. | |
| 76 PrefService* GetPrefs(); | |
| 77 | |
| 78 private: | |
| 79 // An observer used for initializing the status of menu items added in this | |
| 80 // test. A test should delete this RenderViewContextMenuObserver object. | |
| 81 RenderViewContextMenuObserver* observer_; | |
| 82 | |
| 83 // A dummy profile used in this test. Call GetPrefs() when a test needs to | |
| 84 // change this profile and use PrefService methods. | |
| 85 scoped_ptr<TestingProfile> original_profile_; | |
| 86 | |
| 87 // Either |original_profile_| or its incognito profile. | |
| 88 Profile* profile_; | |
| 89 | |
| 90 // A list of menu items added by the SpellingMenuObserver class. | |
| 91 std::vector<MockMenuItem> items_; | |
| 92 | |
| 93 DISALLOW_COPY_AND_ASSIGN(MockRenderViewContextMenu); | |
| 94 }; | |
| 95 | |
| 96 MockRenderViewContextMenu::MockRenderViewContextMenu(bool incognito) | |
| 97 : observer_(NULL) { | |
| 98 original_profile_ = TestingProfile::Builder().Build(); | |
| 99 profile_ = incognito ? original_profile_->GetOffTheRecordProfile() | |
| 100 : original_profile_.get(); | |
| 101 } | |
| 102 | |
| 103 MockRenderViewContextMenu::~MockRenderViewContextMenu() { | |
| 104 } | |
| 105 | |
| 106 void MockRenderViewContextMenu::AddMenuItem(int command_id, | |
| 107 const base::string16& title) { | |
| 108 MockMenuItem item; | |
| 109 item.command_id = command_id; | |
| 110 item.enabled = observer_->IsCommandIdEnabled(command_id); | |
| 111 item.checked = false; | |
| 112 item.hidden = false; | |
| 113 item.title = title; | |
| 114 items_.push_back(item); | |
| 115 } | |
| 116 | |
| 117 void MockRenderViewContextMenu::AddCheckItem(int command_id, | |
| 118 const base::string16& title) { | |
| 119 MockMenuItem item; | |
| 120 item.command_id = command_id; | |
| 121 item.enabled = observer_->IsCommandIdEnabled(command_id); | |
| 122 item.checked = observer_->IsCommandIdChecked(command_id); | |
| 123 item.hidden = false; | |
| 124 item.title = title; | |
| 125 items_.push_back(item); | |
| 126 } | |
| 127 | |
| 128 void MockRenderViewContextMenu::AddSeparator() { | |
| 129 MockMenuItem item; | |
| 130 item.command_id = -1; | |
| 131 item.enabled = false; | |
| 132 item.checked = false; | |
| 133 item.hidden = false; | |
| 134 items_.push_back(item); | |
| 135 } | |
| 136 | |
| 137 void MockRenderViewContextMenu::AddSubMenu(int command_id, | |
| 138 const base::string16& label, | |
| 139 ui::MenuModel* model) { | |
| 140 MockMenuItem item; | |
| 141 item.command_id = -1; | |
| 142 item.enabled = false; | |
| 143 item.checked = false; | |
| 144 item.hidden = false; | |
| 145 items_.push_back(item); | |
| 146 } | |
| 147 | |
| 148 void MockRenderViewContextMenu::UpdateMenuItem(int command_id, | |
| 149 bool enabled, | |
| 150 bool hidden, | |
| 151 const base::string16& title) { | |
| 152 for (std::vector<MockMenuItem>::iterator it = items_.begin(); | |
| 153 it != items_.end(); ++it) { | |
| 154 if (it->command_id == command_id) { | |
| 155 it->enabled = enabled; | |
| 156 it->hidden = hidden; | |
| 157 it->title = title; | |
| 158 return; | |
| 159 } | |
| 160 } | |
| 161 | |
| 162 // The SpellingMenuObserver class tries to change a menu item not added by the | |
| 163 // class. This is an unexpected behavior and we should stop now. | |
| 164 FAIL(); | |
| 165 } | |
| 166 | |
| 167 RenderViewHost* MockRenderViewContextMenu::GetRenderViewHost() const { | |
| 168 return NULL; | |
| 169 } | |
| 170 | |
| 171 WebContents* MockRenderViewContextMenu::GetWebContents() const { | |
| 172 return NULL; | |
| 173 } | |
| 174 | |
| 175 content::BrowserContext* MockRenderViewContextMenu::GetBrowserContext() const { | |
| 176 return profile_; | |
| 177 } | |
| 178 | |
| 179 size_t MockRenderViewContextMenu::GetMenuSize() const { | |
| 180 return items_.size(); | |
| 181 } | |
| 182 | |
| 183 bool MockRenderViewContextMenu::GetMenuItem(size_t i, | |
| 184 MockMenuItem* item) const { | |
| 185 if (i >= items_.size()) | |
| 186 return false; | |
| 187 item->command_id = items_[i].command_id; | |
| 188 item->enabled = items_[i].enabled; | |
| 189 item->checked = items_[i].checked; | |
| 190 item->hidden = items_[i].hidden; | |
| 191 item->title = items_[i].title; | |
| 192 return true; | |
| 193 } | |
| 194 | |
| 195 void MockRenderViewContextMenu::SetObserver( | |
| 196 RenderViewContextMenuObserver* observer) { | |
| 197 observer_ = observer; | |
| 198 } | |
| 199 | |
| 200 PrefService* MockRenderViewContextMenu::GetPrefs() { | |
| 201 return profile_->GetPrefs(); | |
| 202 } | |
| 203 | |
| 204 // A test class used in this file. This test should be a browser test because it | 21 // A test class used in this file. This test should be a browser test because it |
| 205 // accesses resources. | 22 // accesses resources. |
| 206 class SpellingMenuObserverTest : public InProcessBrowserTest { | 23 class SpellingMenuObserverTest : public InProcessBrowserTest { |
| 207 public: | 24 public: |
| 208 SpellingMenuObserverTest(); | 25 SpellingMenuObserverTest(); |
| 209 | 26 |
| 210 void SetUpOnMainThread() override { Reset(false); } | 27 void SetUpOnMainThread() override { Reset(false); } |
| 211 | 28 |
| 212 void TearDownOnMainThread() override { | 29 void TearDownOnMainThread() override { |
| 213 observer_.reset(); | 30 observer_.reset(); |
| (...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 449 ForceSuggestMode(); | 266 ForceSuggestMode(); |
| 450 InitMenu("asdfkj", NULL); | 267 InitMenu("asdfkj", NULL); |
| 451 | 268 |
| 452 // Should have at least 2 entries. Separator, suggestion. | 269 // Should have at least 2 entries. Separator, suggestion. |
| 453 EXPECT_LT(2U, menu()->GetMenuSize()); | 270 EXPECT_LT(2U, menu()->GetMenuSize()); |
| 454 menu()->GetMenuItem(0, &item); | 271 menu()->GetMenuItem(0, &item); |
| 455 EXPECT_EQ(-1, item.command_id); | 272 EXPECT_EQ(-1, item.command_id); |
| 456 menu()->GetMenuItem(1, &item); | 273 menu()->GetMenuItem(1, &item); |
| 457 EXPECT_EQ(IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION, item.command_id); | 274 EXPECT_EQ(IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION, item.command_id); |
| 458 } | 275 } |
| OLD | NEW |