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 <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
257 } | 257 } |
258 | 258 |
259 } // namespace | 259 } // namespace |
260 | 260 |
261 // Tests that right-clicking a correct word does not add any items. | 261 // Tests that right-clicking a correct word does not add any items. |
262 IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest, InitMenuWithCorrectWord) { | 262 IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest, InitMenuWithCorrectWord) { |
263 InitMenu("", NULL); | 263 InitMenu("", NULL); |
264 EXPECT_EQ(static_cast<size_t>(0), menu()->GetMenuSize()); | 264 EXPECT_EQ(static_cast<size_t>(0), menu()->GetMenuSize()); |
265 } | 265 } |
266 | 266 |
267 // Tests that right-clicking a misspelled word adds four items: | 267 // Tests that right-clicking a misspelled word adds three items: |
268 // "No spelling suggestions", "Add to dictionary", "Ask Google for suggestions", | 268 // "Add to dictionary", "Ask Google for suggestions", and a separator. |
269 // and a separator. | |
270 IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest, InitMenuWithMisspelledWord) { | 269 IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest, InitMenuWithMisspelledWord) { |
271 InitMenu("wiimode", NULL); | 270 InitMenu("wiimode", NULL); |
272 EXPECT_EQ(static_cast<size_t>(4), menu()->GetMenuSize()); | 271 EXPECT_EQ(3U, menu()->GetMenuSize()); |
273 | 272 |
274 // Read all the context-menu items added by this test and verify they are | 273 // Read all the context-menu items added by this test and verify they are |
275 // expected ones. We do not check the item titles to prevent resource changes | 274 // expected ones. We do not check the item titles to prevent resource changes |
276 // from breaking this test. (I think it is not expected by those who change | 275 // from breaking this test. (I think it is not expected by those who change |
277 // resources.) | 276 // resources.) |
278 MockRenderViewContextMenu::MockMenuItem item; | 277 MockRenderViewContextMenu::MockMenuItem item; |
279 menu()->GetMenuItem(0, &item); | 278 menu()->GetMenuItem(0, &item); |
280 EXPECT_EQ(IDC_CONTENT_CONTEXT_NO_SPELLING_SUGGESTIONS, item.command_id); | |
281 EXPECT_FALSE(item.enabled); | |
282 EXPECT_FALSE(item.hidden); | |
283 menu()->GetMenuItem(1, &item); | |
284 EXPECT_EQ(IDC_SPELLCHECK_ADD_TO_DICTIONARY, item.command_id); | 279 EXPECT_EQ(IDC_SPELLCHECK_ADD_TO_DICTIONARY, item.command_id); |
285 EXPECT_TRUE(item.enabled); | 280 EXPECT_TRUE(item.enabled); |
286 EXPECT_FALSE(item.hidden); | 281 EXPECT_FALSE(item.hidden); |
287 menu()->GetMenuItem(2, &item); | 282 menu()->GetMenuItem(1, &item); |
288 EXPECT_EQ(IDC_CONTENT_CONTEXT_SPELLING_TOGGLE, item.command_id); | 283 EXPECT_EQ(IDC_CONTENT_CONTEXT_SPELLING_TOGGLE, item.command_id); |
289 EXPECT_TRUE(item.enabled); | 284 EXPECT_TRUE(item.enabled); |
290 EXPECT_FALSE(item.checked); | 285 EXPECT_FALSE(item.checked); |
291 EXPECT_FALSE(item.hidden); | 286 EXPECT_FALSE(item.hidden); |
292 menu()->GetMenuItem(3, &item); | 287 menu()->GetMenuItem(2, &item); |
293 EXPECT_EQ(-1, item.command_id); | 288 EXPECT_EQ(-1, item.command_id); |
294 EXPECT_FALSE(item.enabled); | 289 EXPECT_FALSE(item.enabled); |
295 EXPECT_FALSE(item.hidden); | 290 EXPECT_FALSE(item.hidden); |
296 } | 291 } |
297 | 292 |
298 // Tests that right-clicking a correct word when we enable spelling-service | 293 // Tests that right-clicking a correct word when we enable spelling-service |
299 // integration to verify an item "Ask Google for suggestions" is checked. Even | 294 // integration to verify an item "Ask Google for suggestions" is checked. Even |
300 // though this meanu itself does not add this item, its sub-menu adds the item | 295 // though this meanu itself does not add this item, its sub-menu adds the item |
301 // and calls SpellingMenuObserver::IsChecked() to check it. | 296 // and calls SpellingMenuObserver::IsChecked() to check it. |
302 IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest, | 297 IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest, |
303 EnableSpellingServiceWithCorrectWord) { | 298 EnableSpellingServiceWithCorrectWord) { |
304 menu()->GetPrefs()->SetBoolean(prefs::kSpellCheckUseSpellingService, true); | 299 menu()->GetPrefs()->SetBoolean(prefs::kSpellCheckUseSpellingService, true); |
305 InitMenu("", NULL); | 300 InitMenu("", NULL); |
306 | 301 |
307 EXPECT_TRUE( | 302 EXPECT_TRUE( |
308 observer()->IsCommandIdChecked(IDC_CONTENT_CONTEXT_SPELLING_TOGGLE)); | 303 observer()->IsCommandIdChecked(IDC_CONTENT_CONTEXT_SPELLING_TOGGLE)); |
309 } | 304 } |
310 | 305 |
311 // Tests that right-clicking a misspelled word when we enable spelling-service | 306 // Tests that right-clicking a misspelled word when we enable spelling-service |
312 // integration to verify an item "Ask Google for suggestions" is checked. (This | 307 // integration to verify an item "Ask Google for suggestions" is checked. (This |
313 // test does not actually send JSON-RPC requests to the service because it makes | 308 // test does not actually send JSON-RPC requests to the service because it makes |
314 // this test flaky.) | 309 // this test flaky.) |
315 IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest, EnableSpellingService) { | 310 IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest, EnableSpellingService) { |
316 menu()->GetPrefs()->SetBoolean(prefs::kSpellCheckUseSpellingService, true); | 311 menu()->GetPrefs()->SetBoolean(prefs::kSpellCheckUseSpellingService, true); |
317 base::ListValue dictionary; | 312 base::ListValue dictionary; |
318 menu()->GetPrefs()->Set(prefs::kSpellCheckDictionaries, dictionary); | 313 menu()->GetPrefs()->Set(prefs::kSpellCheckDictionaries, dictionary); |
319 | 314 |
320 InitMenu("wiimode", NULL); | 315 InitMenu("wiimode", NULL); |
321 EXPECT_EQ(static_cast<size_t>(4), menu()->GetMenuSize()); | 316 EXPECT_EQ(3U, menu()->GetMenuSize()); |
322 | 317 |
323 // To avoid duplicates, this test reads only the "Ask Google for suggestions" | 318 // To avoid duplicates, this test reads only the "Ask Google for suggestions" |
324 // item and verifies it is enabled and checked. | 319 // item and verifies it is enabled and checked. |
325 MockRenderViewContextMenu::MockMenuItem item; | 320 MockRenderViewContextMenu::MockMenuItem item; |
326 menu()->GetMenuItem(2, &item); | 321 menu()->GetMenuItem(1, &item); |
327 EXPECT_EQ(IDC_CONTENT_CONTEXT_SPELLING_TOGGLE, item.command_id); | 322 EXPECT_EQ(IDC_CONTENT_CONTEXT_SPELLING_TOGGLE, item.command_id); |
328 EXPECT_TRUE(item.enabled); | 323 EXPECT_TRUE(item.enabled); |
329 EXPECT_TRUE(item.checked); | 324 EXPECT_TRUE(item.checked); |
330 EXPECT_FALSE(item.hidden); | 325 EXPECT_FALSE(item.hidden); |
331 } | 326 } |
332 | 327 |
333 // Test that there will be a separator after "no suggestions" if | |
334 // SpellingServiceClient::SUGGEST is on. | |
335 IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest, SeparatorAfterSuggestions) { | |
336 ForceSuggestMode(); | |
337 InitMenu("jhhj", NULL); | |
338 | |
339 // The test should see a top separator, "No spelling suggestions", | |
340 // "No more Google suggestions" (from SpellingService) and a separator | |
341 // as the first four items, then possibly more (not relevant here). | |
342 EXPECT_LT(4U, menu()->GetMenuSize()); | |
343 | |
344 MockRenderViewContextMenu::MockMenuItem item; | |
345 menu()->GetMenuItem(0, &item); | |
346 EXPECT_EQ(-1, item.command_id); | |
347 EXPECT_FALSE(item.enabled); | |
348 EXPECT_FALSE(item.hidden); | |
349 | |
350 menu()->GetMenuItem(1, &item); | |
351 EXPECT_EQ(IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION, item.command_id); | |
352 EXPECT_FALSE(item.enabled); | |
353 EXPECT_FALSE(item.hidden); | |
354 | |
355 menu()->GetMenuItem(2, &item); | |
356 EXPECT_EQ(IDC_CONTENT_CONTEXT_NO_SPELLING_SUGGESTIONS, item.command_id); | |
357 EXPECT_FALSE(item.enabled); | |
358 EXPECT_FALSE(item.hidden); | |
359 | |
360 menu()->GetMenuItem(3, &item); | |
361 EXPECT_EQ(-1, item.command_id); | |
362 EXPECT_FALSE(item.enabled); | |
363 EXPECT_FALSE(item.hidden); | |
364 } | |
365 | |
366 // Test that we don't show "No more suggestions from Google" if the spelling | 328 // Test that we don't show "No more suggestions from Google" if the spelling |
367 // service is enabled and that there is only one suggestion. | 329 // service is enabled and that there is only one suggestion. |
368 IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest, | 330 IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest, |
369 NoMoreSuggestionsNotDisplayed) { | 331 NoMoreSuggestionsNotDisplayed) { |
370 menu()->GetPrefs()->SetBoolean(prefs::kSpellCheckUseSpellingService, true); | 332 menu()->GetPrefs()->SetBoolean(prefs::kSpellCheckUseSpellingService, true); |
371 | 333 |
372 // Force a non-empty locale so SPELLCHECK is available. | 334 // Force a non-empty locale so SPELLCHECK is available. |
373 base::ListValue dictionary; | 335 base::ListValue dictionary; |
374 dictionary.AppendString("en"); | 336 dictionary.AppendString("en"); |
375 menu()->GetPrefs()->Set(prefs::kSpellCheckDictionaries, dictionary); | 337 menu()->GetPrefs()->Set(prefs::kSpellCheckDictionaries, dictionary); |
376 | 338 |
377 EXPECT_TRUE(SpellingServiceClient::IsAvailable( | 339 EXPECT_TRUE(SpellingServiceClient::IsAvailable( |
378 menu()->GetBrowserContext(), SpellingServiceClient::SPELLCHECK)); | 340 menu()->GetBrowserContext(), SpellingServiceClient::SPELLCHECK)); |
379 InitMenu("asdfkj", "asdf"); | 341 InitMenu("asdfkj", "asdf"); |
380 | 342 |
381 // The test should see a separator, a suggestion and another separator | 343 // The test should see a separator, suggestion, "Add to dictionary", |
382 // as the first two items, then possibly more (not relevant here). | 344 // "Ask Google for suggestions", and a separator. Possibly more items (not |
| 345 // relevant here). |
383 EXPECT_LT(3U, menu()->GetMenuSize()); | 346 EXPECT_LT(3U, menu()->GetMenuSize()); |
384 | 347 |
385 MockRenderViewContextMenu::MockMenuItem item; | 348 MockRenderViewContextMenu::MockMenuItem item; |
386 menu()->GetMenuItem(0, &item); | 349 menu()->GetMenuItem(0, &item); |
387 EXPECT_EQ(-1, item.command_id); | 350 EXPECT_EQ(-1, item.command_id); |
388 EXPECT_FALSE(item.enabled); | 351 EXPECT_FALSE(item.enabled); |
389 EXPECT_FALSE(item.hidden); | 352 EXPECT_FALSE(item.hidden); |
390 | 353 |
391 menu()->GetMenuItem(1, &item); | 354 menu()->GetMenuItem(1, &item); |
392 EXPECT_EQ(IDC_SPELLCHECK_SUGGESTION_0, item.command_id); | 355 EXPECT_EQ(IDC_SPELLCHECK_SUGGESTION_0, item.command_id); |
393 EXPECT_TRUE(item.enabled); | 356 EXPECT_TRUE(item.enabled); |
394 EXPECT_FALSE(item.hidden); | 357 EXPECT_FALSE(item.hidden); |
395 | 358 |
396 menu()->GetMenuItem(2, &item); | 359 menu()->GetMenuItem(2, &item); |
| 360 EXPECT_EQ(IDC_SPELLCHECK_ADD_TO_DICTIONARY, item.command_id); |
| 361 EXPECT_TRUE(item.enabled); |
| 362 EXPECT_FALSE(item.hidden); |
| 363 |
| 364 menu()->GetMenuItem(3, &item); |
| 365 EXPECT_EQ(IDC_CONTENT_CONTEXT_SPELLING_TOGGLE, item.command_id); |
| 366 EXPECT_TRUE(item.enabled); |
| 367 EXPECT_TRUE(item.checked); |
| 368 EXPECT_FALSE(item.hidden); |
| 369 |
| 370 menu()->GetMenuItem(4, &item); |
397 EXPECT_EQ(-1, item.command_id); | 371 EXPECT_EQ(-1, item.command_id); |
398 EXPECT_FALSE(item.enabled); | 372 EXPECT_FALSE(item.enabled); |
399 EXPECT_FALSE(item.hidden); | 373 EXPECT_FALSE(item.hidden); |
400 } | 374 } |
401 | 375 |
402 // Test that "Ask Google For Suggestions" is grayed out when using an | 376 // Test that "Ask Google For Suggestions" is grayed out when using an |
403 // off the record profile. | 377 // off the record profile. |
404 // TODO(rlp): Include graying out of autocorrect in this test when autocorrect | 378 // TODO(rlp): Include graying out of autocorrect in this test when autocorrect |
405 // is functional. | 379 // is functional. |
406 IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest, | 380 IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest, |
(...skipping 11 matching lines...) Expand all Loading... |
418 dictionary.AppendString("en"); | 392 dictionary.AppendString("en"); |
419 menu()->GetPrefs()->Set(prefs::kSpellCheckDictionaries, dictionary); | 393 menu()->GetPrefs()->Set(prefs::kSpellCheckDictionaries, dictionary); |
420 | 394 |
421 EXPECT_FALSE(SpellingServiceClient::IsAvailable( | 395 EXPECT_FALSE(SpellingServiceClient::IsAvailable( |
422 menu()->GetBrowserContext(), SpellingServiceClient::SUGGEST)); | 396 menu()->GetBrowserContext(), SpellingServiceClient::SUGGEST)); |
423 EXPECT_FALSE(SpellingServiceClient::IsAvailable( | 397 EXPECT_FALSE(SpellingServiceClient::IsAvailable( |
424 menu()->GetBrowserContext(), SpellingServiceClient::SPELLCHECK)); | 398 menu()->GetBrowserContext(), SpellingServiceClient::SPELLCHECK)); |
425 | 399 |
426 InitMenu("sjxdjiiiiii", NULL); | 400 InitMenu("sjxdjiiiiii", NULL); |
427 | 401 |
428 // The test should see "No spelling suggestions" (from system checker). | 402 // There should not be a "No more Google suggestions" (from SpellingService) |
429 // They should not see "No more Google suggestions" (from SpellingService) or | 403 // or a separator. The next 2 items should be "Add to Dictionary" followed |
430 // a separator. The next 2 items should be "Add to Dictionary" followed | |
431 // by "Ask Google for suggestions" which should be disabled. | 404 // by "Ask Google for suggestions" which should be disabled. |
432 // TODO(rlp): add autocorrect here when it is functional. | 405 // TODO(rlp): add autocorrect here when it is functional. |
433 EXPECT_LT(3U, menu()->GetMenuSize()); | 406 EXPECT_LT(2U, menu()->GetMenuSize()); |
434 | 407 |
435 MockRenderViewContextMenu::MockMenuItem item; | 408 MockRenderViewContextMenu::MockMenuItem item; |
436 menu()->GetMenuItem(0, &item); | 409 menu()->GetMenuItem(0, &item); |
437 EXPECT_EQ(IDC_CONTENT_CONTEXT_NO_SPELLING_SUGGESTIONS, item.command_id); | |
438 EXPECT_FALSE(item.enabled); | |
439 EXPECT_FALSE(item.hidden); | |
440 | |
441 menu()->GetMenuItem(1, &item); | |
442 EXPECT_EQ(IDC_SPELLCHECK_ADD_TO_DICTIONARY, item.command_id); | 410 EXPECT_EQ(IDC_SPELLCHECK_ADD_TO_DICTIONARY, item.command_id); |
443 EXPECT_TRUE(item.enabled); | 411 EXPECT_TRUE(item.enabled); |
444 EXPECT_FALSE(item.hidden); | 412 EXPECT_FALSE(item.hidden); |
445 | 413 |
446 menu()->GetMenuItem(2, &item); | 414 menu()->GetMenuItem(1, &item); |
447 EXPECT_EQ(IDC_CONTENT_CONTEXT_SPELLING_TOGGLE, item.command_id); | 415 EXPECT_EQ(IDC_CONTENT_CONTEXT_SPELLING_TOGGLE, item.command_id); |
448 EXPECT_FALSE(item.enabled); | 416 EXPECT_FALSE(item.enabled); |
449 EXPECT_FALSE(item.hidden); | 417 EXPECT_FALSE(item.hidden); |
450 } | 418 } |
451 | 419 |
452 // Test that the menu is preceeded by a separator if there are any suggestions, | 420 // Test that the menu is preceeded by a separator if there are any suggestions, |
453 // or if the SpellingServiceClient is available | 421 // or if the SpellingServiceClient is available |
454 IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest, SuggestionsForceTopSeparator) { | 422 IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest, SuggestionsForceTopSeparator) { |
455 menu()->GetPrefs()->SetBoolean(prefs::kSpellCheckUseSpellingService, false); | 423 menu()->GetPrefs()->SetBoolean(prefs::kSpellCheckUseSpellingService, false); |
456 | 424 |
457 // First case: Misspelled word, no suggestions, no spellcheck service. | 425 // First case: Misspelled word, no suggestions, no spellcheck service. |
458 InitMenu("asdfkj", NULL); | 426 InitMenu("asdfkj", NULL); |
459 // See SpellingMenuObserverTest.InitMenuWithMisspelledWord on why 4 items. | 427 // See SpellingMenuObserverTest.InitMenuWithMisspelledWord on why 3 items. |
460 EXPECT_EQ(static_cast<size_t>(4), menu()->GetMenuSize()); | 428 EXPECT_EQ(3U, menu()->GetMenuSize()); |
461 MockRenderViewContextMenu::MockMenuItem item; | 429 MockRenderViewContextMenu::MockMenuItem item; |
462 menu()->GetMenuItem(0, &item); | 430 menu()->GetMenuItem(0, &item); |
463 EXPECT_NE(-1, item.command_id); | 431 EXPECT_NE(-1, item.command_id); |
464 | 432 |
465 // Case #2. Misspelled word, suggestions, no spellcheck service. | 433 // Case #2. Misspelled word, suggestions, no spellcheck service. |
466 Reset(false); | 434 Reset(false); |
467 menu()->GetPrefs()->SetBoolean(prefs::kSpellCheckUseSpellingService, false); | 435 menu()->GetPrefs()->SetBoolean(prefs::kSpellCheckUseSpellingService, false); |
468 InitMenu("asdfkj", "asdf"); | 436 InitMenu("asdfkj", "asdf"); |
469 | 437 |
470 // Expect at least separator and 4 default entries. | 438 // Expect at least separator and 4 default entries. |
471 EXPECT_LT(static_cast<size_t>(5), menu()->GetMenuSize()); | 439 EXPECT_LT(4U, menu()->GetMenuSize()); |
472 // This test only cares that the first one is a separator. | 440 // This test only cares that the first one is a separator. |
473 menu()->GetMenuItem(0, &item); | 441 menu()->GetMenuItem(0, &item); |
474 EXPECT_EQ(-1, item.command_id); | 442 EXPECT_EQ(-1, item.command_id); |
475 | 443 |
476 // Case #3. Misspelled word, suggestion service is on. | 444 // Case #3. Misspelled word, suggestion service is on. |
477 Reset(false); | 445 Reset(false); |
478 ForceSuggestMode(); | 446 ForceSuggestMode(); |
479 InitMenu("asdfkj", NULL); | 447 InitMenu("asdfkj", NULL); |
480 | 448 |
481 // Should have at least 2 entries. Separator, suggestion. | 449 // Should have at least 2 entries. Separator, suggestion. |
482 EXPECT_LT(2U, menu()->GetMenuSize()); | 450 EXPECT_LT(2U, menu()->GetMenuSize()); |
483 menu()->GetMenuItem(0, &item); | 451 menu()->GetMenuItem(0, &item); |
484 EXPECT_EQ(-1, item.command_id); | 452 EXPECT_EQ(-1, item.command_id); |
485 menu()->GetMenuItem(1, &item); | 453 menu()->GetMenuItem(1, &item); |
486 EXPECT_EQ(IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION, item.command_id); | 454 EXPECT_EQ(IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION, item.command_id); |
487 } | 455 } |
OLD | NEW |