#include "components/spellcheck/renderer/spellcheck_provider_test.h"
#include <memory>
#include "base/run_loop.h"
#include "base/task/current_thread.h"
#include "build/build_config.h"
#include "components/spellcheck/common/spellcheck.mojom.h"
#include "components/spellcheck/common/spellcheck_features.h"
#include "components/spellcheck/common/spellcheck_result.h"
#include "components/spellcheck/renderer/hunspell_engine.h"
#include "components/spellcheck/renderer/spellcheck.h"
#include "components/spellcheck/renderer/spellcheck_language.h"
#include "components/spellcheck/spellcheck_buildflags.h"
#if BUILDFLAG(IS_WIN) && BUILDFLAG(USE_BROWSER_SPELLCHECKER)
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/path_service.h"
namespace {
base::FilePath GetHunspellDirectory() {
base::FilePath hunspell_directory;
if (!base::PathService::Get(base::DIR_SRC_TEST_DATA_ROOT,
&hunspell_directory)) {
return base::FilePath();
}
hunspell_directory = hunspell_directory.AppendASCII("third_party");
hunspell_directory = hunspell_directory.AppendASCII("hunspell_dictionaries");
return hunspell_directory;
}
}
#endif
FakeTextCheckingResult::FakeTextCheckingResult() = default;
FakeTextCheckingResult::~FakeTextCheckingResult() = default;
FakeTextCheckingCompletion::FakeTextCheckingCompletion(
FakeTextCheckingResult* result)
: … { … }
FakeTextCheckingCompletion::~FakeTextCheckingCompletion() { … }
void FakeTextCheckingCompletion::DidFinishCheckingText(
const blink::WebVector<blink::WebTextCheckingResult>& results) { … }
void FakeTextCheckingCompletion::DidCancelCheckingText() { … }
FakeSpellCheck::FakeSpellCheck(
service_manager::LocalInterfaceProvider* embedder_provider)
: … { … }
void FakeSpellCheck::SetFakeLanguageCounts(size_t language_count,
size_t enabled_count) { … }
#if BUILDFLAG(IS_WIN) && BUILDFLAG(USE_BROWSER_SPELLCHECKER)
void FakeSpellCheck::InitializeSpellCheckForLocale(const std::string& language,
bool use_hunspell) {
base::File file;
if (use_hunspell) {
base::FilePath hunspell_directory = GetHunspellDirectory();
EXPECT_FALSE(hunspell_directory.empty());
base::FilePath hunspell_file_path =
spellcheck::GetVersionedFileName(language, hunspell_directory);
file.Initialize(hunspell_file_path,
base::File::FLAG_OPEN | base::File::FLAG_READ);
EXPECT_TRUE(file.IsValid()) << hunspell_file_path << " is not valid"
<< file.ErrorToString(file.GetLastFileError());
}
SpellCheck::languages_.push_back(
std::make_unique<SpellcheckLanguage>(embedder_provider_));
SpellCheck::languages_.back()->platform_spelling_engine_ =
std::make_unique<HunspellEngine>(embedder_provider_);
SpellCheck::languages_.back()->Init(std::move(file), language);
}
#endif
size_t FakeSpellCheck::LanguageCount() { … }
size_t FakeSpellCheck::EnabledLanguageCount() { … }
TestingSpellCheckProvider::TestingSpellCheckProvider(
service_manager::LocalInterfaceProvider* embedder_provider)
: … { … }
TestingSpellCheckProvider::TestingSpellCheckProvider(
SpellCheck* spellcheck,
service_manager::LocalInterfaceProvider* embedder_provider)
: … { … }
TestingSpellCheckProvider::~TestingSpellCheckProvider() { … }
void TestingSpellCheckProvider::RequestTextChecking(
const std::u16string& text,
std::unique_ptr<blink::WebTextCheckingCompletion> completion) { … }
void TestingSpellCheckProvider::NotifyChecked(const std::u16string& word,
bool misspelled) { … }
#if BUILDFLAG(USE_RENDERER_SPELLCHECKER)
void TestingSpellCheckProvider::CallSpellingService(
const std::u16string& text,
CallSpellingServiceCallback callback) { … }
void TestingSpellCheckProvider::OnCallSpellingService(
const std::u16string& text) { … }
void TestingSpellCheckProvider::ResetResult() { … }
#endif
#if BUILDFLAG(USE_BROWSER_SPELLCHECKER)
void TestingSpellCheckProvider::RequestTextCheck(
const std::u16string& text,
RequestTextCheckCallback callback) {
text_check_requests_.push_back(std::make_pair(text, std::move(callback)));
}
void TestingSpellCheckProvider::CheckSpelling(const std::u16string&,
CheckSpellingCallback) {
NOTREACHED_IN_MIGRATION();
}
void TestingSpellCheckProvider::FillSuggestionList(const std::u16string&,
FillSuggestionListCallback) {
NOTREACHED_IN_MIGRATION();
}
#if BUILDFLAG(IS_WIN)
void TestingSpellCheckProvider::InitializeDictionaries(
InitializeDictionariesCallback callback) {
if (base::FeatureList::IsEnabled(
spellcheck::kWinDelaySpellcheckServiceInit)) {
std::move(callback).Run({}, {},
false);
return;
}
NOTREACHED_IN_MIGRATION();
}
#endif
#endif
#if BUILDFLAG(IS_ANDROID)
void TestingSpellCheckProvider::DisconnectSessionBridge() {
NOTREACHED_IN_MIGRATION();
}
#endif
void TestingSpellCheckProvider::SetLastResults(
const std::u16string last_request,
blink::WebVector<blink::WebTextCheckingResult>& last_results) { … }
bool TestingSpellCheckProvider::SatisfyRequestFromCache(
const std::u16string& text,
blink::WebTextCheckingCompletion* completion) { … }
#if BUILDFLAG(IS_WIN) && BUILDFLAG(USE_BROWSER_SPELLCHECKER)
int TestingSpellCheckProvider::AddCompletionForTest(
std::unique_ptr<FakeTextCheckingCompletion> completion,
SpellCheckProvider::HybridSpellCheckRequestInfo request_info) {
int id =
SpellCheckProvider::text_check_completions_.Add(std::move(completion));
SpellCheckProvider::hybrid_requests_info_[id] = request_info;
return id;
}
void TestingSpellCheckProvider::OnRespondTextCheck(
int identifier,
const std::u16string& line,
const std::vector<SpellCheckResult>& results) {
SpellCheckProvider::OnRespondTextCheck(identifier, line, results);
base::RunLoop().RunUntilIdle();
}
#endif
base::WeakPtr<SpellCheckProvider> TestingSpellCheckProvider::GetWeakPtr() { … }
SpellCheckProviderTest::SpellCheckProviderTest()
: … { … }
SpellCheckProviderTest::~SpellCheckProviderTest() { … }