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

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

Issue 2591073002: Simplify context menu for misspelled words + add metrics to this context (Closed)
Patch Set: Merge Created 3 years, 11 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 "base/macros.h" 7 #include "base/macros.h"
8 #include "base/strings/utf_string_conversions.h" 8 #include "base/strings/utf_string_conversions.h"
9 #include "base/values.h" 9 #include "base/values.h"
10 #include "chrome/app/chrome_command_ids.h" 10 #include "chrome/app/chrome_command_ids.h"
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
79 } 79 }
80 80
81 } // namespace 81 } // namespace
82 82
83 // Tests that right-clicking a correct word does not add any items. 83 // Tests that right-clicking a correct word does not add any items.
84 IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest, InitMenuWithCorrectWord) { 84 IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest, InitMenuWithCorrectWord) {
85 InitMenu("", nullptr); 85 InitMenu("", nullptr);
86 EXPECT_EQ(static_cast<size_t>(0), menu()->GetMenuSize()); 86 EXPECT_EQ(static_cast<size_t>(0), menu()->GetMenuSize());
87 } 87 }
88 88
89 // Tests that right-clicking a misspelled word adds three items: 89 // Tests that right-clicking a misspelled word adds two items:
90 // "Add to dictionary", "Ask Google for suggestions", and a separator. 90 // "Add to dictionary", "Ask Google for suggestions".
91 IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest, InitMenuWithMisspelledWord) { 91 IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest, InitMenuWithMisspelledWord) {
92 InitMenu("wiimode", nullptr); 92 InitMenu("wiimode", nullptr);
93 EXPECT_EQ(3U, menu()->GetMenuSize()); 93 EXPECT_EQ(2U, menu()->GetMenuSize());
94 94
95 // Read all the context-menu items added by this test and verify they are 95 // Read all the context-menu items added by this test and verify they are
96 // expected ones. We do not check the item titles to prevent resource changes 96 // expected ones. We do not check the item titles to prevent resource changes
97 // from breaking this test. (I think it is not expected by those who change 97 // from breaking this test. (I think it is not expected by those who change
98 // resources.) 98 // resources.)
99 MockRenderViewContextMenu::MockMenuItem item; 99 MockRenderViewContextMenu::MockMenuItem item;
100 menu()->GetMenuItem(0, &item); 100 menu()->GetMenuItem(0, &item);
101 EXPECT_EQ(IDC_SPELLCHECK_ADD_TO_DICTIONARY, item.command_id); 101 EXPECT_EQ(IDC_SPELLCHECK_ADD_TO_DICTIONARY, item.command_id);
102 EXPECT_TRUE(item.enabled); 102 EXPECT_TRUE(item.enabled);
103 EXPECT_FALSE(item.hidden); 103 EXPECT_FALSE(item.hidden);
104 menu()->GetMenuItem(1, &item); 104 menu()->GetMenuItem(1, &item);
105 EXPECT_EQ(IDC_CONTENT_CONTEXT_SPELLING_TOGGLE, item.command_id); 105 EXPECT_EQ(IDC_CONTENT_CONTEXT_SPELLING_TOGGLE, item.command_id);
106 EXPECT_TRUE(item.enabled); 106 EXPECT_TRUE(item.enabled);
107 EXPECT_FALSE(item.checked); 107 EXPECT_FALSE(item.checked);
108 EXPECT_FALSE(item.hidden); 108 EXPECT_FALSE(item.hidden);
109 menu()->GetMenuItem(2, &item); 109 menu()->GetMenuItem(2, &item);
110 EXPECT_EQ(-1, item.command_id);
111 EXPECT_FALSE(item.enabled);
112 EXPECT_FALSE(item.hidden);
113 } 110 }
114 111
115 // Tests that right-clicking a correct word when we enable spelling-service 112 // Tests that right-clicking a correct word when we enable spelling-service
116 // integration to verify an item "Ask Google for suggestions" is checked. Even 113 // integration to verify an item "Ask Google for suggestions" is checked. Even
117 // though this meanu itself does not add this item, its sub-menu adds the item 114 // though this meanu itself does not add this item, its sub-menu adds the item
118 // and calls SpellingMenuObserver::IsChecked() to check it. 115 // and calls SpellingMenuObserver::IsChecked() to check it.
119 IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest, 116 IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest,
120 EnableSpellingServiceWithCorrectWord) { 117 EnableSpellingServiceWithCorrectWord) {
121 menu()->GetPrefs()->SetBoolean( 118 menu()->GetPrefs()->SetBoolean(
122 spellcheck::prefs::kSpellCheckUseSpellingService, true); 119 spellcheck::prefs::kSpellCheckUseSpellingService, true);
123 InitMenu("", nullptr); 120 InitMenu("", nullptr);
124 121
125 EXPECT_TRUE( 122 EXPECT_TRUE(
126 observer()->IsCommandIdChecked(IDC_CONTENT_CONTEXT_SPELLING_TOGGLE)); 123 observer()->IsCommandIdChecked(IDC_CONTENT_CONTEXT_SPELLING_TOGGLE));
127 } 124 }
128 125
129 // 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
130 // 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
131 // 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
132 // this test flaky.) 129 // this test flaky.)
133 IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest, EnableSpellingService) { 130 IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest, EnableSpellingService) {
134 menu()->GetPrefs()->SetBoolean( 131 menu()->GetPrefs()->SetBoolean(
135 spellcheck::prefs::kSpellCheckUseSpellingService, true); 132 spellcheck::prefs::kSpellCheckUseSpellingService, true);
136 base::ListValue dictionary; 133 base::ListValue dictionary;
137 menu()->GetPrefs()->Set(spellcheck::prefs::kSpellCheckDictionaries, 134 menu()->GetPrefs()->Set(spellcheck::prefs::kSpellCheckDictionaries,
138 dictionary); 135 dictionary);
139 136
140 InitMenu("wiimode", nullptr); 137 InitMenu("wiimode", nullptr);
141 EXPECT_EQ(3U, menu()->GetMenuSize()); 138 EXPECT_EQ(2U, menu()->GetMenuSize());
142 139
143 // To avoid duplicates, this test reads only the "Ask Google for suggestions" 140 // To avoid duplicates, this test reads only the "Ask Google for suggestions"
144 // item and verifies it is enabled and checked. 141 // item and verifies it is enabled and checked.
145 MockRenderViewContextMenu::MockMenuItem item; 142 MockRenderViewContextMenu::MockMenuItem item;
146 menu()->GetMenuItem(1, &item); 143 menu()->GetMenuItem(1, &item);
147 EXPECT_EQ(IDC_CONTENT_CONTEXT_SPELLING_TOGGLE, item.command_id); 144 EXPECT_EQ(IDC_CONTENT_CONTEXT_SPELLING_TOGGLE, item.command_id);
148 EXPECT_TRUE(item.enabled); 145 EXPECT_TRUE(item.enabled);
149 EXPECT_TRUE(item.checked); 146 EXPECT_TRUE(item.checked);
150 EXPECT_FALSE(item.hidden); 147 EXPECT_FALSE(item.hidden);
151 } 148 }
152 149
153 // Test that we don't show "No more suggestions from Google" if the spelling 150 // Test that we don't show "No more suggestions from Google" if the spelling
154 // service is enabled and that there is only one suggestion. 151 // service is enabled and that there is only one suggestion.
155 IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest, 152 IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest,
156 NoMoreSuggestionsNotDisplayed) { 153 NoMoreSuggestionsNotDisplayed) {
157 menu()->GetPrefs()->SetBoolean( 154 menu()->GetPrefs()->SetBoolean(
158 spellcheck::prefs::kSpellCheckUseSpellingService, true); 155 spellcheck::prefs::kSpellCheckUseSpellingService, true);
159 156
160 // Force a non-empty locale so SPELLCHECK is available. 157 // Force a non-empty locale so SPELLCHECK is available.
161 base::ListValue dictionary; 158 base::ListValue dictionary;
162 dictionary.AppendString("en"); 159 dictionary.AppendString("en");
163 menu()->GetPrefs()->Set(spellcheck::prefs::kSpellCheckDictionaries, 160 menu()->GetPrefs()->Set(spellcheck::prefs::kSpellCheckDictionaries,
164 dictionary); 161 dictionary);
165 162
166 EXPECT_TRUE(SpellingServiceClient::IsAvailable( 163 EXPECT_TRUE(SpellingServiceClient::IsAvailable(
167 menu()->GetBrowserContext(), SpellingServiceClient::SPELLCHECK)); 164 menu()->GetBrowserContext(), SpellingServiceClient::SPELLCHECK));
168 InitMenu("asdfkj", "asdf"); 165 InitMenu("asdfkj", "asdf");
169 166
170 // The test should see a separator, suggestion, "Add to dictionary", 167 // The test should see a suggestion, separator, "Add to dictionary",
171 // "Ask Google for suggestions", and a separator. Possibly more items (not 168 // "Ask Google for suggestions".
172 // relevant here). 169 // Possibly more items (not relevant here).
173 EXPECT_LT(3U, menu()->GetMenuSize()); 170 EXPECT_LT(3U, menu()->GetMenuSize());
174 171
175 MockRenderViewContextMenu::MockMenuItem item; 172 MockRenderViewContextMenu::MockMenuItem item;
176 menu()->GetMenuItem(0, &item); 173 menu()->GetMenuItem(0, &item);
177 EXPECT_EQ(-1, item.command_id); 174 EXPECT_EQ(-1, item.command_id);
178 EXPECT_FALSE(item.enabled); 175 EXPECT_FALSE(item.enabled);
179 EXPECT_FALSE(item.hidden); 176 EXPECT_FALSE(item.hidden);
180 177
181 menu()->GetMenuItem(1, &item); 178 menu()->GetMenuItem(1, &item);
182 EXPECT_EQ(IDC_SPELLCHECK_SUGGESTION_0, item.command_id); 179 EXPECT_EQ(IDC_SPELLCHECK_SUGGESTION_0, item.command_id);
183 EXPECT_TRUE(item.enabled); 180 EXPECT_TRUE(item.enabled);
184 EXPECT_FALSE(item.hidden); 181 EXPECT_FALSE(item.hidden);
185 182
186 menu()->GetMenuItem(2, &item); 183 menu()->GetMenuItem(2, &item);
187 EXPECT_EQ(-1, item.command_id); 184 EXPECT_EQ(-1, item.command_id);
188 EXPECT_FALSE(item.enabled); 185 EXPECT_FALSE(item.enabled);
189 EXPECT_FALSE(item.hidden); 186 EXPECT_FALSE(item.hidden);
190 187
191 menu()->GetMenuItem(3, &item); 188 menu()->GetMenuItem(3, &item);
192 EXPECT_EQ(IDC_SPELLCHECK_ADD_TO_DICTIONARY, item.command_id); 189 EXPECT_EQ(IDC_SPELLCHECK_ADD_TO_DICTIONARY, item.command_id);
193 EXPECT_TRUE(item.enabled); 190 EXPECT_TRUE(item.enabled);
194 EXPECT_FALSE(item.hidden); 191 EXPECT_FALSE(item.hidden);
195 192
196 menu()->GetMenuItem(4, &item); 193 menu()->GetMenuItem(4, &item);
197 EXPECT_EQ(IDC_CONTENT_CONTEXT_SPELLING_TOGGLE, item.command_id); 194 EXPECT_EQ(IDC_CONTENT_CONTEXT_SPELLING_TOGGLE, item.command_id);
198 EXPECT_TRUE(item.enabled); 195 EXPECT_TRUE(item.enabled);
199 EXPECT_TRUE(item.checked); 196 EXPECT_TRUE(item.checked);
200 EXPECT_FALSE(item.hidden); 197 EXPECT_FALSE(item.hidden);
201
202 menu()->GetMenuItem(5, &item);
203 EXPECT_EQ(-1, item.command_id);
204 EXPECT_FALSE(item.enabled);
205 EXPECT_FALSE(item.hidden);
206 } 198 }
207 199
208 // Test that "Ask Google For Suggestions" is grayed out when using an 200 // Test that "Ask Google For Suggestions" is grayed out when using an
209 // off the record profile. 201 // off the record profile.
210 // TODO(rlp): Include graying out of autocorrect in this test when autocorrect 202 // TODO(rlp): Include graying out of autocorrect in this test when autocorrect
211 // is functional. 203 // is functional.
212 IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest, 204 IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest,
213 NoSpellingServiceWhenOffTheRecord) { 205 NoSpellingServiceWhenOffTheRecord) {
214 // Create a menu in an incognito profile. 206 // Create a menu in an incognito profile.
215 Reset(true); 207 Reset(true);
(...skipping 14 matching lines...) Expand all
230 menu()->GetBrowserContext(), SpellingServiceClient::SUGGEST)); 222 menu()->GetBrowserContext(), SpellingServiceClient::SUGGEST));
231 EXPECT_FALSE(SpellingServiceClient::IsAvailable( 223 EXPECT_FALSE(SpellingServiceClient::IsAvailable(
232 menu()->GetBrowserContext(), SpellingServiceClient::SPELLCHECK)); 224 menu()->GetBrowserContext(), SpellingServiceClient::SPELLCHECK));
233 225
234 InitMenu("sjxdjiiiiii", nullptr); 226 InitMenu("sjxdjiiiiii", nullptr);
235 227
236 // There should not be a "No more Google suggestions" (from SpellingService) 228 // There should not be a "No more Google suggestions" (from SpellingService)
237 // or a separator. The next 2 items should be "Add to Dictionary" followed 229 // or a separator. The next 2 items should be "Add to Dictionary" followed
238 // by "Ask Google for suggestions" which should be disabled. 230 // by "Ask Google for suggestions" which should be disabled.
239 // TODO(rlp): add autocorrect here when it is functional. 231 // TODO(rlp): add autocorrect here when it is functional.
240 EXPECT_LT(2U, menu()->GetMenuSize()); 232 EXPECT_LT(1U, menu()->GetMenuSize());
241 233
242 MockRenderViewContextMenu::MockMenuItem item; 234 MockRenderViewContextMenu::MockMenuItem item;
243 menu()->GetMenuItem(0, &item); 235 menu()->GetMenuItem(0, &item);
244 EXPECT_EQ(IDC_SPELLCHECK_ADD_TO_DICTIONARY, item.command_id); 236 EXPECT_EQ(IDC_SPELLCHECK_ADD_TO_DICTIONARY, item.command_id);
245 EXPECT_TRUE(item.enabled); 237 EXPECT_TRUE(item.enabled);
246 EXPECT_FALSE(item.hidden); 238 EXPECT_FALSE(item.hidden);
247 239
248 menu()->GetMenuItem(1, &item); 240 menu()->GetMenuItem(1, &item);
249 EXPECT_EQ(IDC_CONTENT_CONTEXT_SPELLING_TOGGLE, item.command_id); 241 EXPECT_EQ(IDC_CONTENT_CONTEXT_SPELLING_TOGGLE, item.command_id);
250 EXPECT_FALSE(item.enabled); 242 EXPECT_FALSE(item.enabled);
251 EXPECT_FALSE(item.hidden); 243 EXPECT_FALSE(item.hidden);
252 } 244 }
253 245
254 // Test that the menu is preceeded by a separator if there are any suggestions, 246 // Test that the menu is preceeded by a separator if there are any suggestions,
255 // or if the SpellingServiceClient is available 247 // or if the SpellingServiceClient is available
256 IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest, SuggestionsForceTopSeparator) { 248 IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest, SuggestionsForceTopSeparator) {
257 menu()->GetPrefs()->SetBoolean( 249 menu()->GetPrefs()->SetBoolean(
258 spellcheck::prefs::kSpellCheckUseSpellingService, false); 250 spellcheck::prefs::kSpellCheckUseSpellingService, false);
259 251
260 // First case: Misspelled word, no suggestions, no spellcheck service. 252 // First case: Misspelled word, no suggestions, no spellcheck service.
261 InitMenu("asdfkj", nullptr); 253 InitMenu("asdfkj", nullptr);
262 // See SpellingMenuObserverTest.InitMenuWithMisspelledWord on why 3 items. 254 // See SpellingMenuObserverTest.InitMenuWithMisspelledWord on why 2 items.
263 EXPECT_EQ(3U, menu()->GetMenuSize()); 255 EXPECT_EQ(2U, menu()->GetMenuSize());
264 MockRenderViewContextMenu::MockMenuItem item; 256 MockRenderViewContextMenu::MockMenuItem item;
265 menu()->GetMenuItem(0, &item); 257 menu()->GetMenuItem(0, &item);
266 EXPECT_NE(-1, item.command_id); 258 EXPECT_NE(-1, item.command_id);
267 259
268 // Case #2. Misspelled word, suggestions, no spellcheck service. 260 // Case #2. Misspelled word, suggestions, no spellcheck service.
269 Reset(false); 261 Reset(false);
270 menu()->GetPrefs()->SetBoolean( 262 menu()->GetPrefs()->SetBoolean(
271 spellcheck::prefs::kSpellCheckUseSpellingService, false); 263 spellcheck::prefs::kSpellCheckUseSpellingService, false);
272 InitMenu("asdfkj", "asdf"); 264 InitMenu("asdfkj", "asdf");
273 265
274 // Expect at least separator and 4 default entries. 266 // Expect at least separator and 4 default entries.
275 EXPECT_LT(4U, menu()->GetMenuSize()); 267 EXPECT_LT(4U, menu()->GetMenuSize());
276 // This test only cares that the first one is a separator. 268 // This test only cares that the first one is a separator.
277 menu()->GetMenuItem(0, &item); 269 menu()->GetMenuItem(0, &item);
278 EXPECT_EQ(-1, item.command_id); 270 EXPECT_EQ(-1, item.command_id);
279 271
280 // Case #3. Misspelled word, suggestion service is on. 272 // Case #3. Misspelled word, suggestion service is on.
281 Reset(false); 273 Reset(false);
282 ForceSuggestMode(); 274 ForceSuggestMode();
283 InitMenu("asdfkj", nullptr); 275 InitMenu("asdfkj", nullptr);
284 276
285 // Should have at least 2 entries. Separator, suggestion. 277 // Should have at least 2 entries. Separator, suggestion.
286 EXPECT_LT(2U, menu()->GetMenuSize()); 278 EXPECT_LT(2U, menu()->GetMenuSize());
287 menu()->GetMenuItem(0, &item); 279 menu()->GetMenuItem(0, &item);
288 EXPECT_EQ(-1, item.command_id); 280 EXPECT_EQ(-1, item.command_id);
289 menu()->GetMenuItem(1, &item); 281 menu()->GetMenuItem(1, &item);
290 EXPECT_EQ(IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION, item.command_id); 282 EXPECT_EQ(IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION, item.command_id);
291 } 283 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698