Chromium Code Reviews
|
| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/tab_contents/spellchecker_submenu_observer.h" | |
| 6 | |
| 7 #include <vector> | |
| 8 | |
| 9 #include "base/utf_string_conversions.h" | |
| 10 #include "chrome/app/chrome_command_ids.h" | |
| 11 #include "chrome/browser/prefs/pref_service.h" | |
| 12 #include "chrome/browser/tab_contents/render_view_context_menu.h" | |
| 13 #include "chrome/browser/tab_contents/render_view_context_menu_observer.h" | |
| 14 #include "chrome/common/pref_names.h" | |
| 15 #include "chrome/test/base/in_process_browser_test.h" | |
| 16 #include "chrome/test/base/testing_profile.h" | |
| 17 | |
| 18 using content::RenderViewHost; | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 // A mock context menu used in this test. This class overrides virtual methods | |
| 23 // derived from the RenderViewContextMenuProxy class to monitor calls from the | |
| 24 // SpellingMenuObserver class. | |
| 25 class MockRenderViewContextMenu : public ui::SimpleMenuModel::Delegate, | |
| 26 public RenderViewContextMenuProxy { | |
| 27 public: | |
| 28 // A menu item used in this test. This test uses a vector of this struct to | |
| 29 // hold menu items added by this test. | |
|
jeremy
2012/06/19 12:14:43
I think you can drop the second sentence of this c
Hironori Bono
2012/06/21 09:38:18
Done. I have also removed code not needed by this
| |
| 30 struct MockMenuItem { | |
| 31 MockMenuItem() | |
| 32 : command_id(0), | |
| 33 enabled(false), | |
| 34 checked(false), | |
| 35 hidden(true) { | |
| 36 } | |
| 37 int command_id; | |
| 38 bool enabled; | |
| 39 bool checked; | |
| 40 bool hidden; | |
| 41 string16 title; | |
| 42 }; | |
| 43 | |
| 44 MockRenderViewContextMenu(); | |
| 45 virtual ~MockRenderViewContextMenu(); | |
| 46 | |
| 47 // SimpleMenuModel::Delegate implementation. | |
| 48 virtual bool IsCommandIdChecked(int command_id) const OVERRIDE; | |
| 49 virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE; | |
| 50 virtual void ExecuteCommand(int command_id) OVERRIDE; | |
| 51 virtual void ExecuteCommand(int command_id, int event_flags) OVERRIDE; | |
| 52 virtual void MenuWillShow(ui::SimpleMenuModel* source) OVERRIDE; | |
| 53 virtual void MenuClosed(ui::SimpleMenuModel* source) OVERRIDE; | |
| 54 virtual bool GetAcceleratorForCommandId( | |
| 55 int command_id, | |
| 56 ui::Accelerator* accelerator) OVERRIDE; | |
| 57 | |
| 58 // RenderViewContextMenuProxy implementation. | |
| 59 virtual void AddMenuItem(int command_id, const string16& title) OVERRIDE; | |
| 60 virtual void AddCheckItem(int command_id, const string16& title) OVERRIDE; | |
| 61 virtual void AddSeparator() OVERRIDE; | |
| 62 virtual void AddSubMenu(int command_id, | |
| 63 const string16& label, | |
| 64 ui::MenuModel* model) OVERRIDE; | |
| 65 virtual void UpdateMenuItem(int command_id, | |
| 66 bool enabled, | |
| 67 bool hidden, | |
| 68 const string16& title) OVERRIDE; | |
| 69 virtual RenderViewHost* GetRenderViewHost() const OVERRIDE; | |
| 70 virtual Profile* GetProfile() const OVERRIDE; | |
| 71 | |
| 72 // Attaches a RenderViewContextMenuObserver to be tested. | |
| 73 void SetObserver(RenderViewContextMenuObserver* observer); | |
| 74 | |
| 75 // Returns the number of items added by the test. | |
| 76 size_t GetMenuSize() const; | |
| 77 | |
| 78 // Returns the i-th item. | |
| 79 bool GetMenuItem(size_t i, MockMenuItem* item) const; | |
| 80 | |
| 81 // Returns the writable profile used in this test. | |
| 82 PrefService* GetPrefs(); | |
| 83 | |
| 84 private: | |
| 85 // An observer used for initializing the status of menu items added in this | |
| 86 // test. A test should delete this RenderViewContextMenuObserver object. | |
|
jeremy
2012/06/19 12:14:43
How about this for the second sentence:
This is a
Hironori Bono
2012/06/21 09:38:18
Done. Thanks for correcting my comment.
| |
| 87 RenderViewContextMenuObserver* observer_; | |
| 88 | |
| 89 // A dummy profile used in this test. Call GetPrefs() when a test needs to | |
| 90 // change this profile and use PrefService methods. | |
| 91 scoped_ptr<TestingProfile> profile_; | |
| 92 | |
| 93 // A list of menu items added by the SpellingMenuObserver class. | |
| 94 std::vector<MockMenuItem> items_; | |
| 95 | |
| 96 DISALLOW_COPY_AND_ASSIGN(MockRenderViewContextMenu); | |
| 97 }; | |
| 98 | |
| 99 MockRenderViewContextMenu::MockRenderViewContextMenu() | |
| 100 : observer_(NULL), | |
| 101 profile_(new TestingProfile) { | |
| 102 } | |
| 103 | |
| 104 MockRenderViewContextMenu::~MockRenderViewContextMenu() { | |
| 105 } | |
| 106 | |
| 107 bool MockRenderViewContextMenu::IsCommandIdChecked(int command_id) const { | |
| 108 return observer_->IsCommandIdChecked(command_id); | |
| 109 } | |
| 110 | |
| 111 bool MockRenderViewContextMenu::IsCommandIdEnabled(int command_id) const { | |
| 112 return observer_->IsCommandIdEnabled(command_id); | |
| 113 } | |
| 114 | |
| 115 void MockRenderViewContextMenu::ExecuteCommand(int command_id) { | |
| 116 return observer_->ExecuteCommand(command_id); | |
| 117 } | |
| 118 | |
| 119 void MockRenderViewContextMenu::ExecuteCommand(int command_id, | |
| 120 int event_flags) { | |
| 121 } | |
| 122 | |
| 123 void MockRenderViewContextMenu::MenuWillShow(ui::SimpleMenuModel* source) { | |
| 124 } | |
| 125 | |
| 126 void MockRenderViewContextMenu::MenuClosed(ui::SimpleMenuModel* source) { | |
| 127 } | |
| 128 | |
| 129 bool MockRenderViewContextMenu::GetAcceleratorForCommandId( | |
| 130 int command_id, | |
| 131 ui::Accelerator* accelerator) { | |
| 132 return false; | |
| 133 } | |
| 134 | |
| 135 void MockRenderViewContextMenu::AddMenuItem(int command_id, | |
| 136 const string16& title) { | |
| 137 MockMenuItem item; | |
| 138 item.command_id = command_id; | |
| 139 item.enabled = observer_->IsCommandIdEnabled(command_id); | |
| 140 item.checked = false; | |
| 141 item.hidden = false; | |
| 142 item.title = title; | |
| 143 items_.push_back(item); | |
| 144 } | |
| 145 | |
| 146 void MockRenderViewContextMenu::AddCheckItem(int command_id, | |
| 147 const string16& title) { | |
| 148 MockMenuItem item; | |
| 149 item.command_id = command_id; | |
| 150 item.enabled = observer_->IsCommandIdEnabled(command_id); | |
| 151 item.checked = observer_->IsCommandIdChecked(command_id); | |
| 152 item.hidden = false; | |
| 153 item.title = title; | |
| 154 items_.push_back(item); | |
| 155 } | |
| 156 | |
| 157 void MockRenderViewContextMenu::AddSeparator() { | |
| 158 MockMenuItem item; | |
| 159 item.command_id = -1; | |
| 160 item.enabled = false; | |
| 161 item.checked = false; | |
| 162 item.hidden = false; | |
| 163 items_.push_back(item); | |
| 164 } | |
| 165 | |
| 166 void MockRenderViewContextMenu::AddSubMenu(int command_id, | |
| 167 const string16& label, | |
| 168 ui::MenuModel* model) { | |
| 169 MockMenuItem item; | |
| 170 item.command_id = -1; | |
| 171 item.enabled = false; | |
| 172 item.checked = false; | |
| 173 item.hidden = false; | |
| 174 items_.push_back(item); | |
| 175 } | |
| 176 | |
| 177 void MockRenderViewContextMenu::UpdateMenuItem(int command_id, | |
| 178 bool enabled, | |
| 179 bool hidden, | |
| 180 const string16& title) { | |
| 181 for (std::vector<MockMenuItem>::iterator it = items_.begin(); | |
| 182 it != items_.end(); ++it) { | |
| 183 if (it->command_id == command_id) { | |
| 184 it->enabled = enabled; | |
| 185 it->hidden = hidden; | |
| 186 it->title = title; | |
| 187 return; | |
| 188 } | |
| 189 } | |
| 190 | |
| 191 // The SpellingMenuObserver class tries to change a menu item not added by the | |
| 192 // class. This is an unexpected behavior and we should stop now. | |
| 193 FAIL(); | |
| 194 } | |
| 195 | |
| 196 RenderViewHost* MockRenderViewContextMenu::GetRenderViewHost() const { | |
| 197 return NULL; | |
| 198 } | |
| 199 | |
| 200 Profile* MockRenderViewContextMenu::GetProfile() const { | |
| 201 return profile_.get(); | |
| 202 } | |
| 203 | |
| 204 size_t MockRenderViewContextMenu::GetMenuSize() const { | |
| 205 return items_.size(); | |
| 206 } | |
| 207 | |
| 208 bool MockRenderViewContextMenu::GetMenuItem(size_t i, | |
| 209 MockMenuItem* item) const { | |
| 210 if (i >= items_.size()) | |
| 211 return false; | |
| 212 item->command_id = items_[i].command_id; | |
| 213 item->enabled = items_[i].enabled; | |
| 214 item->checked = items_[i].checked; | |
| 215 item->hidden = items_[i].hidden; | |
| 216 item->title = items_[i].title; | |
| 217 return true; | |
| 218 } | |
| 219 | |
| 220 void MockRenderViewContextMenu::SetObserver( | |
| 221 RenderViewContextMenuObserver* observer) { | |
| 222 observer_ = observer; | |
| 223 } | |
| 224 | |
| 225 PrefService* MockRenderViewContextMenu::GetPrefs() { | |
| 226 return profile_->GetPrefs(); | |
| 227 } | |
| 228 | |
| 229 // A test class used in this file. This test should be a browser test because it | |
| 230 // accesses resources. | |
| 231 class SpellCheckerSubMenuObserverTest : public InProcessBrowserTest { | |
| 232 public: | |
| 233 SpellCheckerSubMenuObserverTest(); | |
| 234 virtual ~SpellCheckerSubMenuObserverTest(); | |
| 235 | |
| 236 private: | |
| 237 DISALLOW_COPY_AND_ASSIGN(SpellCheckerSubMenuObserverTest); | |
| 238 }; | |
| 239 | |
| 240 SpellCheckerSubMenuObserverTest::SpellCheckerSubMenuObserverTest() { | |
| 241 } | |
|
jeremy
2012/06/19 12:14:43
Can you just declare these inline in the class? Sa
Hironori Bono
2012/06/21 09:38:18
Done. Thanks for noticing it.
| |
| 242 | |
| 243 SpellCheckerSubMenuObserverTest::~SpellCheckerSubMenuObserverTest() { | |
| 244 } | |
| 245 | |
| 246 } // namespace | |
| 247 | |
| 248 // Tests that selecting the "Check Spelling While Typing" item toggles the value | |
| 249 // of the "browser.enable_spellchecking" profile. | |
| 250 IN_PROC_BROWSER_TEST_F(SpellCheckerSubMenuObserverTest, ToggleSpelling) { | |
| 251 // Initialize a menu consisting only of a "Spell-checker Options" submenu. | |
| 252 scoped_ptr<MockRenderViewContextMenu> menu(new MockRenderViewContextMenu); | |
| 253 scoped_ptr<SpellCheckerSubMenuObserver> observer( | |
| 254 new SpellCheckerSubMenuObserver(menu.get(), menu.get(), 1)); | |
| 255 menu->SetObserver(observer.get()); | |
| 256 menu->GetPrefs()->SetString(prefs::kAcceptLanguages, "en-US"); | |
| 257 menu->GetPrefs()->SetString(prefs::kSpellCheckDictionary, "en-US"); | |
| 258 menu->GetPrefs()->SetBoolean(prefs::kEnableSpellCheck, true); | |
| 259 content::ContextMenuParams params; | |
| 260 observer->InitMenu(params); | |
| 261 | |
| 262 // Verify this menu has the "Check Spelling While Typing" item and this item | |
| 263 // is checked. | |
| 264 EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CHECK_SPELLING_WHILE_TYPING)); | |
| 265 EXPECT_TRUE(menu->IsCommandIdChecked(IDC_CHECK_SPELLING_WHILE_TYPING)); | |
| 266 | |
| 267 // Select this item after verifying the "Check Spelling While Typing" item is | |
| 268 // checked. If this test works as expected, this item becomes unchecked and | |
| 269 // the value of "browser.enable_spellchecking" become false. | |
| 270 menu->ExecuteCommand(IDC_CHECK_SPELLING_WHILE_TYPING); | |
| 271 EXPECT_FALSE(menu->GetPrefs()->GetBoolean(prefs::kEnableSpellCheck)); | |
| 272 EXPECT_FALSE(menu->IsCommandIdChecked(IDC_CHECK_SPELLING_WHILE_TYPING)); | |
| 273 } | |
| OLD | NEW |