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