Siye Liu | de74ce30 | 2019-06-20 20:44:43 | [diff] [blame] | 1 | // Copyright 2019 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 | |
| 5 | #include "components/spellcheck/browser/spellcheck_platform.h" |
| 6 | |
| 7 | #include <stddef.h> |
| 8 | |
| 9 | #include "base/bind.h" |
| 10 | #include "base/run_loop.h" |
| 11 | #include "base/single_thread_task_runner.h" |
| 12 | #include "base/stl_util.h" |
| 13 | #include "base/strings/string_util.h" |
| 14 | #include "base/strings/utf_string_conversions.h" |
Guillaume Jenkins | ac67c307 | 2020-01-28 17:36:57 | [diff] [blame] | 15 | #include "base/task/post_task.h" |
Gabriel Charette | dd8d5985e | 2020-02-26 18:38:35 | [diff] [blame] | 16 | #include "base/task/thread_pool.h" |
Siye Liu | de74ce30 | 2019-06-20 20:44:43 | [diff] [blame] | 17 | #include "base/test/scoped_feature_list.h" |
Gabriel Charette | c710874 | 2019-08-23 03:31:40 | [diff] [blame] | 18 | #include "base/test/task_environment.h" |
Siye Liu | de74ce30 | 2019-06-20 20:44:43 | [diff] [blame] | 19 | #include "base/win/windows_version.h" |
Guillaume Jenkins | 03764e1b | 2020-01-17 21:47:59 | [diff] [blame] | 20 | #include "build/build_config.h" |
Guillaume Jenkins | ac67c307 | 2020-01-28 17:36:57 | [diff] [blame] | 21 | #include "components/spellcheck/browser/windows_spell_checker.h" |
Siye Liu | de74ce30 | 2019-06-20 20:44:43 | [diff] [blame] | 22 | #include "components/spellcheck/common/spellcheck_features.h" |
| 23 | #include "components/spellcheck/common/spellcheck_result.h" |
Guillaume Jenkins | 03764e1b | 2020-01-17 21:47:59 | [diff] [blame] | 24 | #include "components/spellcheck/spellcheck_buildflags.h" |
Siye Liu | de74ce30 | 2019-06-20 20:44:43 | [diff] [blame] | 25 | #include "content/public/test/test_utils.h" |
| 26 | #include "testing/gtest/include/gtest/gtest.h" |
| 27 | |
| 28 | namespace { |
| 29 | |
Guillaume Jenkins | ac67c307 | 2020-01-28 17:36:57 | [diff] [blame] | 30 | class WindowsSpellCheckerTest : public testing::Test { |
Siye Liu | de74ce30 | 2019-06-20 20:44:43 | [diff] [blame] | 31 | public: |
Guillaume Jenkins | ac67c307 | 2020-01-28 17:36:57 | [diff] [blame] | 32 | WindowsSpellCheckerTest() { |
| 33 | if (spellcheck::WindowsVersionSupportsSpellchecker()) { |
Guillaume Jenkins | 4346eb4 | 2020-02-18 17:50:58 | [diff] [blame] | 34 | feature_list_.InitAndEnableFeature( |
| 35 | spellcheck::kWinUseBrowserSpellChecker); |
| 36 | |
Guillaume Jenkins | ac67c307 | 2020-01-28 17:36:57 | [diff] [blame] | 37 | win_spell_checker_ = std::make_unique<WindowsSpellChecker>( |
Gabriel Charette | dd8d5985e | 2020-02-26 18:38:35 | [diff] [blame] | 38 | base::ThreadPool::CreateCOMSTATaskRunner({base::MayBlock()})); |
Guillaume Jenkins | 4346eb4 | 2020-02-18 17:50:58 | [diff] [blame] | 39 | |
| 40 | win_spell_checker_->CreateSpellChecker( |
| 41 | "en-US", base::BindOnce( |
| 42 | &WindowsSpellCheckerTest::SetLanguageCompletionCallback, |
| 43 | base::Unretained(this))); |
| 44 | |
| 45 | RunUntilResultReceived(); |
Guillaume Jenkins | ac67c307 | 2020-01-28 17:36:57 | [diff] [blame] | 46 | } |
| 47 | } |
| 48 | |
Siye Liu | de74ce30 | 2019-06-20 20:44:43 | [diff] [blame] | 49 | void RunUntilResultReceived() { |
| 50 | if (callback_finished_) |
| 51 | return; |
| 52 | base::RunLoop run_loop; |
| 53 | quit_ = run_loop.QuitClosure(); |
| 54 | run_loop.Run(); |
| 55 | |
| 56 | // reset status |
| 57 | callback_finished_ = false; |
| 58 | } |
| 59 | |
| 60 | void SetLanguageCompletionCallback(bool result) { |
| 61 | set_language_result_ = result; |
| 62 | callback_finished_ = true; |
| 63 | if (quit_) |
| 64 | std::move(quit_).Run(); |
| 65 | } |
| 66 | |
| 67 | void TextCheckCompletionCallback( |
| 68 | const std::vector<SpellCheckResult>& results) { |
| 69 | callback_finished_ = true; |
| 70 | spell_check_results_ = results; |
| 71 | if (quit_) |
| 72 | std::move(quit_).Run(); |
| 73 | } |
| 74 | |
Guillaume Jenkins | e00ab0b | 2020-01-07 22:08:59 | [diff] [blame] | 75 | #if BUILDFLAG(USE_WIN_HYBRID_SPELLCHECKER) |
| 76 | void PerLanguageSuggestionsCompletionCallback( |
Guillaume Jenkins | e071805 | 2019-12-19 20:06:50 | [diff] [blame] | 77 | const spellcheck::PerLanguageSuggestions& suggestions) { |
| 78 | callback_finished_ = true; |
Guillaume Jenkins | e00ab0b | 2020-01-07 22:08:59 | [diff] [blame] | 79 | per_language_suggestions_ = suggestions; |
Guillaume Jenkins | e071805 | 2019-12-19 20:06:50 | [diff] [blame] | 80 | if (quit_) |
| 81 | std::move(quit_).Run(); |
| 82 | } |
Guillaume Jenkins | e00ab0b | 2020-01-07 22:08:59 | [diff] [blame] | 83 | #endif // BUILDFLAG(USE_WIN_HYBRID_SPELLCHECKER) |
Guillaume Jenkins | e071805 | 2019-12-19 20:06:50 | [diff] [blame] | 84 | |
Siye Liu | de74ce30 | 2019-06-20 20:44:43 | [diff] [blame] | 85 | protected: |
Guillaume Jenkins | ac67c307 | 2020-01-28 17:36:57 | [diff] [blame] | 86 | std::unique_ptr<WindowsSpellChecker> win_spell_checker_; |
| 87 | |
Siye Liu | de74ce30 | 2019-06-20 20:44:43 | [diff] [blame] | 88 | bool callback_finished_ = false; |
Guillaume Jenkins | ac67c307 | 2020-01-28 17:36:57 | [diff] [blame] | 89 | base::OnceClosure quit_; |
Guillaume Jenkins | 4346eb4 | 2020-02-18 17:50:58 | [diff] [blame] | 90 | base::test::ScopedFeatureList feature_list_; |
Siye Liu | de74ce30 | 2019-06-20 20:44:43 | [diff] [blame] | 91 | |
| 92 | bool set_language_result_; |
| 93 | std::vector<SpellCheckResult> spell_check_results_; |
Guillaume Jenkins | e00ab0b | 2020-01-07 22:08:59 | [diff] [blame] | 94 | #if BUILDFLAG(USE_WIN_HYBRID_SPELLCHECKER) |
| 95 | spellcheck::PerLanguageSuggestions per_language_suggestions_; |
| 96 | #endif // BUILDFLAG(USE_WIN_HYBRID_SPELLCHECKER) |
Siye Liu | de74ce30 | 2019-06-20 20:44:43 | [diff] [blame] | 97 | |
Gabriel Charette | dfa3604 | 2019-08-19 17:30:11 | [diff] [blame] | 98 | base::test::TaskEnvironment task_environment_{ |
| 99 | base::test::TaskEnvironment::MainThreadType::UI}; |
Siye Liu | de74ce30 | 2019-06-20 20:44:43 | [diff] [blame] | 100 | }; |
| 101 | |
Guillaume Jenkins | 4346eb4 | 2020-02-18 17:50:58 | [diff] [blame] | 102 | TEST_F(WindowsSpellCheckerTest, RequestTextCheck) { |
Guillaume Jenkins | e071805 | 2019-12-19 20:06:50 | [diff] [blame] | 103 | if (!spellcheck::WindowsVersionSupportsSpellchecker()) { |
Siye Liu | de74ce30 | 2019-06-20 20:44:43 | [diff] [blame] | 104 | return; |
Guillaume Jenkins | e071805 | 2019-12-19 20:06:50 | [diff] [blame] | 105 | } |
Siye Liu | de74ce30 | 2019-06-20 20:44:43 | [diff] [blame] | 106 | |
Guillaume Jenkins | 4346eb4 | 2020-02-18 17:50:58 | [diff] [blame] | 107 | ASSERT_TRUE(set_language_result_); |
Siye Liu | de74ce30 | 2019-06-20 20:44:43 | [diff] [blame] | 108 | |
Guillaume Jenkins | 4346eb4 | 2020-02-18 17:50:58 | [diff] [blame] | 109 | static const struct { |
| 110 | const char* text_to_check; |
| 111 | const char* expected_suggestion; |
| 112 | } kTestCases[] = { |
| 113 | {"absense", "absence"}, {"becomeing", "becoming"}, |
| 114 | {"cieling", "ceiling"}, {"definate", "definite"}, |
| 115 | {"eigth", "eight"}, {"exellent", "excellent"}, |
| 116 | {"finaly", "finally"}, {"garantee", "guarantee"}, |
| 117 | {"humerous", "humorous"}, {"imediately", "immediately"}, |
| 118 | {"jellous", "jealous"}, {"knowlege", "knowledge"}, |
| 119 | {"lenght", "length"}, {"manuever", "maneuver"}, |
| 120 | {"naturaly", "naturally"}, {"ommision", "omission"}, |
| 121 | }; |
Siye Liu | de74ce30 | 2019-06-20 20:44:43 | [diff] [blame] | 122 | |
Guillaume Jenkins | 4346eb4 | 2020-02-18 17:50:58 | [diff] [blame] | 123 | for (size_t i = 0; i < base::size(kTestCases); ++i) { |
| 124 | const auto& test_case = kTestCases[i]; |
| 125 | const base::string16 word(base::ASCIIToUTF16(test_case.text_to_check)); |
| 126 | |
| 127 | // Check if the suggested words occur. |
| 128 | win_spell_checker_->RequestTextCheck( |
| 129 | 1, word, |
| 130 | base::BindOnce(&WindowsSpellCheckerTest::TextCheckCompletionCallback, |
| 131 | base::Unretained(this))); |
| 132 | RunUntilResultReceived(); |
| 133 | |
| 134 | ASSERT_EQ(1u, spell_check_results_.size()) |
| 135 | << "RequestTextCheckTests case " << i << ": Wrong number of results"; |
| 136 | |
| 137 | const std::vector<base::string16>& suggestions = |
| 138 | spell_check_results_.front().replacements; |
| 139 | const base::string16 suggested_word( |
| 140 | base::ASCIIToUTF16(test_case.expected_suggestion)); |
| 141 | auto position = |
| 142 | std::find_if(suggestions.begin(), suggestions.end(), |
| 143 | [&](const base::string16& suggestion) { |
| 144 | return suggestion.compare(suggested_word) == 0; |
| 145 | }); |
| 146 | |
| 147 | ASSERT_NE(suggestions.end(), position) << "RequestTextCheckTests case " << i |
| 148 | << ": Expected suggestion not found"; |
| 149 | } |
| 150 | } |
| 151 | |
Guillaume Jenkins | e071805 | 2019-12-19 20:06:50 | [diff] [blame] | 152 | #if BUILDFLAG(USE_WIN_HYBRID_SPELLCHECKER) |
Guillaume Jenkins | 4346eb4 | 2020-02-18 17:50:58 | [diff] [blame] | 153 | TEST_F(WindowsSpellCheckerTest, GetPerLanguageSuggestions) { |
| 154 | if (!spellcheck::WindowsVersionSupportsSpellchecker()) { |
| 155 | return; |
| 156 | } |
| 157 | |
| 158 | ASSERT_TRUE(set_language_result_); |
| 159 | |
| 160 | win_spell_checker_->GetPerLanguageSuggestions( |
| 161 | base::ASCIIToUTF16("tihs"), |
| 162 | base::BindOnce( |
| 163 | &WindowsSpellCheckerTest::PerLanguageSuggestionsCompletionCallback, |
| 164 | base::Unretained(this))); |
| 165 | RunUntilResultReceived(); |
| 166 | |
| 167 | ASSERT_EQ(per_language_suggestions_.size(), 1u); |
| 168 | ASSERT_GT(per_language_suggestions_[0].size(), 0u); |
Siye Liu | de74ce30 | 2019-06-20 20:44:43 | [diff] [blame] | 169 | } |
Guillaume Jenkins | 4346eb4 | 2020-02-18 17:50:58 | [diff] [blame] | 170 | #endif // BUILDFLAG(USE_WIN_HYBRID_SPELLCHECKER) |
Siye Liu | de74ce30 | 2019-06-20 20:44:43 | [diff] [blame] | 171 | |
| 172 | } // namespace |