| 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 "base/utf_string_conversions.h" |
| 8 #include "chrome/app/chrome_command_ids.h" |
| 9 #include "chrome/browser/prefs/pref_service.h" |
| 10 #include "chrome/browser/tab_contents/render_view_context_menu.h" |
| 11 #include "chrome/browser/tab_contents/render_view_context_menu_observer.h" |
| 12 #include "chrome/common/pref_names.h" |
| 13 #include "chrome/test/base/in_process_browser_test.h" |
| 14 #include "chrome/test/base/testing_profile.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, |
| 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 string16 title; |
| 39 }; |
| 40 |
| 41 MockRenderViewContextMenu() : observer_(NULL), profile_(new TestingProfile) {} |
| 42 virtual ~MockRenderViewContextMenu() {} |
| 43 |
| 44 // SimpleMenuModel::Delegate implementation. |
| 45 virtual bool IsCommandIdChecked(int command_id) const OVERRIDE { |
| 46 return observer_->IsCommandIdChecked(command_id); |
| 47 } |
| 48 virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE { |
| 49 return observer_->IsCommandIdEnabled(command_id); |
| 50 } |
| 51 virtual void ExecuteCommand(int command_id) OVERRIDE { |
| 52 observer_->ExecuteCommand(command_id); |
| 53 } |
| 54 virtual void ExecuteCommand(int command_id, int event_flags) OVERRIDE {} |
| 55 virtual void MenuWillShow(ui::SimpleMenuModel* source) OVERRIDE {} |
| 56 virtual void MenuClosed(ui::SimpleMenuModel* source) OVERRIDE {} |
| 57 virtual bool GetAcceleratorForCommandId( |
| 58 int command_id, |
| 59 ui::Accelerator* accelerator) OVERRIDE { |
| 60 return false; |
| 61 } |
| 62 |
| 63 // RenderViewContextMenuProxy implementation. |
| 64 virtual void AddMenuItem(int command_id, const string16& title) OVERRIDE {} |
| 65 virtual void AddCheckItem(int command_id, const string16& title) OVERRIDE {} |
| 66 virtual void AddSeparator() OVERRIDE {} |
| 67 virtual void AddSubMenu(int command_id, |
| 68 const string16& label, |
| 69 ui::MenuModel* model) OVERRIDE {} |
| 70 virtual void UpdateMenuItem(int command_id, |
| 71 bool enabled, |
| 72 bool hidden, |
| 73 const string16& title) OVERRIDE {} |
| 74 virtual RenderViewHost* GetRenderViewHost() const OVERRIDE { |
| 75 return NULL; |
| 76 } |
| 77 virtual Profile* GetProfile() const OVERRIDE { |
| 78 return profile_.get(); |
| 79 } |
| 80 virtual content::WebContents* GetWebContents() const OVERRIDE { |
| 81 return NULL; |
| 82 } |
| 83 |
| 84 // Attaches a RenderViewContextMenuObserver to be tested. |
| 85 void SetObserver(RenderViewContextMenuObserver* observer) { |
| 86 observer_ = observer; |
| 87 } |
| 88 |
| 89 // Returns the number of items added by the test. |
| 90 size_t GetMenuSize() const { |
| 91 return 0; |
| 92 } |
| 93 |
| 94 // Returns the i-th item. |
| 95 bool GetMenuItem(size_t i, MockMenuItem* item) const { |
| 96 return false; |
| 97 } |
| 98 |
| 99 // Returns the writable profile used in this test. |
| 100 PrefService* GetPrefs() { |
| 101 return profile_->GetPrefs(); |
| 102 } |
| 103 |
| 104 private: |
| 105 // An observer used for initializing the status of menu items added in this |
| 106 // test. This is a weak pointer, the test is responsible for deleting this |
| 107 // object. |
| 108 RenderViewContextMenuObserver* observer_; |
| 109 |
| 110 // A dummy profile used in this test. Call GetPrefs() when a test needs to |
| 111 // change this profile and use PrefService methods. |
| 112 scoped_ptr<TestingProfile> profile_; |
| 113 |
| 114 DISALLOW_COPY_AND_ASSIGN(MockRenderViewContextMenu); |
| 115 }; |
| 116 |
| 117 // A test class used in this file. This test should be a browser test because it |
| 118 // accesses resources. |
| 119 class SpellCheckerSubMenuObserverTest : public InProcessBrowserTest { |
| 120 public: |
| 121 SpellCheckerSubMenuObserverTest() {} |
| 122 virtual ~SpellCheckerSubMenuObserverTest() {} |
| 123 |
| 124 private: |
| 125 DISALLOW_COPY_AND_ASSIGN(SpellCheckerSubMenuObserverTest); |
| 126 }; |
| 127 |
| 128 } // namespace |
| 129 |
| 130 // Tests that selecting the "Check Spelling While Typing" item toggles the value |
| 131 // of the "browser.enable_spellchecking" profile. |
| 132 IN_PROC_BROWSER_TEST_F(SpellCheckerSubMenuObserverTest, ToggleSpelling) { |
| 133 // Initialize a menu consisting only of a "Spell-checker Options" submenu. |
| 134 scoped_ptr<MockRenderViewContextMenu> menu(new MockRenderViewContextMenu); |
| 135 scoped_ptr<SpellCheckerSubMenuObserver> observer( |
| 136 new SpellCheckerSubMenuObserver(menu.get(), menu.get(), 1)); |
| 137 menu->SetObserver(observer.get()); |
| 138 menu->GetPrefs()->SetString(prefs::kAcceptLanguages, "en-US"); |
| 139 menu->GetPrefs()->SetString(prefs::kSpellCheckDictionary, "en-US"); |
| 140 menu->GetPrefs()->SetBoolean(prefs::kEnableSpellCheck, true); |
| 141 content::ContextMenuParams params; |
| 142 observer->InitMenu(params); |
| 143 |
| 144 // Verify this menu has the "Check Spelling While Typing" item and this item |
| 145 // is checked. |
| 146 EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CHECK_SPELLING_WHILE_TYPING)); |
| 147 EXPECT_TRUE(menu->IsCommandIdChecked(IDC_CHECK_SPELLING_WHILE_TYPING)); |
| 148 |
| 149 // Select this item and verify that the "Check Spelling While Typing" item is |
| 150 // not checked. Also, verify that the value of "browser.enable_spellchecking" |
| 151 // is now false. |
| 152 menu->ExecuteCommand(IDC_CHECK_SPELLING_WHILE_TYPING); |
| 153 EXPECT_FALSE(menu->GetPrefs()->GetBoolean(prefs::kEnableSpellCheck)); |
| 154 EXPECT_FALSE(menu->IsCommandIdChecked(IDC_CHECK_SPELLING_WHILE_TYPING)); |
| 155 } |
| OLD | NEW |