spellcheck_provider_test.cc 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/spellcheck/renderer/spellcheck_provider_test.h"
  5. #include <memory>
  6. #include "base/run_loop.h"
  7. #include "base/task/current_thread.h"
  8. #include "build/build_config.h"
  9. #include "components/spellcheck/common/spellcheck.mojom.h"
  10. #include "components/spellcheck/common/spellcheck_features.h"
  11. #include "components/spellcheck/common/spellcheck_result.h"
  12. #include "components/spellcheck/renderer/hunspell_engine.h"
  13. #include "components/spellcheck/renderer/spellcheck.h"
  14. #include "components/spellcheck/renderer/spellcheck_language.h"
  15. #include "components/spellcheck/spellcheck_buildflags.h"
  16. #if BUILDFLAG(IS_WIN) && BUILDFLAG(USE_BROWSER_SPELLCHECKER)
  17. #include "base/files/file_path.h"
  18. #include "base/files/file_util.h"
  19. #include "base/path_service.h"
  20. namespace {
  21. base::FilePath GetHunspellDirectory() {
  22. base::FilePath hunspell_directory;
  23. if (!base::PathService::Get(base::DIR_SOURCE_ROOT, &hunspell_directory))
  24. return base::FilePath();
  25. hunspell_directory = hunspell_directory.AppendASCII("third_party");
  26. hunspell_directory = hunspell_directory.AppendASCII("hunspell_dictionaries");
  27. return hunspell_directory;
  28. }
  29. } // namespace
  30. #endif // BUILDFLAG(IS_WIN) && BUILDFLAG(USE_BROWSER_SPELLCHECKER)
  31. FakeTextCheckingResult::FakeTextCheckingResult() = default;
  32. FakeTextCheckingResult::~FakeTextCheckingResult() = default;
  33. FakeTextCheckingCompletion::FakeTextCheckingCompletion(
  34. FakeTextCheckingResult* result)
  35. : result_(result) {}
  36. FakeTextCheckingCompletion::~FakeTextCheckingCompletion() {}
  37. void FakeTextCheckingCompletion::DidFinishCheckingText(
  38. const blink::WebVector<blink::WebTextCheckingResult>& results) {
  39. ++result_->completion_count_;
  40. result_->results_ = results;
  41. }
  42. void FakeTextCheckingCompletion::DidCancelCheckingText() {
  43. ++result_->completion_count_;
  44. ++result_->cancellation_count_;
  45. }
  46. FakeSpellCheck::FakeSpellCheck(
  47. service_manager::LocalInterfaceProvider* embedder_provider)
  48. : SpellCheck(embedder_provider) {}
  49. void FakeSpellCheck::SetFakeLanguageCounts(size_t language_count,
  50. size_t enabled_count) {
  51. use_fake_counts_ = true;
  52. language_count_ = language_count;
  53. enabled_language_count_ = enabled_count;
  54. }
  55. #if BUILDFLAG(IS_WIN) && BUILDFLAG(USE_BROWSER_SPELLCHECKER)
  56. void FakeSpellCheck::InitializeSpellCheckForLocale(const std::string& language,
  57. bool use_hunspell) {
  58. // Non-Hunspell case is passed invalid file to SpellcheckLanguage::Init.
  59. base::File file;
  60. if (use_hunspell) {
  61. base::FilePath hunspell_directory = GetHunspellDirectory();
  62. EXPECT_FALSE(hunspell_directory.empty());
  63. base::FilePath hunspell_file_path =
  64. spellcheck::GetVersionedFileName(language, hunspell_directory);
  65. file.Initialize(hunspell_file_path,
  66. base::File::FLAG_OPEN | base::File::FLAG_READ);
  67. EXPECT_TRUE(file.IsValid()) << hunspell_file_path << " is not valid"
  68. << file.ErrorToString(file.GetLastFileError());
  69. }
  70. // Add the SpellcheckLanguage manually to the SpellCheck object.
  71. SpellCheck::languages_.push_back(
  72. std::make_unique<SpellcheckLanguage>(embedder_provider_));
  73. SpellCheck::languages_.back()->platform_spelling_engine_ =
  74. std::make_unique<HunspellEngine>(embedder_provider_);
  75. SpellCheck::languages_.back()->Init(std::move(file), language);
  76. }
  77. #endif // BUILDFLAG(IS_WIN) && BUILDFLAG(USE_BROWSER_SPELLCHECKER)
  78. size_t FakeSpellCheck::LanguageCount() {
  79. return use_fake_counts_ ? language_count_ : SpellCheck::LanguageCount();
  80. }
  81. size_t FakeSpellCheck::EnabledLanguageCount() {
  82. return use_fake_counts_ ? enabled_language_count_
  83. : SpellCheck::EnabledLanguageCount();
  84. }
  85. TestingSpellCheckProvider::TestingSpellCheckProvider(
  86. service_manager::LocalInterfaceProvider* embedder_provider)
  87. : SpellCheckProvider(nullptr,
  88. new FakeSpellCheck(embedder_provider),
  89. embedder_provider) {}
  90. TestingSpellCheckProvider::TestingSpellCheckProvider(
  91. SpellCheck* spellcheck,
  92. service_manager::LocalInterfaceProvider* embedder_provider)
  93. : SpellCheckProvider(nullptr, spellcheck, embedder_provider) {}
  94. TestingSpellCheckProvider::~TestingSpellCheckProvider() {
  95. receiver_.reset();
  96. // dictionary_update_observer_ must be released before deleting spellcheck_.
  97. ResetDictionaryUpdateObserverForTesting();
  98. delete spellcheck_;
  99. }
  100. void TestingSpellCheckProvider::RequestTextChecking(
  101. const std::u16string& text,
  102. std::unique_ptr<blink::WebTextCheckingCompletion> completion) {
  103. if (!receiver_.is_bound())
  104. SetSpellCheckHostForTesting(receiver_.BindNewPipeAndPassRemote());
  105. SpellCheckProvider::RequestTextChecking(text, std::move(completion));
  106. base::RunLoop().RunUntilIdle();
  107. }
  108. void TestingSpellCheckProvider::RequestDictionary() {}
  109. void TestingSpellCheckProvider::NotifyChecked(const std::u16string& word,
  110. bool misspelled) {}
  111. #if BUILDFLAG(USE_RENDERER_SPELLCHECKER)
  112. void TestingSpellCheckProvider::CallSpellingService(
  113. const std::u16string& text,
  114. CallSpellingServiceCallback callback) {
  115. OnCallSpellingService(text);
  116. std::move(callback).Run(true, std::vector<SpellCheckResult>());
  117. }
  118. void TestingSpellCheckProvider::OnCallSpellingService(
  119. const std::u16string& text) {
  120. ++spelling_service_call_count_;
  121. if (!text_check_completions_.Lookup(last_identifier_)) {
  122. ResetResult();
  123. return;
  124. }
  125. text_.assign(text);
  126. std::unique_ptr<blink::WebTextCheckingCompletion> completion(
  127. text_check_completions_.Replace(last_identifier_, nullptr));
  128. text_check_completions_.Remove(last_identifier_);
  129. std::vector<blink::WebTextCheckingResult> results;
  130. results.push_back(
  131. blink::WebTextCheckingResult(blink::kWebTextDecorationTypeSpelling, 0, 5,
  132. std::vector<blink::WebString>({"hello"})));
  133. completion->DidFinishCheckingText(results);
  134. last_request_ = text;
  135. last_results_ = results;
  136. }
  137. void TestingSpellCheckProvider::ResetResult() {
  138. text_.clear();
  139. }
  140. #endif // BUILDFLAG(USE_RENDERER_SPELLCHECKER)
  141. #if BUILDFLAG(USE_BROWSER_SPELLCHECKER)
  142. void TestingSpellCheckProvider::RequestTextCheck(
  143. const std::u16string& text,
  144. int,
  145. RequestTextCheckCallback callback) {
  146. text_check_requests_.push_back(std::make_pair(text, std::move(callback)));
  147. }
  148. void TestingSpellCheckProvider::CheckSpelling(const std::u16string&,
  149. int,
  150. CheckSpellingCallback) {
  151. NOTREACHED();
  152. }
  153. void TestingSpellCheckProvider::FillSuggestionList(const std::u16string&,
  154. FillSuggestionListCallback) {
  155. NOTREACHED();
  156. }
  157. #if BUILDFLAG(IS_WIN)
  158. void TestingSpellCheckProvider::InitializeDictionaries(
  159. InitializeDictionariesCallback callback) {
  160. if (base::FeatureList::IsEnabled(
  161. spellcheck::kWinDelaySpellcheckServiceInit)) {
  162. std::move(callback).Run(/*dictionaries=*/{}, /*custom_words=*/{},
  163. /*enable=*/false);
  164. return;
  165. }
  166. NOTREACHED();
  167. }
  168. #endif // BUILDFLAG(IS_WIN)
  169. #endif // BUILDFLAG(USE_BROWSER_SPELLCHECKER)
  170. #if BUILDFLAG(IS_ANDROID)
  171. void TestingSpellCheckProvider::DisconnectSessionBridge() {
  172. NOTREACHED();
  173. }
  174. #endif
  175. void TestingSpellCheckProvider::SetLastResults(
  176. const std::u16string last_request,
  177. blink::WebVector<blink::WebTextCheckingResult>& last_results) {
  178. last_request_ = last_request;
  179. last_results_ = last_results;
  180. }
  181. bool TestingSpellCheckProvider::SatisfyRequestFromCache(
  182. const std::u16string& text,
  183. blink::WebTextCheckingCompletion* completion) {
  184. return SpellCheckProvider::SatisfyRequestFromCache(text, completion);
  185. }
  186. #if BUILDFLAG(IS_WIN) && BUILDFLAG(USE_BROWSER_SPELLCHECKER)
  187. int TestingSpellCheckProvider::AddCompletionForTest(
  188. std::unique_ptr<FakeTextCheckingCompletion> completion,
  189. SpellCheckProvider::HybridSpellCheckRequestInfo request_info) {
  190. int id =
  191. SpellCheckProvider::text_check_completions_.Add(std::move(completion));
  192. SpellCheckProvider::hybrid_requests_info_[id] = request_info;
  193. return id;
  194. }
  195. void TestingSpellCheckProvider::OnRespondTextCheck(
  196. int identifier,
  197. const std::u16string& line,
  198. const std::vector<SpellCheckResult>& results) {
  199. SpellCheckProvider::OnRespondTextCheck(identifier, line, results);
  200. base::RunLoop().RunUntilIdle();
  201. }
  202. #endif // BUILDFLAG(IS_WIN) && BUILDFLAG(USE_BROWSER_SPELLCHECKER)
  203. SpellCheckProviderTest::SpellCheckProviderTest()
  204. : provider_(&embedder_provider_) {}
  205. SpellCheckProviderTest::~SpellCheckProviderTest() {}