#include "chrome/browser/renderer_context_menu/spelling_menu_observer.h"
#include <memory>
#include "base/strings/utf_string_conversions.h"
#include "base/test/scoped_feature_list.h"
#include "base/values.h"
#include "build/build_config.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/renderer_context_menu/mock_render_view_context_menu.h"
#include "chrome/browser/spellchecker/spellcheck_factory.h"
#include "chrome/browser/spellchecker/spellcheck_service.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "components/prefs/pref_service.h"
#include "components/spellcheck/browser/pref_names.h"
#include "components/spellcheck/browser/spelling_service_client.h"
#include "components/spellcheck/common/spellcheck_features.h"
#include "components/spellcheck/spellcheck_buildflags.h"
#include "content/public/browser/context_menu_params.h"
#include "content/public/test/browser_test.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
class SpellingMenuObserverTest : public InProcessBrowserTest { … };
#if BUILDFLAG(IS_WIN) && BUILDFLAG(USE_BROWSER_SPELLCHECKER)
SpellingMenuObserverTest::SpellingMenuObserverTest() {
feature_list_.InitWithFeatures(
{spellcheck::kWinRetrieveSuggestionsOnlyOnDemand},
{spellcheck::kWinDelaySpellcheckServiceInit});
}
#else
SpellingMenuObserverTest::SpellingMenuObserverTest() = default;
#endif
SpellingMenuObserverTest::~SpellingMenuObserverTest() = default;
}
IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest, InitMenuWithCorrectWord) { … }
IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest, InitMenuWithMisspelledWord) { … }
#if BUILDFLAG(IS_WIN) && BUILDFLAG(USE_BROWSER_SPELLCHECKER)
IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest,
WinInitMenuWithMisspelledWordCombined) {
InitMenu("mispelled", "misspelling");
EXPECT_EQ(6U, 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);
EXPECT_EQ(u"misspelled", item.title);
menu()->GetMenuItem(2, &item);
EXPECT_EQ(IDC_SPELLCHECK_SUGGESTION_0 + 1, item.command_id);
EXPECT_TRUE(item.enabled);
EXPECT_FALSE(item.hidden);
EXPECT_EQ(u"misspelling", item.title);
menu()->GetMenuItem(3, &item);
EXPECT_EQ(-1, item.command_id);
EXPECT_FALSE(item.enabled);
EXPECT_FALSE(item.hidden);
menu()->GetMenuItem(4, &item);
EXPECT_EQ(IDC_SPELLCHECK_ADD_TO_DICTIONARY, item.command_id);
EXPECT_TRUE(item.enabled);
EXPECT_FALSE(item.hidden);
menu()->GetMenuItem(5, &item);
EXPECT_EQ(IDC_CONTENT_CONTEXT_SPELLING_TOGGLE, item.command_id);
EXPECT_TRUE(item.enabled);
EXPECT_FALSE(item.checked);
EXPECT_FALSE(item.hidden);
}
IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest,
WinInitMenuWithMisspelledWordNoDuplicateSuggestions) {
InitMenu("mispelled", "misspelled");
EXPECT_EQ(5U, 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);
EXPECT_EQ(u"misspelled", item.title);
menu()->GetMenuItem(2, &item);
EXPECT_EQ(-1, item.command_id);
EXPECT_FALSE(item.enabled);
EXPECT_FALSE(item.hidden);
menu()->GetMenuItem(3, &item);
EXPECT_EQ(IDC_SPELLCHECK_ADD_TO_DICTIONARY, item.command_id);
EXPECT_TRUE(item.enabled);
EXPECT_FALSE(item.hidden);
menu()->GetMenuItem(4, &item);
EXPECT_EQ(IDC_CONTENT_CONTEXT_SPELLING_TOGGLE, item.command_id);
EXPECT_TRUE(item.enabled);
EXPECT_FALSE(item.checked);
EXPECT_FALSE(item.hidden);
}
IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest,
WinInitMenuWithMisspelledWordMaxSuggestions) {
InitMenu("wtree", "wee");
EXPECT_EQ(7U, menu()->GetMenuSize());
std::set<std::u16string> suggestions(
{u"tree", u"twee", u"wee", u"ware", u"were"});
bool wee_suggested = false;
for (unsigned int i = 1; i < menu()->GetMenuSize(); i++) {
MockRenderViewContextMenu::MockMenuItem item;
menu()->GetMenuItem(i, &item);
if (!item.title.compare(u"wee")) {
wee_suggested = true;
break;
}
}
EXPECT_TRUE(wee_suggested);
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);
EXPECT_TRUE(suggestions.contains(item.title));
menu()->GetMenuItem(2, &item);
EXPECT_EQ(IDC_SPELLCHECK_SUGGESTION_0 + 1, item.command_id);
EXPECT_TRUE(item.enabled);
EXPECT_FALSE(item.hidden);
EXPECT_TRUE(suggestions.contains(item.title));
menu()->GetMenuItem(3, &item);
EXPECT_EQ(IDC_SPELLCHECK_SUGGESTION_0 + 2, item.command_id);
EXPECT_TRUE(item.enabled);
EXPECT_FALSE(item.hidden);
EXPECT_TRUE(suggestions.contains(item.title));
menu()->GetMenuItem(4, &item);
EXPECT_EQ(-1, item.command_id);
EXPECT_FALSE(item.enabled);
EXPECT_FALSE(item.hidden);
menu()->GetMenuItem(5, &item);
EXPECT_EQ(IDC_SPELLCHECK_ADD_TO_DICTIONARY, item.command_id);
EXPECT_TRUE(item.enabled);
EXPECT_FALSE(item.hidden);
menu()->GetMenuItem(6, &item);
EXPECT_EQ(IDC_CONTENT_CONTEXT_SPELLING_TOGGLE, item.command_id);
EXPECT_TRUE(item.enabled);
EXPECT_FALSE(item.checked);
EXPECT_FALSE(item.hidden);
}
#endif
IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest,
EnableSpellingServiceWithCorrectWord) { … }
IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest, EnableSpellingService) { … }
IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest,
EnableSpellingServiceWhenSpellcheckDisabled) { … }
IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest,
NoMoreSuggestionsNotDisplayed) { … }
#if BUILDFLAG(IS_WIN)
#define MAYBE_NoSpellingServiceWhenOffTheRecord …
#else
#define MAYBE_NoSpellingServiceWhenOffTheRecord …
#endif
IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest,
MAYBE_NoSpellingServiceWhenOffTheRecord) { … }
#if BUILDFLAG(IS_WIN)
#define MAYBE_SuggestionsForceTopSeparator …
#else
#define MAYBE_SuggestionsForceTopSeparator …
#endif
IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest,
MAYBE_SuggestionsForceTopSeparator) { … }