spellcheck_provider_test.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. #ifndef COMPONENTS_SPELLCHECK_RENDERER_SPELLCHECK_PROVIDER_TEST_H_
  5. #define COMPONENTS_SPELLCHECK_RENDERER_SPELLCHECK_PROVIDER_TEST_H_
  6. #include <memory>
  7. #include <string>
  8. #include <vector>
  9. #include "base/test/task_environment.h"
  10. #include "build/build_config.h"
  11. #include "components/spellcheck/renderer/empty_local_interface_provider.h"
  12. #include "components/spellcheck/renderer/spellcheck.h"
  13. #include "components/spellcheck/renderer/spellcheck_provider.h"
  14. #include "components/spellcheck/spellcheck_buildflags.h"
  15. #include "mojo/public/cpp/bindings/receiver.h"
  16. #include "testing/gtest/include/gtest/gtest.h"
  17. #include "third_party/blink/public/platform/web_vector.h"
  18. #include "third_party/blink/public/web/web_text_checking_completion.h"
  19. #include "third_party/blink/public/web/web_text_checking_result.h"
  20. struct FakeTextCheckingResult {
  21. size_t completion_count_ = 0;
  22. size_t cancellation_count_ = 0;
  23. blink::WebVector<blink::WebTextCheckingResult> results_;
  24. explicit FakeTextCheckingResult();
  25. ~FakeTextCheckingResult();
  26. };
  27. // A fake completion object for verification.
  28. class FakeTextCheckingCompletion : public blink::WebTextCheckingCompletion {
  29. public:
  30. explicit FakeTextCheckingCompletion(FakeTextCheckingResult*);
  31. ~FakeTextCheckingCompletion() override;
  32. void DidFinishCheckingText(
  33. const blink::WebVector<blink::WebTextCheckingResult>& results) override;
  34. void DidCancelCheckingText() override;
  35. FakeTextCheckingResult* result_;
  36. };
  37. // A fake SpellCheck object which can fake the number of (enabled) spell check
  38. // languages
  39. class FakeSpellCheck : public SpellCheck {
  40. public:
  41. explicit FakeSpellCheck(
  42. service_manager::LocalInterfaceProvider* embedder_provider);
  43. // Test-only method to set the fake language counts
  44. void SetFakeLanguageCounts(size_t language_count, size_t enabled_count);
  45. #if BUILDFLAG(IS_WIN) && BUILDFLAG(USE_BROWSER_SPELLCHECKER)
  46. // Test-only method to initialize SpellCheck object for the given locale.
  47. void InitializeSpellCheckForLocale(const std::string& language,
  48. bool use_hunspell);
  49. #endif // BUILDFLAG(IS_WIN) && BUILDFLAG(USE_BROWSER_SPELLCHECKER)
  50. // Returns the current number of spell check languages.
  51. size_t LanguageCount() override;
  52. // Returns the current number of spell check languages with enabled engines.
  53. size_t EnabledLanguageCount() override;
  54. private:
  55. bool use_fake_counts_ = false;
  56. size_t language_count_ = 0;
  57. size_t enabled_language_count_ = 0;
  58. };
  59. // Faked test target, which stores sent message for verification.
  60. class TestingSpellCheckProvider : public SpellCheckProvider,
  61. public spellcheck::mojom::SpellCheckHost {
  62. public:
  63. explicit TestingSpellCheckProvider(service_manager::LocalInterfaceProvider*);
  64. // Takes ownership of |spellcheck|.
  65. TestingSpellCheckProvider(SpellCheck* spellcheck,
  66. service_manager::LocalInterfaceProvider*);
  67. ~TestingSpellCheckProvider() override;
  68. void RequestTextChecking(
  69. const std::u16string& text,
  70. std::unique_ptr<blink::WebTextCheckingCompletion> completion);
  71. void SetLastResults(
  72. const std::u16string last_request,
  73. blink::WebVector<blink::WebTextCheckingResult>& last_results);
  74. bool SatisfyRequestFromCache(const std::u16string& text,
  75. blink::WebTextCheckingCompletion* completion);
  76. #if BUILDFLAG(IS_WIN) && BUILDFLAG(USE_BROWSER_SPELLCHECKER)
  77. int AddCompletionForTest(
  78. std::unique_ptr<FakeTextCheckingCompletion> completion,
  79. SpellCheckProvider::HybridSpellCheckRequestInfo request_info);
  80. void OnRespondTextCheck(int identifier,
  81. const std::u16string& line,
  82. const std::vector<SpellCheckResult>& results);
  83. #endif // BUILDFLAG(IS_WIN) && BUILDFLAG(USE_BROWSER_SPELLCHECKER)
  84. #if BUILDFLAG(USE_RENDERER_SPELLCHECKER)
  85. void ResetResult();
  86. // Variables logging CallSpellingService() mojo calls.
  87. std::u16string text_;
  88. size_t spelling_service_call_count_ = 0;
  89. #endif // BUILDFLAG(USE_RENDERER_SPELLCHECKER)
  90. #if BUILDFLAG(USE_BROWSER_SPELLCHECKER)
  91. using RequestTextCheckParams =
  92. std::pair<std::u16string, RequestTextCheckCallback>;
  93. // Variables logging RequestTextCheck() mojo calls.
  94. std::vector<RequestTextCheckParams> text_check_requests_;
  95. #endif // BUILDFLAG(USE_BROWSER_SPELLCHECKER)
  96. // Returns |spellcheck|.
  97. FakeSpellCheck* spellcheck() {
  98. return static_cast<FakeSpellCheck*>(spellcheck_);
  99. }
  100. private:
  101. // spellcheck::mojom::SpellCheckHost:
  102. void RequestDictionary() override;
  103. void NotifyChecked(const std::u16string& word, bool misspelled) override;
  104. #if BUILDFLAG(USE_RENDERER_SPELLCHECKER)
  105. void CallSpellingService(const std::u16string& text,
  106. CallSpellingServiceCallback callback) override;
  107. void OnCallSpellingService(const std::u16string& text);
  108. #endif
  109. #if BUILDFLAG(USE_BROWSER_SPELLCHECKER)
  110. void RequestTextCheck(const std::u16string&,
  111. int,
  112. RequestTextCheckCallback) override;
  113. using SpellCheckProvider::CheckSpelling;
  114. void CheckSpelling(const std::u16string&,
  115. int,
  116. CheckSpellingCallback) override;
  117. void FillSuggestionList(const std::u16string&,
  118. FillSuggestionListCallback) override;
  119. #if BUILDFLAG(IS_WIN)
  120. void InitializeDictionaries(InitializeDictionariesCallback callback) override;
  121. #endif // BUILDFLAG(IS_WIN)
  122. #endif // BUILDFLAG(USE_BROWSER_SPELLCHECKER)
  123. #if BUILDFLAG(IS_ANDROID)
  124. void DisconnectSessionBridge() override;
  125. #endif
  126. // Receiver to receive the SpellCheckHost request flow.
  127. mojo::Receiver<spellcheck::mojom::SpellCheckHost> receiver_{this};
  128. };
  129. // SpellCheckProvider test fixture.
  130. class SpellCheckProviderTest : public testing::Test {
  131. public:
  132. SpellCheckProviderTest();
  133. ~SpellCheckProviderTest() override;
  134. protected:
  135. base::test::SingleThreadTaskEnvironment task_environment_;
  136. spellcheck::EmptyLocalInterfaceProvider embedder_provider_;
  137. TestingSpellCheckProvider provider_;
  138. };
  139. #endif // COMPONENTS_SPELLCHECK_RENDERER_SPELLCHECK_PROVIDER_TEST_H_