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. | |
| 29 struct MockMenuItem { | |
| 30 MockMenuItem() | |
| 31 : command_id(0), | |
| 32 enabled(false), | |
| 33 checked(false), | |
| 34 hidden(true) { | |
| 35 } | |
| 36 int command_id; | |
| 37 bool enabled; | |
| 38 bool checked; | |
| 39 bool hidden; | |
| 40 string16 title; | |
| 41 }; | |
| 42 | |
| 43 MockRenderViewContextMenu() : observer_(NULL), profile_(new TestingProfile) { | |
| 44 } | |
|
jeremy
2012/06/21 09:48:35
I think that for these empty functions you can jus
| |
| 45 virtual ~MockRenderViewContextMenu() { | |
| 46 } | |
|
jeremy
2012/06/21 09:48:35
here too...
| |
| 47 | |
| 48 // SimpleMenuModel::Delegate implementation. | |
| 49 virtual bool IsCommandIdChecked(int command_id) const OVERRIDE { | |
| 50 return observer_->IsCommandIdChecked(command_id); | |
| 51 } | |
| 52 virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE { | |
| 53 return observer_->IsCommandIdEnabled(command_id); | |
| 54 } | |
| 55 virtual void ExecuteCommand(int command_id) OVERRIDE { | |
| 56 observer_->ExecuteCommand(command_id); | |
| 57 } | |
| 58 virtual void ExecuteCommand(int command_id, int event_flags) OVERRIDE { | |
| 59 } | |
| 60 virtual void MenuWillShow(ui::SimpleMenuModel* source) OVERRIDE { | |
| 61 } | |
| 62 virtual void MenuClosed(ui::SimpleMenuModel* source) OVERRIDE { | |
| 63 } | |
| 64 virtual bool GetAcceleratorForCommandId( | |
| 65 int command_id, | |
| 66 ui::Accelerator* accelerator) OVERRIDE { | |
| 67 return false; | |
| 68 } | |
| 69 | |
| 70 // RenderViewContextMenuProxy implementation. | |
| 71 virtual void AddMenuItem(int command_id, const string16& title) OVERRIDE { | |
| 72 } | |
| 73 virtual void AddCheckItem(int command_id, const string16& title) OVERRIDE { | |
| 74 } | |
| 75 virtual void AddSeparator() OVERRIDE { | |
| 76 } | |
| 77 virtual void AddSubMenu(int command_id, | |
| 78 const string16& label, | |
| 79 ui::MenuModel* model) OVERRIDE { | |
| 80 } | |
| 81 virtual void UpdateMenuItem(int command_id, | |
| 82 bool enabled, | |
| 83 bool hidden, | |
| 84 const string16& title) OVERRIDE { | |
| 85 } | |
| 86 virtual RenderViewHost* GetRenderViewHost() const OVERRIDE { | |
| 87 return NULL; | |
|
jeremy
2012/06/21 09:48:35
I just thought the code would be more readable if
| |
| 88 } | |
| 89 virtual Profile* GetProfile() const OVERRIDE { | |
| 90 return profile_.get(); | |
| 91 } | |
| 92 | |
| 93 // Attaches a RenderViewContextMenuObserver to be tested. | |
| 94 void SetObserver(RenderViewContextMenuObserver* observer) { | |
| 95 observer_ = observer; | |
| 96 } | |
| 97 | |
| 98 // Returns the number of items added by the test. | |
| 99 size_t GetMenuSize() const { | |
| 100 return 0; | |
| 101 } | |
| 102 | |
| 103 // Returns the i-th item. | |
| 104 bool GetMenuItem(size_t i, MockMenuItem* item) const { | |
| 105 return false; | |
| 106 } | |
| 107 | |
| 108 // Returns the writable profile used in this test. | |
| 109 PrefService* GetPrefs() { | |
| 110 return profile_->GetPrefs(); | |
| 111 } | |
| 112 | |
| 113 private: | |
| 114 // An observer used for initializing the status of menu items added in this | |
| 115 // test. This is a weak pointer, the test is responsible for deleting this | |
| 116 // object. | |
| 117 RenderViewContextMenuObserver* observer_; | |
| 118 | |
| 119 // A dummy profile used in this test. Call GetPrefs() when a test needs to | |
| 120 // change this profile and use PrefService methods. | |
| 121 scoped_ptr<TestingProfile> profile_; | |
| 122 | |
| 123 // A list of menu items added by the SpellingMenuObserver class. | |
| 124 std::vector<MockMenuItem> items_; | |
| 125 | |
| 126 DISALLOW_COPY_AND_ASSIGN(MockRenderViewContextMenu); | |
| 127 }; | |
| 128 | |
| 129 // A test class used in this file. This test should be a browser test because it | |
| 130 // accesses resources. | |
| 131 class SpellCheckerSubMenuObserverTest : public InProcessBrowserTest { | |
| 132 public: | |
| 133 SpellCheckerSubMenuObserverTest() { | |
| 134 } | |
|
jeremy
2012/06/21 09:48:35
also here.
| |
| 135 virtual ~SpellCheckerSubMenuObserverTest() { | |
| 136 } | |
| 137 | |
| 138 private: | |
| 139 DISALLOW_COPY_AND_ASSIGN(SpellCheckerSubMenuObserverTest); | |
| 140 }; | |
| 141 | |
| 142 } // namespace | |
| 143 | |
| 144 // Tests that selecting the "Check Spelling While Typing" item toggles the value | |
| 145 // of the "browser.enable_spellchecking" profile. | |
| 146 IN_PROC_BROWSER_TEST_F(SpellCheckerSubMenuObserverTest, ToggleSpelling) { | |
| 147 // Initialize a menu consisting only of a "Spell-checker Options" submenu. | |
| 148 scoped_ptr<MockRenderViewContextMenu> menu(new MockRenderViewContextMenu); | |
| 149 scoped_ptr<SpellCheckerSubMenuObserver> observer( | |
| 150 new SpellCheckerSubMenuObserver(menu.get(), menu.get(), 1)); | |
| 151 menu->SetObserver(observer.get()); | |
| 152 menu->GetPrefs()->SetString(prefs::kAcceptLanguages, "en-US"); | |
| 153 menu->GetPrefs()->SetString(prefs::kSpellCheckDictionary, "en-US"); | |
| 154 menu->GetPrefs()->SetBoolean(prefs::kEnableSpellCheck, true); | |
| 155 content::ContextMenuParams params; | |
| 156 observer->InitMenu(params); | |
| 157 | |
| 158 // Verify this menu has the "Check Spelling While Typing" item and this item | |
| 159 // is checked. | |
| 160 EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CHECK_SPELLING_WHILE_TYPING)); | |
| 161 EXPECT_TRUE(menu->IsCommandIdChecked(IDC_CHECK_SPELLING_WHILE_TYPING)); | |
| 162 | |
| 163 // Select this item and verify the "Check Spelling While Typing" item is not | |
| 164 // checked. Also, verify the value of "browser.enable_spellchecking" become | |
| 165 // false. | |
|
jeremy
2012/06/21 09:48:35
nit:
* verify -> verify that
* become -> is now
| |
| 166 menu->ExecuteCommand(IDC_CHECK_SPELLING_WHILE_TYPING); | |
| 167 EXPECT_FALSE(menu->GetPrefs()->GetBoolean(prefs::kEnableSpellCheck)); | |
| 168 EXPECT_FALSE(menu->IsCommandIdChecked(IDC_CHECK_SPELLING_WHILE_TYPING)); | |
| 169 } | |
| OLD | NEW |