Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(115)

Side by Side Diff: chrome/browser/renderer_context_menu/spelling_menu_observer_browsertest.cc

Issue 1647723002: [win/cros/lin] Add back the spellcheck menu. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase. Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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>
8
9 #include <vector>
10
11 #include "base/macros.h" 7 #include "base/macros.h"
12 #include "base/strings/utf_string_conversions.h" 8 #include "base/strings/utf_string_conversions.h"
9 #include "base/values.h"
13 #include "chrome/app/chrome_command_ids.h" 10 #include "chrome/app/chrome_command_ids.h"
14 #include "chrome/browser/renderer_context_menu/render_view_context_menu.h" 11 #include "chrome/browser/renderer_context_menu/mock_render_view_context_menu.h"
15 #include "chrome/browser/spellchecker/spelling_service_client.h" 12 #include "chrome/browser/spellchecker/spelling_service_client.h"
16 #include "chrome/common/pref_names.h" 13 #include "chrome/common/pref_names.h"
17 #include "chrome/test/base/in_process_browser_test.h" 14 #include "chrome/test/base/in_process_browser_test.h"
18 #include "chrome/test/base/testing_profile.h"
19 #include "components/prefs/pref_service.h" 15 #include "components/prefs/pref_service.h"
20 #include "components/renderer_context_menu/render_view_context_menu_observer.h" 16 #include "content/public/common/context_menu_params.h"
21 17 #include "testing/gtest/include/gtest/gtest.h"
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 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
256 SpellingMenuObserverTest::SpellingMenuObserverTest() { 73 SpellingMenuObserverTest::SpellingMenuObserverTest() {
257 } 74 }
258 75
259 SpellingMenuObserverTest::~SpellingMenuObserverTest() { 76 SpellingMenuObserverTest::~SpellingMenuObserverTest() {
260 } 77 }
261 78
262 } // namespace 79 } // namespace
263 80
264 // Tests that right-clicking a correct word does not add any items. 81 // Tests that right-clicking a correct word does not add any items.
265 IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest, InitMenuWithCorrectWord) { 82 IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest, InitMenuWithCorrectWord) {
266 InitMenu("", NULL); 83 InitMenu("", nullptr);
267 EXPECT_EQ(static_cast<size_t>(0), menu()->GetMenuSize()); 84 EXPECT_EQ(static_cast<size_t>(0), menu()->GetMenuSize());
268 } 85 }
269 86
270 // Tests that right-clicking a misspelled word adds three items: 87 // Tests that right-clicking a misspelled word adds three items:
271 // "Add to dictionary", "Ask Google for suggestions", and a separator. 88 // "Add to dictionary", "Ask Google for suggestions", and a separator.
272 IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest, InitMenuWithMisspelledWord) { 89 IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest, InitMenuWithMisspelledWord) {
273 InitMenu("wiimode", NULL); 90 InitMenu("wiimode", nullptr);
274 EXPECT_EQ(3U, menu()->GetMenuSize()); 91 EXPECT_EQ(3U, menu()->GetMenuSize());
275 92
276 // Read all the context-menu items added by this test and verify they are 93 // Read all the context-menu items added by this test and verify they are
277 // expected ones. We do not check the item titles to prevent resource changes 94 // expected ones. We do not check the item titles to prevent resource changes
278 // from breaking this test. (I think it is not expected by those who change 95 // from breaking this test. (I think it is not expected by those who change
279 // resources.) 96 // resources.)
280 MockRenderViewContextMenu::MockMenuItem item; 97 MockRenderViewContextMenu::MockMenuItem item;
281 menu()->GetMenuItem(0, &item); 98 menu()->GetMenuItem(0, &item);
282 EXPECT_EQ(IDC_SPELLCHECK_ADD_TO_DICTIONARY, item.command_id); 99 EXPECT_EQ(IDC_SPELLCHECK_ADD_TO_DICTIONARY, item.command_id);
283 EXPECT_TRUE(item.enabled); 100 EXPECT_TRUE(item.enabled);
284 EXPECT_FALSE(item.hidden); 101 EXPECT_FALSE(item.hidden);
285 menu()->GetMenuItem(1, &item); 102 menu()->GetMenuItem(1, &item);
286 EXPECT_EQ(IDC_CONTENT_CONTEXT_SPELLING_TOGGLE, item.command_id); 103 EXPECT_EQ(IDC_CONTENT_CONTEXT_SPELLING_TOGGLE, item.command_id);
287 EXPECT_TRUE(item.enabled); 104 EXPECT_TRUE(item.enabled);
288 EXPECT_FALSE(item.checked); 105 EXPECT_FALSE(item.checked);
289 EXPECT_FALSE(item.hidden); 106 EXPECT_FALSE(item.hidden);
290 menu()->GetMenuItem(2, &item); 107 menu()->GetMenuItem(2, &item);
291 EXPECT_EQ(-1, item.command_id); 108 EXPECT_EQ(-1, item.command_id);
292 EXPECT_FALSE(item.enabled); 109 EXPECT_FALSE(item.enabled);
293 EXPECT_FALSE(item.hidden); 110 EXPECT_FALSE(item.hidden);
294 } 111 }
295 112
296 // Tests that right-clicking a correct word when we enable spelling-service 113 // Tests that right-clicking a correct word when we enable spelling-service
297 // integration to verify an item "Ask Google for suggestions" is checked. Even 114 // integration to verify an item "Ask Google for suggestions" is checked. Even
298 // though this meanu itself does not add this item, its sub-menu adds the item 115 // though this meanu itself does not add this item, its sub-menu adds the item
299 // and calls SpellingMenuObserver::IsChecked() to check it. 116 // and calls SpellingMenuObserver::IsChecked() to check it.
300 IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest, 117 IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest,
301 EnableSpellingServiceWithCorrectWord) { 118 EnableSpellingServiceWithCorrectWord) {
302 menu()->GetPrefs()->SetBoolean(prefs::kSpellCheckUseSpellingService, true); 119 menu()->GetPrefs()->SetBoolean(prefs::kSpellCheckUseSpellingService, true);
303 InitMenu("", NULL); 120 InitMenu("", nullptr);
304 121
305 EXPECT_TRUE( 122 EXPECT_TRUE(
306 observer()->IsCommandIdChecked(IDC_CONTENT_CONTEXT_SPELLING_TOGGLE)); 123 observer()->IsCommandIdChecked(IDC_CONTENT_CONTEXT_SPELLING_TOGGLE));
307 } 124 }
308 125
309 // Tests that right-clicking a misspelled word when we enable spelling-service 126 // Tests that right-clicking a misspelled word when we enable spelling-service
310 // integration to verify an item "Ask Google for suggestions" is checked. (This 127 // integration to verify an item "Ask Google for suggestions" is checked. (This
311 // test does not actually send JSON-RPC requests to the service because it makes 128 // test does not actually send JSON-RPC requests to the service because it makes
312 // this test flaky.) 129 // this test flaky.)
313 IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest, EnableSpellingService) { 130 IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest, EnableSpellingService) {
314 menu()->GetPrefs()->SetBoolean(prefs::kSpellCheckUseSpellingService, true); 131 menu()->GetPrefs()->SetBoolean(prefs::kSpellCheckUseSpellingService, true);
315 base::ListValue dictionary; 132 base::ListValue dictionary;
316 menu()->GetPrefs()->Set(prefs::kSpellCheckDictionaries, dictionary); 133 menu()->GetPrefs()->Set(prefs::kSpellCheckDictionaries, dictionary);
317 134
318 InitMenu("wiimode", NULL); 135 InitMenu("wiimode", nullptr);
319 EXPECT_EQ(3U, menu()->GetMenuSize()); 136 EXPECT_EQ(3U, menu()->GetMenuSize());
320 137
321 // To avoid duplicates, this test reads only the "Ask Google for suggestions" 138 // To avoid duplicates, this test reads only the "Ask Google for suggestions"
322 // item and verifies it is enabled and checked. 139 // item and verifies it is enabled and checked.
323 MockRenderViewContextMenu::MockMenuItem item; 140 MockRenderViewContextMenu::MockMenuItem item;
324 menu()->GetMenuItem(1, &item); 141 menu()->GetMenuItem(1, &item);
325 EXPECT_EQ(IDC_CONTENT_CONTEXT_SPELLING_TOGGLE, item.command_id); 142 EXPECT_EQ(IDC_CONTENT_CONTEXT_SPELLING_TOGGLE, item.command_id);
326 EXPECT_TRUE(item.enabled); 143 EXPECT_TRUE(item.enabled);
327 EXPECT_TRUE(item.checked); 144 EXPECT_TRUE(item.checked);
328 EXPECT_FALSE(item.hidden); 145 EXPECT_FALSE(item.hidden);
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
393 // Force a non-empty locale so SUGGEST normally would be available. 210 // Force a non-empty locale so SUGGEST normally would be available.
394 base::ListValue dictionary; 211 base::ListValue dictionary;
395 dictionary.AppendString("en"); 212 dictionary.AppendString("en");
396 menu()->GetPrefs()->Set(prefs::kSpellCheckDictionaries, dictionary); 213 menu()->GetPrefs()->Set(prefs::kSpellCheckDictionaries, dictionary);
397 214
398 EXPECT_FALSE(SpellingServiceClient::IsAvailable( 215 EXPECT_FALSE(SpellingServiceClient::IsAvailable(
399 menu()->GetBrowserContext(), SpellingServiceClient::SUGGEST)); 216 menu()->GetBrowserContext(), SpellingServiceClient::SUGGEST));
400 EXPECT_FALSE(SpellingServiceClient::IsAvailable( 217 EXPECT_FALSE(SpellingServiceClient::IsAvailable(
401 menu()->GetBrowserContext(), SpellingServiceClient::SPELLCHECK)); 218 menu()->GetBrowserContext(), SpellingServiceClient::SPELLCHECK));
402 219
403 InitMenu("sjxdjiiiiii", NULL); 220 InitMenu("sjxdjiiiiii", nullptr);
404 221
405 // There should not be a "No more Google suggestions" (from SpellingService) 222 // There should not be a "No more Google suggestions" (from SpellingService)
406 // or a separator. The next 2 items should be "Add to Dictionary" followed 223 // or a separator. The next 2 items should be "Add to Dictionary" followed
407 // by "Ask Google for suggestions" which should be disabled. 224 // by "Ask Google for suggestions" which should be disabled.
408 // TODO(rlp): add autocorrect here when it is functional. 225 // TODO(rlp): add autocorrect here when it is functional.
409 EXPECT_LT(2U, menu()->GetMenuSize()); 226 EXPECT_LT(2U, menu()->GetMenuSize());
410 227
411 MockRenderViewContextMenu::MockMenuItem item; 228 MockRenderViewContextMenu::MockMenuItem item;
412 menu()->GetMenuItem(0, &item); 229 menu()->GetMenuItem(0, &item);
413 EXPECT_EQ(IDC_SPELLCHECK_ADD_TO_DICTIONARY, item.command_id); 230 EXPECT_EQ(IDC_SPELLCHECK_ADD_TO_DICTIONARY, item.command_id);
414 EXPECT_TRUE(item.enabled); 231 EXPECT_TRUE(item.enabled);
415 EXPECT_FALSE(item.hidden); 232 EXPECT_FALSE(item.hidden);
416 233
417 menu()->GetMenuItem(1, &item); 234 menu()->GetMenuItem(1, &item);
418 EXPECT_EQ(IDC_CONTENT_CONTEXT_SPELLING_TOGGLE, item.command_id); 235 EXPECT_EQ(IDC_CONTENT_CONTEXT_SPELLING_TOGGLE, item.command_id);
419 EXPECT_FALSE(item.enabled); 236 EXPECT_FALSE(item.enabled);
420 EXPECT_FALSE(item.hidden); 237 EXPECT_FALSE(item.hidden);
421 } 238 }
422 239
423 // Test that the menu is preceeded by a separator if there are any suggestions, 240 // Test that the menu is preceeded by a separator if there are any suggestions,
424 // or if the SpellingServiceClient is available 241 // or if the SpellingServiceClient is available
425 IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest, SuggestionsForceTopSeparator) { 242 IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest, SuggestionsForceTopSeparator) {
426 menu()->GetPrefs()->SetBoolean(prefs::kSpellCheckUseSpellingService, false); 243 menu()->GetPrefs()->SetBoolean(prefs::kSpellCheckUseSpellingService, false);
427 244
428 // First case: Misspelled word, no suggestions, no spellcheck service. 245 // First case: Misspelled word, no suggestions, no spellcheck service.
429 InitMenu("asdfkj", NULL); 246 InitMenu("asdfkj", nullptr);
430 // See SpellingMenuObserverTest.InitMenuWithMisspelledWord on why 3 items. 247 // See SpellingMenuObserverTest.InitMenuWithMisspelledWord on why 3 items.
431 EXPECT_EQ(3U, menu()->GetMenuSize()); 248 EXPECT_EQ(3U, menu()->GetMenuSize());
432 MockRenderViewContextMenu::MockMenuItem item; 249 MockRenderViewContextMenu::MockMenuItem item;
433 menu()->GetMenuItem(0, &item); 250 menu()->GetMenuItem(0, &item);
434 EXPECT_NE(-1, item.command_id); 251 EXPECT_NE(-1, item.command_id);
435 252
436 // Case #2. Misspelled word, suggestions, no spellcheck service. 253 // Case #2. Misspelled word, suggestions, no spellcheck service.
437 Reset(false); 254 Reset(false);
438 menu()->GetPrefs()->SetBoolean(prefs::kSpellCheckUseSpellingService, false); 255 menu()->GetPrefs()->SetBoolean(prefs::kSpellCheckUseSpellingService, false);
439 InitMenu("asdfkj", "asdf"); 256 InitMenu("asdfkj", "asdf");
440 257
441 // Expect at least separator and 4 default entries. 258 // Expect at least separator and 4 default entries.
442 EXPECT_LT(4U, menu()->GetMenuSize()); 259 EXPECT_LT(4U, menu()->GetMenuSize());
443 // This test only cares that the first one is a separator. 260 // This test only cares that the first one is a separator.
444 menu()->GetMenuItem(0, &item); 261 menu()->GetMenuItem(0, &item);
445 EXPECT_EQ(-1, item.command_id); 262 EXPECT_EQ(-1, item.command_id);
446 263
447 // Case #3. Misspelled word, suggestion service is on. 264 // Case #3. Misspelled word, suggestion service is on.
448 Reset(false); 265 Reset(false);
449 ForceSuggestMode(); 266 ForceSuggestMode();
450 InitMenu("asdfkj", NULL); 267 InitMenu("asdfkj", nullptr);
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 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698