OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/renderer_context_menu/spelling_options_submenu_observer
.h" |
| 6 |
| 7 #include "base/prefs/pref_service.h" |
| 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "chrome/app/chrome_command_ids.h" |
| 10 #include "chrome/browser/renderer_context_menu/mock_render_view_context_menu.h" |
| 11 #include "chrome/common/pref_names.h" |
| 12 #include "chrome/test/base/in_process_browser_test.h" |
| 13 #include "content/public/common/context_menu_params.h" |
| 14 |
| 15 namespace { |
| 16 |
| 17 // A test class used in this file. This test should be a browser test because it |
| 18 // accesses resources. |
| 19 class SpellingOptionsSubMenuObserverTest : public InProcessBrowserTest { |
| 20 public: |
| 21 SpellingOptionsSubMenuObserverTest() {} |
| 22 ~SpellingOptionsSubMenuObserverTest() override {} |
| 23 |
| 24 void SetUpOnMainThread() override { |
| 25 menu_.reset(new MockRenderViewContextMenu(false)); |
| 26 observer_.reset( |
| 27 new SpellingOptionsSubMenuObserver(menu_.get(), menu_.get(), 1)); |
| 28 menu_->SetObserver(observer_.get()); |
| 29 } |
| 30 |
| 31 void TearDownOnMainThread() override { |
| 32 menu_->SetObserver(nullptr); |
| 33 observer_.reset(); |
| 34 menu_.reset(); |
| 35 } |
| 36 |
| 37 void InitMenu(bool enable_spellcheck, |
| 38 const std::string& accept_languages, |
| 39 const std::vector<std::string>& dictionaries) { |
| 40 menu()->GetPrefs()->SetBoolean(prefs::kEnableContinuousSpellcheck, |
| 41 enable_spellcheck); |
| 42 menu()->GetPrefs()->SetString(prefs::kAcceptLanguages, accept_languages); |
| 43 base::ListValue dictionaries_value; |
| 44 dictionaries_value.AppendStrings(dictionaries); |
| 45 menu()->GetPrefs()->Set(prefs::kSpellCheckDictionaries, dictionaries_value); |
| 46 observer()->InitMenu(content::ContextMenuParams()); |
| 47 } |
| 48 |
| 49 void ExpectPreferences(bool spellcheck_enabled, |
| 50 const std::vector<std::string>& dictionaries) { |
| 51 EXPECT_EQ(spellcheck_enabled, menu()->GetPrefs()->GetBoolean( |
| 52 prefs::kEnableContinuousSpellcheck)); |
| 53 base::ListValue dictionaries_value; |
| 54 dictionaries_value.AppendStrings(dictionaries); |
| 55 EXPECT_TRUE(dictionaries_value.Equals( |
| 56 menu()->GetPrefs()->GetList(prefs::kSpellCheckDictionaries))); |
| 57 } |
| 58 |
| 59 MockRenderViewContextMenu* menu() { return menu_.get(); } |
| 60 SpellingOptionsSubMenuObserver* observer() { return observer_.get(); } |
| 61 |
| 62 private: |
| 63 scoped_ptr<MockRenderViewContextMenu> menu_; |
| 64 scoped_ptr<SpellingOptionsSubMenuObserver> observer_; |
| 65 |
| 66 DISALLOW_COPY_AND_ASSIGN(SpellingOptionsSubMenuObserverTest); |
| 67 }; |
| 68 |
| 69 // Tests that selecting the "Check Spelling While Typing" item toggles the value |
| 70 // of the "browser.enable_spellchecking" profile. |
| 71 IN_PROC_BROWSER_TEST_F(SpellingOptionsSubMenuObserverTest, ToggleSpelling) { |
| 72 InitMenu(true, "en-US", std::vector<std::string>(1, "en-US")); |
| 73 |
| 74 // Verify this menu has the "Check Spelling While Typing" item and this item |
| 75 // is checked. |
| 76 EXPECT_TRUE(menu()->IsCommandIdEnabled(IDC_CHECK_SPELLING_WHILE_TYPING)); |
| 77 EXPECT_TRUE(menu()->IsCommandIdChecked(IDC_CHECK_SPELLING_WHILE_TYPING)); |
| 78 |
| 79 // Select this item and verify that the "Check Spelling While Typing" item is |
| 80 // not checked. Also, verify that the value of "browser.enable_spellchecking" |
| 81 // is now false. |
| 82 menu()->ExecuteCommand(IDC_CHECK_SPELLING_WHILE_TYPING, 0); |
| 83 ExpectPreferences(false, std::vector<std::string>(1, "en-US")); |
| 84 EXPECT_FALSE(menu()->IsCommandIdChecked(IDC_CHECK_SPELLING_WHILE_TYPING)); |
| 85 } |
| 86 |
| 87 // Single accept language is selected based on the dictionaries preference. |
| 88 // Consequently selecting multilingual spellcheck should copy all accept |
| 89 // languages into spellcheck dictionaries preference. |
| 90 IN_PROC_BROWSER_TEST_F(SpellingOptionsSubMenuObserverTest, SelectMultilingual) { |
| 91 InitMenu(true, "en-US,es", std::vector<std::string>(1, "en-US")); |
| 92 EXPECT_FALSE(menu()->IsCommandIdChecked(IDC_SPELLCHECK_MULTI_LINGUAL)); |
| 93 EXPECT_TRUE(menu()->IsCommandIdChecked(IDC_SPELLCHECK_LANGUAGES_FIRST)); |
| 94 EXPECT_FALSE(menu()->IsCommandIdChecked(IDC_SPELLCHECK_LANGUAGES_FIRST + 1)); |
| 95 |
| 96 menu()->ExecuteCommand(IDC_SPELLCHECK_MULTI_LINGUAL, 0); |
| 97 std::vector<std::string> dictionaries; |
| 98 dictionaries.push_back("en-US"); |
| 99 dictionaries.push_back("es"); |
| 100 ExpectPreferences(true, dictionaries); |
| 101 } |
| 102 |
| 103 // Multilingual spellcheck is selected when all dictionaries are used for |
| 104 // spellcheck. Consequently selecting "English (United States)" should set |
| 105 // spellcheck dictionaries preferences to ["en-US"]. |
| 106 IN_PROC_BROWSER_TEST_F(SpellingOptionsSubMenuObserverTest, |
| 107 SelectFirstLanguage) { |
| 108 std::vector<std::string> dictionaries; |
| 109 dictionaries.push_back("en-US"); |
| 110 dictionaries.push_back("es"); |
| 111 InitMenu(true, "en-US,es", dictionaries); |
| 112 EXPECT_TRUE(menu()->IsCommandIdChecked(IDC_SPELLCHECK_MULTI_LINGUAL)); |
| 113 EXPECT_FALSE(menu()->IsCommandIdChecked(IDC_SPELLCHECK_LANGUAGES_FIRST)); |
| 114 EXPECT_FALSE(menu()->IsCommandIdChecked(IDC_SPELLCHECK_LANGUAGES_FIRST + 1)); |
| 115 |
| 116 menu()->ExecuteCommand(IDC_SPELLCHECK_LANGUAGES_FIRST, 0); |
| 117 ExpectPreferences(true, std::vector<std::string>(1, "en-US")); |
| 118 } |
| 119 |
| 120 // Single dictionary should be selected based on preferences. |
| 121 IN_PROC_BROWSER_TEST_F(SpellingOptionsSubMenuObserverTest, |
| 122 SingleLanguageSelected) { |
| 123 InitMenu(true, "en-US", std::vector<std::string>(1, "en-US")); |
| 124 EXPECT_TRUE(menu()->IsCommandIdChecked(IDC_SPELLCHECK_LANGUAGES_FIRST)); |
| 125 } |
| 126 |
| 127 // Single dictionary should be deselected based on preferences. Selecting the |
| 128 // dictionary should update the preference. |
| 129 IN_PROC_BROWSER_TEST_F(SpellingOptionsSubMenuObserverTest, |
| 130 SelectTheOnlyLanguage) { |
| 131 InitMenu(true, "en-US", std::vector<std::string>()); |
| 132 EXPECT_FALSE(menu()->IsCommandIdChecked(IDC_SPELLCHECK_LANGUAGES_FIRST)); |
| 133 |
| 134 menu()->ExecuteCommand(IDC_SPELLCHECK_LANGUAGES_FIRST, 0); |
| 135 ExpectPreferences(true, std::vector<std::string>(1, "en-US")); |
| 136 } |
| 137 |
| 138 } // namespace |
OLD | NEW |