This source file includes following definitions.
- AddMenuItem
- AddCheckItem
- AddSeparator
- AddSubMenu
- UpdateMenuItem
- GetRenderViewHost
- GetWebContents
- GetProfile
- GetMenuSize
- GetMenuItem
- SetObserver
- GetPrefs
- SetUpOnMainThread
- CleanUpOnMainThread
- Reset
- InitMenu
- menu
- observer
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
#include "chrome/browser/renderer_context_menu/spelling_menu_observer.h"
#include <vector>
#include "base/command_line.h"
#include "base/prefs/pref_service.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/renderer_context_menu/render_view_context_menu.h"
#include "chrome/browser/renderer_context_menu/render_view_context_menu_observer.h"
#include "chrome/browser/spellchecker/spelling_service_client.h"
#include "chrome/common/chrome_switches.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 {
class MockRenderViewContextMenu : public RenderViewContextMenuProxy {
public:
struct MockMenuItem {
MockMenuItem()
: command_id(0),
enabled(false),
checked(false),
hidden(true) {
}
int command_id;
bool enabled;
bool checked;
bool hidden;
base::string16 title;
};
explicit MockRenderViewContextMenu(bool incognito);
virtual ~MockRenderViewContextMenu();
virtual void AddMenuItem(int command_id,
const base::string16& title) OVERRIDE;
virtual void AddCheckItem(int command_id,
const base::string16& title) OVERRIDE;
virtual void AddSeparator() OVERRIDE;
virtual void AddSubMenu(int command_id,
const base::string16& label,
ui::MenuModel* model) OVERRIDE;
virtual void UpdateMenuItem(int command_id,
bool enabled,
bool hidden,
const base::string16& title) OVERRIDE;
virtual RenderViewHost* GetRenderViewHost() const OVERRIDE;
virtual WebContents* GetWebContents() const OVERRIDE;
virtual Profile* GetProfile() const OVERRIDE;
void SetObserver(RenderViewContextMenuObserver* observer);
size_t GetMenuSize() const;
bool GetMenuItem(size_t i, MockMenuItem* item) const;
PrefService* GetPrefs();
private:
RenderViewContextMenuObserver* observer_;
scoped_ptr<TestingProfile> profile_;
std::vector<MockMenuItem> items_;
DISALLOW_COPY_AND_ASSIGN(MockRenderViewContextMenu);
};
MockRenderViewContextMenu::MockRenderViewContextMenu(bool incognito)
: observer_(NULL) {
TestingProfile::Builder builder;
if (incognito)
builder.SetIncognito();
profile_ = builder.Build();
}
MockRenderViewContextMenu::~MockRenderViewContextMenu() {
}
void MockRenderViewContextMenu::AddMenuItem(int command_id,
const base::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 base::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 base::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 base::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;
}
}
FAIL();
}
RenderViewHost* MockRenderViewContextMenu::GetRenderViewHost() const {
return NULL;
}
WebContents* MockRenderViewContextMenu::GetWebContents() 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();
}
class SpellingMenuObserverTest : public InProcessBrowserTest {
public:
SpellingMenuObserverTest();
virtual void SetUpOnMainThread() OVERRIDE {
Reset(false);
}
virtual void CleanUpOnMainThread() OVERRIDE {
observer_.reset();
menu_.reset();
}
void Reset(bool incognito) {
observer_.reset();
menu_.reset(new MockRenderViewContextMenu(incognito));
observer_.reset(new SpellingMenuObserver(menu_.get()));
menu_->SetObserver(observer_.get());
}
void InitMenu(const char* word, const char* suggestion) {
content::ContextMenuParams params;
params.is_editable = true;
params.misspelled_word = base::ASCIIToUTF16(word);
params.dictionary_suggestions.clear();
if (suggestion)
params.dictionary_suggestions.push_back(base::ASCIIToUTF16(suggestion));
observer_->InitMenu(params);
}
virtual ~SpellingMenuObserverTest();
MockRenderViewContextMenu* menu() { return menu_.get(); }
SpellingMenuObserver* observer() { return observer_.get(); }
private:
scoped_ptr<SpellingMenuObserver> observer_;
scoped_ptr<MockRenderViewContextMenu> menu_;
DISALLOW_COPY_AND_ASSIGN(SpellingMenuObserverTest);
};
SpellingMenuObserverTest::SpellingMenuObserverTest() {
}
SpellingMenuObserverTest::~SpellingMenuObserverTest() {
}
}
IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest, InitMenuWithCorrectWord) {
InitMenu("", NULL);
EXPECT_EQ(static_cast<size_t>(0), menu()->GetMenuSize());
}
IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest, InitMenuWithMisspelledWord) {
InitMenu("wiimode", NULL);
EXPECT_EQ(static_cast<size_t>(4), menu()->GetMenuSize());
MockRenderViewContextMenu::MockMenuItem item;
menu()->GetMenuItem(0, &item);
EXPECT_EQ(IDC_CONTENT_CONTEXT_NO_SPELLING_SUGGESTIONS, item.command_id);
EXPECT_FALSE(item.enabled);
EXPECT_FALSE(item.hidden);
menu()->GetMenuItem(1, &item);
EXPECT_EQ(IDC_SPELLCHECK_ADD_TO_DICTIONARY, item.command_id);
EXPECT_TRUE(item.enabled);
EXPECT_FALSE(item.hidden);
menu()->GetMenuItem(2, &item);
EXPECT_EQ(IDC_CONTENT_CONTEXT_SPELLING_TOGGLE, item.command_id);
EXPECT_TRUE(item.enabled);
EXPECT_FALSE(item.checked);
EXPECT_FALSE(item.hidden);
menu()->GetMenuItem(3, &item);
EXPECT_EQ(-1, item.command_id);
EXPECT_FALSE(item.enabled);
EXPECT_FALSE(item.hidden);
}
IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest,
EnableSpellingServiceWithCorrectWord) {
menu()->GetPrefs()->SetBoolean(prefs::kSpellCheckUseSpellingService, true);
InitMenu("", NULL);
EXPECT_TRUE(
observer()->IsCommandIdChecked(IDC_CONTENT_CONTEXT_SPELLING_TOGGLE));
}
IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest, EnableSpellingService) {
menu()->GetPrefs()->SetBoolean(prefs::kSpellCheckUseSpellingService, true);
menu()->GetPrefs()->SetString(prefs::kSpellCheckDictionary, std::string());
InitMenu("wiimode", NULL);
EXPECT_EQ(static_cast<size_t>(4), menu()->GetMenuSize());
MockRenderViewContextMenu::MockMenuItem item;
menu()->GetMenuItem(2, &item);
EXPECT_EQ(IDC_CONTENT_CONTEXT_SPELLING_TOGGLE, item.command_id);
EXPECT_TRUE(item.enabled);
EXPECT_TRUE(item.checked);
EXPECT_FALSE(item.hidden);
}
IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest, SeparatorAfterSuggestions) {
menu()->GetPrefs()->SetBoolean(prefs::kSpellCheckUseSpellingService, true);
CommandLine* command_line = CommandLine::ForCurrentProcess();
command_line->AppendSwitch(switches::kUseSpellingSuggestions);
menu()->GetPrefs()->SetString(prefs::kSpellCheckDictionary, "en");
EXPECT_TRUE(SpellingServiceClient::IsAvailable(menu()->GetProfile(),
SpellingServiceClient::SUGGEST));
InitMenu("jhhj", NULL);
EXPECT_LT(4U, menu()->GetMenuSize());
MockRenderViewContextMenu::MockMenuItem item;
menu()->GetMenuItem(0, &item);
EXPECT_EQ(-1, item.command_id);
EXPECT_FALSE(item.enabled);
EXPECT_FALSE(item.hidden);
menu()->GetMenuItem(1, &item);
EXPECT_EQ(IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION, item.command_id);
EXPECT_FALSE(item.enabled);
EXPECT_FALSE(item.hidden);
menu()->GetMenuItem(2, &item);
EXPECT_EQ(IDC_CONTENT_CONTEXT_NO_SPELLING_SUGGESTIONS, item.command_id);
EXPECT_FALSE(item.enabled);
EXPECT_FALSE(item.hidden);
menu()->GetMenuItem(3, &item);
EXPECT_EQ(-1, item.command_id);
EXPECT_FALSE(item.enabled);
EXPECT_FALSE(item.hidden);
}
IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest,
NoMoreSuggestionsNotDisplayed) {
menu()->GetPrefs()->SetBoolean(prefs::kSpellCheckUseSpellingService, true);
menu()->GetPrefs()->SetString(prefs::kSpellCheckDictionary, "en");
EXPECT_TRUE(SpellingServiceClient::IsAvailable(menu()->GetProfile(),
SpellingServiceClient::SPELLCHECK));
InitMenu("asdfkj", "asdf");
EXPECT_LT(3U, menu()->GetMenuSize());
MockRenderViewContextMenu::MockMenuItem item;
menu()->GetMenuItem(0, &item);
EXPECT_EQ(-1, item.command_id);
EXPECT_FALSE(item.enabled);
EXPECT_FALSE(item.hidden);
menu()->GetMenuItem(1, &item);
EXPECT_EQ(IDC_SPELLCHECK_SUGGESTION_0, item.command_id);
EXPECT_TRUE(item.enabled);
EXPECT_FALSE(item.hidden);
menu()->GetMenuItem(2, &item);
EXPECT_EQ(-1, item.command_id);
EXPECT_FALSE(item.enabled);
EXPECT_FALSE(item.hidden);
}
IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest,
NoSpellingServiceWhenOffTheRecord) {
Reset(true);
menu()->GetPrefs()->SetBoolean(prefs::kSpellCheckUseSpellingService, true);
menu()->GetPrefs()->SetString(prefs::kSpellCheckDictionary, "en");
EXPECT_FALSE(SpellingServiceClient::IsAvailable(menu()->GetProfile(),
SpellingServiceClient::SUGGEST));
EXPECT_FALSE(SpellingServiceClient::IsAvailable(menu()->GetProfile(),
SpellingServiceClient::SPELLCHECK));
InitMenu("sjxdjiiiiii", NULL);
EXPECT_LT(3U, menu()->GetMenuSize());
MockRenderViewContextMenu::MockMenuItem item;
menu()->GetMenuItem(0, &item);
EXPECT_EQ(IDC_CONTENT_CONTEXT_NO_SPELLING_SUGGESTIONS, item.command_id);
EXPECT_FALSE(item.enabled);
EXPECT_FALSE(item.hidden);
menu()->GetMenuItem(1, &item);
EXPECT_EQ(IDC_SPELLCHECK_ADD_TO_DICTIONARY, item.command_id);
EXPECT_TRUE(item.enabled);
EXPECT_FALSE(item.hidden);
menu()->GetMenuItem(2, &item);
EXPECT_EQ(IDC_CONTENT_CONTEXT_SPELLING_TOGGLE, item.command_id);
EXPECT_FALSE(item.enabled);
EXPECT_FALSE(item.hidden);
}
IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest, SuggestionsForceTopSeparator) {
menu()->GetPrefs()->SetBoolean(prefs::kSpellCheckUseSpellingService, false);
InitMenu("asdfkj", NULL);
EXPECT_EQ(static_cast<size_t>(4), menu()->GetMenuSize());
MockRenderViewContextMenu::MockMenuItem item;
menu()->GetMenuItem(0, &item);
EXPECT_NE(-1, item.command_id);
Reset(false);
menu()->GetPrefs()->SetBoolean(prefs::kSpellCheckUseSpellingService, false);
InitMenu("asdfkj", "asdf");
EXPECT_LT(static_cast<size_t>(5), menu()->GetMenuSize());
menu()->GetMenuItem(0, &item);
EXPECT_EQ(-1, item.command_id);
Reset(false);
menu()->GetPrefs()->SetBoolean(prefs::kSpellCheckUseSpellingService, true);
CommandLine* command_line = CommandLine::ForCurrentProcess();
command_line->AppendSwitch(switches::kUseSpellingSuggestions);
InitMenu("asdfkj", NULL);
EXPECT_LT(2U, menu()->GetMenuSize());
menu()->GetMenuItem(0, &item);
EXPECT_EQ(-1, item.command_id);
menu()->GetMenuItem(1, &item);
EXPECT_EQ(IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION, item.command_id);
}