Chromium Code Reviews| Index: chrome/browser/tab_contents/spellchecker_submenu_observer_browsertest.cc |
| =================================================================== |
| --- chrome/browser/tab_contents/spellchecker_submenu_observer_browsertest.cc (revision 0) |
| +++ chrome/browser/tab_contents/spellchecker_submenu_observer_browsertest.cc (revision 0) |
| @@ -0,0 +1,273 @@ |
| +// 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 <vector> |
| + |
| +#include "base/utf_string_conversions.h" |
| +#include "chrome/app/chrome_command_ids.h" |
| +#include "chrome/browser/prefs/pref_service.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; |
| + |
| +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. This test uses a vector of this struct to |
| + // hold menu items added by this test. |
|
jeremy
2012/06/19 12:14:43
I think you can drop the second sentence of this c
Hironori Bono
2012/06/21 09:38:18
Done. I have also removed code not needed by this
|
| + struct MockMenuItem { |
| + MockMenuItem() |
| + : command_id(0), |
| + enabled(false), |
| + checked(false), |
| + hidden(true) { |
| + } |
| + int command_id; |
| + bool enabled; |
| + bool checked; |
| + bool hidden; |
| + string16 title; |
| + }; |
| + |
| + MockRenderViewContextMenu(); |
| + virtual ~MockRenderViewContextMenu(); |
| + |
| + // SimpleMenuModel::Delegate implementation. |
| + virtual bool IsCommandIdChecked(int command_id) const OVERRIDE; |
| + virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE; |
| + virtual void ExecuteCommand(int command_id) OVERRIDE; |
| + virtual void ExecuteCommand(int command_id, int event_flags) OVERRIDE; |
| + virtual void MenuWillShow(ui::SimpleMenuModel* source) OVERRIDE; |
| + virtual void MenuClosed(ui::SimpleMenuModel* source) OVERRIDE; |
| + virtual bool GetAcceleratorForCommandId( |
| + int command_id, |
| + ui::Accelerator* accelerator) OVERRIDE; |
| + |
| + // 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; |
| + virtual Profile* GetProfile() const OVERRIDE; |
| + |
| + // Attaches a RenderViewContextMenuObserver to be tested. |
| + void SetObserver(RenderViewContextMenuObserver* observer); |
| + |
| + // Returns the number of items added by the test. |
| + size_t GetMenuSize() const; |
| + |
| + // Returns the i-th item. |
| + bool GetMenuItem(size_t i, MockMenuItem* item) const; |
| + |
| + // Returns the writable profile used in this test. |
| + PrefService* GetPrefs(); |
| + |
| + private: |
| + // An observer used for initializing the status of menu items added in this |
| + // test. A test should delete this RenderViewContextMenuObserver object. |
|
jeremy
2012/06/19 12:14:43
How about this for the second sentence:
This is a
Hironori Bono
2012/06/21 09:38:18
Done. Thanks for correcting my comment.
|
| + 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_; |
| + |
| + // A list of menu items added by the SpellingMenuObserver class. |
| + std::vector<MockMenuItem> items_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(MockRenderViewContextMenu); |
| +}; |
| + |
| +MockRenderViewContextMenu::MockRenderViewContextMenu() |
| + : observer_(NULL), |
| + profile_(new TestingProfile) { |
| +} |
| + |
| +MockRenderViewContextMenu::~MockRenderViewContextMenu() { |
| +} |
| + |
| +bool MockRenderViewContextMenu::IsCommandIdChecked(int command_id) const { |
| + return observer_->IsCommandIdChecked(command_id); |
| +} |
| + |
| +bool MockRenderViewContextMenu::IsCommandIdEnabled(int command_id) const { |
| + return observer_->IsCommandIdEnabled(command_id); |
| +} |
| + |
| +void MockRenderViewContextMenu::ExecuteCommand(int command_id) { |
| + return observer_->ExecuteCommand(command_id); |
| +} |
| + |
| +void MockRenderViewContextMenu::ExecuteCommand(int command_id, |
| + int event_flags) { |
| +} |
| + |
| +void MockRenderViewContextMenu::MenuWillShow(ui::SimpleMenuModel* source) { |
| +} |
| + |
| +void MockRenderViewContextMenu::MenuClosed(ui::SimpleMenuModel* source) { |
| +} |
| + |
| +bool MockRenderViewContextMenu::GetAcceleratorForCommandId( |
| + int command_id, |
| + ui::Accelerator* accelerator) { |
| + return false; |
| +} |
| + |
| +void MockRenderViewContextMenu::AddMenuItem(int command_id, |
| + const string16& title) { |
| + MockMenuItem item; |
| + item.command_id = command_id; |
| + item.enabled = observer_->IsCommandIdEnabled(command_id); |
| + item.checked = false; |
| + item.hidden = false; |
| + item.title = title; |
| + items_.push_back(item); |
| +} |
| + |
| +void MockRenderViewContextMenu::AddCheckItem(int command_id, |
| + const string16& title) { |
| + MockMenuItem item; |
| + item.command_id = command_id; |
| + item.enabled = observer_->IsCommandIdEnabled(command_id); |
| + item.checked = observer_->IsCommandIdChecked(command_id); |
| + item.hidden = false; |
| + item.title = title; |
| + items_.push_back(item); |
| +} |
| + |
| +void MockRenderViewContextMenu::AddSeparator() { |
| + MockMenuItem item; |
| + item.command_id = -1; |
| + item.enabled = false; |
| + item.checked = false; |
| + item.hidden = false; |
| + items_.push_back(item); |
| +} |
| + |
| +void MockRenderViewContextMenu::AddSubMenu(int command_id, |
| + const string16& label, |
| + ui::MenuModel* model) { |
| + MockMenuItem item; |
| + item.command_id = -1; |
| + item.enabled = false; |
| + item.checked = false; |
| + item.hidden = false; |
| + items_.push_back(item); |
| +} |
| + |
| +void MockRenderViewContextMenu::UpdateMenuItem(int command_id, |
| + bool enabled, |
| + bool hidden, |
| + const string16& title) { |
| + for (std::vector<MockMenuItem>::iterator it = items_.begin(); |
| + it != items_.end(); ++it) { |
| + if (it->command_id == command_id) { |
| + it->enabled = enabled; |
| + it->hidden = hidden; |
| + it->title = title; |
| + return; |
| + } |
| + } |
| + |
| + // The SpellingMenuObserver class tries to change a menu item not added by the |
| + // class. This is an unexpected behavior and we should stop now. |
| + FAIL(); |
| +} |
| + |
| +RenderViewHost* MockRenderViewContextMenu::GetRenderViewHost() const { |
| + return NULL; |
| +} |
| + |
| +Profile* MockRenderViewContextMenu::GetProfile() const { |
| + return profile_.get(); |
| +} |
| + |
| +size_t MockRenderViewContextMenu::GetMenuSize() const { |
| + return items_.size(); |
| +} |
| + |
| +bool MockRenderViewContextMenu::GetMenuItem(size_t i, |
| + MockMenuItem* item) const { |
| + if (i >= items_.size()) |
| + return false; |
| + item->command_id = items_[i].command_id; |
| + item->enabled = items_[i].enabled; |
| + item->checked = items_[i].checked; |
| + item->hidden = items_[i].hidden; |
| + item->title = items_[i].title; |
| + return true; |
| +} |
| + |
| +void MockRenderViewContextMenu::SetObserver( |
| + RenderViewContextMenuObserver* observer) { |
| + observer_ = observer; |
| +} |
| + |
| +PrefService* MockRenderViewContextMenu::GetPrefs() { |
| + return profile_->GetPrefs(); |
| +} |
| + |
| +// 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); |
| +}; |
| + |
| +SpellCheckerSubMenuObserverTest::SpellCheckerSubMenuObserverTest() { |
| +} |
|
jeremy
2012/06/19 12:14:43
Can you just declare these inline in the class? Sa
Hironori Bono
2012/06/21 09:38:18
Done. Thanks for noticing it.
|
| + |
| +SpellCheckerSubMenuObserverTest::~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::kEnableSpellCheck, 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 after verifying the "Check Spelling While Typing" item is |
| + // checked. If this test works as expected, this item becomes unchecked and |
| + // the value of "browser.enable_spellchecking" become false. |
| + menu->ExecuteCommand(IDC_CHECK_SPELLING_WHILE_TYPING); |
| + EXPECT_FALSE(menu->GetPrefs()->GetBoolean(prefs::kEnableSpellCheck)); |
| + EXPECT_FALSE(menu->IsCommandIdChecked(IDC_CHECK_SPELLING_WHILE_TYPING)); |
| +} |
| Property changes on: chrome\browser\tab_contents\spellchecker_submenu_observer_browsertest.cc |
| ___________________________________________________________________ |
| Added: svn:eol-style |
| + LF |