summaryrefslogtreecommitdiffstats
path: root/chrome/browser/tab_contents/spellchecker_submenu_observer_browsertest.cc
blob: ca017b9ac1429a34f26e4593135c62bc7720621c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/tab_contents/spellchecker_submenu_observer.h"

#include "base/prefs/pref_service.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/tab_contents/render_view_context_menu.h"
#include "chrome/browser/tab_contents/render_view_context_menu_observer.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/testing_profile.h"

using content::RenderViewHost;
using content::WebContents;

namespace {

// A mock context menu used in this test. This class overrides virtual methods
// derived from the RenderViewContextMenuProxy class to monitor calls from the
// SpellingMenuObserver class.
class MockRenderViewContextMenu : public ui::SimpleMenuModel::Delegate,
                                  public RenderViewContextMenuProxy {
 public:
  // A menu item used in this test.
  struct MockMenuItem {
    MockMenuItem()
        : command_id(0),
          enabled(false),
          checked(false),
          hidden(true) {}
    int command_id;
    bool enabled;
    bool checked;
    bool hidden;
    string16 title;
  };

  MockRenderViewContextMenu() : observer_(NULL), profile_(new TestingProfile) {}
  virtual ~MockRenderViewContextMenu() {}

  // SimpleMenuModel::Delegate implementation.
  virtual bool IsCommandIdChecked(int command_id) const OVERRIDE {
    return observer_->IsCommandIdChecked(command_id);
  }
  virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE {
    return observer_->IsCommandIdEnabled(command_id);
  }
  virtual void ExecuteCommand(int command_id, int event_flags) OVERRIDE {
    observer_->ExecuteCommand(command_id);
  }
  virtual void MenuWillShow(ui::SimpleMenuModel* source) OVERRIDE {}
  virtual void MenuClosed(ui::SimpleMenuModel* source) OVERRIDE {}
  virtual bool GetAcceleratorForCommandId(
      int command_id,
      ui::Accelerator* accelerator) OVERRIDE {
    return false;
  }

  // RenderViewContextMenuProxy implementation.
  virtual void AddMenuItem(int command_id, const string16& title) OVERRIDE {}
  virtual void AddCheckItem(int command_id, const string16& title) OVERRIDE {}
  virtual void AddSeparator() OVERRIDE {}
  virtual void AddSubMenu(int command_id,
                          const string16& label,
                          ui::MenuModel* model) OVERRIDE {}
  virtual void UpdateMenuItem(int command_id,
                              bool enabled,
                              bool hidden,
                              const string16& title) OVERRIDE {}
  virtual RenderViewHost* GetRenderViewHost() const OVERRIDE {
    return NULL;
  }
  virtual Profile* GetProfile() const OVERRIDE {
    return profile_.get();
  }
  virtual content::WebContents* GetWebContents() const OVERRIDE {
    return NULL;
  }

  // Attaches a RenderViewContextMenuObserver to be tested.
  void SetObserver(RenderViewContextMenuObserver* observer) {
    observer_ = observer;
  }

  // Returns the number of items added by the test.
  size_t GetMenuSize() const {
    return 0;
  }

  // Returns the i-th item.
  bool GetMenuItem(size_t i, MockMenuItem* item) const {
    return false;
  }

  // Returns the writable profile used in this test.
  PrefService* GetPrefs() {
    return profile_->GetPrefs();
  }

 private:
  // An observer used for initializing the status of menu items added in this
  // test. This is a weak pointer, the test is responsible for deleting this
  // object.
  RenderViewContextMenuObserver* observer_;

  // A dummy profile used in this test. Call GetPrefs() when a test needs to
  // change this profile and use PrefService methods.
  scoped_ptr<TestingProfile> profile_;

  DISALLOW_COPY_AND_ASSIGN(MockRenderViewContextMenu);
};

// A test class used in this file. This test should be a browser test because it
// accesses resources.
class SpellCheckerSubMenuObserverTest : public InProcessBrowserTest {
 public:
  SpellCheckerSubMenuObserverTest() {}
  virtual ~SpellCheckerSubMenuObserverTest() {}

 private:
  DISALLOW_COPY_AND_ASSIGN(SpellCheckerSubMenuObserverTest);
};

}  // namespace

// Tests that selecting the "Check Spelling While Typing" item toggles the value
// of the "browser.enable_spellchecking" profile.
IN_PROC_BROWSER_TEST_F(SpellCheckerSubMenuObserverTest, ToggleSpelling) {
  // Initialize a menu consisting only of a "Spell-checker Options" submenu.
  scoped_ptr<MockRenderViewContextMenu> menu(new MockRenderViewContextMenu);
  scoped_ptr<SpellCheckerSubMenuObserver> observer(
      new SpellCheckerSubMenuObserver(menu.get(), menu.get(), 1));
  menu->SetObserver(observer.get());
  menu->GetPrefs()->SetString(prefs::kAcceptLanguages, "en-US");
  menu->GetPrefs()->SetString(prefs::kSpellCheckDictionary, "en-US");
  menu->GetPrefs()->SetBoolean(prefs::kEnableContinuousSpellcheck, true);
  content::ContextMenuParams params;
  observer->InitMenu(params);

  // Verify this menu has the "Check Spelling While Typing" item and this item
  // is checked.
  EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CHECK_SPELLING_WHILE_TYPING));
  EXPECT_TRUE(menu->IsCommandIdChecked(IDC_CHECK_SPELLING_WHILE_TYPING));

  // Select this item and verify that the "Check Spelling While Typing" item is
  // not checked. Also, verify that the value of "browser.enable_spellchecking"
  // is now false.
  menu->ExecuteCommand(IDC_CHECK_SPELLING_WHILE_TYPING, 0);
  EXPECT_FALSE(
      menu->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck));
  EXPECT_FALSE(menu->IsCommandIdChecked(IDC_CHECK_SPELLING_WHILE_TYPING));
}