spellcheck_provider_hunspell_unittest.cc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 "base/feature_list.h"
  5. #include "base/strings/utf_string_conversions.h"
  6. #include "build/build_config.h"
  7. #include "components/spellcheck/common/spellcheck_features.h"
  8. #include "components/spellcheck/renderer/spellcheck_provider_test.h"
  9. #include "testing/gtest/include/gtest/gtest.h"
  10. // Tests for Hunspell functionality in SpellcheckingProvider
  11. using base::ASCIIToUTF16;
  12. using base::WideToUTF16;
  13. namespace {
  14. void CheckSpellingServiceCallCount(size_t actual, size_t expected) {
  15. // On Windows, if the native spell checker integration is enabled,
  16. // CallSpellingService() is not used, so the call count will always be 0.
  17. // Don't assert the call count in that case.
  18. #if BUILDFLAG(IS_WIN)
  19. if (base::FeatureList::IsEnabled(spellcheck::kWinUseBrowserSpellChecker)) {
  20. return;
  21. }
  22. #endif // BUILDFLAG(IS_WIN)
  23. EXPECT_EQ(actual, expected);
  24. }
  25. void CheckProviderText(std::u16string expected, std::u16string actual) {
  26. // On Windows, if the native spell checker integration is enabled,
  27. // CallSpellingService() is not used, so the fake provider's |text_| is never
  28. // assigned. Don't assert the text in that case.
  29. #if BUILDFLAG(IS_WIN)
  30. if (base::FeatureList::IsEnabled(spellcheck::kWinUseBrowserSpellChecker)) {
  31. return;
  32. }
  33. #endif // BUILDFLAG(IS_WIN)
  34. EXPECT_EQ(actual, expected);
  35. }
  36. // Tests that the SpellCheckProvider object sends a spellcheck request when a
  37. // user finishes typing a word. Also this test verifies that this object checks
  38. // only a line being edited by the user.
  39. TEST_F(SpellCheckProviderTest, MultiLineText) {
  40. FakeTextCheckingResult completion;
  41. // Verify that the SpellCheckProvider class does not spellcheck empty text.
  42. provider_.ResetResult();
  43. provider_.RequestTextChecking(
  44. std::u16string(),
  45. std::make_unique<FakeTextCheckingCompletion>(&completion));
  46. EXPECT_EQ(completion.completion_count_, 1U);
  47. EXPECT_TRUE(provider_.text_.empty());
  48. CheckSpellingServiceCallCount(provider_.spelling_service_call_count_, 0U);
  49. // Verify that the SpellCheckProvider class spellcheck the first word when we
  50. // stop typing after finishing the first word.
  51. provider_.ResetResult();
  52. provider_.RequestTextChecking(
  53. u"First", std::make_unique<FakeTextCheckingCompletion>(&completion));
  54. EXPECT_EQ(completion.completion_count_, 2U);
  55. CheckProviderText(u"First", provider_.text_);
  56. CheckSpellingServiceCallCount(provider_.spelling_service_call_count_, 1U);
  57. // Verify that the SpellCheckProvider class spellcheck the first line when we
  58. // type a return key, i.e. when we finish typing a line.
  59. provider_.ResetResult();
  60. provider_.RequestTextChecking(
  61. u"First Second\n",
  62. std::make_unique<FakeTextCheckingCompletion>(&completion));
  63. EXPECT_EQ(completion.completion_count_, 3U);
  64. CheckProviderText(u"First Second\n", provider_.text_);
  65. CheckSpellingServiceCallCount(provider_.spelling_service_call_count_, 2U);
  66. // Verify that the SpellCheckProvider class spellcheck the lines when we
  67. // finish typing a word "Third" to the second line.
  68. provider_.ResetResult();
  69. provider_.RequestTextChecking(
  70. u"First Second\nThird ",
  71. std::make_unique<FakeTextCheckingCompletion>(&completion));
  72. EXPECT_EQ(completion.completion_count_, 4U);
  73. CheckProviderText(u"First Second\nThird ", provider_.text_);
  74. CheckSpellingServiceCallCount(provider_.spelling_service_call_count_, 3U);
  75. // Verify that the SpellCheckProvider class does not send a spellcheck request
  76. // when a user inserts whitespace characters.
  77. provider_.ResetResult();
  78. provider_.RequestTextChecking(
  79. u"First Second\nThird ",
  80. std::make_unique<FakeTextCheckingCompletion>(&completion));
  81. EXPECT_EQ(completion.completion_count_, 5U);
  82. EXPECT_TRUE(provider_.text_.empty());
  83. CheckSpellingServiceCallCount(provider_.spelling_service_call_count_, 3U);
  84. // Verify that the SpellCheckProvider class spellcheck the lines when we type
  85. // a period.
  86. provider_.ResetResult();
  87. provider_.RequestTextChecking(
  88. u"First Second\nThird Fourth.",
  89. std::make_unique<FakeTextCheckingCompletion>(&completion));
  90. EXPECT_EQ(completion.completion_count_, 6U);
  91. CheckProviderText(u"First Second\nThird Fourth.", provider_.text_);
  92. CheckSpellingServiceCallCount(provider_.spelling_service_call_count_, 4U);
  93. }
  94. // Tests that the SpellCheckProvider class does not send requests to the
  95. // spelling service when not necessary.
  96. TEST_F(SpellCheckProviderTest, CancelUnnecessaryRequests) {
  97. FakeTextCheckingResult completion;
  98. provider_.RequestTextChecking(
  99. u"hello.", std::make_unique<FakeTextCheckingCompletion>(&completion));
  100. EXPECT_EQ(completion.completion_count_, 1U);
  101. EXPECT_EQ(completion.cancellation_count_, 0U);
  102. CheckSpellingServiceCallCount(provider_.spelling_service_call_count_, 1U);
  103. // Test that the SpellCheckProvider does not send a request with the same text
  104. // as above.
  105. provider_.RequestTextChecking(
  106. u"hello.", std::make_unique<FakeTextCheckingCompletion>(&completion));
  107. EXPECT_EQ(completion.completion_count_, 2U);
  108. EXPECT_EQ(completion.cancellation_count_, 0U);
  109. CheckSpellingServiceCallCount(provider_.spelling_service_call_count_, 1U);
  110. // Test that the SpellCheckProvider class cancels an incoming request that
  111. // does not include any words.
  112. provider_.RequestTextChecking(
  113. u":-)", std::make_unique<FakeTextCheckingCompletion>(&completion));
  114. EXPECT_EQ(completion.completion_count_, 3U);
  115. EXPECT_EQ(completion.cancellation_count_, 1U);
  116. CheckSpellingServiceCallCount(provider_.spelling_service_call_count_, 1U);
  117. // Test that the SpellCheckProvider class sends a request when it receives a
  118. // Russian word.
  119. const char16_t kRussianWord[] = u"\x0431\x0451\x0434\x0440\x0430";
  120. provider_.RequestTextChecking(
  121. kRussianWord, std::make_unique<FakeTextCheckingCompletion>(&completion));
  122. EXPECT_EQ(completion.completion_count_, 4U);
  123. EXPECT_EQ(completion.cancellation_count_, 1U);
  124. CheckSpellingServiceCallCount(provider_.spelling_service_call_count_, 2U);
  125. }
  126. // Tests that the SpellCheckProvider calls didFinishCheckingText() when
  127. // necessary.
  128. TEST_F(SpellCheckProviderTest, CompleteNecessaryRequests) {
  129. FakeTextCheckingResult completion;
  130. std::u16string text = u"Icland is an icland ";
  131. provider_.RequestTextChecking(
  132. text, std::make_unique<FakeTextCheckingCompletion>(&completion));
  133. EXPECT_EQ(0U, completion.cancellation_count_) << "Should finish checking \""
  134. << text << "\"";
  135. const int kSubstringLength = 18;
  136. std::u16string substring = text.substr(0, kSubstringLength);
  137. provider_.RequestTextChecking(
  138. substring, std::make_unique<FakeTextCheckingCompletion>(&completion));
  139. EXPECT_EQ(0U, completion.cancellation_count_) << "Should finish checking \""
  140. << substring << "\"";
  141. provider_.RequestTextChecking(
  142. text, std::make_unique<FakeTextCheckingCompletion>(&completion));
  143. EXPECT_EQ(0U, completion.cancellation_count_) << "Should finish checking \""
  144. << text << "\"";
  145. }
  146. } // namespace