Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / renderer_context_menu / spellchecker_submenu_observer_browsertest.cc
blob8c786cd5dd01c94538e50da7d733e2b94503ef69
1 // Copyright 2014 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/renderer_context_menu/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/renderer_context_menu/render_view_context_menu.h"
11 #include "chrome/common/pref_names.h"
12 #include "chrome/test/base/in_process_browser_test.h"
13 #include "chrome/test/base/testing_profile.h"
14 #include "components/renderer_context_menu/render_view_context_menu_observer.h"
16 using content::RenderViewHost;
17 using content::WebContents;
19 namespace {
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 base::string16 title;
41 MockRenderViewContextMenu() : observer_(NULL), profile_(new TestingProfile) {}
42 ~MockRenderViewContextMenu() override {}
44 // SimpleMenuModel::Delegate implementation.
45 bool IsCommandIdChecked(int command_id) const override {
46 return observer_->IsCommandIdChecked(command_id);
48 bool IsCommandIdEnabled(int command_id) const override {
49 return observer_->IsCommandIdEnabled(command_id);
51 void ExecuteCommand(int command_id, int event_flags) override {
52 observer_->ExecuteCommand(command_id);
54 void MenuWillShow(ui::SimpleMenuModel* source) override {}
55 void MenuClosed(ui::SimpleMenuModel* source) override {}
56 bool GetAcceleratorForCommandId(int command_id,
57 ui::Accelerator* accelerator) override {
58 return false;
61 // RenderViewContextMenuProxy implementation.
62 void AddMenuItem(int command_id, const base::string16& title) override {}
63 void AddCheckItem(int command_id, const base::string16& title) override {}
64 void AddSeparator() override {}
65 void AddSubMenu(int command_id,
66 const base::string16& label,
67 ui::MenuModel* model) override {}
68 void UpdateMenuItem(int command_id,
69 bool enabled,
70 bool hidden,
71 const base::string16& title) override {}
72 RenderViewHost* GetRenderViewHost() const override { return NULL; }
73 content::BrowserContext* GetBrowserContext() const override {
74 return profile_.get();
76 content::WebContents* GetWebContents() const override { return NULL; }
78 // Attaches a RenderViewContextMenuObserver to be tested.
79 void SetObserver(RenderViewContextMenuObserver* observer) {
80 observer_ = observer;
83 // Returns the number of items added by the test.
84 size_t GetMenuSize() const {
85 return 0;
88 // Returns the i-th item.
89 bool GetMenuItem(size_t i, MockMenuItem* item) const {
90 return false;
93 // Returns the writable profile used in this test.
94 PrefService* GetPrefs() {
95 return profile_->GetPrefs();
98 private:
99 // An observer used for initializing the status of menu items added in this
100 // test. This is a weak pointer, the test is responsible for deleting this
101 // object.
102 RenderViewContextMenuObserver* observer_;
104 // A dummy profile used in this test. Call GetPrefs() when a test needs to
105 // change this profile and use PrefService methods.
106 scoped_ptr<TestingProfile> profile_;
108 DISALLOW_COPY_AND_ASSIGN(MockRenderViewContextMenu);
111 // A test class used in this file. This test should be a browser test because it
112 // accesses resources.
113 class SpellCheckerSubMenuObserverTest : public InProcessBrowserTest {
114 public:
115 SpellCheckerSubMenuObserverTest() {}
116 ~SpellCheckerSubMenuObserverTest() override {}
118 private:
119 DISALLOW_COPY_AND_ASSIGN(SpellCheckerSubMenuObserverTest);
122 } // namespace
124 // Tests that selecting the "Check Spelling While Typing" item toggles the value
125 // of the "browser.enable_spellchecking" profile.
126 IN_PROC_BROWSER_TEST_F(SpellCheckerSubMenuObserverTest, ToggleSpelling) {
127 // Initialize a menu consisting only of a "Spell-checker Options" submenu.
128 scoped_ptr<MockRenderViewContextMenu> menu(new MockRenderViewContextMenu);
129 scoped_ptr<SpellCheckerSubMenuObserver> observer(
130 new SpellCheckerSubMenuObserver(menu.get(), menu.get(), 1));
131 menu->SetObserver(observer.get());
132 menu->GetPrefs()->SetString(prefs::kAcceptLanguages, "en-US");
134 base::ListValue dictionary;
135 dictionary.AppendString("en-US");
136 menu->GetPrefs()->Set(prefs::kSpellCheckDictionaries, dictionary);
138 menu->GetPrefs()->SetBoolean(prefs::kEnableContinuousSpellcheck, true);
139 content::ContextMenuParams params;
140 observer->InitMenu(params);
142 // Verify this menu has the "Check Spelling While Typing" item and this item
143 // is checked.
144 EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CHECK_SPELLING_WHILE_TYPING));
145 EXPECT_TRUE(menu->IsCommandIdChecked(IDC_CHECK_SPELLING_WHILE_TYPING));
147 // Select this item and verify that the "Check Spelling While Typing" item is
148 // not checked. Also, verify that the value of "browser.enable_spellchecking"
149 // is now false.
150 menu->ExecuteCommand(IDC_CHECK_SPELLING_WHILE_TYPING, 0);
151 EXPECT_FALSE(
152 menu->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck));
153 EXPECT_FALSE(menu->IsCommandIdChecked(IDC_CHECK_SPELLING_WHILE_TYPING));