OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/tab_contents/spelling_menu_observer.h" | 5 #include "chrome/browser/tab_contents/spelling_menu_observer.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
10 #include "chrome/app/chrome_command_ids.h" | 10 #include "chrome/app/chrome_command_ids.h" |
| 11 #include "chrome/browser/prefs/pref_service.h" |
11 #include "chrome/browser/tab_contents/render_view_context_menu.h" | 12 #include "chrome/browser/tab_contents/render_view_context_menu.h" |
12 #include "chrome/browser/tab_contents/render_view_context_menu_observer.h" | 13 #include "chrome/browser/tab_contents/render_view_context_menu_observer.h" |
| 14 #include "chrome/common/pref_names.h" |
13 #include "chrome/test/base/in_process_browser_test.h" | 15 #include "chrome/test/base/in_process_browser_test.h" |
14 #include "chrome/test/base/testing_profile.h" | 16 #include "chrome/test/base/testing_profile.h" |
15 | 17 |
16 namespace { | 18 namespace { |
17 | 19 |
18 // A mock context menu used in this test. This class overrides virtual methods | 20 // A mock context menu used in this test. This class overrides virtual methods |
19 // derived from the RenderViewContextMenuProxy class to monitor calls from the | 21 // derived from the RenderViewContextMenuProxy class to monitor calls from the |
20 // SpellingMenuObserver class. | 22 // SpellingMenuObserver class. |
21 class MockRenderViewContextMenu : public RenderViewContextMenuProxy { | 23 class MockRenderViewContextMenu : public RenderViewContextMenuProxy { |
22 public: | 24 public: |
23 // A menu item used in this test. This test uses a vector of this struct to | 25 // A menu item used in this test. This test uses a vector of this struct to |
24 // hold menu items added by this test. | 26 // hold menu items added by this test. |
25 struct MockMenuItem { | 27 struct MockMenuItem { |
| 28 MockMenuItem() |
| 29 : command_id(0), |
| 30 enabled(false), |
| 31 checked(false), |
| 32 hidden(true) { |
| 33 } |
26 int command_id; | 34 int command_id; |
27 bool enabled; | 35 bool enabled; |
| 36 bool checked; |
28 bool hidden; | 37 bool hidden; |
29 string16 title; | 38 string16 title; |
30 }; | 39 }; |
31 | 40 |
32 MockRenderViewContextMenu(); | 41 MockRenderViewContextMenu(); |
33 virtual ~MockRenderViewContextMenu(); | 42 virtual ~MockRenderViewContextMenu(); |
34 | 43 |
35 // RenderViewContextMenuProxy implementation. | 44 // RenderViewContextMenuProxy implementation. |
36 virtual void AddMenuItem(int command_id, const string16& title) OVERRIDE; | 45 virtual void AddMenuItem(int command_id, const string16& title) OVERRIDE; |
| 46 virtual void AddCheckItem(int command_id, const string16& title) OVERRIDE; |
37 virtual void AddSeparator() OVERRIDE; | 47 virtual void AddSeparator() OVERRIDE; |
38 virtual void AddSubMenu(int command_id, | 48 virtual void AddSubMenu(int command_id, |
39 const string16& label, | 49 const string16& label, |
40 ui::MenuModel* model) OVERRIDE; | 50 ui::MenuModel* model) OVERRIDE; |
41 virtual void UpdateMenuItem(int command_id, | 51 virtual void UpdateMenuItem(int command_id, |
42 bool enabled, | 52 bool enabled, |
43 bool hidden, | 53 bool hidden, |
44 const string16& title) OVERRIDE; | 54 const string16& title) OVERRIDE; |
45 virtual RenderViewHost* GetRenderViewHost() const OVERRIDE; | 55 virtual RenderViewHost* GetRenderViewHost() const OVERRIDE; |
46 virtual Profile* GetProfile() const OVERRIDE; | 56 virtual Profile* GetProfile() const OVERRIDE; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 } | 88 } |
79 | 89 |
80 MockRenderViewContextMenu::~MockRenderViewContextMenu() { | 90 MockRenderViewContextMenu::~MockRenderViewContextMenu() { |
81 } | 91 } |
82 | 92 |
83 void MockRenderViewContextMenu::AddMenuItem(int command_id, | 93 void MockRenderViewContextMenu::AddMenuItem(int command_id, |
84 const string16& title) { | 94 const string16& title) { |
85 MockMenuItem item; | 95 MockMenuItem item; |
86 item.command_id = command_id; | 96 item.command_id = command_id; |
87 item.enabled = observer_->IsCommandIdEnabled(command_id); | 97 item.enabled = observer_->IsCommandIdEnabled(command_id); |
| 98 item.checked = false; |
| 99 item.hidden = false; |
| 100 item.title = title; |
| 101 items_.push_back(item); |
| 102 } |
| 103 |
| 104 void MockRenderViewContextMenu::AddCheckItem(int command_id, |
| 105 const string16& title) { |
| 106 MockMenuItem item; |
| 107 item.command_id = command_id; |
| 108 item.enabled = observer_->IsCommandIdEnabled(command_id); |
| 109 item.checked = observer_->IsCommandIdChecked(command_id); |
88 item.hidden = false; | 110 item.hidden = false; |
89 item.title = title; | 111 item.title = title; |
90 items_.push_back(item); | 112 items_.push_back(item); |
91 } | 113 } |
92 | 114 |
93 void MockRenderViewContextMenu::AddSeparator() { | 115 void MockRenderViewContextMenu::AddSeparator() { |
94 MockMenuItem item; | 116 MockMenuItem item; |
95 item.command_id = -1; | 117 item.command_id = -1; |
96 item.enabled = false; | 118 item.enabled = false; |
| 119 item.checked = false; |
97 item.hidden = false; | 120 item.hidden = false; |
98 items_.push_back(item); | 121 items_.push_back(item); |
99 } | 122 } |
100 | 123 |
101 void MockRenderViewContextMenu::AddSubMenu(int command_id, | 124 void MockRenderViewContextMenu::AddSubMenu(int command_id, |
102 const string16& label, | 125 const string16& label, |
103 ui::MenuModel* model) { | 126 ui::MenuModel* model) { |
104 MockMenuItem item; | 127 MockMenuItem item; |
105 item.command_id = -1; | 128 item.command_id = -1; |
106 item.enabled = false; | 129 item.enabled = false; |
| 130 item.checked = false; |
107 item.hidden = false; | 131 item.hidden = false; |
108 items_.push_back(item); | 132 items_.push_back(item); |
109 } | 133 } |
110 | 134 |
111 void MockRenderViewContextMenu::UpdateMenuItem(int command_id, | 135 void MockRenderViewContextMenu::UpdateMenuItem(int command_id, |
112 bool enabled, | 136 bool enabled, |
113 bool hidden, | 137 bool hidden, |
114 const string16& title) { | 138 const string16& title) { |
115 for (std::vector<MockMenuItem>::iterator it = items_.begin(); | 139 for (std::vector<MockMenuItem>::iterator it = items_.begin(); |
116 it != items_.end(); ++it) { | 140 it != items_.end(); ++it) { |
(...skipping 21 matching lines...) Expand all Loading... |
138 size_t MockRenderViewContextMenu::GetMenuSize() const { | 162 size_t MockRenderViewContextMenu::GetMenuSize() const { |
139 return items_.size(); | 163 return items_.size(); |
140 } | 164 } |
141 | 165 |
142 bool MockRenderViewContextMenu::GetMenuItem(size_t i, | 166 bool MockRenderViewContextMenu::GetMenuItem(size_t i, |
143 MockMenuItem* item) const { | 167 MockMenuItem* item) const { |
144 if (i >= items_.size()) | 168 if (i >= items_.size()) |
145 return false; | 169 return false; |
146 item->command_id = items_[i].command_id; | 170 item->command_id = items_[i].command_id; |
147 item->enabled = items_[i].enabled; | 171 item->enabled = items_[i].enabled; |
| 172 item->checked = items_[i].checked; |
148 item->hidden = items_[i].hidden; | 173 item->hidden = items_[i].hidden; |
149 item->title = items_[i].title; | 174 item->title = items_[i].title; |
150 return true; | 175 return true; |
151 } | 176 } |
152 | 177 |
153 void MockRenderViewContextMenu::SetObserver( | 178 void MockRenderViewContextMenu::SetObserver( |
154 RenderViewContextMenuObserver* observer) { | 179 RenderViewContextMenuObserver* observer) { |
155 observer_ = observer; | 180 observer_ = observer; |
156 } | 181 } |
157 | 182 |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 EXPECT_EQ(IDC_CONTENT_CONTEXT_NO_SPELLING_SUGGESTIONS, item.command_id); | 240 EXPECT_EQ(IDC_CONTENT_CONTEXT_NO_SPELLING_SUGGESTIONS, item.command_id); |
216 EXPECT_FALSE(item.enabled); | 241 EXPECT_FALSE(item.enabled); |
217 EXPECT_FALSE(item.hidden); | 242 EXPECT_FALSE(item.hidden); |
218 menu->GetMenuItem(1, &item); | 243 menu->GetMenuItem(1, &item); |
219 EXPECT_EQ(IDC_SPELLCHECK_ADD_TO_DICTIONARY, item.command_id); | 244 EXPECT_EQ(IDC_SPELLCHECK_ADD_TO_DICTIONARY, item.command_id); |
220 EXPECT_TRUE(item.enabled); | 245 EXPECT_TRUE(item.enabled); |
221 EXPECT_FALSE(item.hidden); | 246 EXPECT_FALSE(item.hidden); |
222 menu->GetMenuItem(2, &item); | 247 menu->GetMenuItem(2, &item); |
223 EXPECT_EQ(IDC_CONTENT_CONTEXT_SPELLING_TOGGLE, item.command_id); | 248 EXPECT_EQ(IDC_CONTENT_CONTEXT_SPELLING_TOGGLE, item.command_id); |
224 EXPECT_TRUE(item.enabled); | 249 EXPECT_TRUE(item.enabled); |
| 250 EXPECT_FALSE(item.checked); |
225 EXPECT_FALSE(item.hidden); | 251 EXPECT_FALSE(item.hidden); |
226 menu->GetMenuItem(3, &item); | 252 menu->GetMenuItem(3, &item); |
227 EXPECT_EQ(-1, item.command_id); | 253 EXPECT_EQ(-1, item.command_id); |
228 EXPECT_FALSE(item.enabled); | 254 EXPECT_FALSE(item.enabled); |
229 EXPECT_FALSE(item.hidden); | 255 EXPECT_FALSE(item.hidden); |
230 } | 256 } |
| 257 |
| 258 // Tests that right-clicking a misspelled word when we enable spelling-service |
| 259 // integration to verify an item "Ask Google for suggestions" is checked. (This |
| 260 // test does not actually send JSON-RPC requests to the service because it makes |
| 261 // this test flaky.) |
| 262 IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest, EnableSpellingService) { |
| 263 scoped_ptr<MockRenderViewContextMenu> menu(new MockRenderViewContextMenu); |
| 264 scoped_ptr<SpellingMenuObserver> observer( |
| 265 new SpellingMenuObserver(menu.get())); |
| 266 menu->SetObserver(observer.get()); |
| 267 menu->GetPrefs()->SetBoolean(prefs::kSpellCheckUseSpellingService, true); |
| 268 |
| 269 content::ContextMenuParams params; |
| 270 params.is_editable = true; |
| 271 params.misspelled_word = ASCIIToUTF16("wiimode"); |
| 272 observer->InitMenu(params); |
| 273 EXPECT_EQ(static_cast<size_t>(4), menu->GetMenuSize()); |
| 274 |
| 275 // To avoid duplicates, this test reads only the "Ask Google for suggestions" |
| 276 // item and verifies it is enabled and checked. |
| 277 MockRenderViewContextMenu::MockMenuItem item; |
| 278 menu->GetMenuItem(2, &item); |
| 279 EXPECT_EQ(IDC_CONTENT_CONTEXT_SPELLING_TOGGLE, item.command_id); |
| 280 EXPECT_TRUE(item.enabled); |
| 281 EXPECT_TRUE(item.checked); |
| 282 EXPECT_FALSE(item.hidden); |
| 283 } |
OLD | NEW |