- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / tab_contents / spellchecker_submenu_observer_browsertest.cc
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/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"
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, int event_flags) OVERRIDE {
52     observer_->ExecuteCommand(command_id);
53   }
54   virtual void MenuWillShow(ui::SimpleMenuModel* source) OVERRIDE {}
55   virtual void MenuClosed(ui::SimpleMenuModel* source) OVERRIDE {}
56   virtual bool GetAcceleratorForCommandId(
57       int command_id,
58       ui::Accelerator* accelerator) OVERRIDE {
59     return false;
60   }
61
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,
70                               bool enabled,
71                               bool hidden,
72                               const string16& title) OVERRIDE {}
73   virtual RenderViewHost* GetRenderViewHost() const OVERRIDE {
74     return NULL;
75   }
76   virtual Profile* GetProfile() const OVERRIDE {
77     return profile_.get();
78   }
79   virtual content::WebContents* GetWebContents() const OVERRIDE {
80     return NULL;
81   }
82
83   // Attaches a RenderViewContextMenuObserver to be tested.
84   void SetObserver(RenderViewContextMenuObserver* observer) {
85     observer_ = observer;
86   }
87
88   // Returns the number of items added by the test.
89   size_t GetMenuSize() const {
90     return 0;
91   }
92
93   // Returns the i-th item.
94   bool GetMenuItem(size_t i, MockMenuItem* item) const {
95     return false;
96   }
97
98   // Returns the writable profile used in this test.
99   PrefService* GetPrefs() {
100     return profile_->GetPrefs();
101   }
102
103  private:
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
106   // object.
107   RenderViewContextMenuObserver* observer_;
108
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_;
112
113   DISALLOW_COPY_AND_ASSIGN(MockRenderViewContextMenu);
114 };
115
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 {
119  public:
120   SpellCheckerSubMenuObserverTest() {}
121   virtual ~SpellCheckerSubMenuObserverTest() {}
122
123  private:
124   DISALLOW_COPY_AND_ASSIGN(SpellCheckerSubMenuObserverTest);
125 };
126
127 }  // namespace
128
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);
142
143   // Verify this menu has the "Check Spelling While Typing" item and this item
144   // is checked.
145   EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CHECK_SPELLING_WHILE_TYPING));
146   EXPECT_TRUE(menu->IsCommandIdChecked(IDC_CHECK_SPELLING_WHILE_TYPING));
147
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"
150   // is now false.
151   menu->ExecuteCommand(IDC_CHECK_SPELLING_WHILE_TYPING, 0);
152   EXPECT_FALSE(
153       menu->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck));
154   EXPECT_FALSE(menu->IsCommandIdChecked(IDC_CHECK_SPELLING_WHILE_TYPING));
155 }