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/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
(...skipping 26 matching lines...) Expand all Loading... |
37 checked(false), | 37 checked(false), |
38 hidden(true) { | 38 hidden(true) { |
39 } | 39 } |
40 int command_id; | 40 int command_id; |
41 bool enabled; | 41 bool enabled; |
42 bool checked; | 42 bool checked; |
43 bool hidden; | 43 bool hidden; |
44 string16 title; | 44 string16 title; |
45 }; | 45 }; |
46 | 46 |
47 MockRenderViewContextMenu(); | 47 explicit MockRenderViewContextMenu(bool incognito); |
48 virtual ~MockRenderViewContextMenu(); | 48 virtual ~MockRenderViewContextMenu(); |
49 | 49 |
50 // RenderViewContextMenuProxy implementation. | 50 // RenderViewContextMenuProxy implementation. |
51 virtual void AddMenuItem(int command_id, const string16& title) OVERRIDE; | 51 virtual void AddMenuItem(int command_id, const string16& title) OVERRIDE; |
52 virtual void AddCheckItem(int command_id, const string16& title) OVERRIDE; | 52 virtual void AddCheckItem(int command_id, const string16& title) OVERRIDE; |
53 virtual void AddSeparator() OVERRIDE; | 53 virtual void AddSeparator() OVERRIDE; |
54 virtual void AddSubMenu(int command_id, | 54 virtual void AddSubMenu(int command_id, |
55 const string16& label, | 55 const string16& label, |
56 ui::MenuModel* model) OVERRIDE; | 56 ui::MenuModel* model) OVERRIDE; |
57 virtual void UpdateMenuItem(int command_id, | 57 virtual void UpdateMenuItem(int command_id, |
(...skipping 24 matching lines...) Expand all Loading... |
82 // A dummy profile used in this test. Call GetPrefs() when a test needs to | 82 // A dummy profile used in this test. Call GetPrefs() when a test needs to |
83 // change this profile and use PrefService methods. | 83 // change this profile and use PrefService methods. |
84 scoped_ptr<TestingProfile> profile_; | 84 scoped_ptr<TestingProfile> profile_; |
85 | 85 |
86 // A list of menu items added by the SpellingMenuObserver class. | 86 // A list of menu items added by the SpellingMenuObserver class. |
87 std::vector<MockMenuItem> items_; | 87 std::vector<MockMenuItem> items_; |
88 | 88 |
89 DISALLOW_COPY_AND_ASSIGN(MockRenderViewContextMenu); | 89 DISALLOW_COPY_AND_ASSIGN(MockRenderViewContextMenu); |
90 }; | 90 }; |
91 | 91 |
92 MockRenderViewContextMenu::MockRenderViewContextMenu() | 92 MockRenderViewContextMenu::MockRenderViewContextMenu(bool incognito) |
93 : observer_(NULL), | 93 : observer_(NULL) { |
94 profile_(new TestingProfile) { | 94 TestingProfile::Builder builder; |
| 95 if (incognito) |
| 96 builder.SetIncognito(); |
| 97 profile_ = builder.Build(); |
95 } | 98 } |
96 | 99 |
97 MockRenderViewContextMenu::~MockRenderViewContextMenu() { | 100 MockRenderViewContextMenu::~MockRenderViewContextMenu() { |
98 } | 101 } |
99 | 102 |
100 void MockRenderViewContextMenu::AddMenuItem(int command_id, | 103 void MockRenderViewContextMenu::AddMenuItem(int command_id, |
101 const string16& title) { | 104 const string16& title) { |
102 MockMenuItem item; | 105 MockMenuItem item; |
103 item.command_id = command_id; | 106 item.command_id = command_id; |
104 item.enabled = observer_->IsCommandIdEnabled(command_id); | 107 item.enabled = observer_->IsCommandIdEnabled(command_id); |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 return profile_->GetPrefs(); | 198 return profile_->GetPrefs(); |
196 } | 199 } |
197 | 200 |
198 // A test class used in this file. This test should be a browser test because it | 201 // A test class used in this file. This test should be a browser test because it |
199 // accesses resources. | 202 // accesses resources. |
200 class SpellingMenuObserverTest : public InProcessBrowserTest { | 203 class SpellingMenuObserverTest : public InProcessBrowserTest { |
201 public: | 204 public: |
202 SpellingMenuObserverTest(); | 205 SpellingMenuObserverTest(); |
203 | 206 |
204 virtual void SetUpOnMainThread() OVERRIDE { | 207 virtual void SetUpOnMainThread() OVERRIDE { |
205 Reset(); | 208 Reset(false); |
206 } | 209 } |
207 | 210 |
208 virtual void CleanUpOnMainThread() OVERRIDE { | 211 virtual void CleanUpOnMainThread() OVERRIDE { |
209 observer_.reset(); | 212 observer_.reset(); |
210 menu_.reset(); | 213 menu_.reset(); |
211 } | 214 } |
212 | 215 |
213 void Reset() { | 216 void Reset(bool incognito) { |
214 observer_.reset(); | 217 observer_.reset(); |
215 menu_.reset(new MockRenderViewContextMenu); | 218 menu_.reset(new MockRenderViewContextMenu(incognito)); |
216 observer_.reset(new SpellingMenuObserver(menu_.get())); | 219 observer_.reset(new SpellingMenuObserver(menu_.get())); |
217 menu_->SetObserver(observer_.get()); | 220 menu_->SetObserver(observer_.get()); |
218 } | 221 } |
219 | 222 |
220 void InitMenu(const char* word, const char* suggestion) { | 223 void InitMenu(const char* word, const char* suggestion) { |
221 content::ContextMenuParams params; | 224 content::ContextMenuParams params; |
222 params.is_editable = true; | 225 params.is_editable = true; |
223 params.misspelled_word = ASCIIToUTF16(word); | 226 params.misspelled_word = ASCIIToUTF16(word); |
224 params.dictionary_suggestions.clear(); | 227 params.dictionary_suggestions.clear(); |
225 if (suggestion) | 228 if (suggestion) |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
388 EXPECT_FALSE(item.enabled); | 391 EXPECT_FALSE(item.enabled); |
389 EXPECT_FALSE(item.hidden); | 392 EXPECT_FALSE(item.hidden); |
390 } | 393 } |
391 | 394 |
392 // Test that "Ask Google For Suggestions" is grayed out when using an | 395 // Test that "Ask Google For Suggestions" is grayed out when using an |
393 // off the record profile. | 396 // off the record profile. |
394 // TODO(rlp): Include graying out of autocorrect in this test when autocorrect | 397 // TODO(rlp): Include graying out of autocorrect in this test when autocorrect |
395 // is functional. | 398 // is functional. |
396 IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest, | 399 IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest, |
397 NoSpellingServiceWhenOffTheRecord) { | 400 NoSpellingServiceWhenOffTheRecord) { |
398 menu()->GetProfile()->AsTestingProfile()->set_incognito(true); | 401 // Create a menu in an incognito profile. |
| 402 Reset(true); |
399 | 403 |
400 // This means spellchecking is allowed. Default is that the service is | 404 // This means spellchecking is allowed. Default is that the service is |
401 // contacted but this test makes sure that if profile is incognito, that | 405 // contacted but this test makes sure that if profile is incognito, that |
402 // is not an option. | 406 // is not an option. |
403 menu()->GetPrefs()->SetBoolean(prefs::kSpellCheckUseSpellingService, true); | 407 menu()->GetPrefs()->SetBoolean(prefs::kSpellCheckUseSpellingService, true); |
404 | 408 |
405 // Force a non-empty locale so SUGGEST normally would be available. | 409 // Force a non-empty locale so SUGGEST normally would be available. |
406 menu()->GetPrefs()->SetString(prefs::kSpellCheckDictionary, "en"); | 410 menu()->GetPrefs()->SetString(prefs::kSpellCheckDictionary, "en"); |
407 EXPECT_FALSE(SpellingServiceClient::IsAvailable(menu()->GetProfile(), | 411 EXPECT_FALSE(SpellingServiceClient::IsAvailable(menu()->GetProfile(), |
408 SpellingServiceClient::SUGGEST)); | 412 SpellingServiceClient::SUGGEST)); |
(...skipping 17 matching lines...) Expand all Loading... |
426 | 430 |
427 menu()->GetMenuItem(1, &item); | 431 menu()->GetMenuItem(1, &item); |
428 EXPECT_EQ(IDC_SPELLCHECK_ADD_TO_DICTIONARY, item.command_id); | 432 EXPECT_EQ(IDC_SPELLCHECK_ADD_TO_DICTIONARY, item.command_id); |
429 EXPECT_TRUE(item.enabled); | 433 EXPECT_TRUE(item.enabled); |
430 EXPECT_FALSE(item.hidden); | 434 EXPECT_FALSE(item.hidden); |
431 | 435 |
432 menu()->GetMenuItem(2, &item); | 436 menu()->GetMenuItem(2, &item); |
433 EXPECT_EQ(IDC_CONTENT_CONTEXT_SPELLING_TOGGLE, item.command_id); | 437 EXPECT_EQ(IDC_CONTENT_CONTEXT_SPELLING_TOGGLE, item.command_id); |
434 EXPECT_FALSE(item.enabled); | 438 EXPECT_FALSE(item.enabled); |
435 EXPECT_FALSE(item.hidden); | 439 EXPECT_FALSE(item.hidden); |
436 | |
437 // Set incognito back to false to allow appropriate test cleanup. | |
438 menu()->GetProfile()->AsTestingProfile()->set_incognito(false); | |
439 } | 440 } |
440 | 441 |
441 // Test that the menu is preceeded by a separator if there are any suggestions, | 442 // Test that the menu is preceeded by a separator if there are any suggestions, |
442 // or if the SpellingServiceClient is available | 443 // or if the SpellingServiceClient is available |
443 IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest, SuggestionsForceTopSeparator) { | 444 IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest, SuggestionsForceTopSeparator) { |
444 menu()->GetPrefs()->SetBoolean(prefs::kSpellCheckUseSpellingService, false); | 445 menu()->GetPrefs()->SetBoolean(prefs::kSpellCheckUseSpellingService, false); |
445 | 446 |
446 // First case: Misspelled word, no suggestions, no spellcheck service. | 447 // First case: Misspelled word, no suggestions, no spellcheck service. |
447 InitMenu("asdfkj", NULL); | 448 InitMenu("asdfkj", NULL); |
448 // See SpellingMenuObserverTest.InitMenuWithMisspelledWord on why 4 items. | 449 // See SpellingMenuObserverTest.InitMenuWithMisspelledWord on why 4 items. |
449 EXPECT_EQ(static_cast<size_t>(4), menu()->GetMenuSize()); | 450 EXPECT_EQ(static_cast<size_t>(4), menu()->GetMenuSize()); |
450 MockRenderViewContextMenu::MockMenuItem item; | 451 MockRenderViewContextMenu::MockMenuItem item; |
451 menu()->GetMenuItem(0, &item); | 452 menu()->GetMenuItem(0, &item); |
452 EXPECT_NE(-1, item.command_id); | 453 EXPECT_NE(-1, item.command_id); |
453 | 454 |
454 // Case #2. Misspelled word, suggestions, no spellcheck service. | 455 // Case #2. Misspelled word, suggestions, no spellcheck service. |
455 Reset(); | 456 Reset(false); |
456 menu()->GetPrefs()->SetBoolean(prefs::kSpellCheckUseSpellingService, false); | 457 menu()->GetPrefs()->SetBoolean(prefs::kSpellCheckUseSpellingService, false); |
457 InitMenu("asdfkj", "asdf"); | 458 InitMenu("asdfkj", "asdf"); |
458 | 459 |
459 // Expect at least separator and 4 default entries. | 460 // Expect at least separator and 4 default entries. |
460 EXPECT_LT(static_cast<size_t>(5), menu()->GetMenuSize()); | 461 EXPECT_LT(static_cast<size_t>(5), menu()->GetMenuSize()); |
461 // This test only cares that the first one is a separator. | 462 // This test only cares that the first one is a separator. |
462 menu()->GetMenuItem(0, &item); | 463 menu()->GetMenuItem(0, &item); |
463 EXPECT_EQ(-1, item.command_id); | 464 EXPECT_EQ(-1, item.command_id); |
464 | 465 |
465 // Case #3. Misspelled word, suggestion service is on. | 466 // Case #3. Misspelled word, suggestion service is on. |
466 Reset(); | 467 Reset(false); |
467 menu()->GetPrefs()->SetBoolean(prefs::kSpellCheckUseSpellingService, true); | 468 menu()->GetPrefs()->SetBoolean(prefs::kSpellCheckUseSpellingService, true); |
468 CommandLine* command_line = CommandLine::ForCurrentProcess(); | 469 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
469 command_line->AppendSwitch(switches::kUseSpellingSuggestions); | 470 command_line->AppendSwitch(switches::kUseSpellingSuggestions); |
470 InitMenu("asdfkj", NULL); | 471 InitMenu("asdfkj", NULL); |
471 | 472 |
472 // Should have at least 2 entries. Separator, suggestion. | 473 // Should have at least 2 entries. Separator, suggestion. |
473 EXPECT_LT(2U, menu()->GetMenuSize()); | 474 EXPECT_LT(2U, menu()->GetMenuSize()); |
474 menu()->GetMenuItem(0, &item); | 475 menu()->GetMenuItem(0, &item); |
475 EXPECT_EQ(-1, item.command_id); | 476 EXPECT_EQ(-1, item.command_id); |
476 menu()->GetMenuItem(1, &item); | 477 menu()->GetMenuItem(1, &item); |
477 EXPECT_EQ(IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION, item.command_id); | 478 EXPECT_EQ(IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION, item.command_id); |
478 } | 479 } |
OLD | NEW |