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.
5 #include "chrome/browser/tab_contents/spellchecker_submenu_observer.h"
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/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"
16 using content::RenderViewHost;
17 using content::WebContents;
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 {
27 // A menu item used in this test.
41 MockRenderViewContextMenu() : observer_(NULL), profile_(new TestingProfile) {}
42 virtual ~MockRenderViewContextMenu() {}
44 // SimpleMenuModel::Delegate implementation.
45 virtual bool IsCommandIdChecked(int command_id) const OVERRIDE {
46 return observer_->IsCommandIdChecked(command_id);
48 virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE {
49 return observer_->IsCommandIdEnabled(command_id);
51 virtual void ExecuteCommand(int command_id, int event_flags) OVERRIDE {
52 observer_->ExecuteCommand(command_id);
54 virtual void MenuWillShow(ui::SimpleMenuModel* source) OVERRIDE {}
55 virtual void MenuClosed(ui::SimpleMenuModel* source) OVERRIDE {}
56 virtual bool GetAcceleratorForCommandId(
58 ui::Accelerator* accelerator) OVERRIDE {
62 // RenderViewContextMenuProxy implementation.
63 virtual void AddMenuItem(int command_id, const string16& title) OVERRIDE {}
64 virtual void AddCheckItem(int command_id, const string16& title) OVERRIDE {}
65 virtual void AddSeparator() OVERRIDE {}
66 virtual void AddSubMenu(int command_id,
67 const string16& label,
68 ui::MenuModel* model) OVERRIDE {}
69 virtual void UpdateMenuItem(int command_id,
72 const string16& title) OVERRIDE {}
73 virtual RenderViewHost* GetRenderViewHost() const OVERRIDE {
76 virtual Profile* GetProfile() const OVERRIDE {
77 return profile_.get();
79 virtual content::WebContents* GetWebContents() const OVERRIDE {
83 // Attaches a RenderViewContextMenuObserver to be tested.
84 void SetObserver(RenderViewContextMenuObserver* observer) {
88 // Returns the number of items added by the test.
89 size_t GetMenuSize() const {
93 // Returns the i-th item.
94 bool GetMenuItem(size_t i, MockMenuItem* item) const {
98 // Returns the writable profile used in this test.
99 PrefService* GetPrefs() {
100 return profile_->GetPrefs();
104 // An observer used for initializing the status of menu items added in this
105 // test. This is a weak pointer, the test is responsible for deleting this
107 RenderViewContextMenuObserver* observer_;
109 // A dummy profile used in this test. Call GetPrefs() when a test needs to
110 // change this profile and use PrefService methods.
111 scoped_ptr<TestingProfile> profile_;
113 DISALLOW_COPY_AND_ASSIGN(MockRenderViewContextMenu);
116 // A test class used in this file. This test should be a browser test because it
117 // accesses resources.
118 class SpellCheckerSubMenuObserverTest : public InProcessBrowserTest {
120 SpellCheckerSubMenuObserverTest() {}
121 virtual ~SpellCheckerSubMenuObserverTest() {}
124 DISALLOW_COPY_AND_ASSIGN(SpellCheckerSubMenuObserverTest);
129 // Tests that selecting the "Check Spelling While Typing" item toggles the value
130 // of the "browser.enable_spellchecking" profile.
131 IN_PROC_BROWSER_TEST_F(SpellCheckerSubMenuObserverTest, ToggleSpelling) {
132 // Initialize a menu consisting only of a "Spell-checker Options" submenu.
133 scoped_ptr<MockRenderViewContextMenu> menu(new MockRenderViewContextMenu);
134 scoped_ptr<SpellCheckerSubMenuObserver> observer(
135 new SpellCheckerSubMenuObserver(menu.get(), menu.get(), 1));
136 menu->SetObserver(observer.get());
137 menu->GetPrefs()->SetString(prefs::kAcceptLanguages, "en-US");
138 menu->GetPrefs()->SetString(prefs::kSpellCheckDictionary, "en-US");
139 menu->GetPrefs()->SetBoolean(prefs::kEnableContinuousSpellcheck, true);
140 content::ContextMenuParams params;
141 observer->InitMenu(params);
143 // Verify this menu has the "Check Spelling While Typing" item and this item
145 EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CHECK_SPELLING_WHILE_TYPING));
146 EXPECT_TRUE(menu->IsCommandIdChecked(IDC_CHECK_SPELLING_WHILE_TYPING));
148 // Select this item and verify that the "Check Spelling While Typing" item is
149 // not checked. Also, verify that the value of "browser.enable_spellchecking"
151 menu->ExecuteCommand(IDC_CHECK_SPELLING_WHILE_TYPING, 0);
153 menu->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck));
154 EXPECT_FALSE(menu->IsCommandIdChecked(IDC_CHECK_SPELLING_WHILE_TYPING));