Chromium Code Reviews| 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/render_view_context_menu.h" | |
| 11 #include "chrome/common/pref_names.h" | |
| 12 #include "chrome/test/base/in_process_browser_test.h" | |
| 13 #include "chrome/test/base/testing_profile.h" | |
| 14 #include "components/renderer_context_menu/render_view_context_menu_observer.h" | |
| 15 | |
| 16 using content::RenderViewHost; | |
| 17 using content::WebContents; | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 // A mock context menu used in this test. This class overrides virtual methods | |
| 22 // derived from the RenderViewContextMenuProxy class to monitor calls from the | |
| 23 // SpellingMenuObserver class. | |
| 24 class MockRenderViewContextMenu : public ui::SimpleMenuModel::Delegate, | |
|
groby-ooo-7-16
2016/01/28 22:49:28
That looks somewhat similar to the one in spelling
please use gerrit instead
2016/01/29 20:09:34
Done.
| |
| 25 public RenderViewContextMenuProxy { | |
| 26 public: | |
| 27 // A menu item used in this test. | |
| 28 struct MockMenuItem { | |
| 29 MockMenuItem() | |
| 30 : command_id(0), | |
| 31 enabled(false), | |
| 32 checked(false), | |
| 33 hidden(true) {} | |
| 34 int command_id; | |
| 35 bool enabled; | |
| 36 bool checked; | |
| 37 bool hidden; | |
| 38 base::string16 title; | |
| 39 }; | |
| 40 | |
| 41 MockRenderViewContextMenu() : observer_(NULL), profile_(new TestingProfile) {} | |
|
lazyboy
2016/02/01 19:23:33
Here and in other places: prefer nullptr instead o
| |
| 42 ~MockRenderViewContextMenu() override {} | |
| 43 | |
| 44 // SimpleMenuModel::Delegate implementation. | |
| 45 bool IsCommandIdChecked(int command_id) const override { | |
| 46 return observer_->IsCommandIdChecked(command_id); | |
| 47 } | |
| 48 bool IsCommandIdEnabled(int command_id) const override { | |
| 49 return observer_->IsCommandIdEnabled(command_id); | |
| 50 } | |
| 51 void ExecuteCommand(int command_id, int event_flags) override { | |
| 52 observer_->ExecuteCommand(command_id); | |
| 53 } | |
| 54 void MenuWillShow(ui::SimpleMenuModel* source) override {} | |
| 55 void MenuClosed(ui::SimpleMenuModel* source) override {} | |
| 56 bool GetAcceleratorForCommandId(int command_id, | |
| 57 ui::Accelerator* accelerator) override { | |
| 58 return false; | |
| 59 } | |
| 60 | |
| 61 // RenderViewContextMenuProxy implementation. | |
| 62 void AddMenuItem(int command_id, const base::string16& title) override {} | |
| 63 void AddCheckItem(int command_id, const base::string16& title) override {} | |
| 64 void AddSeparator() override {} | |
| 65 void AddSubMenu(int command_id, | |
| 66 const base::string16& label, | |
| 67 ui::MenuModel* model) override {} | |
| 68 void UpdateMenuItem(int command_id, | |
| 69 bool enabled, | |
| 70 bool hidden, | |
| 71 const base::string16& title) override {} | |
| 72 RenderViewHost* GetRenderViewHost() const override { return NULL; } | |
| 73 content::BrowserContext* GetBrowserContext() const override { | |
| 74 return profile_.get(); | |
| 75 } | |
| 76 content::WebContents* GetWebContents() const override { return NULL; } | |
| 77 | |
| 78 // Attaches a RenderViewContextMenuObserver to be tested. | |
| 79 void SetObserver(RenderViewContextMenuObserver* observer) { | |
| 80 observer_ = observer; | |
| 81 } | |
| 82 | |
| 83 // Returns the number of items added by the test. | |
| 84 size_t GetMenuSize() const { | |
| 85 return 0; | |
| 86 } | |
| 87 | |
| 88 // Returns the i-th item. | |
| 89 bool GetMenuItem(size_t i, MockMenuItem* item) const { | |
| 90 return false; | |
| 91 } | |
| 92 | |
| 93 // Returns the writable profile used in this test. | |
| 94 PrefService* GetPrefs() { | |
| 95 return profile_->GetPrefs(); | |
| 96 } | |
| 97 | |
| 98 private: | |
| 99 // An observer used for initializing the status of menu items added in this | |
| 100 // test. This is a weak pointer, the test is responsible for deleting this | |
| 101 // object. | |
| 102 RenderViewContextMenuObserver* observer_; | |
| 103 | |
| 104 // A dummy profile used in this test. Call GetPrefs() when a test needs to | |
| 105 // change this profile and use PrefService methods. | |
| 106 scoped_ptr<TestingProfile> profile_; | |
| 107 | |
| 108 DISALLOW_COPY_AND_ASSIGN(MockRenderViewContextMenu); | |
| 109 }; | |
| 110 | |
| 111 // A test class used in this file. This test should be a browser test because it | |
| 112 // accesses resources. | |
| 113 class SpellingOptionsSubMenuObserverTest : public InProcessBrowserTest { | |
| 114 public: | |
| 115 SpellingOptionsSubMenuObserverTest() {} | |
| 116 ~SpellingOptionsSubMenuObserverTest() override {} | |
| 117 | |
| 118 void SetUpOnMainThread() override { | |
| 119 menu_.reset(new MockRenderViewContextMenu); | |
| 120 observer_.reset( | |
| 121 new SpellingOptionsSubMenuObserver(menu_.get(), menu_.get(), 1)); | |
| 122 menu_->SetObserver(observer_.get()); | |
| 123 } | |
| 124 | |
| 125 void TearDownOnMainThread() override { | |
| 126 menu_->SetObserver(nullptr); | |
| 127 observer_.reset(); | |
| 128 menu_.reset(); | |
| 129 } | |
| 130 | |
| 131 void InitMenu(bool enable_spellcheck, | |
| 132 const std::string& accept_languages, | |
| 133 const std::vector<std::string>& dictionaries) { | |
| 134 menu()->GetPrefs()->SetBoolean(prefs::kEnableContinuousSpellcheck, | |
| 135 enable_spellcheck); | |
| 136 menu()->GetPrefs()->SetString(prefs::kAcceptLanguages, accept_languages); | |
| 137 base::ListValue dictionaries_value; | |
| 138 for (const auto& dict : dictionaries) { | |
| 139 dictionaries_value.AppendString(dict); | |
| 140 } | |
| 141 menu()->GetPrefs()->Set(prefs::kSpellCheckDictionaries, dictionaries_value); | |
| 142 observer()->InitMenu(content::ContextMenuParams()); | |
| 143 } | |
| 144 | |
| 145 void ExpectPreferences(bool spellcheck_enabled, | |
| 146 const std::vector<std::string>& dictionaries) { | |
| 147 EXPECT_EQ(spellcheck_enabled, menu()->GetPrefs()->GetBoolean( | |
| 148 prefs::kEnableContinuousSpellcheck)); | |
| 149 base::ListValue dictionaries_value; | |
| 150 for (const auto& dict : dictionaries) { | |
|
groby-ooo-7-16
2016/01/28 22:49:28
Why not AppendStrings(dictionaries) ?
please use gerrit instead
2016/01/29 20:09:34
Done.
| |
| 151 dictionaries_value.AppendString(dict); | |
| 152 } | |
| 153 EXPECT_TRUE(dictionaries_value.Equals( | |
| 154 menu()->GetPrefs()->GetList(prefs::kSpellCheckDictionaries))); | |
| 155 } | |
| 156 | |
| 157 MockRenderViewContextMenu* menu() { return menu_.get(); } | |
| 158 SpellingOptionsSubMenuObserver* observer() { return observer_.get(); } | |
| 159 | |
| 160 private: | |
| 161 scoped_ptr<MockRenderViewContextMenu> menu_; | |
| 162 scoped_ptr<SpellingOptionsSubMenuObserver> observer_; | |
| 163 | |
| 164 DISALLOW_COPY_AND_ASSIGN(SpellingOptionsSubMenuObserverTest); | |
| 165 }; | |
| 166 | |
| 167 } // namespace | |
| 168 | |
| 169 // Tests that selecting the "Check Spelling While Typing" item toggles the value | |
| 170 // of the "browser.enable_spellchecking" profile. | |
| 171 IN_PROC_BROWSER_TEST_F(SpellingOptionsSubMenuObserverTest, ToggleSpelling) { | |
| 172 InitMenu(true, "en-US", {"en-US"}); | |
| 173 | |
| 174 // Verify this menu has the "Check Spelling While Typing" item and this item | |
| 175 // is checked. | |
| 176 EXPECT_TRUE(menu()->IsCommandIdEnabled(IDC_CHECK_SPELLING_WHILE_TYPING)); | |
| 177 EXPECT_TRUE(menu()->IsCommandIdChecked(IDC_CHECK_SPELLING_WHILE_TYPING)); | |
| 178 | |
| 179 // Select this item and verify that the "Check Spelling While Typing" item is | |
| 180 // not checked. Also, verify that the value of "browser.enable_spellchecking" | |
| 181 // is now false. | |
| 182 menu()->ExecuteCommand(IDC_CHECK_SPELLING_WHILE_TYPING, 0); | |
| 183 ExpectPreferences(false, {"en-US"}); | |
| 184 EXPECT_FALSE(menu()->IsCommandIdChecked(IDC_CHECK_SPELLING_WHILE_TYPING)); | |
| 185 } | |
| 186 | |
| 187 // Single accept language is selected based on the dictionaries preference. | |
| 188 // Consequently selecting multilingual spellcheck should copy all accept | |
| 189 // languages into spellcheck dictionaries preference. | |
| 190 IN_PROC_BROWSER_TEST_F(SpellingOptionsSubMenuObserverTest, SelectMultilingual) { | |
| 191 InitMenu(true, "en-US,es", {"en-US"}); | |
| 192 EXPECT_FALSE(menu()->IsCommandIdChecked(IDC_SPELLCHECK_MULTI_LINGUAL)); | |
| 193 EXPECT_TRUE(menu()->IsCommandIdChecked(IDC_SPELLCHECK_LANGUAGES_FIRST)); | |
| 194 EXPECT_FALSE(menu()->IsCommandIdChecked(IDC_SPELLCHECK_LANGUAGES_FIRST + 1)); | |
| 195 | |
| 196 menu()->ExecuteCommand(IDC_SPELLCHECK_MULTI_LINGUAL, 0); | |
| 197 ExpectPreferences(true, {"en-US", "es"}); | |
| 198 } | |
| 199 | |
| 200 // Multilingual spellcheck is selected when all dictionaries are used for | |
| 201 // spellcheck. Consequently selecting "English (United States)" should set | |
| 202 // spellcheck dictionaries preferences to ["en-US"]. | |
| 203 IN_PROC_BROWSER_TEST_F(SpellingOptionsSubMenuObserverTest, | |
| 204 SelectFirstLanguage) { | |
| 205 InitMenu(true, "en-US,es", {"en-US", "es"}); | |
| 206 EXPECT_TRUE(menu()->IsCommandIdChecked(IDC_SPELLCHECK_MULTI_LINGUAL)); | |
| 207 EXPECT_FALSE(menu()->IsCommandIdChecked(IDC_SPELLCHECK_LANGUAGES_FIRST)); | |
| 208 EXPECT_FALSE(menu()->IsCommandIdChecked(IDC_SPELLCHECK_LANGUAGES_FIRST + 1)); | |
| 209 | |
| 210 menu()->ExecuteCommand(IDC_SPELLCHECK_LANGUAGES_FIRST, 0); | |
| 211 ExpectPreferences(true, {"en-US"}); | |
| 212 } | |
| 213 | |
| 214 // Single dictionary should be selected based on preferences. | |
| 215 IN_PROC_BROWSER_TEST_F(SpellingOptionsSubMenuObserverTest, | |
| 216 SingleLanguageSelected) { | |
| 217 InitMenu(true, "en-US", {"en-US"}); | |
| 218 EXPECT_TRUE(menu()->IsCommandIdChecked(IDC_SPELLCHECK_LANGUAGES_FIRST)); | |
| 219 } | |
| 220 | |
| 221 // Single dictionary should be deselected based on preferences. Selecting the | |
| 222 // dictionary should update the preference. | |
| 223 IN_PROC_BROWSER_TEST_F(SpellingOptionsSubMenuObserverTest, | |
| 224 SelectTheOnlyLanguage) { | |
| 225 InitMenu(true, "en-US", std::vector<std::string>()); | |
| 226 EXPECT_FALSE(menu()->IsCommandIdChecked(IDC_SPELLCHECK_LANGUAGES_FIRST)); | |
| 227 | |
| 228 menu()->ExecuteCommand(IDC_SPELLCHECK_LANGUAGES_FIRST, 0); | |
| 229 ExpectPreferences(true, {"en-US"}); | |
|
please use gerrit instead
2016/01/29 20:09:34
Removed use of initializer lists, which are not al
| |
| 230 } | |
| OLD | NEW |